4 Website: http://www.vultaire.net/software/jben/
5 License: GNU General Public License (GPL) version 2
6 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt)
8 File: encoding_convert.h
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>
24 #ifndef encoding_convert_h
25 #define encoding_convert_h
38 void DestroyUTFConv();
39 #define utfconv_wm(data) ConvertString<wchar_t, char>(data, wcType.c_str(), "UTF-8")
40 #define utfconv_mw(data) ConvertString<char, wchar_t>(data, "UTF-8", wcType.c_str())
42 /* This is the core converter function. */
43 template <class tsrc
, class tdest
>
44 basic_string
<tdest
> ConvertString
45 (const basic_string
<tsrc
>& sourceData
, const iconv_t
& converter
) {
46 basic_string
<tdest
> result
;
47 int sLen
= sourceData
.length()+1;
48 size_t inputBytesLeft
= sLen
* sizeof(tsrc
);
49 size_t outputBytesLeft
= sLen
* 4;
50 char *buffer
= new char[outputBytesLeft
];
51 memset((void*)buffer
, 0, outputBytesLeft
);
52 /* The libc iconv function takes a char* source, while the libiconv iconv
53 takes a const char* source. */
55 const char *srcData
= (char *)sourceData
.c_str();
57 char *srcData
= (char *)sourceData
.c_str();
60 char *destData
= buffer
;
64 = iconv(converter
, &srcData
, &inputBytesLeft
,
65 &destData
, &outputBytesLeft
);
66 if(retcode
==(size_t)-1) {
67 /* If we put in error reporting later, we'll want this to return
68 conversion errors. For now though, we'll do nothing and simply
71 oss
<< "Converting encoding using converter 0x"
72 << setw(8) << setfill('0') << hex
<< converter
<< dec
73 << ": error code = " << errno
;
74 el
.Push(EL_Error
, oss
.str());
78 /* Copy result data to the string or wstring */
79 result
= (tdest
*) buffer
;
86 /* The below is for "on-the-fly" iconv conversions. It creates a converter
87 object, and then passes everything into the core converter function. */
88 template <class tsrc
, class tdest
>
89 basic_string
<tdest
> ConvertString
90 (const basic_string
<tsrc
>& sourceData
,
91 const char* sourceEncoding
,
92 const char* targetEncoding
) {
93 basic_string
<tdest
> result
;
94 iconv_t conv
= iconv_open(targetEncoding
, sourceEncoding
);
95 if(conv
== (iconv_t
)-1) return basic_string
<tdest
>();
96 result
= ConvertString
<tsrc
,tdest
>(sourceData
, conv
);