]> git.mxchange.org Git - simgear.git/commitdiff
Part of fixing bug 1055.
authorJames Turner <zakalawe@mac.com>
Sun, 10 Mar 2013 13:38:29 +0000 (13:38 +0000)
committerJames Turner <zakalawe@mac.com>
Sun, 10 Mar 2013 13:38:29 +0000 (13:38 +0000)
Add machinery to convert hateful legacy Windows encodings to UTF-8.

simgear/misc/strutils.cxx
simgear/misc/strutils.hxx

index 8c4accf6bbcf711b80a169bab695b158502a4df3..c6687158140892f176a8978bff5f0ba5742666eb 100644 (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
index f5362073d9a2c5d9f16c4a9e71ba04c3d683a6eb..c9aa525c4b4f8389e2983bb84517478cb6a44533 100644 (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