summaryrefslogtreecommitdiff
path: root/src/shared/util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared/util.cpp')
-rw-r--r--src/shared/util.cpp21
1 files changed, 14 insertions, 7 deletions
diff --git a/src/shared/util.cpp b/src/shared/util.cpp
index 0f49d036..2ce88eca 100644
--- a/src/shared/util.cpp
+++ b/src/shared/util.cpp
@@ -55,15 +55,18 @@ std::string ToString(const std::wstring &source, bool utf8)
{
std::string result;
if (source.length() > 0) {
- UINT codepage = utf8 ? CP_UTF8 : GetACP();
- int sizeRequired = ::WideCharToMultiByte(codepage, 0, &source[0], -1, NULL, 0, NULL, NULL);
+ UINT codepage = CP_UTF8;
+ if (!utf8) {
+ codepage = AreFileApisANSI() ? GetACP() : GetOEMCP();
+ }
+ int sizeRequired = ::WideCharToMultiByte(codepage, 0, &source[0], -1, nullptr, 0, nullptr, nullptr);
if (sizeRequired == 0) {
throw windows_error("failed to convert string to multibyte");
}
// the size returned by WideCharToMultiByte contains zero termination IF -1 is specified for the length.
// we don't want that \0 in the string because then the length field would be wrong. Because madness
result.resize(sizeRequired - 1, '\0');
- ::WideCharToMultiByte(codepage, 0, &source[0], (int)source.size(), &result[0], sizeRequired, NULL, NULL);
+ ::WideCharToMultiByte(codepage, 0, &source[0], (int)source.size(), &result[0], sizeRequired, nullptr, nullptr);
}
return result;
@@ -73,8 +76,11 @@ std::wstring ToWString(const std::string &source, bool utf8)
{
std::wstring result;
if (source.length() > 0) {
- UINT codepage = utf8 ? CP_UTF8 : GetACP();
- int sizeRequired = ::MultiByteToWideChar(codepage, 0, source.c_str(), source.length(), NULL, 0);
+ UINT codepage = CP_UTF8;
+ if (!utf8) {
+ codepage = AreFileApisANSI() ? GetACP() : GetOEMCP();
+ }
+ int sizeRequired = ::MultiByteToWideChar(codepage, 0, source.c_str(), source.length(), nullptr, 0);
if (sizeRequired == 0) {
throw windows_error("failed to convert string to wide character");
}
@@ -113,7 +119,7 @@ std::wstring ToLower(const std::wstring &text)
VS_FIXEDFILEINFO GetFileVersion(const std::wstring &fileName)
{
- DWORD handle;
+ DWORD handle = 0UL;
DWORD size = ::GetFileVersionInfoSizeW(fileName.c_str(), &handle);
if (size == 0) {
throw windows_error("failed to determine file version info size");
@@ -121,11 +127,12 @@ VS_FIXEDFILEINFO GetFileVersion(const std::wstring &fileName)
void *buffer = new char[size];
try {
+ handle = 0UL;
if (!::GetFileVersionInfoW(fileName.c_str(), handle, size, buffer)) {
throw windows_error("failed to determine file version info");
}
- void *versionInfoPtr = NULL;
+ void *versionInfoPtr = nullptr;
UINT versionInfoLength = 0;
if (!::VerQueryValue(buffer, L"\\", &versionInfoPtr, &versionInfoLength)) {
throw windows_error("failed to determine file version");