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