2012-03-11 10:02:22 +01:00
|
|
|
/*
|
2013-02-24 18:40:43 +01:00
|
|
|
Minetest
|
2013-02-24 19:38:45 +01:00
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2012-03-11 10:02:22 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
2012-06-05 16:56:56 +02:00
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
2012-03-11 10:02:22 +01:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2012-06-05 16:56:56 +02:00
|
|
|
GNU Lesser General Public License for more details.
|
2012-03-11 10:02:22 +01:00
|
|
|
|
2012-06-05 16:56:56 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
2012-03-11 10:02:22 +01:00
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
#include "string.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "pointer.h"
|
2013-09-17 08:57:10 +02:00
|
|
|
#include "numeric.h"
|
2014-10-03 06:11:21 +02:00
|
|
|
#include "log.h"
|
2012-03-11 10:02:22 +01:00
|
|
|
|
2015-02-21 14:17:36 +01:00
|
|
|
#include "hex.h"
|
2017-11-08 23:56:20 +01:00
|
|
|
#include "porting.h"
|
|
|
|
#include "translation.h"
|
2012-03-11 10:02:22 +01:00
|
|
|
|
2015-07-06 18:53:30 +02:00
|
|
|
#include <algorithm>
|
2014-12-11 23:58:50 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <map>
|
|
|
|
|
2015-06-10 00:35:21 +02:00
|
|
|
#ifndef _WIN32
|
2015-06-29 08:39:22 +02:00
|
|
|
#include <iconv.h>
|
2015-06-10 00:35:21 +02:00
|
|
|
#else
|
2015-06-29 08:39:22 +02:00
|
|
|
#define _WIN32_WINNT 0x0501
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_ICONV_H_) && (defined(__FreeBSD__) || defined(__NetBSD__) || \
|
|
|
|
defined(__OpenBSD__) || defined(__DragonFly__))
|
|
|
|
#define BSD_ICONV_USED
|
2015-06-10 00:35:21 +02:00
|
|
|
#endif
|
|
|
|
|
2018-04-23 19:50:50 +02:00
|
|
|
static bool parseHexColorString(const std::string &value, video::SColor &color,
|
|
|
|
unsigned char default_alpha = 0xff);
|
2015-03-07 06:09:27 +01:00
|
|
|
static bool parseNamedColorString(const std::string &value, video::SColor &color);
|
|
|
|
|
2015-06-10 00:35:21 +02:00
|
|
|
#ifndef _WIN32
|
2015-06-29 08:39:22 +02:00
|
|
|
|
2015-06-17 19:00:31 +02:00
|
|
|
bool convert(const char *to, const char *from, char *outbuf,
|
2015-06-10 00:35:21 +02:00
|
|
|
size_t outbuf_size, char *inbuf, size_t inbuf_size)
|
|
|
|
{
|
|
|
|
iconv_t cd = iconv_open(to, from);
|
|
|
|
|
2015-06-29 08:39:22 +02:00
|
|
|
#ifdef BSD_ICONV_USED
|
2015-06-10 00:35:21 +02:00
|
|
|
const char *inbuf_ptr = inbuf;
|
|
|
|
#else
|
|
|
|
char *inbuf_ptr = inbuf;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char *outbuf_ptr = outbuf;
|
|
|
|
|
|
|
|
size_t *inbuf_left_ptr = &inbuf_size;
|
|
|
|
size_t *outbuf_left_ptr = &outbuf_size;
|
|
|
|
|
2015-06-17 19:00:31 +02:00
|
|
|
size_t old_size = inbuf_size;
|
|
|
|
while (inbuf_size > 0) {
|
2015-06-10 00:35:21 +02:00
|
|
|
iconv(cd, &inbuf_ptr, inbuf_left_ptr, &outbuf_ptr, outbuf_left_ptr);
|
2015-06-17 19:00:31 +02:00
|
|
|
if (inbuf_size == old_size) {
|
|
|
|
iconv_close(cd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
old_size = inbuf_size;
|
|
|
|
}
|
2015-06-10 00:35:21 +02:00
|
|
|
|
|
|
|
iconv_close(cd);
|
2015-06-17 19:00:31 +02:00
|
|
|
return true;
|
2015-06-10 00:35:21 +02:00
|
|
|
}
|
|
|
|
|
2020-01-13 07:10:15 +01:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
// Android need manual caring to support the full character set possible with wchar_t
|
|
|
|
const char *DEFAULT_ENCODING = "UTF-32LE";
|
|
|
|
#else
|
|
|
|
const char *DEFAULT_ENCODING = "WCHAR_T";
|
|
|
|
#endif
|
|
|
|
|
2015-06-10 00:35:21 +02:00
|
|
|
std::wstring utf8_to_wide(const std::string &input)
|
|
|
|
{
|
|
|
|
size_t inbuf_size = input.length() + 1;
|
|
|
|
// maximum possible size, every character is sizeof(wchar_t) bytes
|
|
|
|
size_t outbuf_size = (input.length() + 1) * sizeof(wchar_t);
|
|
|
|
|
|
|
|
char *inbuf = new char[inbuf_size];
|
|
|
|
memcpy(inbuf, input.c_str(), inbuf_size);
|
|
|
|
char *outbuf = new char[outbuf_size];
|
|
|
|
memset(outbuf, 0, outbuf_size);
|
|
|
|
|
2020-01-13 07:10:15 +01:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
// Android need manual caring to support the full character set possible with wchar_t
|
|
|
|
SANITY_CHECK(sizeof(wchar_t) == 4);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!convert(DEFAULT_ENCODING, "UTF-8", outbuf, outbuf_size, inbuf, inbuf_size)) {
|
2015-06-17 19:00:31 +02:00
|
|
|
infostream << "Couldn't convert UTF-8 string 0x" << hex_encode(input)
|
|
|
|
<< " into wstring" << std::endl;
|
|
|
|
delete[] inbuf;
|
|
|
|
delete[] outbuf;
|
|
|
|
return L"<invalid UTF-8 string>";
|
|
|
|
}
|
2015-06-29 08:39:22 +02:00
|
|
|
std::wstring out((wchar_t *)outbuf);
|
2015-06-10 00:35:21 +02:00
|
|
|
|
|
|
|
delete[] inbuf;
|
|
|
|
delete[] outbuf;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string wide_to_utf8(const std::wstring &input)
|
|
|
|
{
|
|
|
|
size_t inbuf_size = (input.length() + 1) * sizeof(wchar_t);
|
|
|
|
// maximum possible size: utf-8 encodes codepoints using 1 up to 6 bytes
|
|
|
|
size_t outbuf_size = (input.length() + 1) * 6;
|
|
|
|
|
|
|
|
char *inbuf = new char[inbuf_size];
|
|
|
|
memcpy(inbuf, input.c_str(), inbuf_size);
|
|
|
|
char *outbuf = new char[outbuf_size];
|
|
|
|
memset(outbuf, 0, outbuf_size);
|
|
|
|
|
2020-01-13 07:10:15 +01:00
|
|
|
if (!convert("UTF-8", DEFAULT_ENCODING, outbuf, outbuf_size, inbuf, inbuf_size)) {
|
2015-06-17 19:00:31 +02:00
|
|
|
infostream << "Couldn't convert wstring 0x" << hex_encode(inbuf, inbuf_size)
|
2015-06-23 15:04:14 +02:00
|
|
|
<< " into UTF-8 string" << std::endl;
|
2015-06-17 19:00:31 +02:00
|
|
|
delete[] inbuf;
|
|
|
|
delete[] outbuf;
|
|
|
|
return "<invalid wstring>";
|
|
|
|
}
|
2015-06-10 00:35:21 +02:00
|
|
|
std::string out(outbuf);
|
|
|
|
|
|
|
|
delete[] inbuf;
|
|
|
|
delete[] outbuf;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
2015-06-29 08:39:22 +02:00
|
|
|
|
|
|
|
#else // _WIN32
|
|
|
|
|
2015-06-10 00:35:21 +02:00
|
|
|
std::wstring utf8_to_wide(const std::string &input)
|
|
|
|
{
|
|
|
|
size_t outbuf_size = input.size() + 1;
|
|
|
|
wchar_t *outbuf = new wchar_t[outbuf_size];
|
|
|
|
memset(outbuf, 0, outbuf_size * sizeof(wchar_t));
|
2015-06-29 08:39:22 +02:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, input.c_str(), input.size(),
|
|
|
|
outbuf, outbuf_size);
|
2015-06-10 00:35:21 +02:00
|
|
|
std::wstring out(outbuf);
|
|
|
|
delete[] outbuf;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string wide_to_utf8(const std::wstring &input)
|
|
|
|
{
|
|
|
|
size_t outbuf_size = (input.size() + 1) * 6;
|
|
|
|
char *outbuf = new char[outbuf_size];
|
|
|
|
memset(outbuf, 0, outbuf_size);
|
2015-06-29 08:39:22 +02:00
|
|
|
WideCharToMultiByte(CP_UTF8, 0, input.c_str(), input.size(),
|
|
|
|
outbuf, outbuf_size, NULL, NULL);
|
2015-06-10 00:35:21 +02:00
|
|
|
std::string out(outbuf);
|
|
|
|
delete[] outbuf;
|
|
|
|
return out;
|
|
|
|
}
|
2015-06-29 08:39:22 +02:00
|
|
|
|
|
|
|
#endif // _WIN32
|
2015-06-10 00:35:21 +02:00
|
|
|
|
2018-06-27 18:09:45 +02:00
|
|
|
// You must free the returned string!
|
|
|
|
// The returned string is allocated using new
|
2015-07-07 05:55:07 +02:00
|
|
|
wchar_t *utf8_to_wide_c(const char *str)
|
|
|
|
{
|
2017-08-20 13:30:50 +02:00
|
|
|
std::wstring ret = utf8_to_wide(std::string(str));
|
2015-07-07 05:55:07 +02:00
|
|
|
size_t len = ret.length();
|
|
|
|
wchar_t *ret_c = new wchar_t[len + 1];
|
|
|
|
memset(ret_c, 0, (len + 1) * sizeof(wchar_t));
|
|
|
|
memcpy(ret_c, ret.c_str(), len * sizeof(wchar_t));
|
|
|
|
return ret_c;
|
|
|
|
}
|
|
|
|
|
2015-03-07 06:09:27 +01:00
|
|
|
// You must free the returned string!
|
|
|
|
// The returned string is allocated using new
|
|
|
|
wchar_t *narrow_to_wide_c(const char *str)
|
|
|
|
{
|
2020-01-13 07:10:15 +01:00
|
|
|
wchar_t *nstr = nullptr;
|
2015-02-01 23:59:23 +01:00
|
|
|
#if defined(_WIN32)
|
2015-03-07 06:09:27 +01:00
|
|
|
int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, 0, 0);
|
|
|
|
if (nResult == 0) {
|
|
|
|
errorstream<<"gettext: MultiByteToWideChar returned null"<<std::endl;
|
|
|
|
} else {
|
|
|
|
nstr = new wchar_t[nResult];
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, (WCHAR *) nstr, nResult);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
size_t len = strlen(str);
|
2015-06-29 08:39:22 +02:00
|
|
|
nstr = new wchar_t[len + 1];
|
2015-03-07 06:09:27 +01:00
|
|
|
|
|
|
|
std::wstring intermediate = narrow_to_wide(str);
|
|
|
|
memset(nstr, 0, (len + 1) * sizeof(wchar_t));
|
|
|
|
memcpy(nstr, intermediate.c_str(), len * sizeof(wchar_t));
|
2015-02-01 23:59:23 +01:00
|
|
|
#endif
|
|
|
|
|
2015-03-07 06:09:27 +01:00
|
|
|
return nstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring narrow_to_wide(const std::string &mbs) {
|
2020-04-17 23:46:30 +02:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
return utf8_to_wide(mbs);
|
|
|
|
#else
|
2015-03-07 06:09:27 +01:00
|
|
|
size_t wcl = mbs.size();
|
2015-02-12 02:55:50 +01:00
|
|
|
Buffer<wchar_t> wcs(wcl + 1);
|
2015-03-07 06:09:27 +01:00
|
|
|
size_t len = mbstowcs(*wcs, mbs.c_str(), wcl);
|
|
|
|
if (len == (size_t)(-1))
|
2015-02-01 23:59:23 +01:00
|
|
|
return L"<invalid multibyte string>";
|
2015-03-07 06:09:27 +01:00
|
|
|
wcs[len] = 0;
|
2015-02-12 02:55:50 +01:00
|
|
|
return *wcs;
|
2020-04-17 23:46:30 +02:00
|
|
|
#endif
|
2015-02-01 23:59:23 +01:00
|
|
|
}
|
|
|
|
|
2015-03-07 06:09:27 +01:00
|
|
|
|
|
|
|
std::string wide_to_narrow(const std::wstring &wcs)
|
2013-08-11 04:09:45 +02:00
|
|
|
{
|
2020-04-17 23:46:30 +02:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
return wide_to_utf8(wcs);
|
|
|
|
#else
|
2015-03-07 06:09:27 +01:00
|
|
|
size_t mbl = wcs.size() * 4;
|
2013-08-11 04:09:45 +02:00
|
|
|
SharedBuffer<char> mbs(mbl+1);
|
2015-03-07 06:09:27 +01:00
|
|
|
size_t len = wcstombs(*mbs, wcs.c_str(), mbl);
|
|
|
|
if (len == (size_t)(-1))
|
2013-11-03 17:28:16 +01:00
|
|
|
return "Character conversion failed!";
|
2017-08-20 13:30:50 +02:00
|
|
|
|
|
|
|
mbs[len] = 0;
|
2013-08-11 04:09:45 +02:00
|
|
|
return *mbs;
|
2020-04-17 23:46:30 +02:00
|
|
|
#endif
|
2013-08-11 04:09:45 +02:00
|
|
|
}
|
|
|
|
|
2014-04-21 14:10:59 +02:00
|
|
|
|
2016-06-11 09:23:53 +02:00
|
|
|
std::string urlencode(const std::string &str)
|
2013-08-29 05:56:48 +02:00
|
|
|
{
|
|
|
|
// Encodes non-unreserved URI characters by a percent sign
|
|
|
|
// followed by two hex digits. See RFC 3986, section 2.3.
|
|
|
|
static const char url_hex_chars[] = "0123456789ABCDEF";
|
|
|
|
std::ostringstream oss(std::ios::binary);
|
2017-08-20 13:30:50 +02:00
|
|
|
for (unsigned char c : str) {
|
2016-06-11 09:23:53 +02:00
|
|
|
if (isalnum(c) || c == '-' || c == '.' || c == '_' || c == '~') {
|
2013-08-29 05:56:48 +02:00
|
|
|
oss << c;
|
2016-06-11 09:23:53 +02:00
|
|
|
} else {
|
2013-08-29 05:56:48 +02:00
|
|
|
oss << "%"
|
|
|
|
<< url_hex_chars[(c & 0xf0) >> 4]
|
|
|
|
<< url_hex_chars[c & 0x0f];
|
2016-06-11 09:23:53 +02:00
|
|
|
}
|
2013-08-29 05:56:48 +02:00
|
|
|
}
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2016-06-11 09:23:53 +02:00
|
|
|
std::string urldecode(const std::string &str)
|
2013-08-29 05:56:48 +02:00
|
|
|
{
|
|
|
|
// Inverse of urlencode
|
|
|
|
std::ostringstream oss(std::ios::binary);
|
|
|
|
for (u32 i = 0; i < str.size(); i++) {
|
|
|
|
unsigned char highvalue, lowvalue;
|
|
|
|
if (str[i] == '%' &&
|
|
|
|
hex_digit_decode(str[i+1], highvalue) &&
|
|
|
|
hex_digit_decode(str[i+2], lowvalue)) {
|
|
|
|
oss << (char) ((highvalue << 4) | lowvalue);
|
|
|
|
i += 2;
|
2016-06-11 09:23:53 +02:00
|
|
|
} else {
|
2013-08-29 05:56:48 +02:00
|
|
|
oss << str[i];
|
2016-06-11 09:23:53 +02:00
|
|
|
}
|
2013-08-29 05:56:48 +02:00
|
|
|
}
|
|
|
|
return oss.str();
|
2012-12-14 12:30:17 +01:00
|
|
|
}
|
2013-01-23 08:21:26 +01:00
|
|
|
|
2014-09-11 19:42:21 +02:00
|
|
|
u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask)
|
2014-02-08 23:50:26 +01:00
|
|
|
{
|
2016-06-11 09:23:53 +02:00
|
|
|
u32 result = 0;
|
|
|
|
u32 mask = 0;
|
2013-02-05 21:01:33 +01:00
|
|
|
char *s = &str[0];
|
2016-06-11 09:23:53 +02:00
|
|
|
char *flagstr;
|
2020-01-13 07:10:15 +01:00
|
|
|
char *strpos = nullptr;
|
2014-02-08 23:50:26 +01:00
|
|
|
|
2013-02-05 21:01:33 +01:00
|
|
|
while ((flagstr = strtok_r(s, ",", &strpos))) {
|
2020-01-13 07:10:15 +01:00
|
|
|
s = nullptr;
|
2014-02-08 23:50:26 +01:00
|
|
|
|
2013-02-05 21:01:33 +01:00
|
|
|
while (*flagstr == ' ' || *flagstr == '\t')
|
|
|
|
flagstr++;
|
2014-02-08 23:50:26 +01:00
|
|
|
|
|
|
|
bool flagset = true;
|
|
|
|
if (!strncasecmp(flagstr, "no", 2)) {
|
|
|
|
flagset = false;
|
|
|
|
flagstr += 2;
|
|
|
|
}
|
|
|
|
|
2013-02-05 21:01:33 +01:00
|
|
|
for (int i = 0; flagdesc[i].name; i++) {
|
|
|
|
if (!strcasecmp(flagstr, flagdesc[i].name)) {
|
2014-02-08 23:50:26 +01:00
|
|
|
mask |= flagdesc[i].flag;
|
|
|
|
if (flagset)
|
|
|
|
result |= flagdesc[i].flag;
|
2013-02-05 21:01:33 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-08 23:50:26 +01:00
|
|
|
|
|
|
|
if (flagmask)
|
|
|
|
*flagmask = mask;
|
|
|
|
|
2013-02-05 21:01:33 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-09-11 19:42:21 +02:00
|
|
|
std::string writeFlagString(u32 flags, const FlagDesc *flagdesc, u32 flagmask)
|
2014-02-08 23:50:26 +01:00
|
|
|
{
|
2013-02-05 21:01:33 +01:00
|
|
|
std::string result;
|
2014-02-08 23:50:26 +01:00
|
|
|
|
2013-02-05 21:01:33 +01:00
|
|
|
for (int i = 0; flagdesc[i].name; i++) {
|
2014-02-08 23:50:26 +01:00
|
|
|
if (flagmask & flagdesc[i].flag) {
|
|
|
|
if (!(flags & flagdesc[i].flag))
|
|
|
|
result += "no";
|
|
|
|
|
2013-02-05 21:01:33 +01:00
|
|
|
result += flagdesc[i].name;
|
|
|
|
result += ", ";
|
|
|
|
}
|
|
|
|
}
|
2014-02-08 23:50:26 +01:00
|
|
|
|
2013-02-05 21:01:33 +01:00
|
|
|
size_t len = result.length();
|
|
|
|
if (len >= 2)
|
|
|
|
result.erase(len - 2, 2);
|
2014-02-08 23:50:26 +01:00
|
|
|
|
2013-02-05 21:01:33 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-02-09 18:38:50 +01:00
|
|
|
size_t mystrlcpy(char *dst, const char *src, size_t size)
|
|
|
|
{
|
|
|
|
size_t srclen = strlen(src) + 1;
|
|
|
|
size_t copylen = MYMIN(srclen, size);
|
|
|
|
|
|
|
|
if (copylen > 0) {
|
|
|
|
memcpy(dst, src, copylen);
|
|
|
|
dst[copylen - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return srclen;
|
|
|
|
}
|
|
|
|
|
2014-02-08 23:50:26 +01:00
|
|
|
char *mystrtok_r(char *s, const char *sep, char **lasts)
|
|
|
|
{
|
2013-01-23 08:21:26 +01:00
|
|
|
char *t;
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
s = *lasts;
|
|
|
|
|
|
|
|
while (*s && strchr(sep, *s))
|
|
|
|
s++;
|
|
|
|
|
|
|
|
if (!*s)
|
2020-01-13 07:10:15 +01:00
|
|
|
return nullptr;
|
2013-01-23 08:21:26 +01:00
|
|
|
|
|
|
|
t = s;
|
|
|
|
while (*t) {
|
|
|
|
if (strchr(sep, *t)) {
|
2013-01-23 23:37:53 +01:00
|
|
|
*t++ = '\0';
|
|
|
|
break;
|
2013-01-23 08:21:26 +01:00
|
|
|
}
|
|
|
|
t++;
|
|
|
|
}
|
2015-03-06 11:21:51 +01:00
|
|
|
|
2013-01-23 08:21:26 +01:00
|
|
|
*lasts = t;
|
|
|
|
return s;
|
|
|
|
}
|
2013-09-17 08:57:10 +02:00
|
|
|
|
2014-02-08 23:50:26 +01:00
|
|
|
u64 read_seed(const char *str)
|
|
|
|
{
|
2013-09-17 08:57:10 +02:00
|
|
|
char *endptr;
|
|
|
|
u64 num;
|
2015-03-06 11:21:51 +01:00
|
|
|
|
2013-09-17 08:57:10 +02:00
|
|
|
if (str[0] == '0' && str[1] == 'x')
|
|
|
|
num = strtoull(str, &endptr, 16);
|
|
|
|
else
|
|
|
|
num = strtoull(str, &endptr, 10);
|
2015-03-06 11:21:51 +01:00
|
|
|
|
2013-09-17 08:57:10 +02:00
|
|
|
if (*endptr)
|
|
|
|
num = murmur_hash_64_ua(str, (int)strlen(str), 0x1337);
|
2015-03-06 11:21:51 +01:00
|
|
|
|
2013-09-17 08:57:10 +02:00
|
|
|
return num;
|
|
|
|
}
|
2014-10-03 06:11:21 +02:00
|
|
|
|
2018-04-23 19:50:50 +02:00
|
|
|
bool parseColorString(const std::string &value, video::SColor &color, bool quiet,
|
|
|
|
unsigned char default_alpha)
|
2014-10-03 06:11:21 +02:00
|
|
|
{
|
2014-10-29 07:40:55 +01:00
|
|
|
bool success;
|
2014-10-03 06:11:21 +02:00
|
|
|
|
2014-10-29 07:40:55 +01:00
|
|
|
if (value[0] == '#')
|
2018-04-23 19:50:50 +02:00
|
|
|
success = parseHexColorString(value, color, default_alpha);
|
2014-10-29 07:40:55 +01:00
|
|
|
else
|
|
|
|
success = parseNamedColorString(value, color);
|
2014-10-03 06:11:21 +02:00
|
|
|
|
2014-10-29 07:40:55 +01:00
|
|
|
if (!success && !quiet)
|
|
|
|
errorstream << "Invalid color: \"" << value << "\"" << std::endl;
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2018-04-23 19:50:50 +02:00
|
|
|
static bool parseHexColorString(const std::string &value, video::SColor &color,
|
|
|
|
unsigned char default_alpha)
|
2014-10-29 07:40:55 +01:00
|
|
|
{
|
2018-04-23 19:50:50 +02:00
|
|
|
unsigned char components[] = { 0x00, 0x00, 0x00, default_alpha }; // R,G,B,A
|
2014-10-03 06:11:21 +02:00
|
|
|
|
2014-10-29 07:40:55 +01:00
|
|
|
if (value[0] != '#')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size_t len = value.size();
|
|
|
|
bool short_form;
|
|
|
|
|
|
|
|
if (len == 9 || len == 7) // #RRGGBBAA or #RRGGBB
|
|
|
|
short_form = false;
|
|
|
|
else if (len == 5 || len == 4) // #RGBA or #RGB
|
|
|
|
short_form = true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
|
|
|
|
for (size_t pos = 1, cc = 0; pos < len; pos++, cc++) {
|
|
|
|
assert(cc < sizeof components / sizeof components[0]);
|
|
|
|
if (short_form) {
|
|
|
|
unsigned char d;
|
|
|
|
if (!hex_digit_decode(value[pos], d)) {
|
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
components[cc] = (d & 0xf) << 4 | (d & 0xf);
|
2014-10-03 06:11:21 +02:00
|
|
|
} else {
|
2014-10-29 07:40:55 +01:00
|
|
|
unsigned char d1, d2;
|
|
|
|
if (!hex_digit_decode(value[pos], d1) ||
|
|
|
|
!hex_digit_decode(value[pos+1], d2)) {
|
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
components[cc] = (d1 & 0xf) << 4 | (d2 & 0xf);
|
|
|
|
pos++; // skip the second digit -- it's already used
|
2014-10-03 06:11:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-29 07:40:55 +01:00
|
|
|
if (success) {
|
|
|
|
color.setRed(components[0]);
|
|
|
|
color.setGreen(components[1]);
|
|
|
|
color.setBlue(components[2]);
|
|
|
|
color.setAlpha(components[3]);
|
|
|
|
}
|
2014-10-03 06:11:21 +02:00
|
|
|
|
2014-10-29 07:40:55 +01:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ColorContainer {
|
|
|
|
ColorContainer();
|
|
|
|
std::map<const std::string, u32> colors;
|
|
|
|
};
|
|
|
|
|
|
|
|
ColorContainer::ColorContainer()
|
|
|
|
{
|
|
|
|
colors["aliceblue"] = 0xf0f8ff;
|
|
|
|
colors["antiquewhite"] = 0xfaebd7;
|
|
|
|
colors["aqua"] = 0x00ffff;
|
|
|
|
colors["aquamarine"] = 0x7fffd4;
|
|
|
|
colors["azure"] = 0xf0ffff;
|
|
|
|
colors["beige"] = 0xf5f5dc;
|
|
|
|
colors["bisque"] = 0xffe4c4;
|
|
|
|
colors["black"] = 00000000;
|
|
|
|
colors["blanchedalmond"] = 0xffebcd;
|
|
|
|
colors["blue"] = 0x0000ff;
|
|
|
|
colors["blueviolet"] = 0x8a2be2;
|
|
|
|
colors["brown"] = 0xa52a2a;
|
|
|
|
colors["burlywood"] = 0xdeb887;
|
|
|
|
colors["cadetblue"] = 0x5f9ea0;
|
|
|
|
colors["chartreuse"] = 0x7fff00;
|
|
|
|
colors["chocolate"] = 0xd2691e;
|
|
|
|
colors["coral"] = 0xff7f50;
|
|
|
|
colors["cornflowerblue"] = 0x6495ed;
|
|
|
|
colors["cornsilk"] = 0xfff8dc;
|
|
|
|
colors["crimson"] = 0xdc143c;
|
|
|
|
colors["cyan"] = 0x00ffff;
|
|
|
|
colors["darkblue"] = 0x00008b;
|
|
|
|
colors["darkcyan"] = 0x008b8b;
|
|
|
|
colors["darkgoldenrod"] = 0xb8860b;
|
|
|
|
colors["darkgray"] = 0xa9a9a9;
|
|
|
|
colors["darkgreen"] = 0x006400;
|
2017-01-14 22:30:03 +01:00
|
|
|
colors["darkgrey"] = 0xa9a9a9;
|
2014-10-29 07:40:55 +01:00
|
|
|
colors["darkkhaki"] = 0xbdb76b;
|
|
|
|
colors["darkmagenta"] = 0x8b008b;
|
|
|
|
colors["darkolivegreen"] = 0x556b2f;
|
|
|
|
colors["darkorange"] = 0xff8c00;
|
|
|
|
colors["darkorchid"] = 0x9932cc;
|
|
|
|
colors["darkred"] = 0x8b0000;
|
|
|
|
colors["darksalmon"] = 0xe9967a;
|
|
|
|
colors["darkseagreen"] = 0x8fbc8f;
|
|
|
|
colors["darkslateblue"] = 0x483d8b;
|
|
|
|
colors["darkslategray"] = 0x2f4f4f;
|
2017-01-14 22:30:03 +01:00
|
|
|
colors["darkslategrey"] = 0x2f4f4f;
|
2014-10-29 07:40:55 +01:00
|
|
|
colors["darkturquoise"] = 0x00ced1;
|
|
|
|
colors["darkviolet"] = 0x9400d3;
|
|
|
|
colors["deeppink"] = 0xff1493;
|
|
|
|
colors["deepskyblue"] = 0x00bfff;
|
|
|
|
colors["dimgray"] = 0x696969;
|
2017-01-14 22:30:03 +01:00
|
|
|
colors["dimgrey"] = 0x696969;
|
2014-10-29 07:40:55 +01:00
|
|
|
colors["dodgerblue"] = 0x1e90ff;
|
|
|
|
colors["firebrick"] = 0xb22222;
|
|
|
|
colors["floralwhite"] = 0xfffaf0;
|
|
|
|
colors["forestgreen"] = 0x228b22;
|
|
|
|
colors["fuchsia"] = 0xff00ff;
|
|
|
|
colors["gainsboro"] = 0xdcdcdc;
|
|
|
|
colors["ghostwhite"] = 0xf8f8ff;
|
|
|
|
colors["gold"] = 0xffd700;
|
|
|
|
colors["goldenrod"] = 0xdaa520;
|
|
|
|
colors["gray"] = 0x808080;
|
|
|
|
colors["green"] = 0x008000;
|
|
|
|
colors["greenyellow"] = 0xadff2f;
|
2017-01-14 22:30:03 +01:00
|
|
|
colors["grey"] = 0x808080;
|
2014-10-29 07:40:55 +01:00
|
|
|
colors["honeydew"] = 0xf0fff0;
|
|
|
|
colors["hotpink"] = 0xff69b4;
|
2015-08-18 12:57:55 +02:00
|
|
|
colors["indianred"] = 0xcd5c5c;
|
|
|
|
colors["indigo"] = 0x4b0082;
|
2014-10-29 07:40:55 +01:00
|
|
|
colors["ivory"] = 0xfffff0;
|
|
|
|
colors["khaki"] = 0xf0e68c;
|
|
|
|
colors["lavender"] = 0xe6e6fa;
|
|
|
|
colors["lavenderblush"] = 0xfff0f5;
|
|
|
|
colors["lawngreen"] = 0x7cfc00;
|
|
|
|
colors["lemonchiffon"] = 0xfffacd;
|
|
|
|
colors["lightblue"] = 0xadd8e6;
|
|
|
|
colors["lightcoral"] = 0xf08080;
|
|
|
|
colors["lightcyan"] = 0xe0ffff;
|
|
|
|
colors["lightgoldenrodyellow"] = 0xfafad2;
|
|
|
|
colors["lightgray"] = 0xd3d3d3;
|
|
|
|
colors["lightgreen"] = 0x90ee90;
|
2017-01-14 22:30:03 +01:00
|
|
|
colors["lightgrey"] = 0xd3d3d3;
|
2014-10-29 07:40:55 +01:00
|
|
|
colors["lightpink"] = 0xffb6c1;
|
|
|
|
colors["lightsalmon"] = 0xffa07a;
|
|
|
|
colors["lightseagreen"] = 0x20b2aa;
|
|
|
|
colors["lightskyblue"] = 0x87cefa;
|
|
|
|
colors["lightslategray"] = 0x778899;
|
2017-01-14 22:30:03 +01:00
|
|
|
colors["lightslategrey"] = 0x778899;
|
2014-10-29 07:40:55 +01:00
|
|
|
colors["lightsteelblue"] = 0xb0c4de;
|
|
|
|
colors["lightyellow"] = 0xffffe0;
|
|
|
|
colors["lime"] = 0x00ff00;
|
|
|
|
colors["limegreen"] = 0x32cd32;
|
|
|
|
colors["linen"] = 0xfaf0e6;
|
|
|
|
colors["magenta"] = 0xff00ff;
|
|
|
|
colors["maroon"] = 0x800000;
|
|
|
|
colors["mediumaquamarine"] = 0x66cdaa;
|
|
|
|
colors["mediumblue"] = 0x0000cd;
|
|
|
|
colors["mediumorchid"] = 0xba55d3;
|
|
|
|
colors["mediumpurple"] = 0x9370db;
|
|
|
|
colors["mediumseagreen"] = 0x3cb371;
|
|
|
|
colors["mediumslateblue"] = 0x7b68ee;
|
|
|
|
colors["mediumspringgreen"] = 0x00fa9a;
|
|
|
|
colors["mediumturquoise"] = 0x48d1cc;
|
|
|
|
colors["mediumvioletred"] = 0xc71585;
|
|
|
|
colors["midnightblue"] = 0x191970;
|
|
|
|
colors["mintcream"] = 0xf5fffa;
|
|
|
|
colors["mistyrose"] = 0xffe4e1;
|
|
|
|
colors["moccasin"] = 0xffe4b5;
|
|
|
|
colors["navajowhite"] = 0xffdead;
|
|
|
|
colors["navy"] = 0x000080;
|
|
|
|
colors["oldlace"] = 0xfdf5e6;
|
|
|
|
colors["olive"] = 0x808000;
|
|
|
|
colors["olivedrab"] = 0x6b8e23;
|
|
|
|
colors["orange"] = 0xffa500;
|
|
|
|
colors["orangered"] = 0xff4500;
|
|
|
|
colors["orchid"] = 0xda70d6;
|
|
|
|
colors["palegoldenrod"] = 0xeee8aa;
|
|
|
|
colors["palegreen"] = 0x98fb98;
|
|
|
|
colors["paleturquoise"] = 0xafeeee;
|
|
|
|
colors["palevioletred"] = 0xdb7093;
|
|
|
|
colors["papayawhip"] = 0xffefd5;
|
|
|
|
colors["peachpuff"] = 0xffdab9;
|
|
|
|
colors["peru"] = 0xcd853f;
|
|
|
|
colors["pink"] = 0xffc0cb;
|
|
|
|
colors["plum"] = 0xdda0dd;
|
|
|
|
colors["powderblue"] = 0xb0e0e6;
|
|
|
|
colors["purple"] = 0x800080;
|
|
|
|
colors["red"] = 0xff0000;
|
|
|
|
colors["rosybrown"] = 0xbc8f8f;
|
|
|
|
colors["royalblue"] = 0x4169e1;
|
|
|
|
colors["saddlebrown"] = 0x8b4513;
|
|
|
|
colors["salmon"] = 0xfa8072;
|
|
|
|
colors["sandybrown"] = 0xf4a460;
|
|
|
|
colors["seagreen"] = 0x2e8b57;
|
|
|
|
colors["seashell"] = 0xfff5ee;
|
|
|
|
colors["sienna"] = 0xa0522d;
|
|
|
|
colors["silver"] = 0xc0c0c0;
|
|
|
|
colors["skyblue"] = 0x87ceeb;
|
|
|
|
colors["slateblue"] = 0x6a5acd;
|
|
|
|
colors["slategray"] = 0x708090;
|
2017-01-14 22:30:03 +01:00
|
|
|
colors["slategrey"] = 0x708090;
|
2014-10-29 07:40:55 +01:00
|
|
|
colors["snow"] = 0xfffafa;
|
|
|
|
colors["springgreen"] = 0x00ff7f;
|
|
|
|
colors["steelblue"] = 0x4682b4;
|
|
|
|
colors["tan"] = 0xd2b48c;
|
|
|
|
colors["teal"] = 0x008080;
|
|
|
|
colors["thistle"] = 0xd8bfd8;
|
|
|
|
colors["tomato"] = 0xff6347;
|
|
|
|
colors["turquoise"] = 0x40e0d0;
|
|
|
|
colors["violet"] = 0xee82ee;
|
|
|
|
colors["wheat"] = 0xf5deb3;
|
|
|
|
colors["white"] = 0xffffff;
|
|
|
|
colors["whitesmoke"] = 0xf5f5f5;
|
|
|
|
colors["yellow"] = 0xffff00;
|
|
|
|
colors["yellowgreen"] = 0x9acd32;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static const ColorContainer named_colors;
|
|
|
|
|
|
|
|
static bool parseNamedColorString(const std::string &value, video::SColor &color)
|
|
|
|
{
|
|
|
|
std::string color_name;
|
|
|
|
std::string alpha_string;
|
|
|
|
|
|
|
|
/* If the string has a # in it, assume this is the start of a specified
|
|
|
|
* alpha value (if it isn't the string is invalid and the error will be
|
|
|
|
* caught later on, either because the color name won't be found or the
|
|
|
|
* alpha value will fail conversion)
|
|
|
|
*/
|
|
|
|
size_t alpha_pos = value.find('#');
|
|
|
|
if (alpha_pos != std::string::npos) {
|
|
|
|
color_name = value.substr(0, alpha_pos);
|
|
|
|
alpha_string = value.substr(alpha_pos + 1);
|
|
|
|
} else {
|
|
|
|
color_name = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
color_name = lowercase(value);
|
|
|
|
|
|
|
|
std::map<const std::string, unsigned>::const_iterator it;
|
|
|
|
it = named_colors.colors.find(color_name);
|
|
|
|
if (it == named_colors.colors.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
u32 color_temp = it->second;
|
|
|
|
|
|
|
|
/* An empty string for alpha is ok (none of the color table entries
|
|
|
|
* have an alpha value either). Color strings without an alpha specified
|
|
|
|
* are interpreted as fully opaque
|
|
|
|
*
|
|
|
|
* For named colors the supplied alpha string (representing a hex value)
|
|
|
|
* must be exactly two digits. For example: colorname#08
|
|
|
|
*/
|
|
|
|
if (!alpha_string.empty()) {
|
|
|
|
if (alpha_string.length() != 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned char d1, d2;
|
|
|
|
if (!hex_digit_decode(alpha_string.at(0), d1)
|
|
|
|
|| !hex_digit_decode(alpha_string.at(1), d2))
|
|
|
|
return false;
|
|
|
|
color_temp |= ((d1 & 0xf) << 4 | (d2 & 0xf)) << 24;
|
|
|
|
} else {
|
|
|
|
color_temp |= 0xff << 24; // Fully opaque
|
|
|
|
}
|
|
|
|
|
|
|
|
color = video::SColor(color_temp);
|
|
|
|
|
|
|
|
return true;
|
2014-10-03 06:11:21 +02:00
|
|
|
}
|
2014-12-11 23:58:50 +01:00
|
|
|
|
|
|
|
void str_replace(std::string &str, char from, char to)
|
|
|
|
{
|
|
|
|
std::replace(str.begin(), str.end(), from, to);
|
|
|
|
}
|
2017-01-31 18:05:03 +01:00
|
|
|
|
|
|
|
/* Translated strings have the following format:
|
|
|
|
* \x1bT marks the beginning of a translated string
|
|
|
|
* \x1bE marks its end
|
|
|
|
*
|
|
|
|
* \x1bF marks the beginning of an argument, and \x1bE its end.
|
|
|
|
*
|
|
|
|
* Arguments are *not* translated, as they may contain escape codes.
|
|
|
|
* Thus, if you want a translated argument, it should be inside \x1bT/\x1bE tags as well.
|
|
|
|
*
|
|
|
|
* This representation is chosen so that clients ignoring escape codes will
|
|
|
|
* see untranslated strings.
|
|
|
|
*
|
|
|
|
* For instance, suppose we have a string such as "@1 Wool" with the argument "White"
|
|
|
|
* The string will be sent as "\x1bT\x1bF\x1bTWhite\x1bE\x1bE Wool\x1bE"
|
|
|
|
* To translate this string, we extract what is inside \x1bT/\x1bE tags.
|
|
|
|
* When we notice the \x1bF tag, we recursively extract what is there up to the \x1bE end tag,
|
|
|
|
* translating it as well.
|
|
|
|
* We get the argument "White", translated, and create a template string with "@1" instead of it.
|
|
|
|
* We finally get the template "@1 Wool" that was used in the beginning, which we translate
|
|
|
|
* before filling it again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void translate_all(const std::wstring &s, size_t &i, std::wstring &res);
|
|
|
|
|
|
|
|
void translate_string(const std::wstring &s, const std::wstring &textdomain,
|
|
|
|
size_t &i, std::wstring &res) {
|
|
|
|
std::wostringstream output;
|
|
|
|
std::vector<std::wstring> args;
|
|
|
|
int arg_number = 1;
|
|
|
|
while (i < s.length()) {
|
|
|
|
// Not an escape sequence: just add the character.
|
|
|
|
if (s[i] != '\x1b') {
|
|
|
|
output.put(s[i]);
|
|
|
|
// The character is a literal '@'; add it twice
|
|
|
|
// so that it is not mistaken for an argument.
|
|
|
|
if (s[i] == L'@')
|
|
|
|
output.put(L'@');
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have an escape sequence: locate it and its data
|
|
|
|
// It is either a single character, or it begins with '('
|
|
|
|
// and extends up to the following ')', with '\' as an escape character.
|
|
|
|
++i;
|
|
|
|
size_t start_index = i;
|
|
|
|
size_t length;
|
|
|
|
if (i == s.length()) {
|
|
|
|
length = 0;
|
|
|
|
} else if (s[i] == L'(') {
|
|
|
|
++i;
|
|
|
|
++start_index;
|
|
|
|
while (i < s.length() && s[i] != L')') {
|
|
|
|
if (s[i] == L'\\')
|
|
|
|
++i;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
length = i - start_index;
|
|
|
|
++i;
|
|
|
|
if (i > s.length())
|
|
|
|
i = s.length();
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
length = 1;
|
|
|
|
}
|
|
|
|
std::wstring escape_sequence(s, start_index, length);
|
|
|
|
|
|
|
|
// The escape sequence is now reconstructed.
|
|
|
|
std::vector<std::wstring> parts = split(escape_sequence, L'@');
|
|
|
|
if (parts[0] == L"E") {
|
|
|
|
// "End of translation" escape sequence. We are done locating the string to translate.
|
|
|
|
break;
|
|
|
|
} else if (parts[0] == L"F") {
|
|
|
|
// "Start of argument" escape sequence.
|
|
|
|
// Recursively translate the argument, and add it to the argument list.
|
|
|
|
// Add an "@n" instead of the argument to the template to translate.
|
|
|
|
if (arg_number >= 10) {
|
|
|
|
errorstream << "Ignoring too many arguments to translation" << std::endl;
|
|
|
|
std::wstring arg;
|
|
|
|
translate_all(s, i, arg);
|
|
|
|
args.push_back(arg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
output.put(L'@');
|
|
|
|
output << arg_number;
|
|
|
|
++arg_number;
|
|
|
|
std::wstring arg;
|
|
|
|
translate_all(s, i, arg);
|
|
|
|
args.push_back(arg);
|
|
|
|
} else {
|
|
|
|
// This is an escape sequence *inside* the template string to translate itself.
|
|
|
|
// This should not happen, show an error message.
|
|
|
|
errorstream << "Ignoring escape sequence '" << wide_to_narrow(escape_sequence) << "' in translation" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Translate the template.
|
|
|
|
std::wstring toutput = g_translations->getTranslation(textdomain, output.str());
|
|
|
|
|
|
|
|
// Put back the arguments in the translated template.
|
|
|
|
std::wostringstream result;
|
|
|
|
size_t j = 0;
|
|
|
|
while (j < toutput.length()) {
|
|
|
|
// Normal character, add it to output and continue.
|
|
|
|
if (toutput[j] != L'@' || j == toutput.length() - 1) {
|
|
|
|
result.put(toutput[j]);
|
|
|
|
++j;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++j;
|
|
|
|
// Literal escape for '@'.
|
|
|
|
if (toutput[j] == L'@') {
|
|
|
|
result.put(L'@');
|
|
|
|
++j;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here we have an argument; get its index and add the translated argument to the output.
|
|
|
|
int arg_index = toutput[j] - L'1';
|
|
|
|
++j;
|
2017-08-25 13:06:59 +02:00
|
|
|
if (0 <= arg_index && (size_t)arg_index < args.size()) {
|
|
|
|
result << args[arg_index];
|
|
|
|
} else {
|
|
|
|
// This is not allowed: show an error message
|
|
|
|
errorstream << "Ignoring out-of-bounds argument escape sequence in translation" << std::endl;
|
|
|
|
}
|
2017-01-31 18:05:03 +01:00
|
|
|
}
|
|
|
|
res = result.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void translate_all(const std::wstring &s, size_t &i, std::wstring &res) {
|
|
|
|
std::wostringstream output;
|
|
|
|
while (i < s.length()) {
|
|
|
|
// Not an escape sequence: just add the character.
|
|
|
|
if (s[i] != '\x1b') {
|
|
|
|
output.put(s[i]);
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have an escape sequence: locate it and its data
|
|
|
|
// It is either a single character, or it begins with '('
|
|
|
|
// and extends up to the following ')', with '\' as an escape character.
|
|
|
|
size_t escape_start = i;
|
|
|
|
++i;
|
|
|
|
size_t start_index = i;
|
|
|
|
size_t length;
|
|
|
|
if (i == s.length()) {
|
|
|
|
length = 0;
|
|
|
|
} else if (s[i] == L'(') {
|
|
|
|
++i;
|
|
|
|
++start_index;
|
|
|
|
while (i < s.length() && s[i] != L')') {
|
|
|
|
if (s[i] == L'\\') {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
length = i - start_index;
|
|
|
|
++i;
|
|
|
|
if (i > s.length())
|
|
|
|
i = s.length();
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
length = 1;
|
|
|
|
}
|
|
|
|
std::wstring escape_sequence(s, start_index, length);
|
|
|
|
|
|
|
|
// The escape sequence is now reconstructed.
|
|
|
|
std::vector<std::wstring> parts = split(escape_sequence, L'@');
|
|
|
|
if (parts[0] == L"E") {
|
|
|
|
// "End of argument" escape sequence. Exit.
|
|
|
|
break;
|
|
|
|
} else if (parts[0] == L"T") {
|
|
|
|
// Beginning of translated string.
|
|
|
|
std::wstring textdomain;
|
|
|
|
if (parts.size() > 1)
|
|
|
|
textdomain = parts[1];
|
|
|
|
std::wstring translated;
|
|
|
|
translate_string(s, textdomain, i, translated);
|
|
|
|
output << translated;
|
|
|
|
} else {
|
|
|
|
// Another escape sequence, such as colors. Preserve it.
|
|
|
|
output << std::wstring(s, escape_start, i - escape_start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res = output.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring translate_string(const std::wstring &s) {
|
|
|
|
size_t i = 0;
|
|
|
|
std::wstring res;
|
|
|
|
translate_all(s, i, res);
|
|
|
|
return res;
|
|
|
|
}
|