Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / process / detail / windows / locale.hpp
1 // Copyright (c) 2016 Klemens D. Morgenstern\r
2 // Copyright (c) 2008 Beman Dawes\r
3 //\r
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying\r
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)\r
6
7 #ifndef BOOST_PROCESS_DETAIL_WINDOWS_LOCALE_HPP_\r
8 #define BOOST_PROCESS_DETAIL_WINDOWS_LOCALE_HPP_\r
9
10 #include <locale>\r
11 #include <boost/core/ignore_unused.hpp>\r
12 #include <boost/detail/winapi/file_management.hpp>\r
13 #include <boost/detail/winapi/character_code_conversion.hpp>\r
14
15 namespace boost\r
16 {\r
17 namespace process\r
18 {\r
19 namespace detail\r
20 {\r
21 namespace windows\r
22 {\r
23
24 //copied from boost.filesystem\r
25 class windows_file_codecvt\r
26    : public std::codecvt< wchar_t, char, std::mbstate_t >\r
27  {\r
28  public:\r
29    explicit windows_file_codecvt(std::size_t refs = 0)\r
30        : std::codecvt<wchar_t, char, std::mbstate_t>(refs) {}\r
31  protected:\r
32
33    bool do_always_noconv() const noexcept override { return false; }\r
34
35    //  seems safest to assume variable number of characters since we don't\r
36    //  actually know what codepage is active\r
37    int do_encoding() const noexcept override { return 0; }\r
38
39    std::codecvt_base::result do_in(std::mbstate_t& state,\r
40      const char* from, const char* from_end, const char*& from_next,\r
41      wchar_t* to, wchar_t* to_end, wchar_t*& to_next) const override\r
42    {\r
43      boost::ignore_unused(state);\r
44      ::boost::detail::winapi::UINT_ codepage = AreFileApisANSI() ?\r
45              ::boost::detail::winapi::CP_ACP_ :\r
46              ::boost::detail::winapi::CP_OEMCP_;\r
47
48      int count;\r
49      if ((count = ::boost::detail::winapi::MultiByteToWideChar(codepage,\r
50              ::boost::detail::winapi::MB_PRECOMPOSED_, from,\r
51        static_cast<int>(from_end - from), to, static_cast<int>(to_end - to))) == 0)\r
52      {\r
53        return error;  // conversion failed\r
54      }\r
55
56      from_next = from_end;\r
57      to_next = to + count;\r
58      *to_next = L'\0';\r
59      return ok;\r
60   }\r
61
62    std::codecvt_base::result do_out(std::mbstate_t & state,\r
63      const wchar_t* from, const wchar_t* from_end, const wchar_t*& from_next,\r
64      char* to, char* to_end, char*& to_next) const override\r
65    {\r
66      boost::ignore_unused(state);\r
67      auto codepage = ::boost::detail::winapi::AreFileApisANSI() ?\r
68                        ::boost::detail::winapi::CP_ACP_ :\r
69                      ::boost::detail::winapi::CP_OEMCP_;\r
70
71      int count;\r
72      if ((count = ::boost::detail::winapi::WideCharToMultiByte(codepage,\r
73                    ::boost::detail::winapi::WC_NO_BEST_FIT_CHARS_, from,\r
74                   static_cast<int>(from_end - from), to, static_cast<int>(to_end - to), 0, 0)) == 0)\r
75      {\r
76        return error;  // conversion failed\r
77      }\r
78
79      from_next = from_end;\r
80      to_next = to + count;\r
81      *to_next = '\0';\r
82      return ok;\r
83    }\r
84
85    std::codecvt_base::result do_unshift(std::mbstate_t&,\r
86        char* /*from*/, char* /*to*/, char* & /*next*/) const override { return ok; }\r
87
88    int do_length(std::mbstate_t&,\r
89      const char* /*from*/, const char* /*from_end*/, std::size_t /*max*/) const override { return 0; }\r
90
91    int do_max_length() const noexcept override { return 0; }\r
92  };\r
93
94
95
96 }\r
97 }\r
98 }\r
99 }\r
100
101
102
103 #endif /* BOOST_PROCESS_LOCALE_HPP_ */\r