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"
36 #define _WIN32_WINNT 0x0501
40 #if defined(_ICONV_H_) && (defined(__FreeBSD__) || defined(__NetBSD__) || \
41 defined(__OpenBSD__) || defined(__DragonFly__))
42 #define BSD_ICONV_USED
45 static bool parseHexColorString(const std::string &value, video::SColor &color);
46 static bool parseNamedColorString(const std::string &value, video::SColor &color);
50 bool convert(const char *to, const char *from, char *outbuf,
51 size_t outbuf_size, char *inbuf, size_t inbuf_size)
53 iconv_t cd = iconv_open(to, from);
56 const char *inbuf_ptr = inbuf;
58 char *inbuf_ptr = inbuf;
61 char *outbuf_ptr = outbuf;
63 size_t *inbuf_left_ptr = &inbuf_size;
64 size_t *outbuf_left_ptr = &outbuf_size;
66 size_t old_size = inbuf_size;
67 while (inbuf_size > 0) {
68 iconv(cd, &inbuf_ptr, inbuf_left_ptr, &outbuf_ptr, outbuf_left_ptr);
69 if (inbuf_size == old_size) {
73 old_size = inbuf_size;
80 std::wstring utf8_to_wide(const std::string &input)
82 size_t inbuf_size = input.length() + 1;
83 // maximum possible size, every character is sizeof(wchar_t) bytes
84 size_t outbuf_size = (input.length() + 1) * sizeof(wchar_t);
86 char *inbuf = new char[inbuf_size];
87 memcpy(inbuf, input.c_str(), inbuf_size);
88 char *outbuf = new char[outbuf_size];
89 memset(outbuf, 0, outbuf_size);
91 if (!convert("WCHAR_T", "UTF-8", outbuf, outbuf_size, inbuf, inbuf_size)) {
92 infostream << "Couldn't convert UTF-8 string 0x" << hex_encode(input)
93 << " into wstring" << std::endl;
96 return L"<invalid UTF-8 string>";
98 std::wstring out((wchar_t *)outbuf);
107 // TODO: this is an ugly fix for wide_to_utf8 somehow not working on android
108 std::string wide_to_utf8(const std::wstring &input)
110 return wide_to_narrow(input);
113 std::string wide_to_utf8(const std::wstring &input)
115 size_t inbuf_size = (input.length() + 1) * sizeof(wchar_t);
116 // maximum possible size: utf-8 encodes codepoints using 1 up to 6 bytes
117 size_t outbuf_size = (input.length() + 1) * 6;
119 char *inbuf = new char[inbuf_size];
120 memcpy(inbuf, input.c_str(), inbuf_size);
121 char *outbuf = new char[outbuf_size];
122 memset(outbuf, 0, outbuf_size);
124 if (!convert("UTF-8", "WCHAR_T", outbuf, outbuf_size, inbuf, inbuf_size)) {
125 infostream << "Couldn't convert wstring 0x" << hex_encode(inbuf, inbuf_size)
126 << " into UTF-8 string" << std::endl;
129 return "<invalid wstring>";
131 std::string out(outbuf);
142 std::wstring utf8_to_wide(const std::string &input)
144 size_t outbuf_size = input.size() + 1;
145 wchar_t *outbuf = new wchar_t[outbuf_size];
146 memset(outbuf, 0, outbuf_size * sizeof(wchar_t));
147 MultiByteToWideChar(CP_UTF8, 0, input.c_str(), input.size(),
148 outbuf, outbuf_size);
149 std::wstring out(outbuf);
154 std::string wide_to_utf8(const std::wstring &input)
156 size_t outbuf_size = (input.size() + 1) * 6;
157 char *outbuf = new char[outbuf_size];
158 memset(outbuf, 0, outbuf_size);
159 WideCharToMultiByte(CP_UTF8, 0, input.c_str(), input.size(),
160 outbuf, outbuf_size, NULL, NULL);
161 std::string out(outbuf);
168 wchar_t *utf8_to_wide_c(const char *str)
170 std::wstring ret = utf8_to_wide(std::string(str)).c_str();
171 size_t len = ret.length();
172 wchar_t *ret_c = new wchar_t[len + 1];
173 memset(ret_c, 0, (len + 1) * sizeof(wchar_t));
174 memcpy(ret_c, ret.c_str(), len * sizeof(wchar_t));
178 // You must free the returned string!
179 // The returned string is allocated using new
180 wchar_t *narrow_to_wide_c(const char *str)
182 wchar_t *nstr = NULL;
184 int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, 0, 0);
186 errorstream<<"gettext: MultiByteToWideChar returned null"<<std::endl;
188 nstr = new wchar_t[nResult];
189 MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, (WCHAR *) nstr, nResult);
192 size_t len = strlen(str);
193 nstr = new wchar_t[len + 1];
195 std::wstring intermediate = narrow_to_wide(str);
196 memset(nstr, 0, (len + 1) * sizeof(wchar_t));
197 memcpy(nstr, intermediate.c_str(), len * sizeof(wchar_t));
206 const wchar_t* wide_chars =
207 L" !\"#$%&'()*+,-./0123456789:;<=>?@"
208 L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
209 L"abcdefghijklmnopqrstuvwxyz{|}~";
211 int wctomb(char *s, wchar_t wc)
213 for (unsigned int j = 0; j < (sizeof(wide_chars)/sizeof(wchar_t));j++) {
214 if (wc == wide_chars[j]) {
218 else if (wc == L'\n') {
226 int mbtowc(wchar_t *pwc, const char *s, size_t n)
228 std::wstring intermediate = narrow_to_wide(s);
230 if (intermediate.length() > 0) {
231 *pwc = intermediate[0];
239 std::wstring narrow_to_wide(const std::string &mbs) {
240 size_t wcl = mbs.size();
242 std::wstring retval = L"";
244 for (unsigned int i = 0; i < wcl; i++) {
245 if (((unsigned char) mbs[i] >31) &&
246 ((unsigned char) mbs[i] < 127)) {
248 retval += wide_chars[(unsigned char) mbs[i] -32];
251 else if (mbs[i] == '\n') {
261 std::wstring narrow_to_wide(const std::string &mbs)
263 size_t wcl = mbs.size();
264 Buffer<wchar_t> wcs(wcl + 1);
265 size_t len = mbstowcs(*wcs, mbs.c_str(), wcl);
266 if (len == (size_t)(-1))
267 return L"<invalid multibyte string>";
276 std::string wide_to_narrow(const std::wstring &wcs) {
277 size_t mbl = wcs.size()*4;
279 std::string retval = "";
280 for (unsigned int i = 0; i < wcs.size(); i++) {
281 wchar_t char1 = (wchar_t) wcs[i];
283 if (char1 == L'\n') {
288 for (unsigned int j = 0; j < wcslen(wide_chars);j++) {
289 wchar_t char2 = (wchar_t) wide_chars[j];
291 if (char1 == char2) {
304 std::string wide_to_narrow(const std::wstring &wcs)
306 size_t mbl = wcs.size() * 4;
307 SharedBuffer<char> mbs(mbl+1);
308 size_t len = wcstombs(*mbs, wcs.c_str(), mbl);
309 if (len == (size_t)(-1))
310 return "Character conversion failed!";
318 std::string urlencode(const std::string &str)
320 // Encodes non-unreserved URI characters by a percent sign
321 // followed by two hex digits. See RFC 3986, section 2.3.
322 static const char url_hex_chars[] = "0123456789ABCDEF";
323 std::ostringstream oss(std::ios::binary);
324 for (u32 i = 0; i < str.size(); i++) {
325 unsigned char c = str[i];
326 if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~') {
330 << url_hex_chars[(c & 0xf0) >> 4]
331 << url_hex_chars[c & 0x0f];
337 std::string urldecode(const std::string &str)
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;
344 hex_digit_decode(str[i+1], highvalue) &&
345 hex_digit_decode(str[i+2], lowvalue)) {
346 oss << (char) ((highvalue << 4) | lowvalue);
355 u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
363 while ((flagstr = strtok_r(s, ",", &strpos))) {
366 while (*flagstr == ' ' || *flagstr == '\t')
370 if (!strncasecmp(flagstr, "no", 2)) {
375 for (int i = 0; flagdesc[i].name; i++) {
376 if (!strcasecmp(flagstr, flagdesc[i].name)) {
377 mask |= flagdesc[i].flag;
379 result |= flagdesc[i].flag;
391 std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
395 for (int i = 0; flagdesc[i].name; i++) {
396 if (flagmask & flagdesc[i].flag) {
397 if (!(flags & flagdesc[i].flag))
400 result += flagdesc[i].name;
405 size_t len = result.length();
407 result.erase(len - 2, 2);
412 size_t mystrlcpy(char *dst, const char *src, size_t size)
414 size_t srclen = strlen(src) + 1;
415 size_t copylen = MYMIN(srclen, size);
418 memcpy(dst, src, copylen);
419 dst[copylen - 1] = '\0';
425 char *mystrtok_r(char *s, const char *sep, char **lasts)
432 while (*s && strchr(sep, *s))
440 if (strchr(sep, *t)) {
451 u64 read_seed(const char *str)
456 if (str[0] == '0' && str[1] == 'x')
457 num = strtoull(str, &endptr, 16);
459 num = strtoull(str, &endptr, 10);
462 num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
467 bool parseColorString(const std::string &value, video::SColor &color, bool quiet)
472 success = parseHexColorString(value, color);
474 success = parseNamedColorString(value, color);
476 if (!success && !quiet)
477 errorstream << "Invalid color: \"" << value << "\"" << std::endl;
482 static bool parseHexColorString(const std::string &value, video::SColor &color)
484 unsigned char components[] = { 0x00, 0x00, 0x00, 0xff }; // R,G,B,A
489 size_t len = value.size();
492 if (len == 9 || len == 7) // #RRGGBBAA or #RRGGBB
494 else if (len == 5 || len == 4) // #RGBA or #RGB
501 for (size_t pos = 1, cc = 0; pos < len; pos++, cc++) {
502 assert(cc < sizeof components / sizeof components[0]);
505 if (!hex_digit_decode(value[pos], d)) {
509 components[cc] = (d & 0xf) << 4 | (d & 0xf);
511 unsigned char d1, d2;
512 if (!hex_digit_decode(value[pos], d1) ||
513 !hex_digit_decode(value[pos+1], d2)) {
517 components[cc] = (d1 & 0xf) << 4 | (d2 & 0xf);
518 pos++; // skip the second digit -- it's already used
523 color.setRed(components[0]);
524 color.setGreen(components[1]);
525 color.setBlue(components[2]);
526 color.setAlpha(components[3]);
532 struct ColorContainer {
534 std::map<const std::string, u32> colors;
537 ColorContainer::ColorContainer()
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;
689 static const ColorContainer named_colors;
691 static bool parseNamedColorString(const std::string &value, video::SColor &color)
693 std::string color_name;
694 std::string alpha_string;
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)
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);
709 color_name = lowercase(value);
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())
716 u32 color_temp = it->second;
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
722 * For named colors the supplied alpha string (representing a hex value)
723 * must be exactly two digits. For example: colorname#08
725 if (!alpha_string.empty()) {
726 if (alpha_string.length() != 2)
729 unsigned char d1, d2;
730 if (!hex_digit_decode(alpha_string.at(0), d1)
731 || !hex_digit_decode(alpha_string.at(1), d2))
733 color_temp |= ((d1 & 0xf) << 4 | (d2 & 0xf)) << 24;
735 color_temp |= 0xff << 24; // Fully opaque
738 color = video::SColor(color_temp);
743 void str_replace(std::string &str, char from, char to)
745 std::replace(str.begin(), str.end(), from, to);