aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/locale/src/encoding/codepage.cpp
blob: 814a8035df2f319c449624da6d6e465fac4b934f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//
// Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
// Copyright (c) 2022-2025 Alexander Grund
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/locale/encoding.hpp>
#include "../util/make_std_unique.hpp"

#if BOOST_LOCALE_USE_WIN32_API
#    define BOOST_LOCALE_WITH_WCONV
#endif
#ifdef BOOST_LOCALE_WITH_ICONV
#    include "iconv_converter.hpp"
#endif
#ifdef BOOST_LOCALE_WITH_ICU
#    include "uconv_converter.hpp"
#endif
#ifdef BOOST_LOCALE_WITH_WCONV
#    include "wconv_converter.hpp"
#endif

namespace boost { namespace locale { namespace conv {

    std::string between(const char* begin,
                        const char* end,
                        const std::string& to_charset,
                        const std::string& from_charset,
                        method_type how)
    {
#ifdef BOOST_LOCALE_WITH_ICONV
        {
            impl::iconv_between cvt;
            if(cvt.open(to_charset, from_charset, how))
                return cvt.convert(begin, end);
        }
#endif
#ifdef BOOST_LOCALE_WITH_ICU
        {
            impl::uconv_between cvt;
            if(cvt.open(to_charset, from_charset, how))
                return cvt.convert(begin, end);
        }
#endif
#ifdef BOOST_LOCALE_WITH_WCONV
        {
            impl::wconv_between cvt;
            if(cvt.open(to_charset, from_charset, how))
                return cvt.convert(begin, end);
        }
#endif
        throw invalid_charset_error(std::string(to_charset) + " or " + from_charset);
    }

    template<typename CharType>
    std::basic_string<CharType> to_utf(const char* begin, const char* end, const std::string& charset, method_type how)
    {
#ifdef BOOST_LOCALE_WITH_ICONV
        {
            impl::iconv_to_utf<CharType> cvt;
            if(cvt.open(charset, how))
                return cvt.convert(begin, end);
        }
#endif
#ifdef BOOST_LOCALE_WITH_ICU
        {
            impl::uconv_to_utf<CharType> cvt;
            if(cvt.open(charset, how))
                return cvt.convert(begin, end);
        }
#endif
#ifdef BOOST_LOCALE_WITH_WCONV
        {
            impl::wconv_to_utf<CharType> cvt;
            if(cvt.open(charset, how))
                return cvt.convert(begin, end);
        }
#endif
        throw invalid_charset_error(charset);
    }

    template<typename CharType>
    std::string from_utf(const CharType* begin, const CharType* end, const std::string& charset, method_type how)
    {
#ifdef BOOST_LOCALE_WITH_ICONV
        {
            impl::iconv_from_utf<CharType> cvt;
            if(cvt.open(charset, how))
                return cvt.convert(begin, end);
        }
#endif
#ifdef BOOST_LOCALE_WITH_ICU
        {
            impl::uconv_from_utf<CharType> cvt;
            if(cvt.open(charset, how))
                return cvt.convert(begin, end);
        }
#endif
#ifdef BOOST_LOCALE_WITH_WCONV
        {
            impl::wconv_from_utf<CharType> cvt;
            if(cvt.open(charset, how))
                return cvt.convert(begin, end);
        }
#endif
        throw invalid_charset_error(charset);
    }

    namespace detail {
        template<class T>
        static std::unique_ptr<T> move_to_ptr(T& c)
        {
            return make_std_unique<T>(std::move(c));
        }

        template<typename Char>
        std::unique_ptr<utf_encoder<Char>>
        make_utf_encoder(const std::string& charset, method_type how, conv_backend impl)
        {
#ifdef BOOST_LOCALE_WITH_ICONV
            if(impl == conv_backend::Default || impl == conv_backend::IConv) {
                impl::iconv_to_utf<Char> cvt;
                if(cvt.open(charset, how))
                    return move_to_ptr(cvt);
            }
#endif
#ifdef BOOST_LOCALE_WITH_ICU
            if(impl == conv_backend::Default || impl == conv_backend::ICU) {
                impl::uconv_to_utf<Char> cvt;
                if(cvt.open(charset, how))
                    return move_to_ptr(cvt);
            }
#endif
#ifdef BOOST_LOCALE_WITH_WCONV
            if(impl == conv_backend::Default || impl == conv_backend::WinAPI) {
                impl::wconv_to_utf<Char> cvt;
                if(cvt.open(charset, how))
                    return move_to_ptr(cvt);
            }
#endif
            throw invalid_charset_error(charset);
        }

        template<typename Char>
        std::unique_ptr<utf_decoder<Char>>
        make_utf_decoder(const std::string& charset, method_type how, conv_backend impl)
        {
#ifdef BOOST_LOCALE_WITH_ICONV
            if(impl == conv_backend::Default || impl == conv_backend::IConv) {
                impl::iconv_from_utf<Char> cvt;
                if(cvt.open(charset, how))
                    return move_to_ptr(cvt);
            }
#endif
#ifdef BOOST_LOCALE_WITH_ICU
            if(impl == conv_backend::Default || impl == conv_backend::ICU) {
                impl::uconv_from_utf<Char> cvt;
                if(cvt.open(charset, how))
                    return move_to_ptr(cvt);
            }
#endif
#ifdef BOOST_LOCALE_WITH_WCONV
            if(impl == conv_backend::Default || impl == conv_backend::WinAPI) {
                impl::wconv_from_utf<Char> cvt;
                if(cvt.open(charset, how))
                    return move_to_ptr(cvt);
            }
#endif
            throw invalid_charset_error(charset);
        }
        std::unique_ptr<narrow_converter> make_narrow_converter(const std::string& src_encoding,
                                                                const std::string& target_encoding,
                                                                method_type how,
                                                                conv_backend impl)
        {
#ifdef BOOST_LOCALE_WITH_ICONV
            if(impl == conv_backend::Default || impl == conv_backend::IConv) {
                impl::iconv_between cvt;
                if(cvt.open(target_encoding, src_encoding, how))
                    return move_to_ptr(cvt);
            }
#endif
#ifdef BOOST_LOCALE_WITH_ICU
            if(impl == conv_backend::Default || impl == conv_backend::ICU) {
                impl::uconv_between cvt;
                if(cvt.open(target_encoding, src_encoding, how))
                    return move_to_ptr(cvt);
            }
#endif
#ifdef BOOST_LOCALE_WITH_WCONV
            if(impl == conv_backend::Default || impl == conv_backend::WinAPI) {
                impl::wconv_between cvt;
                if(cvt.open(target_encoding, src_encoding, how))
                    return move_to_ptr(cvt);
            }
#endif
            throw invalid_charset_error(std::string(src_encoding) + " or " + target_encoding);
        }
    } // namespace detail

#define BOOST_LOCALE_INSTANTIATE(CHARTYPE)                                                              \
    namespace detail {                                                                                  \
        template class charset_converter<char, CHARTYPE>;                                               \
        template BOOST_LOCALE_DECL std::unique_ptr<utf_encoder<CHARTYPE>>                               \
        make_utf_encoder(const std::string& charset, method_type how, conv_backend impl);               \
        template BOOST_LOCALE_DECL std::unique_ptr<utf_decoder<CHARTYPE>>                               \
        make_utf_decoder(const std::string& charset, method_type how, conv_backend impl);               \
    }                                                                                                   \
    template BOOST_LOCALE_DECL std::basic_string<CHARTYPE> to_utf<CHARTYPE>(const char* begin,          \
                                                                            const char* end,            \
                                                                            const std::string& charset, \
                                                                            method_type how);           \
    template BOOST_LOCALE_DECL std::string from_utf<CHARTYPE>(const CHARTYPE* begin,                    \
                                                              const CHARTYPE* end,                      \
                                                              const std::string& charset,               \
                                                              method_type how)
#define BOOST_LOCALE_INSTANTIATE_NO_CHAR(CHARTYPE)        \
    BOOST_LOCALE_INSTANTIATE(CHARTYPE);                   \
    namespace detail {                                    \
        template class charset_converter<CHARTYPE, char>; \
    }

    BOOST_LOCALE_INSTANTIATE(char);
    BOOST_LOCALE_INSTANTIATE_NO_CHAR(wchar_t);

#ifdef __cpp_lib_char8_t
    BOOST_LOCALE_INSTANTIATE_NO_CHAR(char8_t);
#endif

#ifdef BOOST_LOCALE_ENABLE_CHAR16_T
    BOOST_LOCALE_INSTANTIATE_NO_CHAR(char16_t);
#endif

#ifdef BOOST_LOCALE_ENABLE_CHAR32_T
    BOOST_LOCALE_INSTANTIATE_NO_CHAR(char32_t);
#endif

}}} // namespace boost::locale::conv