Chris@16: /* Chris@101: * Copyright Andrey Semashev 2007 - 2015. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file code_conversion.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 08.11.2008 Chris@16: * Chris@16: * \brief This header is the Boost.Log library implementation, see the library documentation Chris@16: * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: namespace aux { Chris@16: Chris@16: //! The function converts one string to the character type of another Chris@16: BOOST_LOG_API void code_convert(const wchar_t* str1, std::size_t len, std::string& str2, std::locale const& loc = std::locale()); Chris@16: //! The function converts one string to the character type of another Chris@16: BOOST_LOG_API void code_convert(const char* str1, std::size_t len, std::wstring& str2, std::locale const& loc = std::locale()); Chris@101: Chris@101: // Note: MSVC 2015 (aka VC14) implement char16_t and char32_t types but not codecvt locale facets Chris@101: #if !defined(BOOST_MSVC) Chris@16: #if !defined(BOOST_NO_CXX11_CHAR16_T) Chris@16: //! The function converts one string to the character type of another Chris@16: BOOST_LOG_API void code_convert(const char16_t* str1, std::size_t len, std::string& str2, std::locale const& loc = std::locale()); Chris@16: //! The function converts one string to the character type of another Chris@16: BOOST_LOG_API void code_convert(const char* str1, std::size_t len, std::u16string& str2, std::locale const& loc = std::locale()); Chris@16: #endif Chris@16: #if !defined(BOOST_NO_CXX11_CHAR32_T) Chris@16: //! The function converts one string to the character type of another Chris@16: BOOST_LOG_API void code_convert(const char32_t* str1, std::size_t len, std::string& str2, std::locale const& loc = std::locale()); Chris@16: //! The function converts one string to the character type of another Chris@16: BOOST_LOG_API void code_convert(const char* str1, std::size_t len, std::u32string& str2, std::locale const& loc = std::locale()); Chris@16: #endif Chris@101: #endif // !defined(BOOST_MSVC) Chris@16: Chris@16: //! The function converts one string to the character type of another Chris@16: template< typename CharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetTraitsT, typename TargetAllocatorT > Chris@16: inline void code_convert(std::basic_string< CharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< CharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& = std::locale()) Chris@16: { Chris@16: str2.append(str1.c_str(), str1.size()); Chris@16: } Chris@16: //! The function converts one string to the character type of another Chris@16: template< typename CharT, typename TargetTraitsT, typename TargetAllocatorT > Chris@16: inline void code_convert(const CharT* str1, std::size_t len, std::basic_string< CharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& = std::locale()) Chris@16: { Chris@16: str2.append(str1, len); Chris@16: } Chris@16: Chris@16: //! The function converts one string to the character type of another Chris@16: template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT > Chris@16: inline void code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale()) Chris@16: { Chris@16: aux::code_convert(str1.c_str(), str1.size(), str2, loc); Chris@16: } Chris@16: Chris@16: //! The function converts the passed string to the narrow-character encoding Chris@16: inline std::string const& to_narrow(std::string const& str) Chris@16: { Chris@16: return str; Chris@16: } Chris@16: Chris@16: //! The function converts the passed string to the narrow-character encoding Chris@16: inline std::string const& to_narrow(std::string const& str, std::locale const&) Chris@16: { Chris@16: return str; Chris@16: } Chris@16: Chris@16: //! The function converts the passed string to the narrow-character encoding Chris@16: inline std::string to_narrow(std::wstring const& str, std::locale const& loc = std::locale()) Chris@16: { Chris@16: std::string res; Chris@16: aux::code_convert(str, res, loc); Chris@16: return res; Chris@16: } Chris@16: Chris@16: //! The function converts the passed string to the wide-character encoding Chris@16: inline std::wstring const& to_wide(std::wstring const& str) Chris@16: { Chris@16: return str; Chris@16: } Chris@16: Chris@16: //! The function converts the passed string to the wide-character encoding Chris@16: inline std::wstring const& to_wide(std::wstring const& str, std::locale const&) Chris@16: { Chris@16: return str; Chris@16: } Chris@16: Chris@16: //! The function converts the passed string to the wide-character encoding Chris@16: inline std::wstring to_wide(std::string const& str, std::locale const& loc = std::locale()) Chris@16: { Chris@16: std::wstring res; Chris@16: aux::code_convert(str, res, loc); Chris@16: return res; Chris@16: } Chris@16: Chris@16: } // namespace aux Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_