From b703102d9b3981d18ccdec9cc3a936408824a202 Mon Sep 17 00:00:00 2001 From: James Turner Date: Sun, 10 Mar 2013 13:38:29 +0000 Subject: [PATCH] Part of fixing bug 1055. Add machinery to convert hateful legacy Windows encodings to UTF-8. --- simgear/misc/strutils.cxx | 34 ++++++++++++++++++++++++++++++++++ simgear/misc/strutils.hxx | 6 ++++++ 2 files changed, 40 insertions(+) diff --git a/simgear/misc/strutils.cxx b/simgear/misc/strutils.cxx index 8c4accf6..c6687158 100644 --- a/simgear/misc/strutils.cxx +++ b/simgear/misc/strutils.cxx @@ -300,6 +300,40 @@ namespace simgear { return rslt; } + +#ifdef SG_WINDOWS + #include +#endif + +std::string convertWindowsLocal8BitToUtf8(const std::string& a) +{ +#ifdef SG_WINDOWS + DWORD flags = 0; + std::vector 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 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 diff --git a/simgear/misc/strutils.hxx b/simgear/misc/strutils.hxx index f5362073..c9aa525c 100644 --- a/simgear/misc/strutils.hxx +++ b/simgear/misc/strutils.hxx @@ -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 -- 2.39.5