| 1 | // |
| 2 | // Copyright (c) 2009-2011 Artyom Beilis (Tonkikh) |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. |
| 5 | // https://www.boost.org/LICENSE_1_0.txt |
| 6 | |
| 7 | #ifndef BOOST_LOCALE_IMPL_UCONV_CODEPAGE_HPP |
| 8 | #define BOOST_LOCALE_IMPL_UCONV_CODEPAGE_HPP |
| 9 | #include <boost/locale/encoding.hpp> |
| 10 | #include <boost/locale/hold_ptr.hpp> |
| 11 | #include "boost/locale/icu/icu_util.hpp" |
| 12 | #include "boost/locale/icu/uconv.hpp" |
| 13 | #include <unicode/ucnv.h> |
| 14 | #include <unicode/ucnv_err.h> |
| 15 | |
| 16 | namespace boost { namespace locale { namespace conv { namespace impl { |
| 17 | template<typename CharType> |
| 18 | class uconv_to_utf final : public detail::utf_encoder<CharType> { |
| 19 | public: |
| 20 | bool open(const std::string& charset, method_type how) |
| 21 | { |
| 22 | try { |
| 23 | using impl_icu::cpcvt_type; |
| 24 | cvt_from_.reset(p: new from_type(charset, how == skip ? cpcvt_type::skip : cpcvt_type::stop)); |
| 25 | cvt_to_.reset(new to_type("UTF-8" , how == skip ? cpcvt_type::skip : cpcvt_type::stop)); |
| 26 | } catch(const std::exception& /*e*/) { |
| 27 | cvt_from_.reset(); |
| 28 | cvt_to_.reset(); |
| 29 | return false; |
| 30 | } |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | std::basic_string<CharType> convert(const char* begin, const char* end) override |
| 35 | { |
| 36 | try { |
| 37 | return cvt_to_->std(cvt_from_->icu_checked(vb: begin, ve: end)); |
| 38 | } catch(const std::exception& /*e*/) { |
| 39 | throw conversion_error(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | typedef impl_icu::icu_std_converter<char> from_type; |
| 45 | typedef impl_icu::icu_std_converter<CharType> to_type; |
| 46 | |
| 47 | hold_ptr<from_type> cvt_from_; |
| 48 | hold_ptr<to_type> cvt_to_; |
| 49 | }; |
| 50 | |
| 51 | template<typename CharType> |
| 52 | class uconv_from_utf final : public detail::utf_decoder<CharType> { |
| 53 | public: |
| 54 | bool open(const std::string& charset, method_type how) |
| 55 | { |
| 56 | try { |
| 57 | using impl_icu::cpcvt_type; |
| 58 | cvt_from_.reset(new from_type("UTF-8" , how == skip ? cpcvt_type::skip : cpcvt_type::stop)); |
| 59 | cvt_to_.reset(p: new to_type(charset, how == skip ? cpcvt_type::skip : cpcvt_type::stop)); |
| 60 | } catch(const std::exception& /*e*/) { |
| 61 | cvt_from_.reset(); |
| 62 | cvt_to_.reset(); |
| 63 | return false; |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | std::string convert(const CharType* begin, const CharType* end) override |
| 69 | { |
| 70 | try { |
| 71 | return cvt_to_->std(str: cvt_from_->icu_checked(begin, end)); |
| 72 | } catch(const std::exception& /*e*/) { |
| 73 | throw conversion_error(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | typedef impl_icu::icu_std_converter<CharType> from_type; |
| 79 | typedef impl_icu::icu_std_converter<char> to_type; |
| 80 | |
| 81 | hold_ptr<from_type> cvt_from_; |
| 82 | hold_ptr<to_type> cvt_to_; |
| 83 | }; |
| 84 | |
| 85 | class uconv_between final : public detail::narrow_converter { |
| 86 | public: |
| 87 | bool open(const std::string& to_charset, const std::string& from_charset, method_type how) |
| 88 | { |
| 89 | try { |
| 90 | using impl_icu::cpcvt_type; |
| 91 | cvt_from_.reset(p: new from_type(from_charset, how == skip ? cpcvt_type::skip : cpcvt_type::stop)); |
| 92 | cvt_to_.reset(p: new to_type(to_charset, how == skip ? cpcvt_type::skip : cpcvt_type::stop)); |
| 93 | } catch(const std::exception& /*e*/) { |
| 94 | cvt_from_.reset(); |
| 95 | cvt_to_.reset(); |
| 96 | return false; |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | std::string convert(const char* begin, const char* end) override |
| 102 | { |
| 103 | try { |
| 104 | return cvt_to_->std(str: cvt_from_->icu(vb: begin, ve: end)); |
| 105 | } catch(const std::exception& /*e*/) { |
| 106 | throw conversion_error(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private: |
| 111 | typedef impl_icu::icu_std_converter<char> from_type; |
| 112 | typedef impl_icu::icu_std_converter<char> to_type; |
| 113 | |
| 114 | hold_ptr<from_type> cvt_from_; |
| 115 | hold_ptr<to_type> cvt_to_; |
| 116 | }; |
| 117 | |
| 118 | }}}} // namespace boost::locale::conv::impl |
| 119 | |
| 120 | #endif |
| 121 | |