plugins: Replace geany_plugin_register() pdata with a separate API function
[geany-mirror.git] / scintilla / gtk / Converter.h
blobbe530341f3b362a369fce3438ad6f0f7f6b875a9
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 #ifndef CONVERTER_H
7 #define CONVERTER_H
9 #ifdef SCI_NAMESPACE
10 namespace Scintilla {
11 #endif
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.
17 template<typename T>
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);
23 /**
24 * Encapsulate iconv safely and avoid iconv_adaptor complexity in client code.
26 class Converter {
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;
34 public:
35 Converter() {
36 iconvh = iconvhBad;
38 Converter(const char *charSetDestination, const char *charSetSource, bool transliterations) {
39 iconvh = iconvhBad;
40 Open(charSetDestination, charSetSource, transliterations);
42 ~Converter() {
43 Close();
45 operator bool() const {
46 return Succeeded();
48 void Open(const char *charSetDestination, const char *charSetSource, bool transliterations=true) {
49 Close();
50 if (*charSetSource) {
51 // Try allowing approximate transliterations
52 if (transliterations) {
53 char fullDest[200];
54 g_strlcpy(fullDest, charSetDestination, sizeof(fullDest));
55 g_strlcat(fullDest, "//TRANSLIT", sizeof(fullDest));
56 OpenHandle(fullDest, charSetSource);
58 if (!Succeeded()) {
59 // Transliterations failed so try basic name
60 OpenHandle(charSetDestination, charSetSource);
64 void Close() {
65 if (Succeeded()) {
66 g_iconv_close(iconvh);
67 iconvh = iconvhBad;
70 size_t Convert(char** src, size_t *srcleft, char **dst, size_t *dstleft) const {
71 if (!Succeeded()) {
72 return (size_t)(-1);
73 } else {
74 return iconv_adaptor(g_iconv, iconvh, src, srcleft, dst, dstleft);
79 #ifdef SCI_NAMESPACE
81 #endif
83 #endif