From b423b45b224b3a5ab32473b6055c4e582efb561a Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Sun, 16 Nov 2008 21:11:57 +0000 Subject: [PATCH] + to_utf8 function to convert from icu::UnicodeString to utf-8 std::string --- include/mapnik/value.hpp | 51 +++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/include/mapnik/value.hpp b/include/mapnik/value.hpp index e2858ab3d..3cdad1b92 100644 --- a/include/mapnik/value.hpp +++ b/include/mapnik/value.hpp @@ -29,6 +29,7 @@ #include // boost #include +#include // stl #include #include @@ -36,8 +37,33 @@ #include // uci #include +#include + namespace mapnik { + + inline void to_utf8(UnicodeString const& input, std::string & target) + { + if (input.length() == 0) return; + + const int BUF_SIZE = 256; + char buf [BUF_SIZE]; + int len; + + UErrorCode err = U_ZERO_ERROR; + u_strToUTF8(buf, BUF_SIZE, &len, input.getBuffer(), input.length(), &err); + if (err == U_BUFFER_OVERFLOW_ERROR || err == U_STRING_NOT_TERMINATED_WARNING ) + { + boost::scoped_array buf_ptr(new char [len+1]); + err = U_ZERO_ERROR; + u_strToUTF8(buf_ptr.get() , len + 1, &len, input.getBuffer(), input.length(), &err); + target.assign(buf_ptr.get() , len); + } + else + { + target.assign(buf, len); + } + } struct value_null { @@ -505,28 +531,9 @@ namespace mapnik { { std::string operator() (UnicodeString const& val) const { - /* - std::stringstream ss; - UnicodeString::const_iterator pos = val.begin(); - ss << std::hex ; - for (;pos!=val.end();++pos) - { - wchar_t c = *pos; - if (c < 0x80) - { - ss << char(c); - } - else - { - ss << "\\x"; - unsigned c0 = (c >> 8) & 0xff; - if (c0) ss << c0; - ss << (c & 0xff); - } - } - return "\'" + ss.str() + "\'"; - */ - return "TODO"; + std::string utf8; + to_utf8(val,utf8); + return "'" + utf8 + "'"; } template