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
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (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, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef AGS_SHARED_UTIL_STRING_UTILS_H
24 #define AGS_SHARED_UTIL_STRING_UTILS_H
25 
26 #include "ags/shared/util/string_types.h"
27 
28 namespace AGS3 {
29 
30 namespace AGS {
31 namespace Shared {
32 class Stream;
33 } // namespace Shared
34 } // namespace AGS
35 
36 using namespace AGS; // FIXME later
37 
38 //=============================================================================
39 
40 namespace AGS {
41 namespace Shared {
42 namespace StrUtil {
43 
44 enum ConversionError {
45 	kNoError,   // conversion successful
46 	kFailed,    // conversion failed (e.g. wrong format)
47 	kOutOfRange // the resulting value is out of range
48 };
49 
50 // Convert integer to string, by printing its value
51 String          IntToString(int val);
52 // Tries to convert whole string into integer value;
53 // returns def_val on failure
54 int             StringToInt(const String &s, int def_val = 0);
55 // Tries to convert whole string into integer value;
56 // Returns error code if any non-digit character was met or if value is out
57 // of range; the 'val' variable will be set with resulting integer, or
58 // def_val on failure
59 ConversionError StringToInt(const String &s, int &val, int def_val);
60 
61 // A simple unescape string implementation, unescapes '\\x' into '\x'.
62 String          Unescape(const String &s);
63 // Converts a classic wildcard search pattern into C++11 compatible regex pattern
64 String          WildcardToRegex(const String &wildcard);
65 
66 // Serialize and unserialize unterminated string prefixed with 32-bit length;
67 // length is presented as 32-bit integer integer
68 String          ReadString(Stream *in);
69 void            ReadString(char *cstr, Stream *in, size_t buf_limit);
70 void            ReadString(char **cstr, Stream *in);
71 void            ReadString(String &s, Stream *in);
72 void            SkipString(Stream *in);
73 void            WriteString(const String &s, Stream *out);
74 void            WriteString(const char *cstr, Stream *out);
75 void            WriteString(const char *cstr, size_t len, Stream *out);
76 
77 // Serialize and unserialize string as c-string (null-terminated sequence)
78 void            ReadCStr(char *buf, Stream *in, size_t buf_limit);
79 void            SkipCStr(Stream *in);
80 void            WriteCStr(const char *cstr, Stream *out);
81 void            WriteCStr(const String &s, Stream *out);
82 
83 // Serialize and unserialize a string map, both keys and values are read using ReadString
84 void            ReadStringMap(StringMap &map, Stream *in);
85 void            WriteStringMap(const StringMap &map, Stream *out);
86 
87 } // namespace StrUtil
88 } // namespace Shared
89 } // namespace AGS
90 } // namespace AGS3
91 
92 #endif
93