ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
string_utils.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef AGS_SHARED_UTIL_STRING_UTILS_H
23 #define AGS_SHARED_UTIL_STRING_UTILS_H
24 
25 #include "ags/shared/util/string_types.h"
26 
27 namespace AGS3 {
28 
29 namespace AGS {
30 namespace Shared {
31 class Stream;
32 } // namespace Shared
33 } // namespace AGS
34 
35 using namespace AGS; // FIXME later
36 
37 //=============================================================================
38 
39 namespace AGS {
40 namespace Shared {
41 namespace StrUtil {
42 
43 enum ConversionError {
44  kNoError, // conversion successful
45  kFailed, // conversion failed (e.g. wrong format)
46  kOutOfRange // the resulting value is out of range
47 };
48 
49 // Convert integer to string, by printing its value
50 String IntToString(int val);
51 // Tries to convert whole string into integer value;
52 // returns def_val on failure
53 int StringToInt(const String &s, int def_val = 0);
54 // Tries to convert whole string into integer value;
55 // Returns error code if any non-digit character was met or if value is out
56 // of range; the 'val' variable will be set with resulting integer, or
57 // def_val on failure
58 ConversionError StringToInt(const String &s, int &val, int def_val);
59 // Tries to convert whole string into float value;
60 // returns def_val on failure
61 float StringToFloat(const String &s, float def_val = 0.f);
62 
63 // A simple unescape string implementation, unescapes '\\x' into '\x'.
64 String Unescape(const String &s);
65 // Converts a classic wildcard search pattern into C++11 compatible regex pattern
66 String WildcardToRegex(const String &wildcard);
67 
68 // Serialize and unserialize unterminated string prefixed with 32-bit length;
69 // length is presented as 32-bit integer integer
70 String ReadString(Stream *in);
71 void ReadString(char *cstr, Stream *in, size_t buf_limit);
72 void ReadString(char **cstr, Stream *in);
73 void ReadString(String &s, Stream *in);
74 void SkipString(Stream *in);
75 void WriteString(const String &s, Stream *out);
76 void WriteString(const char *cstr, Stream *out);
77 void WriteString(const char *cstr, size_t len, Stream *out);
78 
79 // Serialize and unserialize string as c-string (null-terminated sequence)
80 //
81 // Reads a null-terminated string until getting a null-terminator.
82 // writes into the buffer up to the buf_limit.
83 // Note that this will keep reading the stream out until 0 is read,
84 // even if buffer is already full.
85 // Guarantees that output buffer will contain a null-terminator.
86 void ReadCStr(char *buf, Stream *in, size_t buf_limit);
87 // Reads N characters into the provided buffer.
88 // Guarantees that output buffer will contain a null-terminator.
89 void ReadCStrCount(char *buf, Stream *in, size_t count);
90 // Reads a null-terminated string and !! mallocs !! a char buffer for it;
91 // returns nullptr if the read string is empty.
92 // Buffer is hard-limited to 1024 bytes, including null-terminator.
93 // Strictly for compatibility with the C lib code!
94 char * ReadMallocCStrOrNull(Stream *in);
95 void SkipCStr(Stream *in);
96 void WriteCStr(const char *cstr, Stream *out);
97 void WriteCStr(const String &s, Stream *out);
98 
99 // Serialize and unserialize a string map, both keys and values are read using ReadString
100 void ReadStringMap(StringMap &map, Stream *in);
101 void WriteStringMap(const StringMap &map, Stream *out);
102 
103 // Convert utf-8 string to ascii/ansi representation;
104  // writes into out_cstr buffer limited by out_sz bytes; returns bytes written.
105 size_t ConvertUtf8ToAscii(const char *mbstr, const char *loc_name, char *out_cstr, size_t out_sz);
106 // Convert utf-8 string to wide-string (16-bit char);
107 // writes into out_wcstr buffer limited by out_sz *wchars*; returns *wchars* written.
108 size_t ConvertUtf8ToWstr(const char *mbstr, wchar_t *out_wcstr, size_t out_sz);
109 // Convert wide-string to utf-8 string;
110 // writes into out_mbstr buffer limited by out_sz *bytes*; returns *bytes* written.
111 size_t ConvertWstrToUtf8(const wchar_t *wcstr, char *out_mbstr, size_t out_sz);
112 
113 } // namespace StrUtil
114 } // namespace Shared
115 } // namespace AGS
116 } // namespace AGS3
117 
118 #endif
Definition: achievements_tables.h:27
No error occurred.
Definition: error.h:48
Definition: ags.h:40