]> git.lizzy.rs Git - minetest.git/blob - src/util/string.cpp
231eaf6be02e553f420220f1043b8ea7af0ccbe3
[minetest.git] / src / util / string.cpp
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 #include "string.h"
21 #include "pointer.h"
22 #include "numeric.h"
23 #include "log.h"
24
25 #include "hex.h"
26 #include "../porting.h"
27
28 #include <sstream>
29 #include <iomanip>
30 #include <map>
31
32 static bool parseHexColorString(const std::string &value, video::SColor &color);
33 static bool parseNamedColorString(const std::string &value, video::SColor &color);
34
35
36 // You must free the returned string!
37 // The returned string is allocated using new
38 wchar_t *narrow_to_wide_c(const char *str)
39 {
40         wchar_t* nstr = 0;
41 #if defined(_WIN32)
42         int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, 0, 0);
43         if (nResult == 0) {
44                 errorstream<<"gettext: MultiByteToWideChar returned null"<<std::endl;
45         } else {
46                 nstr = new wchar_t[nResult];
47                 MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, (WCHAR *) nstr, nResult);
48         }
49 #else
50         size_t len = strlen(str);
51         nstr = new wchar_t[len+1];
52
53         std::wstring intermediate = narrow_to_wide(str);
54         memset(nstr, 0, (len + 1) * sizeof(wchar_t));
55         memcpy(nstr, intermediate.c_str(), len * sizeof(wchar_t));
56 #endif
57
58         return nstr;
59 }
60
61
62 #ifdef __ANDROID__
63
64 const wchar_t* wide_chars =
65         L" !\"#$%&'()*+,-./0123456789:;<=>?@"
66         L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
67         L"abcdefghijklmnopqrstuvwxyz{|}~";
68
69 int wctomb(char *s, wchar_t wc)
70 {
71         for (unsigned int j = 0; j < (sizeof(wide_chars)/sizeof(wchar_t));j++) {
72                 if (wc == wide_chars[j]) {
73                         *s = (char) (j+32);
74                         return 1;
75                 }
76                 else if (wc == L'\n') {
77                         *s = '\n';
78                         return 1;
79                 }
80         }
81         return -1;
82 }
83
84 int mbtowc(wchar_t *pwc, const char *s, size_t n)
85 {
86         std::wstring intermediate = narrow_to_wide(s);
87
88         if (intermediate.length() > 0) {
89                 *pwc = intermediate[0];
90                 return 1;
91         }
92         else {
93                 return -1;
94         }
95 }
96
97 std::wstring narrow_to_wide(const std::string &mbs) {
98         size_t wcl = mbs.size();
99
100         std::wstring retval = L"";
101
102         for (unsigned int i = 0; i < wcl; i++) {
103                 if (((unsigned char) mbs[i] >31) &&
104                  ((unsigned char) mbs[i] < 127)) {
105
106                         retval += wide_chars[(unsigned char) mbs[i] -32];
107                 }
108                 //handle newline
109                 else if (mbs[i] == '\n') {
110                         retval += L'\n';
111                 }
112         }
113
114         return retval;
115 }
116
117 #else // not Android
118
119 std::wstring narrow_to_wide(const std::string &mbs)
120 {
121         size_t wcl = mbs.size();
122         Buffer<wchar_t> wcs(wcl + 1);
123         size_t len = mbstowcs(*wcs, mbs.c_str(), wcl);
124         if (len == (size_t)(-1))
125                 return L"<invalid multibyte string>";
126         wcs[len] = 0;
127         return *wcs;
128 }
129
130 #endif
131
132 #ifdef __ANDROID__
133
134 std::string wide_to_narrow(const std::wstring &wcs) {
135         size_t mbl = wcs.size()*4;
136
137         std::string retval = "";
138         for (unsigned int i = 0; i < wcs.size(); i++) {
139                 wchar_t char1 = (wchar_t) wcs[i];
140
141                 if (char1 == L'\n') {
142                         retval += '\n';
143                         continue;
144                 }
145
146                 for (unsigned int j = 0; j < wcslen(wide_chars);j++) {
147                         wchar_t char2 = (wchar_t) wide_chars[j];
148
149                         if (char1 == char2) {
150                                 char toadd = (j+32);
151                                 retval += toadd;
152                                 break;
153                         }
154                 }
155         }
156
157         return retval;
158 }
159
160 #else // not Android
161
162 std::string wide_to_narrow(const std::wstring &wcs)
163 {
164         size_t mbl = wcs.size() * 4;
165         SharedBuffer<char> mbs(mbl+1);
166         size_t len = wcstombs(*mbs, wcs.c_str(), mbl);
167         if (len == (size_t)(-1))
168                 return "Character conversion failed!";
169         else
170                 mbs[len] = 0;
171         return *mbs;
172 }
173
174 #endif
175
176 std::string urlencode(std::string str)
177 {
178         // Encodes non-unreserved URI characters by a percent sign
179         // followed by two hex digits. See RFC 3986, section 2.3.
180         static const char url_hex_chars[] = "0123456789ABCDEF";
181         std::ostringstream oss(std::ios::binary);
182         for (u32 i = 0; i < str.size(); i++) {
183                 unsigned char c = str[i];
184                 if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')
185                         oss << c;
186                 else
187                         oss << "%"
188                                 << url_hex_chars[(c & 0xf0) >> 4]
189                                 << url_hex_chars[c & 0x0f];
190         }
191         return oss.str();
192 }
193
194 std::string urldecode(std::string str)
195 {
196         // Inverse of urlencode
197         std::ostringstream oss(std::ios::binary);
198         for (u32 i = 0; i < str.size(); i++) {
199                 unsigned char highvalue, lowvalue;
200                 if (str[i] == '%' &&
201                                 hex_digit_decode(str[i+1], highvalue) &&
202                                 hex_digit_decode(str[i+2], lowvalue)) {
203                         oss << (char) ((highvalue << 4) | lowvalue);
204                         i += 2;
205                 }
206                 else
207                         oss << str[i];
208         }
209         return oss.str();
210 }
211
212 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
213 {
214         u32 result = 0, mask = 0;
215         char *s = &str[0];
216         char *flagstr, *strpos = NULL;
217
218         while ((flagstr = strtok_r(s, ",", &strpos))) {
219                 s = NULL;
220
221                 while (*flagstr == ' ' || *flagstr == '\t')
222                         flagstr++;
223
224                 bool flagset = true;
225                 if (!strncasecmp(flagstr, "no", 2)) {
226                         flagset = false;
227                         flagstr += 2;
228                 }
229
230                 for (int i = 0; flagdesc[i].name; i++) {
231                         if (!strcasecmp(flagstr, flagdesc[i].name)) {
232                                 mask |= flagdesc[i].flag;
233                                 if (flagset)
234                                         result |= flagdesc[i].flag;
235                                 break;
236                         }
237                 }
238         }
239
240         if (flagmask)
241                 *flagmask = mask;
242
243         return result;
244 }
245
246 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
247 {
248         std::string result;
249
250         for (int i = 0; flagdesc[i].name; i++) {
251                 if (flagmask & flagdesc[i].flag) {
252                         if (!(flags & flagdesc[i].flag))
253                                 result += "no";
254
255                         result += flagdesc[i].name;
256                         result += ", ";
257                 }
258         }
259
260         size_t len = result.length();
261         if (len >= 2)
262                 result.erase(len - 2, 2);
263
264         return result;
265 }
266
267 size_t mystrlcpy(char *dst, const char *src, size_t size)
268 {
269         size_t srclen  = strlen(src) + 1;
270         size_t copylen = MYMIN(srclen, size);
271
272         if (copylen > 0) {
273                 memcpy(dst, src, copylen);
274                 dst[copylen - 1] = '\0';
275         }
276
277         return srclen;
278 }
279
280 char *mystrtok_r(char *s, const char *sep, char **lasts)
281 {
282         char *t;
283
284         if (!s)
285                 s = *lasts;
286
287         while (*s && strchr(sep, *s))
288                 s++;
289
290         if (!*s)
291                 return NULL;
292
293         t = s;
294         while (*t) {
295                 if (strchr(sep, *t)) {
296                         *t++ = '\0';
297                         break;
298                 }
299                 t++;
300         }
301
302         *lasts = t;
303         return s;
304 }
305
306 u64 read_seed(const char *str)
307 {
308         char *endptr;
309         u64 num;
310
311         if (str[0] == '0' && str[1] == 'x')
312                 num = strtoull(str, &endptr, 16);
313         else
314                 num = strtoull(str, &endptr, 10);
315
316         if (*endptr)
317                 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
318
319         return num;
320 }
321
322 bool parseColorString(const std::string &value, video::SColor &color, bool quiet)
323 {
324         bool success;
325
326         if (value[0] == '#')
327                 success = parseHexColorString(value, color);
328         else
329                 success = parseNamedColorString(value, color);
330
331         if (!success && !quiet)
332                 errorstream << "Invalid color: \"" << value << "\"" << std::endl;
333
334         return success;
335 }
336
337 static bool parseHexColorString(const std::string &value, video::SColor &color)
338 {
339         unsigned char components[] = { 0x00, 0x00, 0x00, 0xff }; // R,G,B,A
340
341         if (value[0] != '#')
342                 return false;
343
344         size_t len = value.size();
345         bool short_form;
346
347         if (len == 9 || len == 7) // #RRGGBBAA or #RRGGBB
348                 short_form = false;
349         else if (len == 5 || len == 4) // #RGBA or #RGB
350                 short_form = true;
351         else
352                 return false;
353
354         bool success = true;
355
356         for (size_t pos = 1, cc = 0; pos < len; pos++, cc++) {
357                 assert(cc < sizeof components / sizeof components[0]);
358                 if (short_form) {
359                         unsigned char d;
360                         if (!hex_digit_decode(value[pos], d)) {
361                                 success = false;
362                                 break;
363                         }
364                         components[cc] = (d & 0xf) << 4 | (d & 0xf);
365                 } else {
366                         unsigned char d1, d2;
367                         if (!hex_digit_decode(value[pos], d1) ||
368                                         !hex_digit_decode(value[pos+1], d2)) {
369                                 success = false;
370                                 break;
371                         }
372                         components[cc] = (d1 & 0xf) << 4 | (d2 & 0xf);
373                         pos++;  // skip the second digit -- it's already used
374                 }
375         }
376
377         if (success) {
378                 color.setRed(components[0]);
379                 color.setGreen(components[1]);
380                 color.setBlue(components[2]);
381                 color.setAlpha(components[3]);
382         }
383
384         return success;
385 }
386
387 struct ColorContainer {
388         ColorContainer();
389         std::map<const std::string, u32> colors;
390 };
391
392 ColorContainer::ColorContainer()
393 {
394         colors["aliceblue"]              = 0xf0f8ff;
395         colors["antiquewhite"]           = 0xfaebd7;
396         colors["aqua"]                   = 0x00ffff;
397         colors["aquamarine"]             = 0x7fffd4;
398         colors["azure"]                  = 0xf0ffff;
399         colors["beige"]                  = 0xf5f5dc;
400         colors["bisque"]                 = 0xffe4c4;
401         colors["black"]                  = 00000000;
402         colors["blanchedalmond"]         = 0xffebcd;
403         colors["blue"]                   = 0x0000ff;
404         colors["blueviolet"]             = 0x8a2be2;
405         colors["brown"]                  = 0xa52a2a;
406         colors["burlywood"]              = 0xdeb887;
407         colors["cadetblue"]              = 0x5f9ea0;
408         colors["chartreuse"]             = 0x7fff00;
409         colors["chocolate"]              = 0xd2691e;
410         colors["coral"]                  = 0xff7f50;
411         colors["cornflowerblue"]         = 0x6495ed;
412         colors["cornsilk"]               = 0xfff8dc;
413         colors["crimson"]                = 0xdc143c;
414         colors["cyan"]                   = 0x00ffff;
415         colors["darkblue"]               = 0x00008b;
416         colors["darkcyan"]               = 0x008b8b;
417         colors["darkgoldenrod"]          = 0xb8860b;
418         colors["darkgray"]               = 0xa9a9a9;
419         colors["darkgreen"]              = 0x006400;
420         colors["darkkhaki"]              = 0xbdb76b;
421         colors["darkmagenta"]            = 0x8b008b;
422         colors["darkolivegreen"]         = 0x556b2f;
423         colors["darkorange"]             = 0xff8c00;
424         colors["darkorchid"]             = 0x9932cc;
425         colors["darkred"]                = 0x8b0000;
426         colors["darksalmon"]             = 0xe9967a;
427         colors["darkseagreen"]           = 0x8fbc8f;
428         colors["darkslateblue"]          = 0x483d8b;
429         colors["darkslategray"]          = 0x2f4f4f;
430         colors["darkturquoise"]          = 0x00ced1;
431         colors["darkviolet"]             = 0x9400d3;
432         colors["deeppink"]               = 0xff1493;
433         colors["deepskyblue"]            = 0x00bfff;
434         colors["dimgray"]                = 0x696969;
435         colors["dodgerblue"]             = 0x1e90ff;
436         colors["firebrick"]              = 0xb22222;
437         colors["floralwhite"]            = 0xfffaf0;
438         colors["forestgreen"]            = 0x228b22;
439         colors["fuchsia"]                = 0xff00ff;
440         colors["gainsboro"]              = 0xdcdcdc;
441         colors["ghostwhite"]             = 0xf8f8ff;
442         colors["gold"]                   = 0xffd700;
443         colors["goldenrod"]              = 0xdaa520;
444         colors["gray"]                   = 0x808080;
445         colors["green"]                  = 0x008000;
446         colors["greenyellow"]            = 0xadff2f;
447         colors["honeydew"]               = 0xf0fff0;
448         colors["hotpink"]                = 0xff69b4;
449         colors["indianred "]             = 0xcd5c5c;
450         colors["indigo "]                = 0x4b0082;
451         colors["ivory"]                  = 0xfffff0;
452         colors["khaki"]                  = 0xf0e68c;
453         colors["lavender"]               = 0xe6e6fa;
454         colors["lavenderblush"]          = 0xfff0f5;
455         colors["lawngreen"]              = 0x7cfc00;
456         colors["lemonchiffon"]           = 0xfffacd;
457         colors["lightblue"]              = 0xadd8e6;
458         colors["lightcoral"]             = 0xf08080;
459         colors["lightcyan"]              = 0xe0ffff;
460         colors["lightgoldenrodyellow"]   = 0xfafad2;
461         colors["lightgray"]              = 0xd3d3d3;
462         colors["lightgreen"]             = 0x90ee90;
463         colors["lightpink"]              = 0xffb6c1;
464         colors["lightsalmon"]            = 0xffa07a;
465         colors["lightseagreen"]          = 0x20b2aa;
466         colors["lightskyblue"]           = 0x87cefa;
467         colors["lightslategray"]         = 0x778899;
468         colors["lightsteelblue"]         = 0xb0c4de;
469         colors["lightyellow"]            = 0xffffe0;
470         colors["lime"]                   = 0x00ff00;
471         colors["limegreen"]              = 0x32cd32;
472         colors["linen"]                  = 0xfaf0e6;
473         colors["magenta"]                = 0xff00ff;
474         colors["maroon"]                 = 0x800000;
475         colors["mediumaquamarine"]       = 0x66cdaa;
476         colors["mediumblue"]             = 0x0000cd;
477         colors["mediumorchid"]           = 0xba55d3;
478         colors["mediumpurple"]           = 0x9370db;
479         colors["mediumseagreen"]         = 0x3cb371;
480         colors["mediumslateblue"]        = 0x7b68ee;
481         colors["mediumspringgreen"]      = 0x00fa9a;
482         colors["mediumturquoise"]        = 0x48d1cc;
483         colors["mediumvioletred"]        = 0xc71585;
484         colors["midnightblue"]           = 0x191970;
485         colors["mintcream"]              = 0xf5fffa;
486         colors["mistyrose"]              = 0xffe4e1;
487         colors["moccasin"]               = 0xffe4b5;
488         colors["navajowhite"]            = 0xffdead;
489         colors["navy"]                   = 0x000080;
490         colors["oldlace"]                = 0xfdf5e6;
491         colors["olive"]                  = 0x808000;
492         colors["olivedrab"]              = 0x6b8e23;
493         colors["orange"]                 = 0xffa500;
494         colors["orangered"]              = 0xff4500;
495         colors["orchid"]                 = 0xda70d6;
496         colors["palegoldenrod"]          = 0xeee8aa;
497         colors["palegreen"]              = 0x98fb98;
498         colors["paleturquoise"]          = 0xafeeee;
499         colors["palevioletred"]          = 0xdb7093;
500         colors["papayawhip"]             = 0xffefd5;
501         colors["peachpuff"]              = 0xffdab9;
502         colors["peru"]                   = 0xcd853f;
503         colors["pink"]                   = 0xffc0cb;
504         colors["plum"]                   = 0xdda0dd;
505         colors["powderblue"]             = 0xb0e0e6;
506         colors["purple"]                 = 0x800080;
507         colors["red"]                    = 0xff0000;
508         colors["rosybrown"]              = 0xbc8f8f;
509         colors["royalblue"]              = 0x4169e1;
510         colors["saddlebrown"]            = 0x8b4513;
511         colors["salmon"]                 = 0xfa8072;
512         colors["sandybrown"]             = 0xf4a460;
513         colors["seagreen"]               = 0x2e8b57;
514         colors["seashell"]               = 0xfff5ee;
515         colors["sienna"]                 = 0xa0522d;
516         colors["silver"]                 = 0xc0c0c0;
517         colors["skyblue"]                = 0x87ceeb;
518         colors["slateblue"]              = 0x6a5acd;
519         colors["slategray"]              = 0x708090;
520         colors["snow"]                   = 0xfffafa;
521         colors["springgreen"]            = 0x00ff7f;
522         colors["steelblue"]              = 0x4682b4;
523         colors["tan"]                    = 0xd2b48c;
524         colors["teal"]                   = 0x008080;
525         colors["thistle"]                = 0xd8bfd8;
526         colors["tomato"]                 = 0xff6347;
527         colors["turquoise"]              = 0x40e0d0;
528         colors["violet"]                 = 0xee82ee;
529         colors["wheat"]                  = 0xf5deb3;
530         colors["white"]                  = 0xffffff;
531         colors["whitesmoke"]             = 0xf5f5f5;
532         colors["yellow"]                 = 0xffff00;
533         colors["yellowgreen"]            = 0x9acd32;
534
535 }
536
537 static const ColorContainer named_colors;
538
539 static bool parseNamedColorString(const std::string &value, video::SColor &color)
540 {
541         std::string color_name;
542         std::string alpha_string;
543
544         /* If the string has a # in it, assume this is the start of a specified
545          * alpha value (if it isn't the string is invalid and the error will be
546          * caught later on, either because the color name won't be found or the
547          * alpha value will fail conversion)
548          */
549         size_t alpha_pos = value.find('#');
550         if (alpha_pos != std::string::npos) {
551                 color_name = value.substr(0, alpha_pos);
552                 alpha_string = value.substr(alpha_pos + 1);
553         } else {
554                 color_name = value;
555         }
556
557         color_name = lowercase(value);
558
559         std::map<const std::string, unsigned>::const_iterator it;
560         it = named_colors.colors.find(color_name);
561         if (it == named_colors.colors.end())
562                 return false;
563
564         u32 color_temp = it->second;
565
566         /* An empty string for alpha is ok (none of the color table entries
567          * have an alpha value either). Color strings without an alpha specified
568          * are interpreted as fully opaque
569          *
570          * For named colors the supplied alpha string (representing a hex value)
571          * must be exactly two digits. For example:  colorname#08
572          */
573         if (!alpha_string.empty()) {
574                 if (alpha_string.length() != 2)
575                         return false;
576
577                 unsigned char d1, d2;
578                 if (!hex_digit_decode(alpha_string.at(0), d1)
579                                 || !hex_digit_decode(alpha_string.at(1), d2))
580                         return false;
581                 color_temp |= ((d1 & 0xf) << 4 | (d2 & 0xf)) << 24;
582         } else {
583                 color_temp |= 0xff << 24;  // Fully opaque
584         }
585
586         color = video::SColor(color_temp);
587
588         return true;
589 }
590
591 void str_replace(std::string &str, char from, char to)
592 {
593         std::replace(str.begin(), str.end(), from, to);
594 }