locale.hpp 6.8 KB

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