1 /*
2  *          Copyright Andrey Semashev 2007 - 2015.
3  * Distributed under the Boost Software License, Version 1.0.
4  *    (See accompanying file LICENSE_1_0.txt or copy at
5  *          http://www.boost.org/LICENSE_1_0.txt)
6  */
7 /*!
8  * \file   code_conversion.hpp
9  * \author Andrey Semashev
10  * \date   08.11.2008
11  *
12  * \brief  This header is the Boost.Log library implementation, see the library documentation
13  *         at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
14  */
15 
16 #ifndef BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_
17 #define BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_
18 
19 #include <cstddef>
20 #include <locale>
21 #include <string>
22 #include <boost/core/enable_if.hpp>
23 #include <boost/log/detail/config.hpp>
24 #include <boost/log/detail/is_character_type.hpp>
25 #include <boost/log/detail/header.hpp>
26 
27 #ifdef BOOST_HAS_PRAGMA_ONCE
28 #pragma once
29 #endif
30 
31 namespace boost {
32 
33 BOOST_LOG_OPEN_NAMESPACE
34 
35 namespace aux {
36 
37 //! The function converts one string to the character type of another
38 BOOST_LOG_API void code_convert_impl(const wchar_t* str1, std::size_t len, std::string& str2, std::locale const& loc = std::locale());
39 //! The function converts one string to the character type of another
40 BOOST_LOG_API void code_convert_impl(const char* str1, std::size_t len, std::wstring& str2, std::locale const& loc = std::locale());
41 
42 #if !defined(BOOST_LOG_NO_CXX11_CODECVT_FACETS)
43 #if !defined(BOOST_NO_CXX11_CHAR16_T)
44 //! The function converts one string to the character type of another
45 BOOST_LOG_API void code_convert_impl(const char16_t* str1, std::size_t len, std::string& str2, std::locale const& loc = std::locale());
46 //! The function converts one string to the character type of another
47 BOOST_LOG_API void code_convert_impl(const char* str1, std::size_t len, std::u16string& str2, std::locale const& loc = std::locale());
48 //! The function converts one string to the character type of another
49 BOOST_LOG_API void code_convert_impl(const char16_t* str1, std::size_t len, std::wstring& str2, std::locale const& loc = std::locale());
50 #endif
51 #if !defined(BOOST_NO_CXX11_CHAR32_T)
52 //! The function converts one string to the character type of another
53 BOOST_LOG_API void code_convert_impl(const char32_t* str1, std::size_t len, std::string& str2, std::locale const& loc = std::locale());
54 //! The function converts one string to the character type of another
55 BOOST_LOG_API void code_convert_impl(const char* str1, std::size_t len, std::u32string& str2, std::locale const& loc = std::locale());
56 //! The function converts one string to the character type of another
57 BOOST_LOG_API void code_convert_impl(const char32_t* str1, std::size_t len, std::wstring& str2, std::locale const& loc = std::locale());
58 #endif
59 #if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_CHAR32_T)
60 //! The function converts one string to the character type of another
61 BOOST_LOG_API void code_convert_impl(const char16_t* str1, std::size_t len, std::u32string& str2, std::locale const& loc = std::locale());
62 //! The function converts one string to the character type of another
63 BOOST_LOG_API void code_convert_impl(const char32_t* str1, std::size_t len, std::u16string& str2, std::locale const& loc = std::locale());
64 #endif
65 #endif // !defined(BOOST_LOG_NO_CXX11_CODECVT_FACETS)
66 
67 //! The function converts one string to the character type of another
68 template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
69 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT) >::type
code_convert(std::basic_string<SourceCharT,SourceTraitsT,SourceAllocatorT> const & str1,std::basic_string<TargetCharT,TargetTraitsT,TargetAllocatorT> & str2,std::locale const &=std::locale ())70 code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& = std::locale())
71 {
72     str2.append(reinterpret_cast< const TargetCharT* >(str1.c_str()), str1.size());
73 }
74 //! The function converts one string to the character type of another
75 template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
76 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT) >::type
code_convert(const SourceCharT * str1,std::size_t len,std::basic_string<TargetCharT,TargetTraitsT,TargetAllocatorT> & str2,std::locale const &=std::locale ())77 code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& = std::locale())
78 {
79     str2.append(reinterpret_cast< const TargetCharT* >(str1), len);
80 }
81 
82 //! The function converts one string to the character type of another
83 template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
84 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT) >::type
code_convert(std::basic_string<SourceCharT,SourceTraitsT,SourceAllocatorT> const & str1,std::basic_string<TargetCharT,TargetTraitsT,TargetAllocatorT> & str2,std::locale const & loc=std::locale ())85 code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
86 {
87     aux::code_convert_impl(str1.c_str(), str1.size(), str2, loc);
88 }
89 
90 //! The function converts one string to the character type of another
91 template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
92 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT) >::type
code_convert(const SourceCharT * str1,std::size_t len,std::basic_string<TargetCharT,TargetTraitsT,TargetAllocatorT> & str2,std::locale const & loc=std::locale ())93 code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
94 {
95     aux::code_convert_impl(str1, len, str2, loc);
96 }
97 
98 //! The function converts the passed string to the narrow-character encoding
to_narrow(std::string const & str)99 inline std::string const& to_narrow(std::string const& str)
100 {
101     return str;
102 }
103 
104 //! The function converts the passed string to the narrow-character encoding
to_narrow(std::string const & str,std::locale const &)105 inline std::string const& to_narrow(std::string const& str, std::locale const&)
106 {
107     return str;
108 }
109 
110 //! The function converts the passed string to the narrow-character encoding
to_narrow(std::wstring const & str,std::locale const & loc=std::locale ())111 inline std::string to_narrow(std::wstring const& str, std::locale const& loc = std::locale())
112 {
113     std::string res;
114     aux::code_convert(str, res, loc);
115     return res;
116 }
117 
118 //! The function converts the passed string to the wide-character encoding
to_wide(std::wstring const & str)119 inline std::wstring const& to_wide(std::wstring const& str)
120 {
121     return str;
122 }
123 
124 //! The function converts the passed string to the wide-character encoding
to_wide(std::wstring const & str,std::locale const &)125 inline std::wstring const& to_wide(std::wstring const& str, std::locale const&)
126 {
127     return str;
128 }
129 
130 //! The function converts the passed string to the wide-character encoding
to_wide(std::string const & str,std::locale const & loc=std::locale ())131 inline std::wstring to_wide(std::string const& str, std::locale const& loc = std::locale())
132 {
133     std::wstring res;
134     aux::code_convert(str, res, loc);
135     return res;
136 }
137 
138 } // namespace aux
139 
140 BOOST_LOG_CLOSE_NAMESPACE // namespace log
141 
142 } // namespace boost
143 
144 #include <boost/log/detail/footer.hpp>
145 
146 #endif // BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_
147