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.
13 typedef GIConv ConverterHandle
;
14 const ConverterHandle iconvhBad
= (ConverterHandle
)(-1);
15 // Since various versions of iconv can not agree on whether the src argument
16 // is char ** or const char ** provide a templatised adaptor.
18 size_t iconv_adaptor(size_t(*f_iconv
)(ConverterHandle
, T
, size_t *, char **, size_t *),
19 ConverterHandle cd
, char** src
, size_t *srcleft
,
20 char **dst
, size_t *dstleft
) {
21 return f_iconv(cd
, (T
)src
, srcleft
, dst
, dstleft
);
24 * Encapsulate iconv safely and avoid iconv_adaptor complexity in client code.
27 ConverterHandle iconvh
;
28 void OpenHandle(const char *fullDestination
, const char *charSetSource
) {
29 iconvh
= g_iconv_open(fullDestination
, charSetSource
);
31 bool Succeeded() const {
32 return iconvh
!= iconvhBad
;
38 Converter(const char *charSetDestination
, const char *charSetSource
, bool transliterations
) {
40 Open(charSetDestination
, charSetSource
, transliterations
);
45 operator bool() const {
48 void Open(const char *charSetDestination
, const char *charSetSource
, bool transliterations
=true) {
51 // Try allowing approximate transliterations
52 if (transliterations
) {
54 g_strlcpy(fullDest
, charSetDestination
, sizeof(fullDest
));
55 g_strlcat(fullDest
, "//TRANSLIT", sizeof(fullDest
));
56 OpenHandle(fullDest
, charSetSource
);
59 // Transliterations failed so try basic name
60 OpenHandle(charSetDestination
, charSetSource
);
66 g_iconv_close(iconvh
);
70 size_t Convert(char** src
, size_t *srcleft
, char **dst
, size_t *dstleft
) const {
74 return iconv_adaptor(g_iconv
, iconvh
, src
, srcleft
, dst
, dstleft
);