aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/libs/locale/src/encoding/iconv_codepage.ipp
blob: 276c60b6b0b3e2619f878dc983f6c26edd3fedb9 (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
//
//  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)
//
#ifndef BOOST_LOCALE_IMPL_ICONV_CODEPAGE_HPP
#define BOOST_LOCALE_IMPL_ICONV_CODEPAGE_HPP

#include <boost/locale/encoding.hpp>
#include "../util/iconv.hpp"
#include <errno.h>
#include "conv.hpp"
#include <assert.h>
#include <vector>

namespace boost {
namespace locale {
namespace conv {
namespace impl {

class iconverter_base {
public:
    
    iconverter_base() : 
    cvt_((iconv_t)(-1))
    {
    }

    ~iconverter_base()
    {
        close();
    }

    size_t conv(char const **inbufc,size_t *inchar_left,
                char **outbuf,size_t *outchar_left)
    {
        char **inbuf = const_cast<char **>(inbufc);
        return call_iconv(cvt_,inbuf,inchar_left,outbuf,outchar_left);
    }

    bool do_open(char const *to,char const *from,method_type how)
    {
        close();
        cvt_ = iconv_open(to,from);
        how_ = how;
        return cvt_ != (iconv_t)(-1);
    }
    
    template<typename OutChar,typename InChar>
    std::basic_string<OutChar> real_convert(InChar const *ubegin,InChar const *uend)
    {
        std::basic_string<OutChar> sresult;
        
        sresult.reserve(uend - ubegin);

        OutChar result[64];

        char *out_start   = reinterpret_cast<char *>(&result[0]);
        char const *begin = reinterpret_cast<char const *>(ubegin);
        char const *end   = reinterpret_cast<char const *>(uend);
        
        enum { normal , unshifting , done } state = normal;

        while(state!=done) {

            size_t in_left = end - begin;
            size_t out_left = sizeof(result);
            
            char *out_ptr = out_start;
            size_t res = 0;
            if(in_left == 0)
                state = unshifting;

            if(state == normal) 
                res = conv(&begin,&in_left,&out_ptr,&out_left);
            else
                res = conv(0,0,&out_ptr,&out_left);

            int err = errno;

            size_t output_count = (out_ptr - out_start) / sizeof(OutChar);
            
            if(res!=0 && res!=(size_t)(-1)) {
                    if(how_ == stop) {
                        throw conversion_error();
                    }
            }

            sresult.append(&result[0],output_count);

            if(res == (size_t)(-1)) {
                if(err == EILSEQ || err == EINVAL) {
                    if(how_ == stop) {
                        throw conversion_error();
                    }

                    if(begin != end) {
                        begin+=sizeof(InChar);
                        if(begin >= end)
                            break;
                    }
                    else {
                        break;
                    }
                }
                else if (err==E2BIG) {
                    continue;
                }
                else {
                    // We should never get there
                    // but if we do
                    if(how_ == stop)
                        throw conversion_error();
                    else
                        break;
                }
            }
            if(state == unshifting)
                state = done;
        }
        return sresult;
    }


private:

    void close()
    {
        if(cvt_!=(iconv_t)(-1)) {
            iconv_close(cvt_);
            cvt_ = (iconv_t)(-1);
        }
    }
    
    iconv_t cvt_;

    method_type how_;

};

template<typename CharType>
class iconv_from_utf : public converter_from_utf<CharType>
{
public:

    typedef CharType char_type;

    virtual bool open(char const *charset,method_type how)
    {
        return self_.do_open(charset,utf_name<CharType>(),how);
    }

    virtual std::string convert(char_type const *ubegin,char_type const *uend)
    {
        return self_.template real_convert<char,char_type>(ubegin,uend);
    }
    virtual ~iconv_from_utf() {}
private:
    iconverter_base self_;
};

class iconv_between:  public converter_between
{
public:
    virtual bool open(char const *to_charset,char const *from_charset,method_type how)
    {
        return self_.do_open(to_charset,from_charset,how);
    }
    virtual std::string convert(char const *begin,char const *end)
    {
        return self_.real_convert<char,char>(begin,end);
    }
    virtual ~iconv_between() {}
private:
    iconverter_base self_;

};

template<typename CharType>
class iconv_to_utf :  public converter_to_utf<CharType>
{
public:

    typedef CharType char_type;
    typedef std::basic_string<char_type> string_type;

    virtual bool open(char const *charset,method_type how)
    {
        return self_.do_open(utf_name<CharType>(),charset,how);
    }

    virtual string_type convert(char const *begin,char const *end)
    {
        return self_.template real_convert<char_type,char>(begin,end);
    }
    virtual ~iconv_to_utf() {}
private:
    iconverter_base self_;
};



} // impl
} // conv
} // locale 
} // boost




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