1 // Scintilla source code edit control
\r
2 // Converter.h - Encapsulation of iconv
\r
3 // Copyright 2004 by Neil Hodgson <neilh@scintilla.org>
\r
4 // The License.txt file describes the conditions under which this software may be distributed.
\r
7 #if GTK_MAJOR_VERSION >= 2
\r
8 typedef GIConv ConverterHandle;
\r
10 typedef iconv_t ConverterHandle;
\r
12 const ConverterHandle iconvhBad = (ConverterHandle)(-1);
\r
13 // Since various versions of iconv can not agree on whether the src argument
\r
14 // is char ** or const char ** provide a templatised adaptor.
\r
15 template<typename T>
\r
16 size_t iconv_adaptor(size_t(*f_iconv)(ConverterHandle, T, size_t *, char **, size_t *),
\r
17 ConverterHandle cd, char** src, size_t *srcleft,
\r
18 char **dst, size_t *dstleft) {
\r
19 return f_iconv(cd, (T)src, srcleft, dst, dstleft);
\r
22 * Encapsulate iconv safely and avoid iconv_adaptor complexity in client code.
\r
25 ConverterHandle iconvh;
\r
26 void OpenHandle(const char *fullDestination, const char *charSetSource) {
\r
27 #if GTK_MAJOR_VERSION >= 2
\r
28 iconvh = g_iconv_open(fullDestination, charSetSource);
\r
30 iconvh = iconv_open(fullDestination, charSetSource);
\r
33 bool Succeeded() const {
\r
34 return iconvh != iconvhBad;
\r
40 Converter(const char *charSetDestination, const char *charSetSource, bool transliterations) {
\r
42 Open(charSetDestination, charSetSource, transliterations);
\r
47 operator bool() const {
\r
50 void Open(const char *charSetDestination, const char *charSetSource, bool transliterations=true) {
\r
52 if (*charSetSource) {
\r
53 // Try allowing approximate transliterations
\r
54 if (transliterations) {
\r
56 strcpy(fullDest, charSetDestination);
\r
57 strcat(fullDest, "//TRANSLIT");
\r
58 OpenHandle(fullDest, charSetSource);
\r
61 // Transliterations failed so try basic name
\r
62 OpenHandle(charSetDestination, charSetSource);
\r
68 #if GTK_MAJOR_VERSION >= 2
\r
69 g_iconv_close(iconvh);
\r
71 iconv_close(iconvh);
\r
76 size_t Convert(char** src, size_t *srcleft, char **dst, size_t *dstleft) const {
\r
78 return (size_t)(-1);
\r
80 #if GTK_MAJOR_VERSION >= 2
\r
81 return iconv_adaptor(g_iconv, iconvh, src, srcleft, dst, dstleft);
\r
83 return iconv_adaptor(iconv, iconvh, src, srcleft, dst, dstleft);
\r