String case conversion, UTF-8 conversion.

This commit is contained in:
James Turner
2013-11-07 10:36:12 -08:00
parent 743244a69c
commit 237752ff62
2 changed files with 57 additions and 14 deletions

View File

@@ -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 <windows.h>
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 <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
WCharVec wideString = convertMultiByteToWString(CP_ACP, a);
// convert down to UTF-8
std::vector<char> 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"

View File

@@ -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<wchar_t> 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