wchar_from_mb.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #ifndef BOOST_ARCHIVE_ITERATORS_WCHAR_FROM_MB_HPP
  2. #define BOOST_ARCHIVE_ITERATORS_WCHAR_FROM_MB_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // wchar_from_mb.hpp
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // Use, modification and distribution is subject to the Boost Software
  11. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // See http://www.boost.org for updates, documentation, and revision history.
  14. #include <cassert>
  15. #include <cctype>
  16. #include <cstddef> // size_t
  17. #include <cstdlib> // mblen
  18. #include <boost/config.hpp> // for BOOST_DEDUCED_TYPENAME
  19. #if defined(BOOST_NO_STDC_NAMESPACE)
  20. namespace std{
  21. using ::mblen;
  22. using ::mbtowc;
  23. } // namespace std
  24. #endif
  25. #include <boost/serialization/throw_exception.hpp>
  26. #include <boost/serialization/pfto.hpp>
  27. #include <boost/iterator/iterator_adaptor.hpp>
  28. #include <boost/archive/iterators/dataflow_exception.hpp>
  29. namespace boost {
  30. namespace archive {
  31. namespace iterators {
  32. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  33. // class used by text archives to translate char strings to wchar_t
  34. // strings of the currently selected locale
  35. template<class Base>
  36. class wchar_from_mb
  37. : public boost::iterator_adaptor<
  38. wchar_from_mb<Base>,
  39. Base,
  40. wchar_t,
  41. single_pass_traversal_tag,
  42. wchar_t
  43. >
  44. {
  45. friend class boost::iterator_core_access;
  46. typedef BOOST_DEDUCED_TYPENAME boost::iterator_adaptor<
  47. wchar_from_mb<Base>,
  48. Base,
  49. wchar_t,
  50. single_pass_traversal_tag,
  51. wchar_t
  52. > super_t;
  53. typedef wchar_from_mb<Base> this_t;
  54. wchar_t drain();
  55. wchar_t dereference_impl() {
  56. if(! m_full){
  57. m_current_value = drain();
  58. m_full = true;
  59. }
  60. return m_current_value;
  61. }
  62. wchar_t dereference() const {
  63. return const_cast<this_t *>(this)->dereference_impl();
  64. }
  65. void increment(){
  66. dereference_impl();
  67. m_full = false;
  68. ++(this->base_reference());
  69. };
  70. wchar_t m_current_value;
  71. bool m_full;
  72. public:
  73. // make composible buy using templated constructor
  74. template<class T>
  75. wchar_from_mb(BOOST_PFTO_WRAPPER(T) start) :
  76. super_t(Base(BOOST_MAKE_PFTO_WRAPPER(static_cast<T>(start)))),
  77. m_full(false)
  78. {}
  79. // intel 7.1 doesn't like default copy constructor
  80. wchar_from_mb(const wchar_from_mb & rhs) :
  81. super_t(rhs.base_reference()),
  82. m_full(rhs.m_full)
  83. {}
  84. };
  85. template<class Base>
  86. wchar_t wchar_from_mb<Base>::drain(){
  87. char buffer[9];
  88. char * bptr = buffer;
  89. char val;
  90. for(std::size_t i = 0; i++ < (unsigned)MB_CUR_MAX;){
  91. val = * this->base_reference();
  92. *bptr++ = val;
  93. int result = std::mblen(buffer, i);
  94. if(-1 != result)
  95. break;
  96. ++(this->base_reference());
  97. }
  98. wchar_t retval;
  99. int result = std::mbtowc(& retval, buffer, MB_CUR_MAX);
  100. if(0 >= result)
  101. boost::serialization::throw_exception(iterators::dataflow_exception(
  102. iterators::dataflow_exception::invalid_conversion
  103. ));
  104. return retval;
  105. }
  106. } // namespace iterators
  107. } // namespace archive
  108. } // namespace boost
  109. #endif // BOOST_ARCHIVE_ITERATORS_WCHAR_FROM_MB_HPP