convert.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright Vladimir Prus 2004.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt
  4. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_CONVERT_HPP_VP_2004_04_28
  6. #define BOOST_CONVERT_HPP_VP_2004_04_28
  7. #include <boost/program_options/config.hpp>
  8. #if !defined(BOOST_NO_STD_WSTRING)
  9. #include <boost/detail/workaround.hpp>
  10. #include <string>
  11. #include <vector>
  12. #include <locale>
  13. // for mbstate_t
  14. #include <cwchar>
  15. #include <stdexcept>
  16. #if defined(BOOST_NO_STDC_NAMESPACE)
  17. #include <wchar.h>
  18. namespace std
  19. {
  20. using ::mbstate_t;
  21. }
  22. #endif
  23. namespace boost {
  24. /** Converts from local 8 bit encoding into wchar_t string using
  25. the specified locale facet. */
  26. BOOST_PROGRAM_OPTIONS_DECL std::wstring
  27. from_8_bit(const std::string& s,
  28. const std::codecvt<wchar_t, char, std::mbstate_t>& cvt);
  29. /** Converts from wchar_t string into local 8 bit encoding into using
  30. the specified locale facet. */
  31. BOOST_PROGRAM_OPTIONS_DECL std::string
  32. to_8_bit(const std::wstring& s,
  33. const std::codecvt<wchar_t, char, std::mbstate_t>& cvt);
  34. /** Converts 's', which is assumed to be in UTF8 encoding, into wide
  35. string. */
  36. BOOST_PROGRAM_OPTIONS_DECL std::wstring
  37. from_utf8(const std::string& s);
  38. /** Converts wide string 's' into string in UTF8 encoding. */
  39. BOOST_PROGRAM_OPTIONS_DECL std::string
  40. to_utf8(const std::wstring& s);
  41. /** Converts wide string 's' into local 8 bit encoding determined by
  42. the current locale. */
  43. BOOST_PROGRAM_OPTIONS_DECL std::string
  44. to_local_8_bit(const std::wstring& s);
  45. /** Converts 's', which is assumed to be in local 8 bit encoding, into wide
  46. string. */
  47. BOOST_PROGRAM_OPTIONS_DECL std::wstring
  48. from_local_8_bit(const std::string& s);
  49. namespace program_options
  50. {
  51. /** Convert the input string into internal encoding used by
  52. program_options. Presence of this function allows to avoid
  53. specializing all methods which access input on wchar_t.
  54. */
  55. BOOST_PROGRAM_OPTIONS_DECL std::string to_internal(const std::string&);
  56. /** @overload */
  57. BOOST_PROGRAM_OPTIONS_DECL std::string to_internal(const std::wstring&);
  58. template<class T>
  59. std::vector<std::string> to_internal(const std::vector<T>& s)
  60. {
  61. std::vector<std::string> result;
  62. for (unsigned i = 0; i < s.size(); ++i)
  63. result.push_back(to_internal(s[i]));
  64. return result;
  65. }
  66. }
  67. }
  68. #else
  69. #include <vector>
  70. #include <string>
  71. namespace boost{
  72. namespace program_options{
  73. BOOST_PROGRAM_OPTIONS_DECL std::string to_internal(const std::string&);
  74. template<class T>
  75. std::vector<std::string> to_internal(const std::vector<T>& s)
  76. {
  77. std::vector<std::string> result;
  78. for (unsigned i = 0; i < s.size(); ++i)
  79. result.push_back(to_internal(s[i]));
  80. return result;
  81. }
  82. }
  83. }
  84. #endif
  85. #endif