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