Fix compile under MingW
[oweals/minetest.git] / src / util / string.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2012 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 "../strfnd.h" // For trim()
25 #include "pointer.h"
26 #include <string>
27 #include <cstring>
28 #include <vector>
29 #include <sstream>
30
31 static inline std::string padStringRight(std::string s, size_t len)
32 {
33         if(len > s.size())
34                 s.insert(s.end(), len - s.size(), ' ');
35         return s;
36 }
37
38 // ends: NULL- or ""-terminated array of strings
39 // Returns "" if no end could be removed.
40 static inline std::string removeStringEnd(const std::string &s, const char *ends[])
41 {
42         const char **p = ends;
43         for(; (*p) && (*p)[0] != '\0'; p++){
44                 std::string end = *p;
45                 if(s.size() < end.size())
46                         continue;
47                 if(s.substr(s.size()-end.size(), end.size()) == end)
48                         return s.substr(0, s.size() - end.size());
49         }
50         return "";
51 }
52
53 // Tests if two strings are equal, optionally case insensitive
54 inline bool str_equal(const std::wstring& s1, const std::wstring& s2,
55                 bool case_insensitive = false)
56 {
57         if(case_insensitive)
58         {
59                 if(s1.size() != s2.size())
60                         return false;
61                 for(size_t i = 0; i < s1.size(); ++i)
62                         if(tolower(s1[i]) != tolower(s2[i]))
63                                 return false;
64                 return true;
65         }
66         else
67         {
68                 return s1 == s2;
69         }
70 }
71
72 // Tests if the second string is a prefix of the first, optionally case insensitive
73 inline bool str_starts_with(const std::wstring& str, const std::wstring& prefix,
74                 bool case_insensitive = false)
75 {
76         if(str.size() < prefix.size())
77                 return false;
78         if(case_insensitive)
79         {
80                 for(size_t i = 0; i < prefix.size(); ++i)
81                         if(tolower(str[i]) != tolower(prefix[i]))
82                                 return false;
83         }
84         else
85         {
86                 for(size_t i = 0; i < prefix.size(); ++i)
87                         if(str[i] != prefix[i])
88                                 return false;
89         }
90         return true;
91 }
92
93 inline std::wstring narrow_to_wide(const std::string& mbs)
94 {
95         size_t wcl = mbs.size();
96         Buffer<wchar_t> wcs(wcl+1);
97         size_t l = mbstowcs(*wcs, mbs.c_str(), wcl);
98         if(l == (size_t)(-1))
99                 return L"<invalid multibyte string>";
100         wcs[l] = 0;
101         return *wcs;
102 }
103
104 inline std::string wide_to_narrow(const std::wstring& wcs)
105 {
106         size_t mbl = wcs.size()*4;
107         SharedBuffer<char> mbs(mbl+1);
108         size_t l = wcstombs(*mbs, wcs.c_str(), mbl);
109         if(l == (size_t)(-1))
110                 mbs[0] = 0;
111         else
112                 mbs[l] = 0;
113         return *mbs;
114 }
115
116 // Split a string using the given delimiter. Returns a vector containing
117 // the component parts.
118 inline std::vector<std::wstring> str_split(const std::wstring &str, wchar_t delimiter)
119 {
120         std::vector<std::wstring> parts;
121         std::wstringstream sstr(str);
122         std::wstring part;
123         while(std::getline(sstr, part, delimiter))
124                 parts.push_back(part);
125         return parts;
126 }
127
128 inline std::string lowercase(const std::string &s)
129 {
130         std::string s2;
131         for(size_t i=0; i<s.size(); i++)
132         {
133                 char c = s[i];
134                 if(c >= 'A' && c <= 'Z')
135                         c -= 'A' - 'a';
136                 s2 += c;
137         }
138         return s2;
139 }
140
141 inline bool is_yes(const std::string &s)
142 {
143         std::string s2 = lowercase(trim(s));
144         if(s2 == "y" || s2 == "yes" || s2 == "true" || s2 == "1")
145                 return true;
146         return false;
147 }
148
149 inline s32 mystoi(const std::string &s, s32 min, s32 max)
150 {
151         s32 i = atoi(s.c_str());
152         if(i < min)
153                 i = min;
154         if(i > max)
155                 i = max;
156         return i;
157 }
158
159
160 // MSVC2010 includes it's own versions of these
161 //#if !defined(_MSC_VER) || _MSC_VER < 1600
162
163 inline s32 mystoi(const std::string &s)
164 {
165         return atoi(s.c_str());
166 }
167
168 inline s32 mystoi(const std::wstring &s)
169 {
170         return atoi(wide_to_narrow(s).c_str());
171 }
172
173 inline float mystof(const std::string &s)
174 {
175         // This crap causes a segfault in certain cases on MinGW
176         /*float f;
177         std::istringstream ss(s);
178         ss>>f;
179         return f;*/
180         // This works in that case
181         return atof(s.c_str());
182 }
183
184 //#endif
185
186 #define stoi mystoi
187 #define stof mystof
188
189 inline std::string itos(s32 i)
190 {
191         std::ostringstream o;
192         o<<i;
193         return o.str();
194 }
195
196 inline std::string ftos(float f)
197 {
198         std::ostringstream o;
199         o<<f;
200         return o.str();
201 }
202
203 inline void str_replace(std::string & str, std::string const & pattern,
204                 std::string const & replacement)
205 {
206         std::string::size_type start = str.find(pattern, 0);
207         while(start != str.npos)
208         {
209                 str.replace(start, pattern.size(), replacement);
210                 start = str.find(pattern, start+replacement.size());
211         }
212 }
213
214 inline void str_replace_char(std::string & str, char from, char to)
215 {
216         for(unsigned int i=0; i<str.size(); i++)
217         {
218                 if(str[i] == from)
219                         str[i] = to;
220         }
221 }
222
223 /*
224         Checks if a string contains only supplied characters
225 */
226 inline bool string_allowed(const std::string &s, const std::string &allowed_chars)
227 {
228         for(u32 i=0; i<s.size(); i++)
229         {
230                 bool confirmed = false;
231                 for(u32 j=0; j<allowed_chars.size(); j++)
232                 {
233                         if(s[i] == allowed_chars[j])
234                         {
235                                 confirmed = true;
236                                 break;
237                         }
238                 }
239                 if(confirmed == false)
240                         return false;
241         }
242         return true;
243 }
244
245 /*
246         Checks if a string contains no blacklisted characters (opposite
247         function of string_allowed())
248 */
249 inline bool string_allowed_blacklist(const std::string & s, const std::string & blacklisted_chars)
250 {
251         for(unsigned int i = 0; i < s.length(); i++)
252         {
253                 bool invalid = false;
254                 for(unsigned int j = 0; j < blacklisted_chars.length(); j++)
255                 {
256                         if(s[i] == blacklisted_chars[j])
257                         {
258                                 invalid = true;
259                                 break;
260                         }
261                 }
262                 if(invalid)
263                         return false;
264         }
265         return true;
266 }
267
268 /*
269         Forcefully wraps string into rows using \n
270         (no word wrap, used for showing paths in gui)
271 */
272 inline std::string wrap_rows(const std::string &from, u32 rowlen)
273 {
274         std::string to;
275         for(u32 i=0; i<from.size(); i++)
276         {
277                 if(i != 0 && i%rowlen == 0)
278                         to += '\n';
279                 to += from[i];
280         }
281         return to;
282 }
283
284 std::string translatePassword(std::string playername, std::wstring password);
285 size_t curl_write_data(char *ptr, size_t size, size_t nmemb, void *userdata);
286 char *mystrtok_r(char *s, const char *sep, char **lasts);
287
288 #endif
289