Part of fixing bug 1055.

Add machinery to convert hateful legacy Windows encodings to UTF-8.
This commit is contained in:
James Turner
2013-03-10 13:38:29 +00:00
parent 3c2ef75b50
commit b703102d9b
2 changed files with 40 additions and 0 deletions

View File

@@ -300,6 +300,40 @@ namespace simgear {
return rslt;
}
#ifdef SG_WINDOWS
#include <windows.h>
#endif
std::string convertWindowsLocal8BitToUtf8(const std::string& a)
{
#ifdef SG_WINDOWS
DWORD flags = 0;
std::vector<wchar_t> wideString;
// call to query transform size
int requiredWideChars = MultiByteToWideChar(CP_ACP, flags, a.c_str(), a.size(),
NULL, 0);
// allocate storage and call for real
wideString.resize(requiredWideChars);
MultiByteToWideChar(CP_ACP, flags, a.c_str(), a.size(),
wideString.data(), wideString.size());
// now convert back down to UTF-8
std::vector<char> result;
int requiredUTF8Chars = WideCharToMultiByte(CP_UTF8, flags,
wideString.data(), wideString.size(),
NULL, 0, NULL, NULL);
result.resize(requiredUTF8Chars);
WideCharToMultiByte(CP_UTF8, flags,
wideString.data(), wideString.size(),
result.data(), result.size(), NULL, NULL);
return std::string(result.data(), result.size());
#else
return a;
#endif
}
} // end namespace strutils
} // end namespace simgear

View File

@@ -149,6 +149,12 @@ namespace simgear {
*/
std::string uppercase(const std::string &s);
/**
* convert a string in the local Windows 8-bit encoding to UTF-8
* (no-op on other platforms)
*/
std::string convertWindowsLocal8BitToUtf8(const std::string& a);
} // end namespace strutils
} // end namespace simgear