aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/libs/locale/src/icu/codecvt.cpp
blob: 2cecdb7dda6d4cc991a2e5f686e18e03f5a9fe4c (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
//
//  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/locale/encoding.hpp>
#include <boost/locale/encoding_errors.hpp>
#include "../encoding/conv.hpp"
#include "all_generator.hpp"
#include "uconv.hpp"
#include <unicode/ucnv.h>
#include <unicode/ucnv_err.h>
#include <boost/locale/util.hpp>
#include <boost/locale/hold_ptr.hpp>
#include "codecvt.hpp"

#ifdef BOOST_MSVC
#  pragma warning(disable : 4244) // loose data 
#endif

#include "icu_util.hpp"
#include <vector>
namespace boost {
namespace locale {
namespace impl_icu {
    class uconv_converter : public util::base_converter {
    public:
       
        uconv_converter(std::string const &encoding) :
            encoding_(encoding)
        {
            UErrorCode err=U_ZERO_ERROR;
            
            // No need to check err each time, this
            // is how ICU works.
            cvt_ = ucnv_open(encoding.c_str(),&err);
            ucnv_setFromUCallBack(cvt_,UCNV_FROM_U_CALLBACK_STOP,0,0,0,&err);
            ucnv_setToUCallBack(cvt_,UCNV_TO_U_CALLBACK_STOP,0,0,0,&err);

            if(!cvt_ || U_FAILURE(err)) {
                if(cvt_)
                    ucnv_close(cvt_);
                throw conv::invalid_charset_error(encoding);
            }
            
            max_len_ = ucnv_getMaxCharSize(cvt_);
        }
        
        virtual ~uconv_converter()
        {
            ucnv_close(cvt_);
        }

        virtual bool is_thread_safe() const
        {
            return false;
        }

        virtual uconv_converter *clone() const
        {
            return new uconv_converter(encoding_);
        }

        uint32_t to_unicode(char const *&begin,char const *end)
        {
            UErrorCode err=U_ZERO_ERROR;
            char const *tmp = begin;
            UChar32 c=ucnv_getNextUChar(cvt_,&tmp,end,&err);
            ucnv_reset(cvt_);
            if(err == U_TRUNCATED_CHAR_FOUND) {
                return incomplete;
            }
            if(U_FAILURE(err)) {
                return illegal;
            }

            begin = tmp;
            return c;
        }

        uint32_t from_unicode(uint32_t u,char *begin,char const *end)
        {
            UChar code_point[2]={0};
            int len;
            if(u<=0xFFFF) {
                if(0xD800 <=u && u<= 0xDFFF) // No surragates
                    return illegal;
                code_point[0]=u;
                len=1;
            }
            else {
                u-=0x10000;
                code_point[0]=0xD800 | (u>>10);
                code_point[1]=0xDC00 | (u & 0x3FF);
                len=2;
            }
            UErrorCode err=U_ZERO_ERROR;
            int olen = ucnv_fromUChars(cvt_,begin,end-begin,code_point,len,&err);
            ucnv_reset(cvt_);
            if(err == U_BUFFER_OVERFLOW_ERROR)
                return incomplete;
            if(U_FAILURE(err))
                return illegal;
            return olen;
        }

        virtual int max_len() const
        {
            return max_len_;
        }

    private:
        std::string encoding_;
        UConverter *cvt_;
        int max_len_;
    };
    
    util::base_converter *create_uconv_converter(std::string const &encoding)
    {
        hold_ptr<util::base_converter> cvt;
        try {
            cvt.reset(new uconv_converter(encoding));
        }
        catch(std::exception const &/*e*/)
        {
            // no encoding so we return empty pointer
        }
        return cvt.release();
    }

    std::locale create_codecvt(std::locale const &in,std::string const &encoding,character_facet_type type)
    {
        if(conv::impl::normalize_encoding(encoding.c_str())=="utf8")
            return util::create_utf8_codecvt(in,type);

        try {
            return util::create_simple_codecvt(in,encoding,type);
        }
        catch(boost::locale::conv::invalid_charset_error const &) {
            hold_ptr<util::base_converter> cvt;
            try {
                cvt.reset(create_uconv_converter(encoding));
            }
            catch(std::exception const &/*e*/)
            {
                cvt.reset(new util::base_converter());
            }
            return util::create_codecvt_from_pointer(in,cvt.release(),type);
        }
    }

} // impl_icu
} // locale 
} // boost

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