3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
26 #include "../porting.h"
32 static bool parseHexColorString(const std::string &value, video::SColor &color);
33 static bool parseNamedColorString(const std::string &value, video::SColor &color);
36 // You must free the returned string!
37 // The returned string is allocated using new
38 wchar_t *narrow_to_wide_c(const char *str)
42 int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, 0, 0);
44 errorstream<<"gettext: MultiByteToWideChar returned null"<<std::endl;
46 nstr = new wchar_t[nResult];
47 MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, (WCHAR *) nstr, nResult);
50 size_t len = strlen(str);
51 nstr = new wchar_t[len+1];
53 std::wstring intermediate = narrow_to_wide(str);
54 memset(nstr, 0, (len + 1) * sizeof(wchar_t));
55 memcpy(nstr, intermediate.c_str(), len * sizeof(wchar_t));
64 const wchar_t* wide_chars =
65 L" !\"#$%&'()*+,-./0123456789:;<=>?@"
66 L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
67 L"abcdefghijklmnopqrstuvwxyz{|}~";
69 int wctomb(char *s, wchar_t wc)
71 for (unsigned int j = 0; j < (sizeof(wide_chars)/sizeof(wchar_t));j++) {
72 if (wc == wide_chars[j]) {
76 else if (wc == L'\n') {
84 int mbtowc(wchar_t *pwc, const char *s, size_t n)
86 std::wstring intermediate = narrow_to_wide(s);
88 if (intermediate.length() > 0) {
89 *pwc = intermediate[0];
97 std::wstring narrow_to_wide(const std::string &mbs) {
98 size_t wcl = mbs.size();
100 std::wstring retval = L"";
102 for (unsigned int i = 0; i < wcl; i++) {
103 if (((unsigned char) mbs[i] >31) &&
104 ((unsigned char) mbs[i] < 127)) {
106 retval += wide_chars[(unsigned char) mbs[i] -32];
109 else if (mbs[i] == '\n') {
119 std::wstring narrow_to_wide(const std::string &mbs)
121 size_t wcl = mbs.size();
122 Buffer<wchar_t> wcs(wcl + 1);
123 size_t len = mbstowcs(*wcs, mbs.c_str(), wcl);
124 if (len == (size_t)(-1))
125 return L"<invalid multibyte string>";
134 std::string wide_to_narrow(const std::wstring &wcs) {
135 size_t mbl = wcs.size()*4;
137 std::string retval = "";
138 for (unsigned int i = 0; i < wcs.size(); i++) {
139 wchar_t char1 = (wchar_t) wcs[i];
141 if (char1 == L'\n') {
146 for (unsigned int j = 0; j < wcslen(wide_chars);j++) {
147 wchar_t char2 = (wchar_t) wide_chars[j];
149 if (char1 == char2) {
162 std::string wide_to_narrow(const std::wstring &wcs)
164 size_t mbl = wcs.size() * 4;
165 SharedBuffer<char> mbs(mbl+1);
166 size_t len = wcstombs(*mbs, wcs.c_str(), mbl);
167 if (len == (size_t)(-1))
168 return "Character conversion failed!";
176 std::string urlencode(std::string str)
178 // Encodes non-unreserved URI characters by a percent sign
179 // followed by two hex digits. See RFC 3986, section 2.3.
180 static const char url_hex_chars[] = "0123456789ABCDEF";
181 std::ostringstream oss(std::ios::binary);
182 for (u32 i = 0; i < str.size(); i++) {
183 unsigned char c = str[i];
184 if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~')
188 << url_hex_chars[(c & 0xf0) >> 4]
189 << url_hex_chars[c & 0x0f];
194 std::string urldecode(std::string str)
196 // Inverse of urlencode
197 std::ostringstream oss(std::ios::binary);
198 for (u32 i = 0; i < str.size(); i++) {
199 unsigned char highvalue, lowvalue;
201 hex_digit_decode(str[i+1], highvalue) &&
202 hex_digit_decode(str[i+2], lowvalue)) {
203 oss << (char) ((highvalue << 4) | lowvalue);
212 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
214 u32 result = 0, mask = 0;
216 char *flagstr, *strpos = NULL;
218 while ((flagstr = strtok_r(s, ",", &strpos))) {
221 while (*flagstr == ' ' || *flagstr == '\t')
225 if (!strncasecmp(flagstr, "no", 2)) {
230 for (int i = 0; flagdesc[i].name; i++) {
231 if (!strcasecmp(flagstr, flagdesc[i].name)) {
232 mask |= flagdesc[i].flag;
234 result |= flagdesc[i].flag;
246 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
250 for (int i = 0; flagdesc[i].name; i++) {
251 if (flagmask & flagdesc[i].flag) {
252 if (!(flags & flagdesc[i].flag))
255 result += flagdesc[i].name;
260 size_t len = result.length();
262 result.erase(len - 2, 2);
267 size_t mystrlcpy(char *dst, const char *src, size_t size)
269 size_t srclen = strlen(src) + 1;
270 size_t copylen = MYMIN(srclen, size);
273 memcpy(dst, src, copylen);
274 dst[copylen - 1] = '\0';
280 char *mystrtok_r(char *s, const char *sep, char **lasts)
287 while (*s && strchr(sep, *s))
295 if (strchr(sep, *t)) {
306 u64 read_seed(const char *str)
311 if (str[0] == '0' && str[1] == 'x')
312 num = strtoull(str, &endptr, 16);
314 num = strtoull(str, &endptr, 10);
317 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
322 bool parseColorString(const std::string &value, video::SColor &color, bool quiet)
327 success = parseHexColorString(value, color);
329 success = parseNamedColorString(value, color);
331 if (!success && !quiet)
332 errorstream << "Invalid color: \"" << value << "\"" << std::endl;
337 static bool parseHexColorString(const std::string &value, video::SColor &color)
339 unsigned char components[] = { 0x00, 0x00, 0x00, 0xff }; // R,G,B,A
344 size_t len = value.size();
347 if (len == 9 || len == 7) // #RRGGBBAA or #RRGGBB
349 else if (len == 5 || len == 4) // #RGBA or #RGB
356 for (size_t pos = 1, cc = 0; pos < len; pos++, cc++) {
357 assert(cc < sizeof components / sizeof components[0]);
360 if (!hex_digit_decode(value[pos], d)) {
364 components[cc] = (d & 0xf) << 4 | (d & 0xf);
366 unsigned char d1, d2;
367 if (!hex_digit_decode(value[pos], d1) ||
368 !hex_digit_decode(value[pos+1], d2)) {
372 components[cc] = (d1 & 0xf) << 4 | (d2 & 0xf);
373 pos++; // skip the second digit -- it's already used
378 color.setRed(components[0]);
379 color.setGreen(components[1]);
380 color.setBlue(components[2]);
381 color.setAlpha(components[3]);
387 struct ColorContainer {
389 std::map<const std::string, u32> colors;
392 ColorContainer::ColorContainer()
394 colors["aliceblue"] = 0xf0f8ff;
395 colors["antiquewhite"] = 0xfaebd7;
396 colors["aqua"] = 0x00ffff;
397 colors["aquamarine"] = 0x7fffd4;
398 colors["azure"] = 0xf0ffff;
399 colors["beige"] = 0xf5f5dc;
400 colors["bisque"] = 0xffe4c4;
401 colors["black"] = 00000000;
402 colors["blanchedalmond"] = 0xffebcd;
403 colors["blue"] = 0x0000ff;
404 colors["blueviolet"] = 0x8a2be2;
405 colors["brown"] = 0xa52a2a;
406 colors["burlywood"] = 0xdeb887;
407 colors["cadetblue"] = 0x5f9ea0;
408 colors["chartreuse"] = 0x7fff00;
409 colors["chocolate"] = 0xd2691e;
410 colors["coral"] = 0xff7f50;
411 colors["cornflowerblue"] = 0x6495ed;
412 colors["cornsilk"] = 0xfff8dc;
413 colors["crimson"] = 0xdc143c;
414 colors["cyan"] = 0x00ffff;
415 colors["darkblue"] = 0x00008b;
416 colors["darkcyan"] = 0x008b8b;
417 colors["darkgoldenrod"] = 0xb8860b;
418 colors["darkgray"] = 0xa9a9a9;
419 colors["darkgreen"] = 0x006400;
420 colors["darkkhaki"] = 0xbdb76b;
421 colors["darkmagenta"] = 0x8b008b;
422 colors["darkolivegreen"] = 0x556b2f;
423 colors["darkorange"] = 0xff8c00;
424 colors["darkorchid"] = 0x9932cc;
425 colors["darkred"] = 0x8b0000;
426 colors["darksalmon"] = 0xe9967a;
427 colors["darkseagreen"] = 0x8fbc8f;
428 colors["darkslateblue"] = 0x483d8b;
429 colors["darkslategray"] = 0x2f4f4f;
430 colors["darkturquoise"] = 0x00ced1;
431 colors["darkviolet"] = 0x9400d3;
432 colors["deeppink"] = 0xff1493;
433 colors["deepskyblue"] = 0x00bfff;
434 colors["dimgray"] = 0x696969;
435 colors["dodgerblue"] = 0x1e90ff;
436 colors["firebrick"] = 0xb22222;
437 colors["floralwhite"] = 0xfffaf0;
438 colors["forestgreen"] = 0x228b22;
439 colors["fuchsia"] = 0xff00ff;
440 colors["gainsboro"] = 0xdcdcdc;
441 colors["ghostwhite"] = 0xf8f8ff;
442 colors["gold"] = 0xffd700;
443 colors["goldenrod"] = 0xdaa520;
444 colors["gray"] = 0x808080;
445 colors["green"] = 0x008000;
446 colors["greenyellow"] = 0xadff2f;
447 colors["honeydew"] = 0xf0fff0;
448 colors["hotpink"] = 0xff69b4;
449 colors["indianred "] = 0xcd5c5c;
450 colors["indigo "] = 0x4b0082;
451 colors["ivory"] = 0xfffff0;
452 colors["khaki"] = 0xf0e68c;
453 colors["lavender"] = 0xe6e6fa;
454 colors["lavenderblush"] = 0xfff0f5;
455 colors["lawngreen"] = 0x7cfc00;
456 colors["lemonchiffon"] = 0xfffacd;
457 colors["lightblue"] = 0xadd8e6;
458 colors["lightcoral"] = 0xf08080;
459 colors["lightcyan"] = 0xe0ffff;
460 colors["lightgoldenrodyellow"] = 0xfafad2;
461 colors["lightgray"] = 0xd3d3d3;
462 colors["lightgreen"] = 0x90ee90;
463 colors["lightpink"] = 0xffb6c1;
464 colors["lightsalmon"] = 0xffa07a;
465 colors["lightseagreen"] = 0x20b2aa;
466 colors["lightskyblue"] = 0x87cefa;
467 colors["lightslategray"] = 0x778899;
468 colors["lightsteelblue"] = 0xb0c4de;
469 colors["lightyellow"] = 0xffffe0;
470 colors["lime"] = 0x00ff00;
471 colors["limegreen"] = 0x32cd32;
472 colors["linen"] = 0xfaf0e6;
473 colors["magenta"] = 0xff00ff;
474 colors["maroon"] = 0x800000;
475 colors["mediumaquamarine"] = 0x66cdaa;
476 colors["mediumblue"] = 0x0000cd;
477 colors["mediumorchid"] = 0xba55d3;
478 colors["mediumpurple"] = 0x9370db;
479 colors["mediumseagreen"] = 0x3cb371;
480 colors["mediumslateblue"] = 0x7b68ee;
481 colors["mediumspringgreen"] = 0x00fa9a;
482 colors["mediumturquoise"] = 0x48d1cc;
483 colors["mediumvioletred"] = 0xc71585;
484 colors["midnightblue"] = 0x191970;
485 colors["mintcream"] = 0xf5fffa;
486 colors["mistyrose"] = 0xffe4e1;
487 colors["moccasin"] = 0xffe4b5;
488 colors["navajowhite"] = 0xffdead;
489 colors["navy"] = 0x000080;
490 colors["oldlace"] = 0xfdf5e6;
491 colors["olive"] = 0x808000;
492 colors["olivedrab"] = 0x6b8e23;
493 colors["orange"] = 0xffa500;
494 colors["orangered"] = 0xff4500;
495 colors["orchid"] = 0xda70d6;
496 colors["palegoldenrod"] = 0xeee8aa;
497 colors["palegreen"] = 0x98fb98;
498 colors["paleturquoise"] = 0xafeeee;
499 colors["palevioletred"] = 0xdb7093;
500 colors["papayawhip"] = 0xffefd5;
501 colors["peachpuff"] = 0xffdab9;
502 colors["peru"] = 0xcd853f;
503 colors["pink"] = 0xffc0cb;
504 colors["plum"] = 0xdda0dd;
505 colors["powderblue"] = 0xb0e0e6;
506 colors["purple"] = 0x800080;
507 colors["red"] = 0xff0000;
508 colors["rosybrown"] = 0xbc8f8f;
509 colors["royalblue"] = 0x4169e1;
510 colors["saddlebrown"] = 0x8b4513;
511 colors["salmon"] = 0xfa8072;
512 colors["sandybrown"] = 0xf4a460;
513 colors["seagreen"] = 0x2e8b57;
514 colors["seashell"] = 0xfff5ee;
515 colors["sienna"] = 0xa0522d;
516 colors["silver"] = 0xc0c0c0;
517 colors["skyblue"] = 0x87ceeb;
518 colors["slateblue"] = 0x6a5acd;
519 colors["slategray"] = 0x708090;
520 colors["snow"] = 0xfffafa;
521 colors["springgreen"] = 0x00ff7f;
522 colors["steelblue"] = 0x4682b4;
523 colors["tan"] = 0xd2b48c;
524 colors["teal"] = 0x008080;
525 colors["thistle"] = 0xd8bfd8;
526 colors["tomato"] = 0xff6347;
527 colors["turquoise"] = 0x40e0d0;
528 colors["violet"] = 0xee82ee;
529 colors["wheat"] = 0xf5deb3;
530 colors["white"] = 0xffffff;
531 colors["whitesmoke"] = 0xf5f5f5;
532 colors["yellow"] = 0xffff00;
533 colors["yellowgreen"] = 0x9acd32;
537 static const ColorContainer named_colors;
539 static bool parseNamedColorString(const std::string &value, video::SColor &color)
541 std::string color_name;
542 std::string alpha_string;
544 /* If the string has a # in it, assume this is the start of a specified
545 * alpha value (if it isn't the string is invalid and the error will be
546 * caught later on, either because the color name won't be found or the
547 * alpha value will fail conversion)
549 size_t alpha_pos = value.find('#');
550 if (alpha_pos != std::string::npos) {
551 color_name = value.substr(0, alpha_pos);
552 alpha_string = value.substr(alpha_pos + 1);
557 color_name = lowercase(value);
559 std::map<const std::string, unsigned>::const_iterator it;
560 it = named_colors.colors.find(color_name);
561 if (it == named_colors.colors.end())
564 u32 color_temp = it->second;
566 /* An empty string for alpha is ok (none of the color table entries
567 * have an alpha value either). Color strings without an alpha specified
568 * are interpreted as fully opaque
570 * For named colors the supplied alpha string (representing a hex value)
571 * must be exactly two digits. For example: colorname#08
573 if (!alpha_string.empty()) {
574 if (alpha_string.length() != 2)
577 unsigned char d1, d2;
578 if (!hex_digit_decode(alpha_string.at(0), d1)
579 || !hex_digit_decode(alpha_string.at(1), d2))
581 color_temp |= ((d1 & 0xf) << 4 | (d2 & 0xf)) << 24;
583 color_temp |= 0xff << 24; // Fully opaque
586 color = video::SColor(color_temp);
591 void str_replace(std::string &str, char from, char to)
593 std::replace(str.begin(), str.end(), from, to);