Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:10

0001 // Copyright (c) 2022 Klemens D. Morgenstern
0002 //
0003 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0004 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 #ifndef BOOST_PROCESS_V2_DETAIL_UTF8_HPP
0006 #define BOOST_PROCESS_V2_DETAIL_UTF8_HPP
0007 
0008 #include <boost/process/v2/detail/config.hpp>
0009 #include <boost/process/v2/detail/throw_error.hpp>
0010 
0011 BOOST_PROCESS_V2_BEGIN_NAMESPACE
0012 
0013 namespace detail
0014 {
0015 
0016 BOOST_PROCESS_V2_DECL std::size_t size_as_utf8(const wchar_t * in, std::size_t size, error_code & ec);
0017 BOOST_PROCESS_V2_DECL std::size_t size_as_wide(const  char   * in, std::size_t size, error_code & ec);
0018 
0019 BOOST_PROCESS_V2_DECL std::size_t convert_to_utf8(const wchar_t * in, std::size_t size, 
0020                                                    char   * out, std::size_t max_size, error_code & ec);
0021 BOOST_PROCESS_V2_DECL std::size_t convert_to_wide(const  char   * in, std::size_t size,  
0022                                                   wchar_t * out, std::size_t max_size, error_code & ec);
0023 
0024 template<typename CharOut, typename Traits = std::char_traits<CharOut>, 
0025          typename Allocator = std::allocator<CharOut>, typename CharIn,
0026          typename = typename std::enable_if<std::is_same<CharOut, CharIn>::value>::type>
0027 BOOST_PROCESS_V2_DECL 
0028 std::basic_string<CharOut, Traits, Allocator> conv_string(
0029     const CharIn * data, std::size_t size, 
0030     const Allocator allocator = Allocator{})
0031 {
0032     return std::basic_string<CharOut, Traits, Allocator>(data, size, allocator);
0033 }
0034 
0035 
0036 template<typename CharOut, typename Traits = std::char_traits<CharOut>, 
0037          typename Allocator = std::allocator<CharOut>,
0038          typename = typename std::enable_if<std::is_same<CharOut, char>::value>::type>
0039 BOOST_PROCESS_V2_DECL 
0040 std::basic_string<CharOut, Traits, Allocator> conv_string(
0041     const wchar_t * data, std::size_t size, 
0042     const Allocator allocator = Allocator{})
0043 {
0044     error_code ec;
0045     const auto req_size = size_as_utf8(data, size, ec);
0046     if (ec)
0047         detail::throw_error(ec, "size_as_utf8");
0048 
0049     std::basic_string<CharOut, Traits, Allocator> res(allocator);
0050     res.resize(req_size);
0051 
0052     auto res_size = convert_to_utf8(data, size, &res.front(), req_size,  ec);
0053     if (ec)
0054         detail::throw_error(ec, "convert_to_utf8");
0055 
0056     res.resize(res_size);
0057     return res;
0058 }
0059 
0060 template<typename CharOut, typename Traits = std::char_traits<CharOut>, 
0061          typename Allocator = std::allocator<CharOut>,
0062          typename = typename std::enable_if<std::is_same<CharOut, wchar_t>::value>::type>
0063 BOOST_PROCESS_V2_DECL 
0064 std::basic_string<CharOut, Traits, Allocator> conv_string(
0065     const char * data, std::size_t size, 
0066     const Allocator allocator = Allocator{})
0067 {
0068     error_code ec;
0069     const auto req_size = size_as_wide(data, size, ec);
0070     if (ec)
0071         detail::throw_error(ec, "size_as_wide");
0072 
0073     std::basic_string<CharOut, Traits, Allocator> res(allocator);
0074     res.resize(req_size);
0075 
0076     auto res_size = convert_to_wide(data, size, &res.front(), req_size,  ec);
0077     if (ec)
0078         detail::throw_error(ec, "convert_to_wide");
0079 
0080     res.resize(res_size);
0081     return res;
0082 }
0083 
0084 }
0085 
0086 BOOST_PROCESS_V2_END_NAMESPACE
0087 
0088 #if defined(BOOST_PROCESS_V2_HEADER_ONLY)
0089 
0090 #include <boost/process/v2/detail/impl/utf8.ipp>
0091 
0092 #endif
0093 
0094 
0095 #endif //BOOST_PROCESS_V2_DETAIL_UTF8_HPP