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.
6 typedef GIConv ConverterHandle
;
7 const ConverterHandle iconvhBad
= (ConverterHandle
)(-1);
8 // Since various versions of iconv can not agree on whether the src argument
9 // is char ** or const char ** provide a templatised adaptor.
11 size_t iconv_adaptor(size_t(*f_iconv
)(ConverterHandle
, T
, size_t *, char **, size_t *),
12 ConverterHandle cd
, char** src
, size_t *srcleft
,
13 char **dst
, size_t *dstleft
) {
14 return f_iconv(cd
, (T
)src
, srcleft
, dst
, dstleft
);
17 * Encapsulate iconv safely and avoid iconv_adaptor complexity in client code.
20 ConverterHandle iconvh
;
21 void OpenHandle(const char *fullDestination
, const char *charSetSource
) {
22 iconvh
= g_iconv_open(fullDestination
, charSetSource
);
24 bool Succeeded() const {
25 return iconvh
!= iconvhBad
;
31 Converter(const char *charSetDestination
, const char *charSetSource
, bool transliterations
) {
33 Open(charSetDestination
, charSetSource
, transliterations
);
38 operator bool() const {
41 void Open(const char *charSetDestination
, const char *charSetSource
, bool transliterations
=true) {
44 // Try allowing approximate transliterations
45 if (transliterations
) {
47 strcpy(fullDest
, charSetDestination
);
48 strcat(fullDest
, "//TRANSLIT");
49 OpenHandle(fullDest
, charSetSource
);
52 // Transliterations failed so try basic name
53 OpenHandle(charSetDestination
, charSetSource
);
59 g_iconv_close(iconvh
);
63 size_t Convert(char** src
, size_t *srcleft
, char **dst
, size_t *dstleft
) const {
67 return iconv_adaptor(g_iconv
, iconvh
, src
, srcleft
, dst
, dstleft
);