namespace simgear {
namespace strutils {
+ /*
+ * utf8ToLatin1() convert utf8 to latin, useful for accent character (i.e éâàîè...)
+ */
+ template <typename Iterator> size_t get_length (Iterator p) {
+ unsigned char c = static_cast<unsigned char> (*p);
+ if (c < 0x80) return 1;
+ else if (!(c & 0x20)) return 2;
+ else if (!(c & 0x10)) return 3;
+ else if (!(c & 0x08)) return 4;
+ else if (!(c & 0x04)) return 5;
+ else return 6;
+ }
+
+ typedef unsigned int value_type;
+ template <typename Iterator> value_type get_value (Iterator p) {
+ size_t len = get_length (p);
+ if (len == 1) return *p;
+ value_type res = static_cast<unsigned char> ( *p & (0xff >> (len + 1))) << ((len - 1) * 6 );
+ for (--len; len; --len)
+ res |= (static_cast<unsigned char> (*(++p)) - 0x80) << ((len - 1) * 6);
+ return res;
+ }
+
+ string utf8ToLatin1( string& s_utf8 ) {
+ string s_latin1;
+ for (string::iterator p = s_utf8.begin(); p != s_utf8.end(); ++p) {
+ value_type value = get_value<string::iterator&>(p);
+ if (value > 0xff) SG_LOG(SG_IO, SG_WARN, "utf8ToLatin1: wrong char value: " << value);
+ s_latin1 += static_cast<char>(value);
+ }
+ return s_latin1;
+ }
+
/**
*
*/
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(),
#endif
}
+
+
static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"