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"
35 #define _WIN32_WINNT 0x0501
39 #if defined(_ICONV_H_) && (defined(__FreeBSD__) || defined(__NetBSD__) || \
40 defined(__OpenBSD__) || defined(__DragonFly__))
41 #define BSD_ICONV_USED
44 static bool parseHexColorString(const std::string &value, video::SColor &color);
45 static bool parseNamedColorString(const std::string &value, video::SColor &color);
49 bool convert(const char *to, const char *from, char *outbuf,
50 size_t outbuf_size, char *inbuf, size_t inbuf_size)
52 iconv_t cd = iconv_open(to, from);
55 const char *inbuf_ptr = inbuf;
57 char *inbuf_ptr = inbuf;
60 char *outbuf_ptr = outbuf;
62 size_t *inbuf_left_ptr = &inbuf_size;
63 size_t *outbuf_left_ptr = &outbuf_size;
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) {
72 old_size = inbuf_size;
79 std::wstring utf8_to_wide(const std::string &input)
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);
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);
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;
95 return L"<invalid UTF-8 string>";
97 std::wstring out((wchar_t *)outbuf);
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)
109 return wide_to_narrow(input);
112 std::string wide_to_utf8(const std::wstring &input)
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;
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);
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;
128 return "<invalid wstring>";
130 std::string out(outbuf);
141 std::wstring utf8_to_wide(const std::string &input)
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);
153 std::string wide_to_utf8(const std::wstring &input)
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);
167 wchar_t *utf8_to_wide_c(const char *str)
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));
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)
181 wchar_t *nstr = NULL;
183 int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, 0, 0);
185 errorstream<<"gettext: MultiByteToWideChar returned null"<<std::endl;
187 nstr = new wchar_t[nResult];
188 MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, (WCHAR *) nstr, nResult);
191 size_t len = strlen(str);
192 nstr = new wchar_t[len + 1];
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));
205 const wchar_t* wide_chars =
206 L" !\"#$%&'()*+,-./0123456789:;<=>?@"
207 L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
208 L"abcdefghijklmnopqrstuvwxyz{|}~";
210 int wctomb(char *s, wchar_t wc)
212 for (unsigned int j = 0; j < (sizeof(wide_chars)/sizeof(wchar_t));j++) {
213 if (wc == wide_chars[j]) {
217 else if (wc == L'\n') {
225 int mbtowc(wchar_t *pwc, const char *s, size_t n)
227 std::wstring intermediate = narrow_to_wide(s);
229 if (intermediate.length() > 0) {
230 *pwc = intermediate[0];
238 std::wstring narrow_to_wide(const std::string &mbs) {
239 size_t wcl = mbs.size();
241 std::wstring retval = L"";
243 for (unsigned int i = 0; i < wcl; i++) {
244 if (((unsigned char) mbs[i] >31) &&
245 ((unsigned char) mbs[i] < 127)) {
247 retval += wide_chars[(unsigned char) mbs[i] -32];
250 else if (mbs[i] == '\n') {
260 std::wstring narrow_to_wide(const std::string &mbs)
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>";
275 std::string wide_to_narrow(const std::wstring &wcs) {
276 size_t mbl = wcs.size()*4;
278 std::string retval = "";
279 for (unsigned int i = 0; i < wcs.size(); i++) {
280 wchar_t char1 = (wchar_t) wcs[i];
282 if (char1 == L'\n') {
287 for (unsigned int j = 0; j < wcslen(wide_chars);j++) {
288 wchar_t char2 = (wchar_t) wide_chars[j];
290 if (char1 == char2) {
303 std::string wide_to_narrow(const std::wstring &wcs)
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!";
317 std::string urlencode(const std::string &str)
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 == '~') {
329 << url_hex_chars[(c & 0xf0) >> 4]
330 << url_hex_chars[c & 0x0f];
336 std::string urldecode(const std::string &str)
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;
343 hex_digit_decode(str[i+1], highvalue) &&
344 hex_digit_decode(str[i+2], lowvalue)) {
345 oss << (char) ((highvalue << 4) | lowvalue);
354 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
362 while ((flagstr = strtok_r(s, ",", &strpos))) {
365 while (*flagstr == ' ' || *flagstr == '\t')
369 if (!strncasecmp(flagstr, "no", 2)) {
374 for (int i = 0; flagdesc[i].name; i++) {
375 if (!strcasecmp(flagstr, flagdesc[i].name)) {
376 mask |= flagdesc[i].flag;
378 result |= flagdesc[i].flag;
390 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
394 for (int i = 0; flagdesc[i].name; i++) {
395 if (flagmask & flagdesc[i].flag) {
396 if (!(flags & flagdesc[i].flag))
399 result += flagdesc[i].name;
404 size_t len = result.length();
406 result.erase(len - 2, 2);
411 size_t mystrlcpy(char *dst, const char *src, size_t size)
413 size_t srclen = strlen(src) + 1;
414 size_t copylen = MYMIN(srclen, size);
417 memcpy(dst, src, copylen);
418 dst[copylen - 1] = '\0';
424 char *mystrtok_r(char *s, const char *sep, char **lasts)
431 while (*s && strchr(sep, *s))
439 if (strchr(sep, *t)) {
450 u64 read_seed(const char *str)
455 if (str[0] == '0' && str[1] == 'x')
456 num = strtoull(str, &endptr, 16);
458 num = strtoull(str, &endptr, 10);
461 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
466 bool parseColorString(const std::string &value, video::SColor &color, bool quiet)
471 success = parseHexColorString(value, color);
473 success = parseNamedColorString(value, color);
475 if (!success && !quiet)
476 errorstream << "Invalid color: \"" << value << "\"" << std::endl;
481 static bool parseHexColorString(const std::string &value, video::SColor &color)
483 unsigned char components[] = { 0x00, 0x00, 0x00, 0xff }; // R,G,B,A
488 size_t len = value.size();
491 if (len == 9 || len == 7) // #RRGGBBAA or #RRGGBB
493 else if (len == 5 || len == 4) // #RGBA or #RGB
500 for (size_t pos = 1, cc = 0; pos < len; pos++, cc++) {
501 assert(cc < sizeof components / sizeof components[0]);
504 if (!hex_digit_decode(value[pos], d)) {
508 components[cc] = (d & 0xf) << 4 | (d & 0xf);
510 unsigned char d1, d2;
511 if (!hex_digit_decode(value[pos], d1) ||
512 !hex_digit_decode(value[pos+1], d2)) {
516 components[cc] = (d1 & 0xf) << 4 | (d2 & 0xf);
517 pos++; // skip the second digit -- it's already used
522 color.setRed(components[0]);
523 color.setGreen(components[1]);
524 color.setBlue(components[2]);
525 color.setAlpha(components[3]);
531 struct ColorContainer {
533 std::map<const std::string, u32> colors;
536 ColorContainer::ColorContainer()
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;
688 static const ColorContainer named_colors;
690 static bool parseNamedColorString(const std::string &value, video::SColor &color)
692 std::string color_name;
693 std::string alpha_string;
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)
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);
708 color_name = lowercase(value);
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())
715 u32 color_temp = it->second;
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
721 * For named colors the supplied alpha string (representing a hex value)
722 * must be exactly two digits. For example: colorname#08
724 if (!alpha_string.empty()) {
725 if (alpha_string.length() != 2)
728 unsigned char d1, d2;
729 if (!hex_digit_decode(alpha_string.at(0), d1)
730 || !hex_digit_decode(alpha_string.at(1), d2))
732 color_temp |= ((d1 & 0xf) << 4 | (d2 & 0xf)) << 24;
734 color_temp |= 0xff << 24; // Fully opaque
737 color = video::SColor(color_temp);
742 void str_replace(std::string &str, char from, char to)
744 std::replace(str.begin(), str.end(), from, to);