1 // Copyright Vladimir Prus 2004. 2 // Distributed under the Boost Software License, Version 1.0. 3 // (See accompanying file LICENSE_1_0.txt 4 // or copy at http://www.boost.org/LICENSE_1_0.txt) 5 6 #ifndef BOOST_CONVERT_HPP_VP_2004_04_28 7 #define BOOST_CONVERT_HPP_VP_2004_04_28 8 9 #include <boost/program_options/config.hpp> 10 11 #if !defined(BOOST_NO_STD_WSTRING) 12 13 #include <boost/detail/workaround.hpp> 14 15 #include <string> 16 #include <vector> 17 #include <locale> 18 // for mbstate_t 19 #include <cwchar> 20 #include <stdexcept> 21 22 #if defined(BOOST_NO_STDC_NAMESPACE) 23 #include <wchar.h> 24 namespace std 25 { 26 using ::mbstate_t; 27 } 28 #endif 29 30 namespace boost { 31 32 /** Converts from local 8 bit encoding into wchar_t string using 33 the specified locale facet. */ 34 BOOST_PROGRAM_OPTIONS_DECL std::wstring 35 from_8_bit(const std::string& s, 36 const std::codecvt<wchar_t, char, std::mbstate_t>& cvt); 37 38 /** Converts from wchar_t string into local 8 bit encoding into using 39 the specified locale facet. */ 40 BOOST_PROGRAM_OPTIONS_DECL std::string 41 to_8_bit(const std::wstring& s, 42 const std::codecvt<wchar_t, char, std::mbstate_t>& cvt); 43 44 45 /** Converts 's', which is assumed to be in UTF8 encoding, into wide 46 string. */ 47 BOOST_PROGRAM_OPTIONS_DECL std::wstring 48 from_utf8(const std::string& s); 49 50 /** Converts wide string 's' into string in UTF8 encoding. */ 51 BOOST_PROGRAM_OPTIONS_DECL std::string 52 to_utf8(const std::wstring& s); 53 54 /** Converts wide string 's' into local 8 bit encoding determined by 55 the current locale. */ 56 BOOST_PROGRAM_OPTIONS_DECL std::string 57 to_local_8_bit(const std::wstring& s); 58 59 /** Converts 's', which is assumed to be in local 8 bit encoding, into wide 60 string. */ 61 BOOST_PROGRAM_OPTIONS_DECL std::wstring 62 from_local_8_bit(const std::string& s); 63 64 namespace program_options 65 { 66 /** Convert the input string into internal encoding used by 67 program_options. Presence of this function allows to avoid 68 specializing all methods which access input on wchar_t. 69 */ 70 BOOST_PROGRAM_OPTIONS_DECL std::string to_internal(const std::string&); 71 /** @overload */ 72 BOOST_PROGRAM_OPTIONS_DECL std::string to_internal(const std::wstring&); 73 74 template<class T> to_internal(const std::vector<T> & s)75 std::vector<std::string> to_internal(const std::vector<T>& s) 76 { 77 std::vector<std::string> result; 78 for (unsigned i = 0; i < s.size(); ++i) 79 result.push_back(to_internal(s[i])); 80 return result; 81 } 82 83 } 84 85 86 87 } 88 89 #else 90 #include <vector> 91 #include <string> 92 namespace boost{ 93 namespace program_options{ 94 BOOST_PROGRAM_OPTIONS_DECL std::string to_internal(const std::string&); 95 96 template<class T> to_internal(const std::vector<T> & s)97 std::vector<std::string> to_internal(const std::vector<T>& s) 98 { 99 std::vector<std::string> result; 100 for (unsigned i = 0; i < s.size(); ++i) 101 result.push_back(to_internal(s[i])); 102 return result; 103 } 104 } 105 } 106 #endif 107 #endif 108