Show
Ignore:
Timestamp:
11/16/08 16:11:57 (8 weeks ago)
Author:
artem
Message:

+ to_utf8 function to convert from icu::UnicodeString? to utf-8 std::string

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/include/mapnik/value.hpp

    r693 r759  
    3030// boost 
    3131#include <boost/variant.hpp> 
     32#include <boost/scoped_array.hpp> 
    3233// stl 
    3334#include <iostream> 
     
    3738// uci 
    3839#include <unicode/unistr.h> 
     40#include <unicode/ustring.h> 
     41 
    3942 
    4043namespace mapnik  { 
     44 
     45   inline void to_utf8(UnicodeString const& input, std::string & target) 
     46   { 
     47      if (input.length() == 0) return; 
     48       
     49      const int BUF_SIZE = 256; 
     50      char  buf [BUF_SIZE]; 
     51      int len; 
     52       
     53      UErrorCode err = U_ZERO_ERROR; 
     54      u_strToUTF8(buf, BUF_SIZE, &len, input.getBuffer(), input.length(), &err); 
     55      if (err == U_BUFFER_OVERFLOW_ERROR || err == U_STRING_NOT_TERMINATED_WARNING )  
     56      { 
     57         boost::scoped_array<char> buf_ptr(new char [len+1]); 
     58         err = U_ZERO_ERROR; 
     59         u_strToUTF8(buf_ptr.get() , len + 1, &len, input.getBuffer(), input.length(), &err); 
     60         target.assign(buf_ptr.get() , len); 
     61      } 
     62      else 
     63      { 
     64         target.assign(buf, len); 
     65      } 
     66   } 
    4167    
    4268   struct value_null 
     
    506532            std::string operator() (UnicodeString const& val) const 
    507533            { 
    508                /* 
    509                std::stringstream ss; 
    510                UnicodeString::const_iterator pos = val.begin(); 
    511                ss << std::hex ; 
    512                for (;pos!=val.end();++pos) 
    513                { 
    514                   wchar_t c = *pos; 
    515                   if (c < 0x80)  
    516                   { 
    517                      ss << char(c); 
    518                   } 
    519                   else 
    520                   { 
    521                      ss << "\\x"; 
    522                      unsigned c0 = (c >> 8) & 0xff; 
    523                      if (c0) ss << c0; 
    524                      ss << (c & 0xff); 
    525                   } 
    526                } 
    527                return "\'" + ss.str() + "\'"; 
    528                */ 
    529                return "TODO"; 
     534               std::string utf8; 
     535               to_utf8(val,utf8); 
     536               return "'" + utf8 + "'"; 
    530537            }  
    531538