]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | // Implementation note: We have to implement char<->wchar_t conversions even in the absence of the native wchar_t | |
38 | // type. These conversions are used in sinks, e.g. to convert multibyte strings to wide-character filesystem paths. | |
39 | ||
40 | //! The function converts one string to the character type of another | |
41 | BOOST_LOG_API bool code_convert_impl(const wchar_t* str1, std::size_t len, std::string& str2, std::size_t max_size, std::locale const& loc = std::locale()); | |
42 | //! The function converts one string to the character type of another | |
43 | BOOST_LOG_API bool code_convert_impl(const char* str1, std::size_t len, std::wstring& str2, std::size_t max_size, std::locale const& loc = std::locale()); | |
44 | ||
45 | #if !defined(BOOST_LOG_NO_CXX11_CODECVT_FACETS) | |
46 | #if !defined(BOOST_NO_CXX11_CHAR16_T) | |
47 | //! The function converts one string to the character type of another | |
48 | BOOST_LOG_API bool code_convert_impl(const char16_t* str1, std::size_t len, std::string& str2, std::size_t max_size, std::locale const& loc = std::locale()); | |
49 | //! The function converts one string to the character type of another | |
50 | BOOST_LOG_API bool code_convert_impl(const char* str1, std::size_t len, std::u16string& str2, std::size_t max_size, std::locale const& loc = std::locale()); | |
51 | //! The function converts one string to the character type of another | |
52 | BOOST_LOG_API bool code_convert_impl(const char16_t* str1, std::size_t len, std::wstring& str2, std::size_t max_size, std::locale const& loc = std::locale()); | |
53 | #endif | |
54 | #if !defined(BOOST_NO_CXX11_CHAR32_T) | |
55 | //! The function converts one string to the character type of another | |
56 | BOOST_LOG_API bool code_convert_impl(const char32_t* str1, std::size_t len, std::string& str2, std::size_t max_size, std::locale const& loc = std::locale()); | |
57 | //! The function converts one string to the character type of another | |
58 | BOOST_LOG_API bool code_convert_impl(const char* str1, std::size_t len, std::u32string& str2, std::size_t max_size, std::locale const& loc = std::locale()); | |
59 | //! The function converts one string to the character type of another | |
60 | BOOST_LOG_API bool code_convert_impl(const char32_t* str1, std::size_t len, std::wstring& str2, std::size_t max_size, std::locale const& loc = std::locale()); | |
61 | #endif | |
62 | #if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_CHAR32_T) | |
63 | //! The function converts one string to the character type of another | |
64 | BOOST_LOG_API bool code_convert_impl(const char16_t* str1, std::size_t len, std::u32string& str2, std::size_t max_size, std::locale const& loc = std::locale()); | |
65 | //! The function converts one string to the character type of another | |
66 | BOOST_LOG_API bool code_convert_impl(const char32_t* str1, std::size_t len, std::u16string& str2, std::size_t max_size, std::locale const& loc = std::locale()); | |
67 | #endif | |
68 | #endif // !defined(BOOST_LOG_NO_CXX11_CODECVT_FACETS) | |
69 | ||
70 | //! The function converts one string to the character type of another | |
71 | template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT > | |
72 | inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type | |
73 | code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::size_t max_size, std::locale const& = std::locale()) | |
74 | { | |
75 | std::size_t size_left = str2.size() < max_size ? max_size - str2.size() : static_cast< std::size_t >(0u); | |
76 | const bool overflow = len > size_left; | |
77 | str2.append(reinterpret_cast< const TargetCharT* >(str1), overflow ? size_left : len); | |
78 | return !overflow; | |
79 | } | |
80 | ||
81 | //! The function converts one string to the character type of another | |
82 | template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT > | |
83 | inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type | |
84 | code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::size_t max_size, std::locale const& loc = std::locale()) | |
85 | { | |
86 | return aux::code_convert(str1.data(), str1.size(), str2, max_size, loc); | |
87 | } | |
88 | ||
89 | //! The function converts one string to the character type of another | |
90 | template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT > | |
91 | inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type | |
92 | code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale()) | |
93 | { | |
94 | return aux::code_convert(str1.data(), str1.size(), str2, str2.max_size(), loc); | |
95 | } | |
96 | //! The function converts one string to the character type of another | |
97 | template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT > | |
98 | inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type | |
99 | code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale()) | |
100 | { | |
101 | return aux::code_convert(str1, len, str2, str2.max_size(), loc); | |
102 | } | |
103 | ||
104 | //! The function converts one string to the character type of another | |
105 | template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT > | |
106 | inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type | |
107 | code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale()) | |
108 | { | |
109 | return aux::code_convert_impl(str1.c_str(), str1.size(), str2, str2.max_size(), loc); | |
110 | } | |
111 | ||
112 | //! The function converts one string to the character type of another | |
113 | template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT > | |
114 | inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type | |
115 | code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale()) | |
116 | { | |
117 | return aux::code_convert_impl(str1, len, str2, str2.max_size(), loc); | |
118 | } | |
119 | ||
120 | //! The function converts one string to the character type of another | |
121 | template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT > | |
122 | inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type | |
123 | code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::size_t max_size, std::locale const& loc = std::locale()) | |
124 | { | |
125 | return aux::code_convert_impl(str1.c_str(), str1.size(), str2, max_size, loc); | |
126 | } | |
127 | ||
128 | //! The function converts one string to the character type of another | |
129 | template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT > | |
130 | inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type | |
131 | code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::size_t max_size, std::locale const& loc = std::locale()) | |
132 | { | |
133 | return aux::code_convert_impl(str1, len, str2, max_size, loc); | |
134 | } | |
135 | ||
136 | //! The function converts the passed string to the narrow-character encoding | |
137 | inline std::string const& to_narrow(std::string const& str) | |
138 | { | |
139 | return str; | |
140 | } | |
141 | ||
142 | //! The function converts the passed string to the narrow-character encoding | |
143 | inline std::string const& to_narrow(std::string const& str, std::locale const&) | |
144 | { | |
145 | return str; | |
146 | } | |
147 | ||
148 | //! The function converts the passed string to the narrow-character encoding | |
149 | inline std::string to_narrow(std::wstring const& str, std::locale const& loc = std::locale()) | |
150 | { | |
151 | std::string res; | |
152 | aux::code_convert_impl(str.c_str(), str.size(), res, res.max_size(), loc); | |
153 | #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_HAS_NRVO) | |
154 | return static_cast< std::string&& >(res); | |
155 | #else | |
156 | return res; | |
157 | #endif | |
158 | } | |
159 | ||
160 | //! The function converts the passed string to the wide-character encoding | |
161 | inline std::wstring const& to_wide(std::wstring const& str) | |
162 | { | |
163 | return str; | |
164 | } | |
165 | ||
166 | //! The function converts the passed string to the wide-character encoding | |
167 | inline std::wstring const& to_wide(std::wstring const& str, std::locale const&) | |
168 | { | |
169 | return str; | |
170 | } | |
171 | ||
172 | //! The function converts the passed string to the wide-character encoding | |
173 | inline std::wstring to_wide(std::string const& str, std::locale const& loc = std::locale()) | |
174 | { | |
175 | std::wstring res; | |
176 | aux::code_convert_impl(str.c_str(), str.size(), res, res.max_size(), loc); | |
177 | #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_HAS_NRVO) | |
178 | return static_cast< std::wstring&& >(res); | |
179 | #else | |
180 | return res; | |
181 | #endif | |
182 | } | |
183 | ||
184 | } // namespace aux | |
185 | ||
186 | BOOST_LOG_CLOSE_NAMESPACE // namespace log | |
187 | ||
188 | } // namespace boost | |
189 | ||
190 | #include <boost/log/detail/footer.hpp> | |
191 | ||
192 | #endif // BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_ |