Fix gettext on MSVC

This commit is contained in:
BlockMen 2015-02-12 02:55:50 +01:00
parent e102cbd840
commit d302629392
3 changed files with 31 additions and 25 deletions

View File

@ -26,7 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "log.h" #include "log.h"
#if USE_GETTEXT && defined(_MSC_VER) #if USE_GETTEXT && defined(_MSC_VER)
#include <WinNls.h> #include <windows.h>
#include <map> #include <map>
#include <direct.h> #include <direct.h>
#include "filesys.h" #include "filesys.h"

View File

@ -41,16 +41,19 @@ void init_gettext(const char *path, const std::string &configured_language);
extern const wchar_t *narrow_to_wide_c(const char *mbs); extern const wchar_t *narrow_to_wide_c(const char *mbs);
extern std::wstring narrow_to_wide(const std::string &mbs); extern std::wstring narrow_to_wide(const std::string &mbs);
// You must free the returned string! // You must free the returned string!
inline const wchar_t *wgettext(const char *str) inline const wchar_t *wgettext(const char *str)
{ {
return narrow_to_wide_c(gettext(str)); return narrow_to_wide_c(gettext(str));
} }
// Gettext under MSVC needs this strange way. Just don't ask...
inline std::wstring wstrgettext(const std::string &text) inline std::wstring wstrgettext(const std::string &text)
{ {
return narrow_to_wide(gettext(text.c_str())); const wchar_t *tmp = wgettext(text.c_str());
std::wstring retval = (std::wstring)tmp;
delete[] tmp;
return retval;
} }
inline std::string strgettext(const std::string &text) inline std::string strgettext(const std::string &text)

View File

@ -99,11 +99,13 @@ const wchar_t *narrow_to_wide_c(const char *mbs)
{ {
wchar_t *wcs = NULL; wchar_t *wcs = NULL;
#if defined(_WIN32) #if defined(_WIN32)
int wcl = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) mbs, -1, NULL, 0); int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) mbs, -1, 0, 0);
if (!wcl) if (nResult == 0) {
return NULL; errorstream << "gettext: MultiByteToWideChar returned null" << std::endl;
wcs = new wchar_t[wcl]; } else {
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) mbs, -1, (WCHAR *) wcs, wcl); wcs = new wchar_t[nResult];
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) mbs, -1, (WCHAR *) wcs, nResult);
}
#else #else
size_t wcl = mbstowcs(NULL, mbs, 0); size_t wcl = mbstowcs(NULL, mbs, 0);
if (wcl == (size_t) -1) 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) std::wstring narrow_to_wide(const std::string& mbs)
{ {
const wchar_t *wcs = narrow_to_wide_c(mbs.c_str()); size_t wcl = mbs.size();
if (!wcs) 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>"; return L"<invalid multibyte string>";
std::wstring wstr(wcs); wcs[l] = 0;
delete [] wcs; return *wcs;
return wstr;
} }
#ifdef __ANDROID__ #ifdef __ANDROID__