aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/libs/locale/src/encoding/uconv_codepage.ipp
blob: 51e7301bb10d1950fd8f0a017b65e4d6449f32ae (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
//
//  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_UCONV_CODEPAGE_HPP
#define BOOST_LOCALE_IMPL_UCONV_CODEPAGE_HPP
#include <boost/locale/encoding.hpp>
#include "conv.hpp"
#include "../icu/icu_util.hpp"
#include "../icu/uconv.hpp"
#include <unicode/ucnv.h>
#include <unicode/ucnv_err.h>
#include <vector>
#include <memory>

#include <boost/locale/hold_ptr.hpp>

namespace boost {
namespace locale {
namespace conv {
namespace impl {
    template<typename CharType>
    class uconv_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)
        {
            close();
            try {
                cvt_from_.reset(new from_type(charset,how == skip ? impl_icu::cvt_skip : impl_icu::cvt_stop));
                cvt_to_.reset(new to_type("UTF-8",how == skip ? impl_icu::cvt_skip : impl_icu::cvt_stop));
            }
            catch(std::exception const &/*e*/) {
                close();
                return false;
            }
            return true;
        }
        void close()
        {
            cvt_from_.reset();
            cvt_to_.reset();
        }

        virtual string_type convert(char const *begin,char const *end) 
        {
            try {
                return cvt_to_->std(cvt_from_->icu_checked(begin,end));
            }
            catch(std::exception const &/*e*/) {
                throw conversion_error();
            }
        }

    private:

        typedef impl_icu::icu_std_converter<char> from_type;
        typedef impl_icu::icu_std_converter<CharType> to_type;

        hold_ptr<from_type> cvt_from_;
        hold_ptr<to_type> cvt_to_;

    };
  
  
    template<typename CharType>
    class uconv_from_utf : public converter_from_utf<CharType> {
    public:
        typedef CharType char_type;
        virtual bool open(char const *charset,method_type how)
        {
            close();
            try {
                cvt_from_.reset(new from_type("UTF-8",how == skip ? impl_icu::cvt_skip : impl_icu::cvt_stop));
                cvt_to_.reset(new to_type(charset,how == skip ? impl_icu::cvt_skip : impl_icu::cvt_stop));
            }
            catch(std::exception const &/*e*/) {
                close();
                return false;
            }
            return true;
        }
        void close()
        {
            cvt_from_.reset();
            cvt_to_.reset();
        }

        virtual std::string convert(CharType const *begin,CharType const *end) 
        {
            try {
                return cvt_to_->std(cvt_from_->icu_checked(begin,end));
            }
            catch(std::exception const &/*e*/) {
                throw conversion_error();
            }
        }

    private:

        typedef impl_icu::icu_std_converter<CharType> from_type;
        typedef impl_icu::icu_std_converter<char> to_type;

        hold_ptr<from_type> cvt_from_;
        hold_ptr<to_type> cvt_to_;

    };

    class uconv_between : public converter_between {
    public:
        virtual bool open(char const *to_charset,char const *from_charset,method_type how)
        {
            close();
            try {
                cvt_from_.reset(new from_type(from_charset,how == skip ? impl_icu::cvt_skip : impl_icu::cvt_stop));
                cvt_to_.reset(new to_type(to_charset,how == skip ? impl_icu::cvt_skip : impl_icu::cvt_stop));
            }
            catch(std::exception const &/*e*/) {
                close();
                return false;
            }
            return true;
        }
        void close()
        {
            cvt_from_.reset();
            cvt_to_.reset();
        }

        virtual std::string convert(char const *begin,char const *end) 
        {
            try {
                return cvt_to_->std(cvt_from_->icu(begin,end));
            }
            catch(std::exception const &/*e*/) {
                throw conversion_error();
            }
        }

    private:

        typedef impl_icu::icu_std_converter<char> from_type;
        typedef impl_icu::icu_std_converter<char> to_type;

        hold_ptr<from_type> cvt_from_;
        hold_ptr<to_type> cvt_to_;

    };


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

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

#endif