test_codecvt.hpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // test_codecvt.hpp ------------------------------------------------------------------//
  2. // Copyright Beman Dawes 2009, 2010
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See http://www.boost.org/LICENSE_1_0.txt
  5. // Library home page: http://www.boost.org/libs/filesystem
  6. #ifndef BOOST_FILESYSTEM3_TEST_CODECVT_HPP
  7. #define BOOST_FILESYSTEM3_TEST_CODECVT_HPP
  8. #include <boost/filesystem/config.hpp>
  9. #include <locale>
  10. #include <cwchar> // for mbstate_t
  11. //------------------------------------------------------------------------------------//
  12. // //
  13. // class test_codecvt //
  14. // //
  15. // Warning: partial implementation; even do_in and do_out only partially meet the //
  16. // standard library specifications as the "to" buffer must hold the entire result. //
  17. // //
  18. // The value of a wide character is the value of the corresponding narrow character //
  19. // plus 1. This ensures that compares against expected values will fail if the //
  20. // code conversion did not occur as expected. //
  21. // //
  22. //------------------------------------------------------------------------------------//
  23. class test_codecvt
  24. : public std::codecvt< wchar_t, char, std::mbstate_t >
  25. {
  26. public:
  27. explicit test_codecvt()
  28. : std::codecvt<wchar_t, char, std::mbstate_t>() {}
  29. protected:
  30. virtual bool do_always_noconv() const throw() { return false; }
  31. virtual int do_encoding() const throw() { return 0; }
  32. virtual std::codecvt_base::result do_in(std::mbstate_t&,
  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
  35. {
  36. for (; from != from_end && to != to_end; ++from, ++to)
  37. *to = wchar_t(*from + 1);
  38. if (to == to_end)
  39. return error;
  40. *to = L'\0';
  41. from_next = from;
  42. to_next = to;
  43. return ok;
  44. }
  45. virtual std::codecvt_base::result do_out(std::mbstate_t&,
  46. const wchar_t* from, const wchar_t* from_end, const wchar_t*& from_next,
  47. char* to, char* to_end, char*& to_next) const
  48. {
  49. for (; from != from_end && to != to_end; ++from, ++to)
  50. *to = static_cast<char>(*from - 1);
  51. if (to == to_end)
  52. return error;
  53. *to = '\0';
  54. from_next = from;
  55. to_next = to;
  56. return ok;
  57. }
  58. virtual std::codecvt_base::result do_unshift(std::mbstate_t&,
  59. char* /*from*/, char* /*to*/, char* & /*next*/) const { return ok; }
  60. virtual int do_length(std::mbstate_t&,
  61. const char* /*from*/, const char* /*from_end*/, std::size_t /*max*/) const { return 0; }
  62. virtual int do_max_length() const throw () { return 0; }
  63. };
  64. #endif // BOOST_FILESYSTEM3_TEST_CODECVT_HPP