27 #include "../Defines.h"
37 #ifdef TERRALIB_GNU_ICONV_ENABLED
43 static const char* iconv_names[] = {
"UTF-8",
"CP1250",
"CP1251",
"CP1252",
"CP1253",
"CP1254",
"CP1257",
"ISO-8859-1"};
46 #define TE_CONVERSION_BUFFERSIZE_SIZE 64
52 : m_fromCode(fromCode),
56 throw Exception(
TE_TR(
"Impossible conversion of unknown char encoding!"));
58 #ifdef TERRALIB_GNU_ICONV_ENABLED
59 m_cd = iconv_open(iconv_names[toCode], iconv_names[fromCode]);
64 throw Exception(
TE_TR(
"Failed to start iconv to start converting charsets: the conversion from fromCode to toCode is not supported!"));
66 throw Exception(
TE_TR(
"Failed to start iconv to start converting charsets!"));
73 #ifdef TERRALIB_GNU_ICONV_ENABLED
75 throw Exception(
TE_TR(
"Failed to close iconv! This wasn't supposed to occur! Contact TerraLib Team!"));
81 #ifdef TERRALIB_GNU_ICONV_ENABLED
82 std::ostringstream outstring(std::ios_base::out);
83 const char* inbuff = src.c_str();
84 std::size_t inbytesleft = src.length();
88 std::size_t nbytes = 0;
92 char* outbuff = outchar;
95 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
96 nbytes = iconv(m_cd, &inbuff, &inbytesleft, &outbuff, &outbytesleft);
98 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX || TE_PLATFORM == TE_PLATFORMCODE_APPLE
99 nbytes = iconv(m_cd, (
char**)(&inbuff), &inbytesleft, &outbuff, &outbytesleft);
102 #error "Platform not supported! Please contact terralib-team@dpi.inpe.br"
105 if((nbytes == (std::size_t)(-1)) && (errno != E2BIG))
107 nbytes = iconv(m_cd, 0, 0, 0, 0);
109 if(nbytes == (std::size_t)(-1))
110 throw Exception(
TE_TR(
"Failed to convert character sets and also to bring iconv to its initial state!"));
121 nbytes = iconv(m_cd, 0, 0, 0, 0);
123 if(nbytes == (std::size_t)(-1))
124 throw Exception(
TE_TR(
"Failed to bring iconv to its initial state!"));
126 return outstring.str();
135 throw Exception(
TE_TR(
"Impossible conversion of unknown char encoding!"));
137 #ifdef TERRALIB_GNU_ICONV_ENABLED
138 iconv_t cd = iconv_open(iconv_names[toCode], iconv_names[fromCode]);
143 throw Exception(
TE_TR(
"Failed to start iconv to start converting charsets: the conversion from fromCode to toCode is not supported!"));
145 throw Exception(
TE_TR(
"Failed to start iconv to start converting charsets!"));
148 std::ostringstream outstring(std::ios_base::out);
149 const char* inbuff = src.c_str();
150 std::size_t inbytesleft = src.length();
156 char* outbuff = outchar;
159 #if TE_PLATFORM == TE_PLATFORMCODE_MSWINDOWS
160 std::size_t nbytes = iconv(cd, &inbuff, &inbytesleft, &outbuff, &outbytesleft);
162 #elif TE_PLATFORM == TE_PLATFORMCODE_LINUX || TE_PLATFORM == TE_PLATFORMCODE_APPLE
163 std::size_t nbytes = iconv(cd, (
char**)(&inbuff), &inbytesleft, &outbuff, &outbytesleft);
166 #error "Platform not supported! Please contact terralib-team@dpi.inpe.br"
169 if((nbytes == (std::size_t)(-1)) && (errno != E2BIG))
184 return outstring.str();
192 return sm_encodingNames[code];
197 std::map<CharEncoding, std::string>::const_iterator it;
198 for(it = sm_encodingNames.begin(); it != sm_encodingNames.end(); ++it)
199 if(it->second == name)
207 if(!sm_encodingNames.empty())
210 sm_encodingNames[
UTF8 ] =
"UTF-8";
211 sm_encodingNames[
CP1250] =
"CP1250";
212 sm_encodingNames[
CP1251] =
"CP1251";
213 sm_encodingNames[
CP1252] =
"CP1252";
214 sm_encodingNames[
CP1253] =
"CP1253";
215 sm_encodingNames[
CP1254] =
"CP1254";
216 sm_encodingNames[
CP1257] =
"CP1257";
217 sm_encodingNames[
LATIN1] =
"Latin1";
CharEncoding
Supported charsets (character encoding).
static std::map< CharEncoding, std::string > sm_encodingNames
Static map with the encoding names.
iconv_t m_cd
The iconv state machine used to convert string from one character to another one. ...
static std::string convert(const std::string &src, const CharEncoding &fromCode, const CharEncoding &toCode)
An static method that converts the source string to a target charset.
CharEncodingConv(const CharEncoding &fromCode, const CharEncoding &toCode)
It prepare sthe conversion from a charset code to another one.
static void initialize()
It initializes the CharEncoding Converter.
#define TE_TR(message)
It marks a string in order to get translated.
static CharEncoding getCharEncodingType(const std::string &name)
It returns the charset type of the given charset name.
const CharEncoding m_fromCode
The origin charset.
std::string conv(const std::string &src)
It converts the source string to a target charset.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
A class that deals with character encoding/decoding.
This class is designed for dealing with multi-language text translation in TerraLib.
const CharEncoding m_toCode
The target charset.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
static std::string getCharEncodingName(const CharEncoding &code)
It returns the name of the given charset.
~CharEncodingConv()
Destructor.
#define TE_CONVERSION_BUFFERSIZE_SIZE