From 237752ff621266277b9bdbab7f77861c13ea7787 Mon Sep 17 00:00:00 2001 From: James Turner Date: Thu, 7 Nov 2013 10:36:12 -0800 Subject: [PATCH] String case conversion, UTF-8 conversion. --- simgear/misc/strutils.cxx | 55 +++++++++++++++++++++++++++++---------- simgear/misc/strutils.hxx | 16 ++++++++++++ 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/simgear/misc/strutils.cxx b/simgear/misc/strutils.cxx index 10a4c679..52a8d619 100644 --- a/simgear/misc/strutils.cxx +++ b/simgear/misc/strutils.cxx @@ -305,26 +305,51 @@ namespace simgear { return rslt; } + string lowercase(const string &s) { + string rslt(s); + for(string::iterator p = rslt.begin(); p != rslt.end(); p++){ + *p = tolower(*p); + } + return rslt; + } + + void lowercase(string &s) { + for(string::iterator p = s.begin(); p != s.end(); p++){ + *p = tolower(*p); + } + } + +#if defined(SG_WINDOWS) + +#include + +static WCharVec convertMultiByteToWString(DWORD encoding, const std::string& a) +{ + WCharVec result; + DWORD flags = 0; + int requiredWideChars = MultiByteToWideChar(encoding, flags, + a.c_str(), a.size(), + NULL, 0); + result.resize(requiredWideChars); + MultiByteToWideChar(encoding, flags, a.c_str(), a.size(), + result.data(), result.size()); + return result; +} + +WCharVec convertUtf8ToWString(const std::string& a) +{ + return convertMultiByteToWString(CP_UTF8, a); +} -#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 + WCharVec wideString = convertMultiByteToWString(CP_ACP, a); + + // convert down to UTF-8 std::vector result; int requiredUTF8Chars = WideCharToMultiByte(CP_UTF8, flags, wideString.data(), wideString.size(), @@ -339,6 +364,8 @@ std::string convertWindowsLocal8BitToUtf8(const std::string& a) #endif } + + static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" diff --git a/simgear/misc/strutils.hxx b/simgear/misc/strutils.hxx index 7fce94a5..47f69bac 100644 --- a/simgear/misc/strutils.hxx +++ b/simgear/misc/strutils.hxx @@ -149,12 +149,28 @@ namespace simgear { */ std::string uppercase(const std::string &s); + /** + * Convert a string to lower case. + * @return lower case string + */ + std::string lowercase(const std::string &s); + + /** + * Convert a string to lower case in place + */ + void lowercase(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); +#if defined(SG_WINDOWS) + typedef std::vector WCharVec; + WCharVec convertUtf8ToWString(const std::string& a); +#endif + /** * convert base-64 encoded data to raw bytes (possibly with embedded * NULs). Throws an exception if input data is not base64, or is -- 2.39.5