Merge pull request #3720 from b4n/encodings-ui-improvements
[geany-mirror.git] / scintilla / gtk / Converter.h
blob0b95701f8a7be58fc448e811e914666f885c1443
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 namespace Scintilla {
11 const GIConv iconvhBad = (GIConv)(-1);
12 const gsize sizeFailure = static_cast<gsize>(-1);
13 /**
14 * Encapsulate g_iconv safely.
16 class Converter {
17 GIConv iconvh = iconvhBad;
18 void OpenHandle(const char *fullDestination, const char *charSetSource) noexcept {
19 iconvh = g_iconv_open(fullDestination, charSetSource);
21 bool Succeeded() const noexcept {
22 return iconvh != iconvhBad;
24 public:
25 Converter() noexcept = default;
26 Converter(const char *charSetDestination, const char *charSetSource, bool transliterations) {
27 Open(charSetDestination, charSetSource, transliterations);
29 // Deleted so Converter objects can not be copied.
30 Converter(const Converter &) = delete;
31 Converter(Converter &&) = delete;
32 Converter &operator=(const Converter &) = delete;
33 Converter &operator=(Converter &&) = delete;
34 ~Converter() {
35 Close();
37 operator bool() const noexcept {
38 return Succeeded();
40 void Open(const char *charSetDestination, const char *charSetSource, bool transliterations) {
41 Close();
42 if (*charSetSource) {
43 // Try allowing approximate transliterations
44 if (transliterations) {
45 std::string fullDest(charSetDestination);
46 fullDest.append("//TRANSLIT");
47 OpenHandle(fullDest.c_str(), charSetSource);
49 if (!Succeeded()) {
50 // Transliterations failed so try basic name
51 OpenHandle(charSetDestination, charSetSource);
55 void Close() noexcept {
56 if (Succeeded()) {
57 g_iconv_close(iconvh);
58 iconvh = iconvhBad;
61 gsize Convert(char **src, gsize *srcleft, char **dst, gsize *dstleft) const noexcept {
62 if (!Succeeded()) {
63 return sizeFailure;
64 } else {
65 return g_iconv(iconvh, src, srcleft, dst, dstleft);
72 #endif