From 5698e2baf8008b11706a4bbc4d62c8b584703834 Mon Sep 17 00:00:00 2001 From: Craig Robbins Date: Sat, 7 Mar 2015 15:09:27 +1000 Subject: Fix Android text bug (no text displaying) --- src/util/string.cpp | 131 +++++++++++++++++++++++++--------------------------- 1 file changed, 63 insertions(+), 68 deletions(-) (limited to 'src/util/string.cpp') diff --git a/src/util/string.cpp b/src/util/string.cpp index c48abe835..651293bfe 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -22,9 +22,9 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "numeric.h" #include "log.h" +#include "sha1.h" #include "base64.h" #include "hex.h" -#include "sha1.h" #include "../porting.h" #include @@ -32,12 +32,35 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include +static bool parseHexColorString(const std::string &value, video::SColor &color); +static bool parseNamedColorString(const std::string &value, video::SColor &color); + + +// You must free the returned string! +// The returned string is allocated using new +wchar_t *narrow_to_wide_c(const char *str) +{ + wchar_t* nstr = 0; #if defined(_WIN32) -#include // MultiByteToWideChar + int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, 0, 0); + if (nResult == 0) { + errorstream<<"gettext: MultiByteToWideChar returned null"< 0) { + *pwc = intermediate[0]; + return 1; + } + else { + return -1; + } +} - if (success) - *pwc = tmp[0]; +std::wstring narrow_to_wide(const std::string &mbs) { + size_t wcl = mbs.size(); - delete tmp; + std::wstring retval = L""; - return success ? 1 : -1; -} + for (unsigned int i = 0; i < wcl; i++) { + if (((unsigned char) mbs[i] >31) && + ((unsigned char) mbs[i] < 127)) { -// You must free the returned string! -const wchar_t *narrow_to_wide_c(const char *mbs) -{ - size_t mbl = strlen(mbs); - wchar_t *wcs = new wchar_t[mbl + 1]; - - size_t i, dest_i = 0; - for (i = 0; i < mbl; i++) { - if (((unsigned char) mbs[i] > 31) && - ((unsigned char) mbs[i] < 127)) { - wcs[dest_i++] = wide_chars[(unsigned char) mbs[i] - 32]; + retval += wide_chars[(unsigned char) mbs[i] -32]; } //handle newline else if (mbs[i] == '\n') { - wcs[dest_i++] = L'\n'; + retval += L'\n'; } } - wcs[dest_i] = '\0'; - - return wcs; -} -#else - -// You must free the returned string! -const wchar_t *narrow_to_wide_c(const char *mbs) -{ - wchar_t *wcs = NULL; -#if defined(_WIN32) - int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) mbs, -1, 0, 0); - if (nResult == 0) { - errorstream << "gettext: MultiByteToWideChar returned null" << std::endl; - } else { - wcs = new wchar_t[nResult]; - MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) mbs, -1, (WCHAR *) wcs, nResult); - } -#else - size_t wcl = mbstowcs(NULL, mbs, 0); - if (wcl == (size_t) -1) - return NULL; - wcs = new wchar_t[wcl + 1]; - size_t l = mbstowcs(wcs, mbs, wcl); - assert(l != (size_t) -1); // Should never happen if the last call worked - wcs[l] = '\0'; -#endif - - return wcs; + return retval; } -#endif +#else // not Android -std::wstring narrow_to_wide(const std::string& mbs) +std::wstring narrow_to_wide(const std::string &mbs) { size_t wcl = mbs.size(); Buffer wcs(wcl + 1); - size_t l = mbstowcs(*wcs, mbs.c_str(), wcl); - if (l == (size_t)(-1)) + size_t len = mbstowcs(*wcs, mbs.c_str(), wcl); + if (len == (size_t)(-1)) return L""; - wcs[l] = 0; + wcs[len] = 0; return *wcs; } +#endif + #ifdef __ANDROID__ -std::string wide_to_narrow(const std::wstring& wcs) { + +std::string wide_to_narrow(const std::wstring &wcs) { size_t mbl = wcs.size()*4; std::string retval = ""; @@ -165,17 +159,18 @@ std::string wide_to_narrow(const std::wstring& wcs) { return retval; } -#else -std::string wide_to_narrow(const std::wstring& wcs) + +#else // not Android + +std::string wide_to_narrow(const std::wstring &wcs) { - size_t mbl = wcs.size()*4; + size_t mbl = wcs.size() * 4; SharedBuffer mbs(mbl+1); - size_t l = wcstombs(*mbs, wcs.c_str(), mbl); - if(l == (size_t)(-1)) { + size_t len = wcstombs(*mbs, wcs.c_str(), mbl); + if (len == (size_t)(-1)) return "Character conversion failed!"; - } else - mbs[l] = 0; + mbs[len] = 0; return *mbs; } @@ -188,7 +183,7 @@ std::string wide_to_narrow(const std::wstring& wcs) // compatibility with password-less players). std::string translatePassword(std::string playername, std::wstring password) { - if(password.length() == 0) + if (password.length() == 0) return ""; std::string slt = playername + wide_to_narrow(password); -- cgit v1.2.3