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