locale.hpp 3.1 KB

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