Support more folding icon styles: arrows, +/- and no lines
[geany-mirror.git] / scintilla / Converter.h
blobd3038a2f2ad72a03468b7f6773a9b32f7f06a46e
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 #include <iconv.h>
7 #if GTK_MAJOR_VERSION >= 2
8 typedef GIConv ConverterHandle;
9 #else
10 typedef iconv_t ConverterHandle;
11 #endif
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.
15 template<typename T>
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);
21 /**
22 * Encapsulate iconv safely and avoid iconv_adaptor complexity in client code.
24 class Converter {
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);
29 #else
30 iconvh = iconv_open(fullDestination, charSetSource);
31 #endif
33 bool Succeeded() const {
34 return iconvh != iconvhBad;
36 public:
37 Converter() {
38 iconvh = iconvhBad;
40 Converter(const char *charSetDestination, const char *charSetSource, bool transliterations) {
41 iconvh = iconvhBad;
42 Open(charSetDestination, charSetSource, transliterations);
44 ~Converter() {
45 Close();
47 operator bool() const {
48 return Succeeded();
50 void Open(const char *charSetDestination, const char *charSetSource, bool transliterations=true) {
51 Close();
52 if (*charSetSource) {
53 // Try allowing approximate transliterations
54 if (transliterations) {
55 char fullDest[200];
56 strcpy(fullDest, charSetDestination);
57 strcat(fullDest, "//TRANSLIT");
58 OpenHandle(fullDest, charSetSource);
60 if (!Succeeded()) {
61 // Transliterations failed so try basic name
62 OpenHandle(charSetDestination, charSetSource);
66 void Close() {
67 if (Succeeded()) {
68 #if GTK_MAJOR_VERSION >= 2
69 g_iconv_close(iconvh);
70 #else
71 iconv_close(iconvh);
72 #endif
73 iconvh = iconvhBad;
76 size_t Convert(char** src, size_t *srcleft, char **dst, size_t *dstleft) const {
77 if (!Succeeded()) {
78 return (size_t)(-1);
79 } else {
80 #if GTK_MAJOR_VERSION >= 2
81 return iconv_adaptor(g_iconv, iconvh, src, srcleft, dst, dstleft);
82 #else
83 return iconv_adaptor(iconv, iconvh, src, srcleft, dst, dstleft);
84 #endif