From 4a53a30b91933cc0e34cfb76e8c7d5bd814e9f56 Mon Sep 17 00:00:00 2001 From: zeromus Date: Sun, 12 May 2024 21:33:33 -0400 Subject: [PATCH] =?UTF-8?q?winport=20-=20fix=20bug=20where=20desmume=20wou?= =?UTF-8?q?ld=20create=20working=20directory=20using=20some=20wrong=20loca?= =?UTF-8?q?le=20encoding=20and=20produce=20a=20Pok=E9=AD=AFn=20directory?= =?UTF-8?q?=20instead=20of=20using=20the=20Pok=C3=A9mon=20that=20was=20alr?= =?UTF-8?q?eady=20there=20(fixes=20#791)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- desmume/src/frontend/windows/winutil.cpp | 2 +- desmume/src/path.cpp | 2 +- desmume/src/utils/xstring.cpp | 13 +++++++++++++ desmume/src/utils/xstring.h | 2 ++ 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/desmume/src/frontend/windows/winutil.cpp b/desmume/src/frontend/windows/winutil.cpp index 1bab5f8d4..07e8b4bad 100644 --- a/desmume/src/frontend/windows/winutil.cpp +++ b/desmume/src/frontend/windows/winutil.cpp @@ -87,7 +87,7 @@ void GetINIPath() } FCEUD_MakePathDirs(IniName); - wcscpy(IniNameW,mbstowcs(IniName).c_str()); //careful to use locale C-style mbstowcs to get IniName (which is with locale encoding) to unicode + wcscpy(IniNameW,mbstowcs_locale(IniName).c_str()); //write BOM to get unicode FILE* test = fopen(IniName,"rb"); diff --git a/desmume/src/path.cpp b/desmume/src/path.cpp index c2c2b883d..d71b07ecf 100644 --- a/desmume/src/path.cpp +++ b/desmume/src/path.cpp @@ -170,7 +170,7 @@ void createDirectoryRecursively(std::wstring path) void FCEUD_MakePathDirs(const char *fname) { - createDirectoryRecursively(mbstowcs(fname)); + createDirectoryRecursively(mbstowcs_locale(fname)); } #endif //------------------------------ diff --git a/desmume/src/utils/xstring.cpp b/desmume/src/utils/xstring.cpp index d491b180b..6b92f5f30 100644 --- a/desmume/src/utils/xstring.cpp +++ b/desmume/src/utils/xstring.cpp @@ -284,6 +284,19 @@ std::string mass_replace(const std::string &source, const std::string &victim, c return answer; } +std::wstring mbstowcs_locale(std::string str) +{ + #ifdef HOST_WINDOWS + int plenty = str.size()*4+1; + wchar_t *wgarbage = new wchar_t[plenty]; + MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, str.data(), -1, wgarbage, plenty); + std::wstring ret = wgarbage; + delete[] wgarbage; + return ret; + #endif + return mbstowcs(str); +} + //convert a std::string to std::wstring std::wstring mbstowcs(std::string str) { diff --git a/desmume/src/utils/xstring.h b/desmume/src/utils/xstring.h index 92a881ceb..5741d998f 100644 --- a/desmume/src/utils/xstring.h +++ b/desmume/src/utils/xstring.h @@ -107,5 +107,7 @@ std::string mass_replace(const std::string &source, const std::string &victim, c std::wstring mbstowcs(std::string str); std::string wcstombs(std::wstring str); +std::wstring mbstowcs_locale(std::string str); + #endif