summaryrefslogtreecommitdiff
path: root/src/shared/util.cpp
diff options
context:
space:
mode:
authorTannin <devnull@localhost>2015-01-03 12:34:26 +0100
committerTannin <devnull@localhost>2015-01-03 12:34:26 +0100
commite7b6a7cbddec32d300a1f758281c717402525053 (patch)
tree1f6415a5c53b05319e7035917e654f09bfee5e7a /src/shared/util.cpp
parentab925e1e7ca7c2b82c69c9db30848a025d94f75a (diff)
- bugfix: no exec info returned for .exe
- bugfix: wide string conversion functions seem to have failed for empty string
Diffstat (limited to 'src/shared/util.cpp')
-rw-r--r--src/shared/util.cpp34
1 files changed, 20 insertions, 14 deletions
diff --git a/src/shared/util.cpp b/src/shared/util.cpp
index d4a77929..d284b517 100644
--- a/src/shared/util.cpp
+++ b/src/shared/util.cpp
@@ -66,28 +66,34 @@ bool FileExists(const std::wstring &searchPath, const std::wstring &filename)
std::string ToString(const std::wstring &source, bool utf8)
{
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");
+ if (source.length() > 0) {
+ 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");
+ }
+ // 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);
}
- // 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);
+
return result;
}
std::wstring ToWString(const std::string &source, bool utf8)
{
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");
+ if (source.length() > 0) {
+ UINT codepage = utf8 ? CP_UTF8 : GetACP();
+ int sizeRequired = ::MultiByteToWideChar(codepage, 0, source.c_str(), source.length(), NULL, 0);
+ if (sizeRequired == 0) {
+ throw windows_error("failed to convert string to wide character");
+ }
+ result.resize(sizeRequired, L'\0');
+ ::MultiByteToWideChar(codepage, 0, source.c_str(), source.length(), &result[0], sizeRequired);
}
- result.resize(sizeRequired, L'\0');
- ::MultiByteToWideChar(codepage, 0, &source[0], (int)source.size(), &result[0], sizeRequired);
+
return result;
}