aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/libs/locale/src/encoding/codepage.cpp
blob: c19e36b8cffbf80b7150dffe1e15c5f7cf9a4a0d (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
//
//  Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
//
//  Distributed under the Boost Software License, Version 1.0. (See
//  accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
#define BOOST_LOCALE_SOURCE
#include <boost/config.hpp>

#if defined(BOOST_WINDOWS) || defined(__CYGWIN__)
#define BOOST_LOCALE_WITH_WCONV
#endif

#ifdef BOOST_LOCALE_WITH_ICONV
#include "iconv_codepage.ipp"
#endif
#ifdef BOOST_LOCALE_WITH_ICU
#include "uconv_codepage.ipp"
#endif
#ifdef BOOST_LOCALE_WITH_WCONV
#include "wconv_codepage.ipp"
#endif

#include <boost/locale/encoding.hpp>
#include <boost/locale/hold_ptr.hpp>

#include <string>
#include <cstring>
#include <memory>

namespace boost {
    namespace locale {
        namespace conv {
            namespace impl {
                
                std::string convert_between(char const *begin,
                                            char const *end,
                                            char const *to_charset,
                                            char const *from_charset,
                                            method_type how)
                {
                    hold_ptr<converter_between> cvt;
                    #ifdef BOOST_LOCALE_WITH_ICONV
                    cvt.reset(new iconv_between());
                    if(cvt->open(to_charset,from_charset,how))
                        return cvt->convert(begin,end);
                    #endif
                    #ifdef BOOST_LOCALE_WITH_ICU
                    cvt.reset(new uconv_between());
                    if(cvt->open(to_charset,from_charset,how))
                        return cvt->convert(begin,end);
                    #endif
                    #ifdef BOOST_LOCALE_WITH_WCONV
                    cvt.reset(new wconv_between());
                    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> convert_to(
                                        char const *begin,
                                        char const *end,
                                        char const *charset,
                                        method_type how)
                {
                    hold_ptr<converter_to_utf<CharType> > cvt;
                    #ifdef BOOST_LOCALE_WITH_ICONV
                    cvt.reset(new iconv_to_utf<CharType>());
                    if(cvt->open(charset,how))
                        return cvt->convert(begin,end);
                    #endif
                    #ifdef BOOST_LOCALE_WITH_ICU
                    cvt.reset(new uconv_to_utf<CharType>());
                    if(cvt->open(charset,how))
                        return cvt->convert(begin,end);
                    #endif
                    #ifdef BOOST_LOCALE_WITH_WCONV
                    cvt.reset(new wconv_to_utf<CharType>());
                    if(cvt->open(charset,how))
                        return cvt->convert(begin,end);
                    #endif
                    throw invalid_charset_error(charset);
                }

                template<typename CharType>
                std::string convert_from(
                                        CharType const *begin,
                                        CharType const *end,
                                        char const *charset,
                                        method_type how)
                {
                    hold_ptr<converter_from_utf<CharType> > cvt;
                    #ifdef BOOST_LOCALE_WITH_ICONV
                    cvt.reset(new iconv_from_utf<CharType>());
                    if(cvt->open(charset,how))
                        return cvt->convert(begin,end);
                    #endif
                    #ifdef BOOST_LOCALE_WITH_ICU
                    cvt.reset(new uconv_from_utf<CharType>());
                    if(cvt->open(charset,how))
                        return cvt->convert(begin,end);
                    #endif
                    #ifdef BOOST_LOCALE_WITH_WCONV
                    cvt.reset(new wconv_from_utf<CharType>());
                    if(cvt->open(charset,how))
                        return cvt->convert(begin,end);
                    #endif
                    throw invalid_charset_error(charset);
                }

                std::string normalize_encoding(char const *ccharset)
                {
                    std::string charset;
                    charset.reserve(std::strlen(ccharset));
                    while(*ccharset!=0) {
                        char c=*ccharset++;
                        if('0' <= c && c<= '9')
                            charset+=c;
                        else if('a' <=c && c <='z')
                            charset+=c;
                        else if('A' <=c && c <='Z')
                            charset+=char(c-'A'+'a');
                    }
                    return charset;
                }
                

            } // impl 

            using namespace impl;
            
            std::string between(char const *begin,char const *end,
                                std::string const &to_charset,std::string const &from_charset,method_type how)
            {
                return convert_between(begin,end,to_charset.c_str(),from_charset.c_str(),how);
            }

            template<>
            std::basic_string<char> to_utf(char const *begin,char const *end,std::string const &charset,method_type how)
            {
                return convert_to<char>(begin,end,charset.c_str(),how);
            }

            template<>
            std::string from_utf(char const *begin,char const *end,std::string const &charset,method_type how)
            {
                return convert_from<char>(begin,end,charset.c_str(),how);
            }

            template<>
            std::basic_string<wchar_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how)
            {
                return convert_to<wchar_t>(begin,end,charset.c_str(),how);
            }

            template<>
            std::string from_utf(wchar_t const *begin,wchar_t const *end,std::string const &charset,method_type how)
            {
                return convert_from<wchar_t>(begin,end,charset.c_str(),how);
            }

            #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
            template<>
            std::basic_string<char16_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how)
            {
                return convert_to<char16_t>(begin,end,charset.c_str(),how);
            }

            template<>
            std::string from_utf(char16_t const *begin,char16_t const *end,std::string const &charset,method_type how)
            {
                return convert_from<char16_t>(begin,end,charset.c_str(),how);
            }
            #endif

            #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
            template<>
            std::basic_string<char32_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how)
            {
                return convert_to<char32_t>(begin,end,charset.c_str(),how);
            }

            template<>
            std::string from_utf(char32_t const *begin,char32_t const *end,std::string const &charset,method_type how)
            {
                return convert_from<char32_t>(begin,end,charset.c_str(),how);
            }
            #endif


        }
    }
}



// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4