• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1//
2//  Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3//
4//  Distributed under the Boost Software License, Version 1.0. (See
5//  accompanying file LICENSE_1_0.txt or copy at
6//  http://www.boost.org/LICENSE_1_0.txt)
7//
8#ifndef BOOST_LOCALE_IMPL_UCONV_CODEPAGE_HPP
9#define BOOST_LOCALE_IMPL_UCONV_CODEPAGE_HPP
10#include <boost/locale/encoding.hpp>
11#include "conv.hpp"
12#include "../icu/icu_util.hpp"
13#include "../icu/uconv.hpp"
14#include <unicode/ucnv.h>
15#include <unicode/ucnv_err.h>
16#include <vector>
17#include <memory>
18
19#include <boost/locale/hold_ptr.hpp>
20
21namespace boost {
22namespace locale {
23namespace conv {
24namespace impl {
25    template<typename CharType>
26    class uconv_to_utf : public converter_to_utf<CharType> {
27    public:
28        typedef CharType char_type;
29
30        typedef std::basic_string<char_type> string_type;
31
32        virtual bool open(char const *charset,method_type how)
33        {
34            close();
35            try {
36                cvt_from_.reset(new from_type(charset,how == skip ? impl_icu::cvt_skip : impl_icu::cvt_stop));
37                cvt_to_.reset(new to_type("UTF-8",how == skip ? impl_icu::cvt_skip : impl_icu::cvt_stop));
38            }
39            catch(std::exception const &/*e*/) {
40                close();
41                return false;
42            }
43            return true;
44        }
45        void close()
46        {
47            cvt_from_.reset();
48            cvt_to_.reset();
49        }
50
51        virtual string_type convert(char const *begin,char const *end)
52        {
53            try {
54                return cvt_to_->std(cvt_from_->icu_checked(begin,end));
55            }
56            catch(std::exception const &/*e*/) {
57                throw conversion_error();
58            }
59        }
60
61    private:
62
63        typedef impl_icu::icu_std_converter<char> from_type;
64        typedef impl_icu::icu_std_converter<CharType> to_type;
65
66        hold_ptr<from_type> cvt_from_;
67        hold_ptr<to_type> cvt_to_;
68
69    };
70
71
72    template<typename CharType>
73    class uconv_from_utf : public converter_from_utf<CharType> {
74    public:
75        typedef CharType char_type;
76        virtual bool open(char const *charset,method_type how)
77        {
78            close();
79            try {
80                cvt_from_.reset(new from_type("UTF-8",how == skip ? impl_icu::cvt_skip : impl_icu::cvt_stop));
81                cvt_to_.reset(new to_type(charset,how == skip ? impl_icu::cvt_skip : impl_icu::cvt_stop));
82            }
83            catch(std::exception const &/*e*/) {
84                close();
85                return false;
86            }
87            return true;
88        }
89        void close()
90        {
91            cvt_from_.reset();
92            cvt_to_.reset();
93        }
94
95        virtual std::string convert(CharType const *begin,CharType const *end)
96        {
97            try {
98                return cvt_to_->std(cvt_from_->icu_checked(begin,end));
99            }
100            catch(std::exception const &/*e*/) {
101                throw conversion_error();
102            }
103        }
104
105    private:
106
107        typedef impl_icu::icu_std_converter<CharType> from_type;
108        typedef impl_icu::icu_std_converter<char> to_type;
109
110        hold_ptr<from_type> cvt_from_;
111        hold_ptr<to_type> cvt_to_;
112
113    };
114
115    class uconv_between : public converter_between {
116    public:
117        virtual bool open(char const *to_charset,char const *from_charset,method_type how)
118        {
119            close();
120            try {
121                cvt_from_.reset(new from_type(from_charset,how == skip ? impl_icu::cvt_skip : impl_icu::cvt_stop));
122                cvt_to_.reset(new to_type(to_charset,how == skip ? impl_icu::cvt_skip : impl_icu::cvt_stop));
123            }
124            catch(std::exception const &/*e*/) {
125                close();
126                return false;
127            }
128            return true;
129        }
130        void close()
131        {
132            cvt_from_.reset();
133            cvt_to_.reset();
134        }
135
136        virtual std::string convert(char const *begin,char const *end)
137        {
138            try {
139                return cvt_to_->std(cvt_from_->icu(begin,end));
140            }
141            catch(std::exception const &/*e*/) {
142                throw conversion_error();
143            }
144        }
145
146    private:
147
148        typedef impl_icu::icu_std_converter<char> from_type;
149        typedef impl_icu::icu_std_converter<char> to_type;
150
151        hold_ptr<from_type> cvt_from_;
152        hold_ptr<to_type> cvt_to_;
153
154    };
155
156
157} // impl
158} // conv
159} // locale
160} // boost
161
162// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
163
164#endif
165