1
0
mirror of https://github.com/doitsujin/dxvk.git synced 2025-01-18 11:52:12 +01:00

[util] Reimplement fromws using WideCharToMultiByte and system codepage.

Also, using UTF-8 for convertion may not be what's intended, esp. if such strings end up being passed back to system API (eg. open file). The patch uses CP_ACP on mingw build (which is how Windows APIs will interpret it) or CP_UNIXCP on Wine (which is Wine extension to convert to whatever glibc and other host libs expect).

It's also needed for the next patch.
This commit is contained in:
Jacek Caban 2018-07-18 01:49:30 +02:00 committed by Philip Rebohle
parent 02ae42c7de
commit c2c10cc207
2 changed files with 24 additions and 7 deletions

View File

@ -16,7 +16,7 @@ namespace dxvk::env {
}
result.resize(len);
return str::fromws(result);
return str::fromws(result.data());
}
@ -27,7 +27,7 @@ namespace dxvk::env {
DWORD len = ::GetModuleFileNameW(NULL, &exePath.at(0), MAX_PATH);
exePath.resize(len);
std::string fullPath = str::fromws(exePath);
std::string fullPath = str::fromws(exePath.data());
auto n = fullPath.find_last_of('\\');
return (n != std::string::npos)
@ -52,3 +52,23 @@ namespace dxvk::env {
}
}
#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, 0, ws, -1, NULL, 0, NULL, NULL);
if (len <= 1)
return "";
len--;
std::string result;
result.resize(len);
WideCharToMultiByte(cp, 0, ws, -1, &result.at(0), len, NULL, NULL);
return result;
}
}

View File

@ -1,9 +1,8 @@
#pragma once
#include <locale>
#include <codecvt>
#include <string>
#include <sstream>
#include <windef.h>
namespace dxvk::str {
@ -22,8 +21,6 @@ namespace dxvk::str {
return stream.str();
}
inline std::string fromws(const std::wstring& ws) {
return std::wstring_convert<std::codecvt_utf8<wchar_t>>().to_bytes(ws);
}
std::string fromws(const WCHAR *ws);
}