+++ /dev/null
---- a/libcharset/lib/localcharset.c 2015-06-10 11:55:25.933870724 +0200
-+++ b/libcharset/lib/localcharset.c 2015-06-10 11:55:39.578063493 +0200
-@@ -47,7 +47,7 @@
-
- #if !defined WIN32_NATIVE
- # include <unistd.h>
--# if HAVE_LANGINFO_CODESET
-+# if HAVE_LANGINFO_CODESET && !(defined __ANDROID__)
- # include <langinfo.h>
- # else
- # if 0 /* see comment below */
-@@ -124,7 +124,7 @@ get_charset_aliases (void)
- cp = charset_aliases;
- if (cp == NULL)
- {
--#if !(defined DARWIN7 || defined VMS || defined WIN32_NATIVE || defined __CYGWIN__)
-+#if !(defined DARWIN7 || defined VMS || defined WIN32_NATIVE || defined __CYGWIN__ || defined __ANDROID__)
- const char *dir;
- const char *base = "charset.alias";
- char *file_name;
-@@ -338,6 +338,9 @@ get_charset_aliases (void)
- "CP54936" "\0" "GB18030" "\0"
- "CP65001" "\0" "UTF-8" "\0";
- # endif
-+# if defined __ANDROID__
-+ cp = "*" "\0" "UTF-8" "\0";
-+# endif
- #endif
-
- charset_aliases = cp;
-@@ -361,7 +364,7 @@ locale_charset (void)
- const char *codeset;
- const char *aliases;
-
--#if !(defined WIN32_NATIVE || defined OS2)
-+#if !(defined WIN32_NATIVE || defined OS2 || defined __ANDROID__)
-
- # if HAVE_LANGINFO_CODESET
-
return true;
}
+#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
+
std::wstring utf8_to_wide(const std::string &input)
{
size_t inbuf_size = input.length() + 1;
char *outbuf = new char[outbuf_size];
memset(outbuf, 0, outbuf_size);
- if (!convert("WCHAR_T", "UTF-8", outbuf, outbuf_size, inbuf, inbuf_size)) {
+#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)) {
infostream << "Couldn't convert UTF-8 string 0x" << hex_encode(input)
<< " into wstring" << std::endl;
delete[] inbuf;
return out;
}
-#ifdef __ANDROID__
-// TODO: this is an ugly fix for wide_to_utf8 somehow not working on android
-std::string wide_to_utf8(const std::wstring &input)
-{
- return wide_to_narrow(input);
-}
-#else
std::string wide_to_utf8(const std::wstring &input)
{
size_t inbuf_size = (input.length() + 1) * sizeof(wchar_t);
char *outbuf = new char[outbuf_size];
memset(outbuf, 0, outbuf_size);
- if (!convert("UTF-8", "WCHAR_T", outbuf, outbuf_size, inbuf, inbuf_size)) {
+ if (!convert("UTF-8", DEFAULT_ENCODING, outbuf, outbuf_size, inbuf, inbuf_size)) {
infostream << "Couldn't convert wstring 0x" << hex_encode(inbuf, inbuf_size)
<< " into UTF-8 string" << std::endl;
delete[] inbuf;
return out;
}
-#endif
#else // _WIN32
std::wstring utf8_to_wide(const std::string &input)
// The returned string is allocated using new
wchar_t *narrow_to_wide_c(const char *str)
{
- wchar_t *nstr = NULL;
+ wchar_t *nstr = nullptr;
#if defined(_WIN32)
int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, 0, 0);
if (nResult == 0) {
return nstr;
}
-
-#ifdef __ANDROID__
-
-const wchar_t* wide_chars =
- L" !\"#$%&'()*+,-./0123456789:;<=>?@"
- L"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"
- L"abcdefghijklmnopqrstuvwxyz{|}~";
-
-int wctomb(char *s, wchar_t wc)
-{
- for (unsigned int j = 0; j < (sizeof(wide_chars)/sizeof(wchar_t));j++) {
- if (wc == wide_chars[j]) {
- *s = (char) (j+32);
- return 1;
- }
- else if (wc == L'\n') {
- *s = '\n';
- return 1;
- }
- }
- return -1;
-}
-
-int mbtowc(wchar_t *pwc, const char *s, size_t n)
-{
- std::wstring intermediate = narrow_to_wide(s);
-
- if (intermediate.length() > 0) {
- *pwc = intermediate[0];
- return 1;
- }
- else {
- return -1;
- }
-}
-
std::wstring narrow_to_wide(const std::string &mbs) {
size_t wcl = mbs.size();
-
- std::wstring retval = L"";
-
- for (unsigned int i = 0; i < wcl; i++) {
- if (((unsigned char) mbs[i] >31) &&
- ((unsigned char) mbs[i] < 127)) {
-
- retval += wide_chars[(unsigned char) mbs[i] -32];
- }
- //handle newline
- else if (mbs[i] == '\n') {
- retval += L'\n';
- }
- }
-
- return retval;
-}
-
-#else // not Android
-
-std::wstring narrow_to_wide(const std::string &mbs)
-{
- size_t wcl = mbs.size();
Buffer<wchar_t> wcs(wcl + 1);
size_t len = mbstowcs(*wcs, mbs.c_str(), wcl);
if (len == (size_t)(-1))
return *wcs;
}
-#endif
-
-#ifdef __ANDROID__
-
-std::string wide_to_narrow(const std::wstring &wcs) {
- size_t mbl = wcs.size()*4;
-
- std::string retval = "";
- for (unsigned int i = 0; i < wcs.size(); i++) {
- wchar_t char1 = (wchar_t) wcs[i];
-
- if (char1 == L'\n') {
- retval += '\n';
- continue;
- }
-
- for (unsigned int j = 0; j < wcslen(wide_chars);j++) {
- wchar_t char2 = (wchar_t) wide_chars[j];
-
- if (char1 == char2) {
- char toadd = (j+32);
- retval += toadd;
- break;
- }
- }
- }
-
- return retval;
-}
-
-#else // not Android
std::string wide_to_narrow(const std::wstring &wcs)
{
return *mbs;
}
-#endif
std::string urlencode(const std::string &str)
{
u32 mask = 0;
char *s = &str[0];
char *flagstr;
- char *strpos = NULL;
+ char *strpos = nullptr;
while ((flagstr = strtok_r(s, ",", &strpos))) {
- s = NULL;
+ s = nullptr;
while (*flagstr == ' ' || *flagstr == '\t')
flagstr++;
s++;
if (!*s)
- return NULL;
+ return nullptr;
t = s;
while (*t) {