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.
11 const GIConv iconvhBad
= (GIConv
)(-1);
12 const gsize sizeFailure
= static_cast<gsize
>(-1);
14 * Encapsulate g_iconv safely.
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
;
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;
37 operator bool() const noexcept
{
40 void Open(const char *charSetDestination
, const char *charSetSource
, bool transliterations
) {
43 // Try allowing approximate transliterations
44 if (transliterations
) {
45 std::string
fullDest(charSetDestination
);
46 fullDest
.append("//TRANSLIT");
47 OpenHandle(fullDest
.c_str(), charSetSource
);
50 // Transliterations failed so try basic name
51 OpenHandle(charSetDestination
, charSetSource
);
55 void Close() noexcept
{
57 g_iconv_close(iconvh
);
61 gsize
Convert(char **src
, gsize
*srcleft
, char **dst
, gsize
*dstleft
) const noexcept
{
65 return g_iconv(iconvh
, src
, srcleft
, dst
, dstleft
);