locale.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // Copyright (c) 2016 Klemens D. Morgenstern
  2. // Copyright (c) 2008 Beman Dawes
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PROCESS_LOCALE_HPP_
  6. #define BOOST_PROCESS_LOCALE_HPP_
  7. #include <system_error>
  8. #include <boost/process/v1/detail/config.hpp>
  9. #if defined(BOOST_WINDOWS_API)
  10. #include <boost/process/v1/detail/windows/locale.hpp>
  11. # elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) \
  12. || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__HAIKU__)
  13. #include <codecvt>
  14. #endif
  15. #include <locale>
  16. namespace boost
  17. {
  18. namespace process
  19. {
  20. BOOST_PROCESS_V1_INLINE namespace v1
  21. {
  22. namespace detail
  23. {
  24. class codecvt_category_t : public std::error_category
  25. {
  26. public:
  27. codecvt_category_t() = default;
  28. const char* name() const noexcept override {return "codecvt";}
  29. std::string message(int ev) const override
  30. {
  31. std::string str;
  32. switch (ev)
  33. {
  34. case std::codecvt_base::ok:
  35. str = "ok";
  36. break;
  37. case std::codecvt_base::partial:
  38. str = "partial";
  39. break;
  40. case std::codecvt_base::error:
  41. str = "error";
  42. break;
  43. case std::codecvt_base::noconv:
  44. str = "noconv";
  45. break;
  46. default:
  47. str = "unknown error";
  48. }
  49. return str;
  50. }
  51. };
  52. }
  53. ///Internally used error cateory for code conversion.
  54. inline const std::error_category& codecvt_category()
  55. {
  56. static const ::boost::process::v1::detail::codecvt_category_t cat;
  57. return cat;
  58. }
  59. namespace detail
  60. {
  61. //copied from boost.filesystem
  62. inline std::locale default_locale()
  63. {
  64. # if defined(BOOST_WINDOWS_API)
  65. std::locale global_loc = std::locale();
  66. return std::locale(global_loc, new boost::process::v1::detail::windows::windows_file_codecvt);
  67. # elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__) \
  68. || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__HAIKU__)
  69. std::locale global_loc = std::locale();
  70. return std::locale(global_loc, new std::codecvt_utf8<wchar_t>);
  71. # else // Other POSIX
  72. // Return a default locale object.
  73. return std::locale();
  74. # endif
  75. }
  76. inline std::locale& process_locale()
  77. {
  78. static std::locale loc(default_locale());
  79. return loc;
  80. }
  81. }
  82. ///The internally used type for code conversion.
  83. typedef std::codecvt<wchar_t, char, std::mbstate_t> codecvt_type;
  84. ///Get a reference to the currently used code converter.
  85. inline const codecvt_type& codecvt()
  86. {
  87. return std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(
  88. detail::process_locale());
  89. }
  90. ///Set the locale of the library.
  91. inline std::locale imbue(const std::locale& loc)
  92. {
  93. std::locale temp(detail::process_locale());
  94. detail::process_locale() = loc;
  95. return temp;
  96. }
  97. namespace detail
  98. {
  99. inline std::size_t convert(const char* from,
  100. const char* from_end,
  101. wchar_t* to, wchar_t* to_end,
  102. const ::boost::process::v1::codecvt_type & cvt =
  103. ::boost::process::v1::codecvt())
  104. {
  105. std::mbstate_t state = std::mbstate_t(); // perhaps unneeded, but cuts bug reports
  106. const char* from_next;
  107. wchar_t* to_next;
  108. auto res = cvt.in(state, from, from_end, from_next,
  109. to, to_end, to_next);
  110. if (res != std::codecvt_base::ok)
  111. throw process_error(res, ::boost::process::v1::codecvt_category(),
  112. "boost::process codecvt to wchar_t");
  113. return to_next - to;
  114. }
  115. inline std::size_t convert(const wchar_t* from,
  116. const wchar_t* from_end,
  117. char* to, char* to_end,
  118. const ::boost::process::v1::codecvt_type & cvt =
  119. ::boost::process::v1::codecvt())
  120. {
  121. std::mbstate_t state = std::mbstate_t(); // perhaps unneeded, but cuts bug reports
  122. const wchar_t* from_next;
  123. char* to_next;
  124. std::codecvt_base::result res;
  125. if ((res=cvt.out(state, from, from_end, from_next,
  126. to, to_end, to_next)) != std::codecvt_base::ok)
  127. throw process_error(res, ::boost::process::v1::codecvt_category(),
  128. "boost::process codecvt to char");
  129. return to_next - to;
  130. }
  131. inline std::wstring convert(const std::string & st,
  132. const ::boost::process::v1::codecvt_type & cvt =
  133. ::boost::process::v1::codecvt())
  134. {
  135. std::wstring out(st.size() + 10, ' '); //just to be sure
  136. auto sz = convert(st.c_str(), st.c_str() + st.size(),
  137. &out.front(), &out.back(), cvt);
  138. out.resize(sz);
  139. return out;
  140. }
  141. inline std::string convert(const std::wstring & st,
  142. const ::boost::process::v1::codecvt_type & cvt =
  143. ::boost::process::v1::codecvt())
  144. {
  145. std::string out(st.size() * 2, ' '); //just to be sure
  146. auto sz = convert(st.c_str(), st.c_str() + st.size(),
  147. &out.front(), &out.back(), cvt);
  148. out.resize(sz);
  149. return out;
  150. }
  151. inline std::vector<wchar_t> convert(const std::vector<char> & st,
  152. const ::boost::process::v1::codecvt_type & cvt =
  153. ::boost::process::v1::codecvt())
  154. {
  155. std::vector<wchar_t> out(st.size() + 10); //just to be sure
  156. auto sz = convert(st.data(), st.data() + st.size(),
  157. &out.front(), &out.back(), cvt);
  158. out.resize(sz);
  159. return out;
  160. }
  161. inline std::vector<char> convert(const std::vector<wchar_t> & st,
  162. const ::boost::process::v1::codecvt_type & cvt =
  163. ::boost::process::v1::codecvt())
  164. {
  165. std::vector<char> out(st.size() * 2); //just to be sure
  166. auto sz = convert(st.data(), st.data() + st.size(),
  167. &out.front(), &out.back(), cvt);
  168. out.resize(sz);
  169. return out;
  170. }
  171. inline std::wstring convert(const char *begin, const char* end,
  172. const ::boost::process::v1::codecvt_type & cvt =
  173. ::boost::process::v1::codecvt())
  174. {
  175. auto size = end-begin;
  176. std::wstring out(size + 10, ' '); //just to be sure
  177. using namespace std;
  178. auto sz = convert(begin, end,
  179. &out.front(), &out.back(), cvt);
  180. out.resize(sz);
  181. return out;
  182. }
  183. inline std::string convert(const wchar_t * begin, const wchar_t *end,
  184. const ::boost::process::v1::codecvt_type & cvt =
  185. ::boost::process::v1::codecvt())
  186. {
  187. auto size = end-begin;
  188. std::string out(size * 2, ' '); //just to be sure
  189. auto sz = convert(begin, end ,
  190. &out.front(), &out.back(), cvt);
  191. out.resize(sz);
  192. return out;
  193. }
  194. }
  195. }}}
  196. #endif /* BOOST_PROCESS_LOCALE_HPP_ */