aboutsummaryrefslogtreecommitdiff
path: root/src/util/string.cpp
diff options
context:
space:
mode:
authorBlockMen <nmuelll@web.de>2015-02-12 02:55:50 +0100
committerBlockMen <nmuelll@web.de>2015-02-12 16:03:55 +0100
commitd3026293927f560465b325280fef697cc3361daa (patch)
tree6dfe86bb89450f7a47be1e787e51541a20a33846 /src/util/string.cpp
parente102cbd840c3dc3d52865868369aea3b8ee21028 (diff)
downloadminetest-d3026293927f560465b325280fef697cc3361daa.tar.gz
minetest-d3026293927f560465b325280fef697cc3361daa.tar.bz2
minetest-d3026293927f560465b325280fef697cc3361daa.zip
Fix gettext on MSVC
Diffstat (limited to 'src/util/string.cpp')
-rw-r--r--src/util/string.cpp23
1 files changed, 13 insertions, 10 deletions
diff --git a/src/util/string.cpp b/src/util/string.cpp
index c18e58613..de669b473 100644
--- a/src/util/string.cpp
+++ b/src/util/string.cpp
@@ -99,11 +99,13 @@ const wchar_t *narrow_to_wide_c(const char *mbs)
{
wchar_t *wcs = NULL;
#if defined(_WIN32)
- int wcl = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) mbs, -1, NULL, 0);
- if (!wcl)
- return NULL;
- wcs = new wchar_t[wcl];
- MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) mbs, -1, (WCHAR *) wcs, wcl);
+ 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)
@@ -121,12 +123,13 @@ const wchar_t *narrow_to_wide_c(const char *mbs)
std::wstring narrow_to_wide(const std::string& mbs)
{
- const wchar_t *wcs = narrow_to_wide_c(mbs.c_str());
- if (!wcs)
+ size_t wcl = mbs.size();
+ Buffer<wchar_t> wcs(wcl + 1);
+ size_t l = mbstowcs(*wcs, mbs.c_str(), wcl);
+ if (l == (size_t)(-1))
return L"<invalid multibyte string>";
- std::wstring wstr(wcs);
- delete [] wcs;
- return wstr;
+ wcs[l] = 0;
+ return *wcs;
}
#ifdef __ANDROID__