From: Clément de l'Hamaide Date: Sun, 1 Dec 2013 17:47:49 +0000 (+0100) Subject: Implement UTF-8 to Latin1 converter X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=5861b52cffd9f0a7969562216645ec8b7a440db0;p=simgear.git Implement UTF-8 to Latin1 converter --- diff --git a/simgear/misc/strutils.cxx b/simgear/misc/strutils.cxx index 52a8d619..25480271 100644 --- a/simgear/misc/strutils.cxx +++ b/simgear/misc/strutils.cxx @@ -35,6 +35,39 @@ using std::stringstream; namespace simgear { namespace strutils { + /* + * utf8ToLatin1() convert utf8 to latin, useful for accent character (i.e éâàîè...) + */ + template size_t get_length (Iterator p) { + unsigned char c = static_cast (*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 value_type get_value (Iterator p) { + size_t len = get_length (p); + if (len == 1) return *p; + value_type res = static_cast ( *p & (0xff >> (len + 1))) << ((len - 1) * 6 ); + for (--len; len; --len) + res |= (static_cast (*(++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(p); + if (value > 0xff) SG_LOG(SG_IO, SG_WARN, "utf8ToLatin1: wrong char value: " << value); + s_latin1 += static_cast(value); + } + return s_latin1; + } + /** * */ diff --git a/simgear/misc/strutils.hxx b/simgear/misc/strutils.hxx index 47f69bac..9070b246 100644 --- a/simgear/misc/strutils.hxx +++ b/simgear/misc/strutils.hxx @@ -38,6 +38,11 @@ typedef std::vector < std::string > string_list; namespace simgear { namespace strutils { + /** + * utf8ToLatin1() convert utf8 to latin, useful for accent character (i.e éâàîè...) + */ + std::string utf8ToLatin1( std::string & s_utf8 ); + // /** // * atof() wrapper for "string" type // */