From bf14371f9e8ee2ce9d6a04fd7149511735abdb3e Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Sat, 16 Nov 2019 19:18:21 +0000 Subject: [PATCH] [util] Wide character conversion changes Replaces tows with an easier helper that fits in nicer with fixed-size arrays in DXGI, etc. Prefer UTF8 in tows/fromws. --- src/dxgi/dxgi_adapter.cpp | 3 +-- src/util/util_env.cpp | 8 +++++--- src/util/util_string.cpp | 24 +++++------------------- src/util/util_string.h | 7 ++++++- 4 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/dxgi/dxgi_adapter.cpp b/src/dxgi/dxgi_adapter.cpp index 2cad802f..abce251b 100644 --- a/src/dxgi/dxgi_adapter.cpp +++ b/src/dxgi/dxgi_adapter.cpp @@ -223,8 +223,7 @@ namespace dxvk { // Convert device name std::memset(pDesc->Description, 0, sizeof(pDesc->Description)); - ::MultiByteToWideChar(CP_UTF8, 0, deviceProp.deviceName, -1, - pDesc->Description, sizeof(pDesc->Description) / sizeof(*pDesc->Description)); + str::tows(deviceProp.deviceName, pDesc->Description); // Get amount of video memory // based on the Vulkan heaps diff --git a/src/util/util_env.cpp b/src/util/util_env.cpp index df0587b7..a804de6a 100644 --- a/src/util/util_env.cpp +++ b/src/util/util_env.cpp @@ -40,15 +40,17 @@ namespace dxvk::env { ::GetProcAddress(::GetModuleHandleW(L"kernel32.dll"), "SetThreadDescription")); if (proc != nullptr) { - auto wideName = str::tows(name); + auto wideName = std::vector(name.length() + 1); + str::tows(name.c_str(), wideName.data(), wideName.size()); (*proc)(::GetCurrentThread(), wideName.data()); } } bool createDirectory(const std::string& path) { - auto widePath = str::tows(path); - return !!CreateDirectoryW(widePath.data(), nullptr); + WCHAR widePath[MAX_PATH]; + str::tows(path.c_str(), widePath); + return !!CreateDirectoryW(widePath, nullptr); } } diff --git a/src/util/util_string.cpp b/src/util/util_string.cpp index c684ca0d..d19e0db7 100644 --- a/src/util/util_string.cpp +++ b/src/util/util_string.cpp @@ -1,14 +1,8 @@ #include "util_string.h" -#ifdef CP_UNIXCP -static constexpr int cp = CP_UNIXCP; -#else -static constexpr int cp = CP_ACP; -#endif - namespace dxvk::str { std::string fromws(const WCHAR *ws) { - size_t len = ::WideCharToMultiByte(cp, + size_t len = ::WideCharToMultiByte(CP_UTF8, 0, ws, -1, nullptr, 0, nullptr, nullptr); if (len <= 1) @@ -18,24 +12,16 @@ namespace dxvk::str { std::string result; result.resize(len); - ::WideCharToMultiByte(cp, 0, ws, -1, + ::WideCharToMultiByte(CP_UTF8, 0, ws, -1, &result.at(0), len, nullptr, nullptr); return result; } - std::vector tows(const std::string& str) { - int strLen = ::MultiByteToWideChar( - CP_ACP, 0, str.c_str(), str.length() + 1, - nullptr, 0); - - std::vector wideStr(strLen); - + void tows(const char* mbs, WCHAR* wcs, size_t wcsLen) { ::MultiByteToWideChar( - CP_ACP, 0, str.c_str(), str.length() + 1, - wideStr.data(), strLen); - - return wideStr; + CP_UTF8, 0, mbs, -1, + wcs, wcsLen); } } diff --git a/src/util/util_string.h b/src/util/util_string.h index 36807e12..8bf8802b 100644 --- a/src/util/util_string.h +++ b/src/util/util_string.h @@ -10,7 +10,12 @@ namespace dxvk::str { std::string fromws(const WCHAR *ws); - std::vector tows(const std::string& str); + void tows(const char* mbs, WCHAR* wcs, size_t wcsLen); + + template + void tows(const char* mbs, WCHAR (&wcs)[N]) { + return tows(mbs, wcs, N); + } inline void format1(std::stringstream&) { }