]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/string.h
Accept hexadecimal and string values for seeds
[dragonfireclient.git] / src / util / string.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef UTIL_STRING_HEADER
21 #define UTIL_STRING_HEADER
22
23 #include "../irrlichttypes.h"
24 #include <stdlib.h>
25 #include <string>
26 #include <cstring>
27 #include <vector>
28 #include <sstream>
29
30 struct FlagDesc {
31         const char *name;
32         u32 flag;
33 };
34
35 std::wstring narrow_to_wide(const std::string& mbs);
36 std::string wide_to_narrow(const std::wstring& wcs);
37
38 static inline std::string padStringRight(std::string s, size_t len)
39 {
40         if(len > s.size())
41                 s.insert(s.end(), len - s.size(), ' ');
42         return s;
43 }
44
45 // ends: NULL- or ""-terminated array of strings
46 // Returns "" if no end could be removed.
47 static inline std::string removeStringEnd(const std::string &s, const char *ends[])
48 {
49         const char **p = ends;
50         for(; (*p) && (*p)[0] != '\0'; p++){
51                 std::string end = *p;
52                 if(s.size() < end.size())
53                         continue;
54                 if(s.substr(s.size()-end.size(), end.size()) == end)
55                         return s.substr(0, s.size() - end.size());
56         }
57         return "";
58 }
59
60 // Tests if two strings are equal, optionally case insensitive
61 inline bool str_equal(const std::wstring& s1, const std::wstring& s2,
62                 bool case_insensitive = false)
63 {
64         if(case_insensitive)
65         {
66                 if(s1.size() != s2.size())
67                         return false;
68                 for(size_t i = 0; i < s1.size(); ++i)
69                         if(tolower(s1[i]) != tolower(s2[i]))
70                                 return false;
71                 return true;
72         }
73         else
74         {
75                 return s1 == s2;
76         }
77 }
78
79 // Tests if the second string is a prefix of the first, optionally case insensitive
80 inline bool str_starts_with(const std::wstring& str, const std::wstring& prefix,
81                 bool case_insensitive = false)
82 {
83         if(str.size() < prefix.size())
84                 return false;
85         if(case_insensitive)
86         {
87                 for(size_t i = 0; i < prefix.size(); ++i)
88                         if(tolower(str[i]) != tolower(prefix[i]))
89                                 return false;
90         }
91         else
92         {
93                 for(size_t i = 0; i < prefix.size(); ++i)
94                         if(str[i] != prefix[i])
95                                 return false;
96         }
97         return true;
98 }
99
100 // Split a string using the given delimiter. Returns a vector containing
101 // the component parts.
102 inline std::vector<std::wstring> str_split(const std::wstring &str, wchar_t delimiter)
103 {
104         std::vector<std::wstring> parts;
105         std::wstringstream sstr(str);
106         std::wstring part;
107         while(std::getline(sstr, part, delimiter))
108                 parts.push_back(part);
109         return parts;
110 }
111
112 inline std::string lowercase(const std::string &s)
113 {
114         std::string s2;
115         for(size_t i=0; i<s.size(); i++)
116         {
117                 char c = s[i];
118                 if(c >= 'A' && c <= 'Z')
119                         c -= 'A' - 'a';
120                 s2 += c;
121         }
122         return s2;
123 }
124
125 inline std::string trim(const std::string &s)
126 {
127         size_t front = 0;
128         while(s[front] == ' '    ||
129               s[front] == '\t'   ||
130               s[front] == '\r'   ||
131               s[front] == '\n'
132              )
133                 ++front;
134
135         size_t back = s.size();
136         while(back > front &&
137               (s[back-1] == ' '  ||
138                s[back-1] == '\t' ||
139                s[back-1] == '\r' ||
140                s[back-1] == '\n'
141               )
142              )
143                 --back;
144
145         return s.substr(front, back - front);
146 }
147
148 inline bool is_yes(const std::string &s)
149 {
150         std::string s2 = lowercase(trim(s));
151         if(s2 == "y" || s2 == "yes" || s2 == "true" || atoi(s2.c_str()) != 0)
152                 return true;
153         return false;
154 }
155
156 inline s32 mystoi(const std::string &s, s32 min, s32 max)
157 {
158         s32 i = atoi(s.c_str());
159         if(i < min)
160                 i = min;
161         if(i > max)
162                 i = max;
163         return i;
164 }
165
166 inline s64 stoi64(const std::string &s) {
167         std::stringstream tmp(s);
168         long long t;
169         tmp >> t;
170         return t;
171 }
172
173 // MSVC2010 includes it's own versions of these
174 //#if !defined(_MSC_VER) || _MSC_VER < 1600
175
176 inline s32 mystoi(const std::string &s)
177 {
178         return atoi(s.c_str());
179 }
180
181 inline s32 mystoi(const std::wstring &s)
182 {
183         return atoi(wide_to_narrow(s).c_str());
184 }
185
186 inline float mystof(const std::string &s)
187 {
188         // This crap causes a segfault in certain cases on MinGW
189         /*float f;
190         std::istringstream ss(s);
191         ss>>f;
192         return f;*/
193         // This works in that case
194         return atof(s.c_str());
195 }
196
197 //#endif
198
199 #define stoi mystoi
200 #define stof mystof
201
202 inline std::string itos(s32 i)
203 {
204         std::ostringstream o;
205         o<<i;
206         return o.str();
207 }
208
209 inline std::string i64tos(s64 i) {
210         std::ostringstream o;
211         o<<i;
212         return o.str();
213 }
214
215 inline std::string ftos(float f)
216 {
217         std::ostringstream o;
218         o<<f;
219         return o.str();
220 }
221
222 inline void str_replace(std::string & str, std::string const & pattern,
223                 std::string const & replacement)
224 {
225         std::string::size_type start = str.find(pattern, 0);
226         while(start != str.npos)
227         {
228                 str.replace(start, pattern.size(), replacement);
229                 start = str.find(pattern, start+replacement.size());
230         }
231 }
232
233 inline void str_replace_char(std::string & str, char from, char to)
234 {
235         for(unsigned int i=0; i<str.size(); i++)
236         {
237                 if(str[i] == from)
238                         str[i] = to;
239         }
240 }
241
242 /*
243         Checks if a string contains only supplied characters
244 */
245 inline bool string_allowed(const std::string &s, const std::string &allowed_chars)
246 {
247         for(u32 i=0; i<s.size(); i++)
248         {
249                 bool confirmed = false;
250                 for(u32 j=0; j<allowed_chars.size(); j++)
251                 {
252                         if(s[i] == allowed_chars[j])
253                         {
254                                 confirmed = true;
255                                 break;
256                         }
257                 }
258                 if(confirmed == false)
259                         return false;
260         }
261         return true;
262 }
263
264 /*
265         Checks if a string contains no blacklisted characters (opposite
266         function of string_allowed())
267 */
268 inline bool string_allowed_blacklist(const std::string & s, const std::string & blacklisted_chars)
269 {
270         for(unsigned int i = 0; i < s.length(); i++)
271         {
272                 bool invalid = false;
273                 for(unsigned int j = 0; j < blacklisted_chars.length(); j++)
274                 {
275                         if(s[i] == blacklisted_chars[j])
276                         {
277                                 invalid = true;
278                                 break;
279                         }
280                 }
281                 if(invalid)
282                         return false;
283         }
284         return true;
285 }
286
287 /*
288         Forcefully wraps string into rows using \n
289         (no word wrap, used for showing paths in gui)
290 */
291 inline std::string wrap_rows(const std::string &from, u32 rowlen)
292 {
293         std::string to;
294         for(u32 i=0; i<from.size(); i++)
295         {
296                 if(i != 0 && i%rowlen == 0)
297                         to += '\n';
298                 to += from[i];
299         }
300         return to;
301 }
302
303 /*
304         Removes all \\ from a string that had been escaped (FormSpec strings)
305 */
306 inline std::string unescape_string(std::string &s)
307 {
308         std::string res;
309         
310         for (size_t i = 0; i < s.length(); i++) {
311                 if (s[i] == '\\')
312                         i++;
313                 res += s[i];
314         }
315         
316         return res;
317 }
318
319 std::string translatePassword(std::string playername, std::wstring password);
320 size_t curl_write_data(char *ptr, size_t size, size_t nmemb, void *userdata);
321 u32 readFlagString(std::string str, FlagDesc *flagdesc);
322 std::string writeFlagString(u32 flags, FlagDesc *flagdesc);
323 char *mystrtok_r(char *s, const char *sep, char **lasts);
324 u64 read_seed(const char *str);
325
326 #endif
327