Skip to content

Commit

Permalink
The returned format is now always UTF-8 even for Windows. This means …
Browse files Browse the repository at this point in the history
…that all arcitectures can now trust it*.

* Linux can be configured with something besides UTF-8 but then the user is asking for trouble if he goes outside UTF-7.
  • Loading branch information
sago007 committed Oct 3, 2016
1 parent 3931579 commit b81b63a
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions sago/platform_folders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,33 @@

#define strtok_r strtok_s

static std::string win32_utf16_to_utf8(const wchar_t* wstr)
{
std::string res;
// If the 6th parameter is 0 then WideCharToMultiByte returns the number of bytes needed to store the result.
int actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
if (actualSize > 0) {
//If the converted UTF-8 string could not be in the initial buffer. Allocate one that can hold it.
std::vector<char> buffer(actualSize);
actualSize = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &buffer[0], buffer.size(), NULL, NULL);
res = buffer.data();
}
if (actualSize == 0) {
// WideCharToMultiByte return 0 for errors.
std::string errorMsg = "UTF16 to UTF8 failed with error code: " + GetLastError();
throw std::runtime_error(errorMsg.c_str());
}
return res;
}

static std::string GetWindowsFolder(int folderId, const char* errorMsg) {
char szPath[MAX_PATH];
wchar_t szPath[MAX_PATH];
szPath[0] = 0;
if ( !SUCCEEDED( SHGetFolderPathA( NULL, folderId, NULL, 0, szPath ) ) )
if ( !SUCCEEDED( SHGetFolderPathW( NULL, folderId, NULL, 0, szPath ) ) )
{
throw std::runtime_error(errorMsg);
}
return szPath;
return win32_utf16_to_utf8(szPath);
}

static std::string GetAppData() {
Expand Down

0 comments on commit b81b63a

Please sign in to comment.