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_ENCODING_HPP_INCLUDED 9 #define BOOST_LOCALE_ENCODING_HPP_INCLUDED 10 11 #include <boost/locale/config.hpp> 12 #ifdef BOOST_MSVC 13 # pragma warning(push) 14 # pragma warning(disable : 4275 4251 4231 4660) 15 #endif 16 #include <boost/locale/info.hpp> 17 #include <boost/locale/encoding_errors.hpp> 18 #include <boost/locale/encoding_utf.hpp> 19 20 21 22 namespace boost { 23 namespace locale { 24 25 /// 26 /// \brief Namespace that contains all functions related to character set conversion 27 /// 28 namespace conv { 29 /// 30 /// \defgroup codepage Character conversion functions 31 /// 32 /// @{ 33 34 /// 35 /// convert string to UTF string from text in range [begin,end) encoded with \a charset according to policy \a how 36 /// 37 template<typename CharType> 38 std::basic_string<CharType> to_utf(char const *begin,char const *end,std::string const &charset,method_type how=default_method); 39 40 /// 41 /// convert UTF text in range [begin,end) to a text encoded with \a charset according to policy \a how 42 /// 43 template<typename CharType> 44 std::string from_utf(CharType const *begin,CharType const *end,std::string const &charset,method_type how=default_method); 45 46 /// 47 /// convert string to UTF string from text in range [begin,end) encoded according to locale \a loc according to policy \a how 48 /// 49 /// \note throws std::bad_cast if the loc does not have \ref info facet installed 50 /// 51 template<typename CharType> to_utf(char const * begin,char const * end,std::locale const & loc,method_type how=default_method)52 std::basic_string<CharType> to_utf(char const *begin,char const *end,std::locale const &loc,method_type how=default_method) 53 { 54 return to_utf<CharType>(begin,end,std::use_facet<info>(loc).encoding(),how); 55 } 56 57 /// 58 /// convert UTF text in range [begin,end) to a text encoded according to locale \a loc according to policy \a how 59 /// 60 /// \note throws std::bad_cast if the loc does not have \ref info facet installed 61 /// 62 template<typename CharType> from_utf(CharType const * begin,CharType const * end,std::locale const & loc,method_type how=default_method)63 std::string from_utf(CharType const *begin,CharType const *end,std::locale const &loc,method_type how=default_method) 64 { 65 return from_utf(begin,end,std::use_facet<info>(loc).encoding(),how); 66 } 67 68 /// 69 /// convert a string \a text encoded with \a charset to UTF string 70 /// 71 72 template<typename CharType> to_utf(std::string const & text,std::string const & charset,method_type how=default_method)73 std::basic_string<CharType> to_utf(std::string const &text,std::string const &charset,method_type how=default_method) 74 { 75 return to_utf<CharType>(text.c_str(),text.c_str()+text.size(),charset,how); 76 } 77 78 /// 79 /// Convert a \a text from \a charset to UTF string 80 /// 81 template<typename CharType> from_utf(std::basic_string<CharType> const & text,std::string const & charset,method_type how=default_method)82 std::string from_utf(std::basic_string<CharType> const &text,std::string const &charset,method_type how=default_method) 83 { 84 return from_utf(text.c_str(),text.c_str()+text.size(),charset,how); 85 } 86 87 /// 88 /// Convert a \a text from \a charset to UTF string 89 /// 90 template<typename CharType> to_utf(char const * text,std::string const & charset,method_type how=default_method)91 std::basic_string<CharType> to_utf(char const *text,std::string const &charset,method_type how=default_method) 92 { 93 char const *text_end = text; 94 while(*text_end) 95 text_end++; 96 return to_utf<CharType>(text,text_end,charset,how); 97 } 98 99 /// 100 /// Convert a \a text from UTF to \a charset 101 /// 102 template<typename CharType> from_utf(CharType const * text,std::string const & charset,method_type how=default_method)103 std::string from_utf(CharType const *text,std::string const &charset,method_type how=default_method) 104 { 105 CharType const *text_end = text; 106 while(*text_end) 107 text_end++; 108 return from_utf(text,text_end,charset,how); 109 } 110 111 /// 112 /// Convert a \a text in locale encoding given by \a loc to UTF 113 /// 114 /// \note throws std::bad_cast if the loc does not have \ref info facet installed 115 /// 116 template<typename CharType> to_utf(std::string const & text,std::locale const & loc,method_type how=default_method)117 std::basic_string<CharType> to_utf(std::string const &text,std::locale const &loc,method_type how=default_method) 118 { 119 return to_utf<CharType>(text.c_str(),text.c_str()+text.size(),loc,how); 120 } 121 122 /// 123 /// Convert a \a text in UTF to locale encoding given by \a loc 124 /// 125 /// \note throws std::bad_cast if the loc does not have \ref info facet installed 126 /// 127 template<typename CharType> from_utf(std::basic_string<CharType> const & text,std::locale const & loc,method_type how=default_method)128 std::string from_utf(std::basic_string<CharType> const &text,std::locale const &loc,method_type how=default_method) 129 { 130 return from_utf(text.c_str(),text.c_str()+text.size(),loc,how); 131 } 132 133 /// 134 /// Convert a \a text in locale encoding given by \a loc to UTF 135 /// 136 /// \note throws std::bad_cast if the loc does not have \ref info facet installed 137 /// 138 template<typename CharType> to_utf(char const * text,std::locale const & loc,method_type how=default_method)139 std::basic_string<CharType> to_utf(char const *text,std::locale const &loc,method_type how=default_method) 140 { 141 char const *text_end = text; 142 while(*text_end) 143 text_end++; 144 return to_utf<CharType>(text,text_end,loc,how); 145 } 146 147 /// 148 /// Convert a \a text in UTF to locale encoding given by \a loc 149 /// 150 /// \note throws std::bad_cast if the loc does not have \ref info facet installed 151 /// 152 template<typename CharType> from_utf(CharType const * text,std::locale const & loc,method_type how=default_method)153 std::string from_utf(CharType const *text,std::locale const &loc,method_type how=default_method) 154 { 155 CharType const *text_end = text; 156 while(*text_end) 157 text_end++; 158 return from_utf(text,text_end,loc,how); 159 } 160 161 162 /// 163 /// Convert a text in range [begin,end) to \a to_encoding from \a from_encoding 164 /// 165 166 BOOST_LOCALE_DECL 167 std::string between(char const *begin, 168 char const *end, 169 std::string const &to_encoding, 170 std::string const &from_encoding, 171 method_type how=default_method); 172 173 /// 174 /// Convert a \a text to \a to_encoding from \a from_encoding 175 /// 176 177 inline between(char const * text,std::string const & to_encoding,std::string const & from_encoding,method_type how=default_method)178 std::string between(char const *text, 179 std::string const &to_encoding, 180 std::string const &from_encoding, 181 method_type how=default_method) 182 { 183 char const *end=text; 184 while(*end) 185 end++; 186 return boost::locale::conv::between(text,end,to_encoding,from_encoding,how); 187 } 188 189 /// 190 /// Convert a \a text to \a to_encoding from \a from_encoding 191 /// 192 inline between(std::string const & text,std::string const & to_encoding,std::string const & from_encoding,method_type how=default_method)193 std::string between(std::string const &text, 194 std::string const &to_encoding, 195 std::string const &from_encoding, 196 method_type how=default_method) 197 { 198 return boost::locale::conv::between(text.c_str(),text.c_str()+text.size(),to_encoding,from_encoding,how); 199 } 200 201 /// \cond INTERNAL 202 203 template<> 204 BOOST_LOCALE_DECL std::basic_string<char> to_utf(char const *begin,char const *end,std::string const &charset,method_type how); 205 206 template<> 207 BOOST_LOCALE_DECL std::string from_utf(char const *begin,char const *end,std::string const &charset,method_type how); 208 209 template<> 210 BOOST_LOCALE_DECL std::basic_string<wchar_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how); 211 212 template<> 213 BOOST_LOCALE_DECL std::string from_utf(wchar_t const *begin,wchar_t const *end,std::string const &charset,method_type how); 214 215 #ifdef BOOST_HAS_CHAR16_T 216 template<> 217 BOOST_LOCALE_DECL std::basic_string<char16_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how); 218 219 template<> 220 BOOST_LOCALE_DECL std::string from_utf(char16_t const *begin,char16_t const *end,std::string const &charset,method_type how); 221 #endif 222 223 #ifdef BOOST_HAS_CHAR32_T 224 template<> 225 BOOST_LOCALE_DECL std::basic_string<char32_t> to_utf(char const *begin,char const *end,std::string const &charset,method_type how); 226 227 template<> 228 BOOST_LOCALE_DECL std::string from_utf(char32_t const *begin,char32_t const *end,std::string const &charset,method_type how); 229 #endif 230 231 232 /// \endcond 233 234 /// @} 235 236 } // conv 237 238 } // locale 239 } // boost 240 241 #ifdef BOOST_MSVC 242 #pragma warning(pop) 243 #endif 244 245 #endif 246 247 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 248 249