]> git.lizzy.rs Git - minetest.git/blob - src/util/string.cpp
Fix indianred and indigo of color-string
[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 #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(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         return oss.str();
333 }
334
335 std::string urldecode(std::string str)
336 {
337         // Inverse of urlencode
338         std::ostringstream oss(std::ios::binary);
339         for (u32 i = 0; i < str.size(); i++) {
340                 unsigned char highvalue, lowvalue;
341                 if (str[i] == '%' &&
342                                 hex_digit_decode(str[i+1], highvalue) &&
343                                 hex_digit_decode(str[i+2], lowvalue)) {
344                         oss << (char) ((highvalue << 4) | lowvalue);
345                         i += 2;
346                 }
347                 else
348                         oss << str[i];
349         }
350         return oss.str();
351 }
352
353 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
354 {
355         u32 result = 0, mask = 0;
356         char *s = &str[0];
357         char *flagstr, *strpos = NULL;
358
359         while ((flagstr = strtok_r(s, ",", &strpos))) {
360                 s = NULL;
361
362                 while (*flagstr == ' ' || *flagstr == '\t')
363                         flagstr++;
364
365                 bool flagset = true;
366                 if (!strncasecmp(flagstr, "no", 2)) {
367                         flagset = false;
368                         flagstr += 2;
369                 }
370
371                 for (int i = 0; flagdesc[i].name; i++) {
372                         if (!strcasecmp(flagstr, flagdesc[i].name)) {
373                                 mask |= flagdesc[i].flag;
374                                 if (flagset)
375                                         result |= flagdesc[i].flag;
376                                 break;
377                         }
378                 }
379         }
380
381         if (flagmask)
382                 *flagmask = mask;
383
384         return result;
385 }
386
387 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
388 {
389         std::string result;
390
391         for (int i = 0; flagdesc[i].name; i++) {
392                 if (flagmask & flagdesc[i].flag) {
393                         if (!(flags & flagdesc[i].flag))
394                                 result += "no";
395
396                         result += flagdesc[i].name;
397                         result += ", ";
398                 }
399         }
400
401         size_t len = result.length();
402         if (len >= 2)
403                 result.erase(len - 2, 2);
404
405         return result;
406 }
407
408 size_t mystrlcpy(char *dst, const char *src, size_t size)
409 {
410         size_t srclen  = strlen(src) + 1;
411         size_t copylen = MYMIN(srclen, size);
412
413         if (copylen > 0) {
414                 memcpy(dst, src, copylen);
415                 dst[copylen - 1] = '\0';
416         }
417
418         return srclen;
419 }
420
421 char *mystrtok_r(char *s, const char *sep, char **lasts)
422 {
423         char *t;
424
425         if (!s)
426                 s = *lasts;
427
428         while (*s && strchr(sep, *s))
429                 s++;
430
431         if (!*s)
432                 return NULL;
433
434         t = s;
435         while (*t) {
436                 if (strchr(sep, *t)) {
437                         *t++ = '\0';
438                         break;
439                 }
440                 t++;
441         }
442
443         *lasts = t;
444         return s;
445 }
446
447 u64 read_seed(const char *str)
448 {
449         char *endptr;
450         u64 num;
451
452         if (str[0] == '0' && str[1] == 'x')
453                 num = strtoull(str, &endptr, 16);
454         else
455                 num = strtoull(str, &endptr, 10);
456
457         if (*endptr)
458                 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
459
460         return num;
461 }
462
463 bool parseColorString(const std::string &value, video::SColor &color, bool quiet)
464 {
465         bool success;
466
467         if (value[0] == '#')
468                 success = parseHexColorString(value, color);
469         else
470                 success = parseNamedColorString(value, color);
471
472         if (!success && !quiet)
473                 errorstream << "Invalid color: \"" << value << "\"" << std::endl;
474
475         return success;
476 }
477
478 static bool parseHexColorString(const std::string &value, video::SColor &color)
479 {
480         unsigned char components[] = { 0x00, 0x00, 0x00, 0xff }; // R,G,B,A
481
482         if (value[0] != '#')
483                 return false;
484
485         size_t len = value.size();
486         bool short_form;
487
488         if (len == 9 || len == 7) // #RRGGBBAA or #RRGGBB
489                 short_form = false;
490         else if (len == 5 || len == 4) // #RGBA or #RGB
491                 short_form = true;
492         else
493                 return false;
494
495         bool success = true;
496
497         for (size_t pos = 1, cc = 0; pos < len; pos++, cc++) {
498                 assert(cc < sizeof components / sizeof components[0]);
499                 if (short_form) {
500                         unsigned char d;
501                         if (!hex_digit_decode(value[pos], d)) {
502                                 success = false;
503                                 break;
504                         }
505                         components[cc] = (d & 0xf) << 4 | (d & 0xf);
506                 } else {
507                         unsigned char d1, d2;
508                         if (!hex_digit_decode(value[pos], d1) ||
509                                         !hex_digit_decode(value[pos+1], d2)) {
510                                 success = false;
511                                 break;
512                         }
513                         components[cc] = (d1 & 0xf) << 4 | (d2 & 0xf);
514                         pos++;  // skip the second digit -- it's already used
515                 }
516         }
517
518         if (success) {
519                 color.setRed(components[0]);
520                 color.setGreen(components[1]);
521                 color.setBlue(components[2]);
522                 color.setAlpha(components[3]);
523         }
524
525         return success;
526 }
527
528 struct ColorContainer {
529         ColorContainer();
530         std::map<const std::string, u32> colors;
531 };
532
533 ColorContainer::ColorContainer()
534 {
535         colors["aliceblue"]              = 0xf0f8ff;
536         colors["antiquewhite"]           = 0xfaebd7;
537         colors["aqua"]                   = 0x00ffff;
538         colors["aquamarine"]             = 0x7fffd4;
539         colors["azure"]                  = 0xf0ffff;
540         colors["beige"]                  = 0xf5f5dc;
541         colors["bisque"]                 = 0xffe4c4;
542         colors["black"]                  = 00000000;
543         colors["blanchedalmond"]         = 0xffebcd;
544         colors["blue"]                   = 0x0000ff;
545         colors["blueviolet"]             = 0x8a2be2;
546         colors["brown"]                  = 0xa52a2a;
547         colors["burlywood"]              = 0xdeb887;
548         colors["cadetblue"]              = 0x5f9ea0;
549         colors["chartreuse"]             = 0x7fff00;
550         colors["chocolate"]              = 0xd2691e;
551         colors["coral"]                  = 0xff7f50;
552         colors["cornflowerblue"]         = 0x6495ed;
553         colors["cornsilk"]               = 0xfff8dc;
554         colors["crimson"]                = 0xdc143c;
555         colors["cyan"]                   = 0x00ffff;
556         colors["darkblue"]               = 0x00008b;
557         colors["darkcyan"]               = 0x008b8b;
558         colors["darkgoldenrod"]          = 0xb8860b;
559         colors["darkgray"]               = 0xa9a9a9;
560         colors["darkgreen"]              = 0x006400;
561         colors["darkkhaki"]              = 0xbdb76b;
562         colors["darkmagenta"]            = 0x8b008b;
563         colors["darkolivegreen"]         = 0x556b2f;
564         colors["darkorange"]             = 0xff8c00;
565         colors["darkorchid"]             = 0x9932cc;
566         colors["darkred"]                = 0x8b0000;
567         colors["darksalmon"]             = 0xe9967a;
568         colors["darkseagreen"]           = 0x8fbc8f;
569         colors["darkslateblue"]          = 0x483d8b;
570         colors["darkslategray"]          = 0x2f4f4f;
571         colors["darkturquoise"]          = 0x00ced1;
572         colors["darkviolet"]             = 0x9400d3;
573         colors["deeppink"]               = 0xff1493;
574         colors["deepskyblue"]            = 0x00bfff;
575         colors["dimgray"]                = 0x696969;
576         colors["dodgerblue"]             = 0x1e90ff;
577         colors["firebrick"]              = 0xb22222;
578         colors["floralwhite"]            = 0xfffaf0;
579         colors["forestgreen"]            = 0x228b22;
580         colors["fuchsia"]                = 0xff00ff;
581         colors["gainsboro"]              = 0xdcdcdc;
582         colors["ghostwhite"]             = 0xf8f8ff;
583         colors["gold"]                   = 0xffd700;
584         colors["goldenrod"]              = 0xdaa520;
585         colors["gray"]                   = 0x808080;
586         colors["green"]                  = 0x008000;
587         colors["greenyellow"]            = 0xadff2f;
588         colors["honeydew"]               = 0xf0fff0;
589         colors["hotpink"]                = 0xff69b4;
590         colors["indianred"]              = 0xcd5c5c;
591         colors["indigo"]                 = 0x4b0082;
592         colors["ivory"]                  = 0xfffff0;
593         colors["khaki"]                  = 0xf0e68c;
594         colors["lavender"]               = 0xe6e6fa;
595         colors["lavenderblush"]          = 0xfff0f5;
596         colors["lawngreen"]              = 0x7cfc00;
597         colors["lemonchiffon"]           = 0xfffacd;
598         colors["lightblue"]              = 0xadd8e6;
599         colors["lightcoral"]             = 0xf08080;
600         colors["lightcyan"]              = 0xe0ffff;
601         colors["lightgoldenrodyellow"]   = 0xfafad2;
602         colors["lightgray"]              = 0xd3d3d3;
603         colors["lightgreen"]             = 0x90ee90;
604         colors["lightpink"]              = 0xffb6c1;
605         colors["lightsalmon"]            = 0xffa07a;
606         colors["lightseagreen"]          = 0x20b2aa;
607         colors["lightskyblue"]           = 0x87cefa;
608         colors["lightslategray"]         = 0x778899;
609         colors["lightsteelblue"]         = 0xb0c4de;
610         colors["lightyellow"]            = 0xffffe0;
611         colors["lime"]                   = 0x00ff00;
612         colors["limegreen"]              = 0x32cd32;
613         colors["linen"]                  = 0xfaf0e6;
614         colors["magenta"]                = 0xff00ff;
615         colors["maroon"]                 = 0x800000;
616         colors["mediumaquamarine"]       = 0x66cdaa;
617         colors["mediumblue"]             = 0x0000cd;
618         colors["mediumorchid"]           = 0xba55d3;
619         colors["mediumpurple"]           = 0x9370db;
620         colors["mediumseagreen"]         = 0x3cb371;
621         colors["mediumslateblue"]        = 0x7b68ee;
622         colors["mediumspringgreen"]      = 0x00fa9a;
623         colors["mediumturquoise"]        = 0x48d1cc;
624         colors["mediumvioletred"]        = 0xc71585;
625         colors["midnightblue"]           = 0x191970;
626         colors["mintcream"]              = 0xf5fffa;
627         colors["mistyrose"]              = 0xffe4e1;
628         colors["moccasin"]               = 0xffe4b5;
629         colors["navajowhite"]            = 0xffdead;
630         colors["navy"]                   = 0x000080;
631         colors["oldlace"]                = 0xfdf5e6;
632         colors["olive"]                  = 0x808000;
633         colors["olivedrab"]              = 0x6b8e23;
634         colors["orange"]                 = 0xffa500;
635         colors["orangered"]              = 0xff4500;
636         colors["orchid"]                 = 0xda70d6;
637         colors["palegoldenrod"]          = 0xeee8aa;
638         colors["palegreen"]              = 0x98fb98;
639         colors["paleturquoise"]          = 0xafeeee;
640         colors["palevioletred"]          = 0xdb7093;
641         colors["papayawhip"]             = 0xffefd5;
642         colors["peachpuff"]              = 0xffdab9;
643         colors["peru"]                   = 0xcd853f;
644         colors["pink"]                   = 0xffc0cb;
645         colors["plum"]                   = 0xdda0dd;
646         colors["powderblue"]             = 0xb0e0e6;
647         colors["purple"]                 = 0x800080;
648         colors["red"]                    = 0xff0000;
649         colors["rosybrown"]              = 0xbc8f8f;
650         colors["royalblue"]              = 0x4169e1;
651         colors["saddlebrown"]            = 0x8b4513;
652         colors["salmon"]                 = 0xfa8072;
653         colors["sandybrown"]             = 0xf4a460;
654         colors["seagreen"]               = 0x2e8b57;
655         colors["seashell"]               = 0xfff5ee;
656         colors["sienna"]                 = 0xa0522d;
657         colors["silver"]                 = 0xc0c0c0;
658         colors["skyblue"]                = 0x87ceeb;
659         colors["slateblue"]              = 0x6a5acd;
660         colors["slategray"]              = 0x708090;
661         colors["snow"]                   = 0xfffafa;
662         colors["springgreen"]            = 0x00ff7f;
663         colors["steelblue"]              = 0x4682b4;
664         colors["tan"]                    = 0xd2b48c;
665         colors["teal"]                   = 0x008080;
666         colors["thistle"]                = 0xd8bfd8;
667         colors["tomato"]                 = 0xff6347;
668         colors["turquoise"]              = 0x40e0d0;
669         colors["violet"]                 = 0xee82ee;
670         colors["wheat"]                  = 0xf5deb3;
671         colors["white"]                  = 0xffffff;
672         colors["whitesmoke"]             = 0xf5f5f5;
673         colors["yellow"]                 = 0xffff00;
674         colors["yellowgreen"]            = 0x9acd32;
675
676 }
677
678 static const ColorContainer named_colors;
679
680 static bool parseNamedColorString(const std::string &value, video::SColor &color)
681 {
682         std::string color_name;
683         std::string alpha_string;
684
685         /* If the string has a # in it, assume this is the start of a specified
686          * alpha value (if it isn't the string is invalid and the error will be
687          * caught later on, either because the color name won't be found or the
688          * alpha value will fail conversion)
689          */
690         size_t alpha_pos = value.find('#');
691         if (alpha_pos != std::string::npos) {
692                 color_name = value.substr(0, alpha_pos);
693                 alpha_string = value.substr(alpha_pos + 1);
694         } else {
695                 color_name = value;
696         }
697
698         color_name = lowercase(value);
699
700         std::map<const std::string, unsigned>::const_iterator it;
701         it = named_colors.colors.find(color_name);
702         if (it == named_colors.colors.end())
703                 return false;
704
705         u32 color_temp = it->second;
706
707         /* An empty string for alpha is ok (none of the color table entries
708          * have an alpha value either). Color strings without an alpha specified
709          * are interpreted as fully opaque
710          *
711          * For named colors the supplied alpha string (representing a hex value)
712          * must be exactly two digits. For example:  colorname#08
713          */
714         if (!alpha_string.empty()) {
715                 if (alpha_string.length() != 2)
716                         return false;
717
718                 unsigned char d1, d2;
719                 if (!hex_digit_decode(alpha_string.at(0), d1)
720                                 || !hex_digit_decode(alpha_string.at(1), d2))
721                         return false;
722                 color_temp |= ((d1 & 0xf) << 4 | (d2 & 0xf)) << 24;
723         } else {
724                 color_temp |= 0xff << 24;  // Fully opaque
725         }
726
727         color = video::SColor(color_temp);
728
729         return true;
730 }
731
732 void str_replace(std::string &str, char from, char to)
733 {
734         std::replace(str.begin(), str.end(), from, to);
735 }