r5123 | ntrel | 2010-08-10 17:12:24 +0100 (Tue, 10 Aug 2010) | 4 lines
[geany-mirror.git] / scintilla / Converter.h
blob8e7e3e9efddf209004c07e44aa77eaf2e29b16aa
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.
10 template<typename T>
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);
16 /**
17 * Encapsulate iconv safely and avoid iconv_adaptor complexity in client code.
19 class Converter {
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;
27 public:
28 Converter() {
29 iconvh = iconvhBad;
31 Converter(const char *charSetDestination, const char *charSetSource, bool transliterations) {
32 iconvh = iconvhBad;
33 Open(charSetDestination, charSetSource, transliterations);
35 ~Converter() {
36 Close();
38 operator bool() const {
39 return Succeeded();
41 void Open(const char *charSetDestination, const char *charSetSource, bool transliterations=true) {
42 Close();
43 if (*charSetSource) {
44 // Try allowing approximate transliterations
45 if (transliterations) {
46 char fullDest[200];
47 strcpy(fullDest, charSetDestination);
48 strcat(fullDest, "//TRANSLIT");
49 OpenHandle(fullDest, charSetSource);
51 if (!Succeeded()) {
52 // Transliterations failed so try basic name
53 OpenHandle(charSetDestination, charSetSource);
57 void Close() {
58 if (Succeeded()) {
59 g_iconv_close(iconvh);
60 iconvh = iconvhBad;
63 size_t Convert(char** src, size_t *srcleft, char **dst, size_t *dstleft) const {
64 if (!Succeeded()) {
65 return (size_t)(-1);
66 } else {
67 return iconv_adaptor(g_iconv, iconvh, src, srcleft, dst, dstleft);