2 // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
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)
8 #ifndef BOOST_LOCALE_IMPL_UCONV_CODEPAGE_HPP
9 #define BOOST_LOCALE_IMPL_UCONV_CODEPAGE_HPP
10 #include <boost/locale/encoding.hpp>
12 #include "../icu/icu_util.hpp"
13 #include "../icu/uconv.hpp"
14 #include <unicode/ucnv.h>
15 #include <unicode/ucnv_err.h>
19 #include <boost/locale/hold_ptr.hpp>
25 template<typename CharType>
26 class uconv_to_utf : public converter_to_utf<CharType> {
28 typedef CharType char_type;
30 typedef std::basic_string<char_type> string_type;
32 virtual bool open(char const *charset,method_type how)
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));
39 catch(std::exception const &/*e*/) {
51 virtual string_type convert(char const *begin,char const *end)
54 return cvt_to_->std(cvt_from_->icu_checked(begin,end));
56 catch(std::exception const &/*e*/) {
57 throw conversion_error();
63 typedef impl_icu::icu_std_converter<char> from_type;
64 typedef impl_icu::icu_std_converter<CharType> to_type;
66 hold_ptr<from_type> cvt_from_;
67 hold_ptr<to_type> cvt_to_;
72 template<typename CharType>
73 class uconv_from_utf : public converter_from_utf<CharType> {
75 typedef CharType char_type;
76 virtual bool open(char const *charset,method_type how)
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));
83 catch(std::exception const &/*e*/) {
95 virtual std::string convert(CharType const *begin,CharType const *end)
98 return cvt_to_->std(cvt_from_->icu_checked(begin,end));
100 catch(std::exception const &/*e*/) {
101 throw conversion_error();
107 typedef impl_icu::icu_std_converter<CharType> from_type;
108 typedef impl_icu::icu_std_converter<char> to_type;
110 hold_ptr<from_type> cvt_from_;
111 hold_ptr<to_type> cvt_to_;
115 class uconv_between : public converter_between {
117 virtual bool open(char const *to_charset,char const *from_charset,method_type how)
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));
124 catch(std::exception const &/*e*/) {
136 virtual std::string convert(char const *begin,char const *end)
139 return cvt_to_->std(cvt_from_->icu(begin,end));
141 catch(std::exception const &/*e*/) {
142 throw conversion_error();
148 typedef impl_icu::icu_std_converter<char> from_type;
149 typedef impl_icu::icu_std_converter<char> to_type;
151 hold_ptr<from_type> cvt_from_;
152 hold_ptr<to_type> cvt_to_;
162 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4