From 5f1022917b655650f842dc8f34967716081685ef Mon Sep 17 00:00:00 2001 From: Tannin Date: Fri, 19 Sep 2014 22:01:37 +0200 Subject: - removed a use of wostringstream which seems to have been involved in a bug. Still not sure how - rewrote widechar <-> multibyte conversion functions to handle arbitrary string lengths --- src/shared/util.cpp | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'src/shared/util.cpp') diff --git a/src/shared/util.cpp b/src/shared/util.cpp index 61ca9cdc..4bc5a8a4 100644 --- a/src/shared/util.cpp +++ b/src/shared/util.cpp @@ -25,6 +25,7 @@ along with Mod Organizer. If not, see . #include #include #include +#include namespace MOShared { @@ -64,24 +65,28 @@ bool FileExists(const std::wstring &searchPath, const std::wstring &filename) std::string ToString(const std::wstring &source, bool utf8) { - char buffer[MAX_PATH]; - if (utf8) { - ::WideCharToMultiByte(CP_UTF8, 0, source.c_str(), -1, buffer, MAX_PATH, NULL, NULL); - } else { - ::WideCharToMultiByte(GetACP(), 0, source.c_str(), -1, buffer, MAX_PATH, NULL, NULL); + std::string result; + UINT codepage = utf8 ? CP_UTF8 : GetACP(); + int sizeRequired = ::WideCharToMultiByte(codepage, 0, &source[0], -1, NULL, 0, NULL, NULL); + if (sizeRequired == 0) { + throw windows_error("failed to convert string to multibyte"); } - return std::string(buffer); + result.resize(sizeRequired, '\0'); + ::WideCharToMultiByte(codepage, 0, &source[0], (int)source.size(), &result[0], sizeRequired, NULL, NULL); + return result; } std::wstring ToWString(const std::string &source, bool utf8) { - wchar_t buffer[MAX_PATH]; - if (utf8) { - ::MultiByteToWideChar(CP_UTF8, 0, source.c_str(), -1, buffer, MAX_PATH); - } else { - ::MultiByteToWideChar(GetACP(), 0, source.c_str(), -1, buffer, MAX_PATH); + std::wstring result; + UINT codepage = utf8 ? CP_UTF8 : GetACP(); + int sizeRequired = ::MultiByteToWideChar(codepage, 0, &source[0], (int)source.size(), NULL, 0); + if (sizeRequired == 0) { + throw windows_error("failed to convert string to wide character"); } - return std::wstring(buffer); + result.resize(sizeRequired, L'\0'); + ::MultiByteToWideChar(codepage, 0, &source[0], (int)source.size(), &result[0], sizeRequired); + return result; } std::string &ToLower(std::string &text) -- cgit v1.3.1