wchar_from_mb.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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)
  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 <boost/assert.hpp>
  15. #include <cctype>
  16. #include <cstddef> // size_t
  17. #ifndef BOOST_NO_CWCHAR
  18. #include <cwchar> // mbstate_t
  19. #endif
  20. #include <algorithm> // copy
  21. #include <boost/config.hpp>
  22. #if defined(BOOST_NO_STDC_NAMESPACE)
  23. namespace std{
  24. using ::mbstate_t;
  25. } // namespace std
  26. #endif
  27. #include <boost/array.hpp>
  28. #include <boost/iterator/iterator_adaptor.hpp>
  29. #include <boost/archive/detail/utf8_codecvt_facet.hpp>
  30. #include <boost/archive/iterators/dataflow_exception.hpp>
  31. #include <boost/serialization/throw_exception.hpp>
  32. #include <iostream>
  33. namespace boost {
  34. namespace archive {
  35. namespace iterators {
  36. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  37. // class used by text archives to translate char strings to wchar_t
  38. // strings of the currently selected locale
  39. template<class Base>
  40. class wchar_from_mb
  41. : public boost::iterator_adaptor<
  42. wchar_from_mb<Base>,
  43. Base,
  44. wchar_t,
  45. single_pass_traversal_tag,
  46. wchar_t
  47. >
  48. {
  49. friend class boost::iterator_core_access;
  50. typedef typename boost::iterator_adaptor<
  51. wchar_from_mb<Base>,
  52. Base,
  53. wchar_t,
  54. single_pass_traversal_tag,
  55. wchar_t
  56. > super_t;
  57. typedef wchar_from_mb<Base> this_t;
  58. void drain();
  59. wchar_t dereference() const {
  60. if(m_output.m_next == m_output.m_next_available)
  61. return static_cast<wchar_t>(0);
  62. return * m_output.m_next;
  63. }
  64. void increment(){
  65. if(m_output.m_next == m_output.m_next_available)
  66. return;
  67. if(++m_output.m_next == m_output.m_next_available){
  68. if(m_input.m_done)
  69. return;
  70. drain();
  71. }
  72. }
  73. bool equal(this_t const & rhs) const {
  74. return dereference() == rhs.dereference();
  75. }
  76. boost::archive::detail::utf8_codecvt_facet m_codecvt_facet;
  77. std::mbstate_t m_mbs;
  78. template<typename T>
  79. struct sliding_buffer {
  80. boost::array<T, 32> m_buffer;
  81. typename boost::array<T, 32>::const_iterator m_next_available;
  82. typename boost::array<T, 32>::iterator m_next;
  83. bool m_done;
  84. // default ctor
  85. sliding_buffer() :
  86. m_next_available(m_buffer.begin()),
  87. m_next(m_buffer.begin()),
  88. m_done(false)
  89. {}
  90. // copy ctor
  91. sliding_buffer(const sliding_buffer & rhs) :
  92. m_next_available(
  93. std::copy(
  94. rhs.m_buffer.begin(),
  95. rhs.m_next_available,
  96. m_buffer.begin()
  97. )
  98. ),
  99. m_next(
  100. m_buffer.begin() + (rhs.m_next - rhs.m_buffer.begin())
  101. ),
  102. m_done(rhs.m_done)
  103. {}
  104. };
  105. sliding_buffer<typename iterator_value<Base>::type> m_input;
  106. sliding_buffer<typename iterator_value<this_t>::type> m_output;
  107. public:
  108. // make composible buy using templated constructor
  109. template<class T>
  110. wchar_from_mb(T start) :
  111. super_t(Base(static_cast< T >(start))),
  112. m_mbs(std::mbstate_t())
  113. {
  114. BOOST_ASSERT(std::mbsinit(&m_mbs));
  115. drain();
  116. }
  117. // default constructor used as an end iterator
  118. wchar_from_mb(){}
  119. // copy ctor
  120. wchar_from_mb(const wchar_from_mb & rhs) :
  121. super_t(rhs.base_reference()),
  122. m_mbs(rhs.m_mbs),
  123. m_input(rhs.m_input),
  124. m_output(rhs.m_output)
  125. {}
  126. };
  127. template<class Base>
  128. void wchar_from_mb<Base>::drain(){
  129. BOOST_ASSERT(! m_input.m_done);
  130. for(;;){
  131. typename boost::iterators::iterator_reference<Base>::type c = *(this->base_reference());
  132. // a null character in a multibyte stream is takes as end of string
  133. if(0 == c){
  134. m_input.m_done = true;
  135. break;
  136. }
  137. ++(this->base_reference());
  138. * const_cast<typename iterator_value<Base>::type *>(
  139. (m_input.m_next_available++)
  140. ) = c;
  141. // if input buffer is full - we're done for now
  142. if(m_input.m_buffer.end() == m_input.m_next_available)
  143. break;
  144. }
  145. const typename boost::iterators::iterator_value<Base>::type * input_new_start;
  146. typename iterator_value<this_t>::type * next_available;
  147. std::codecvt_base::result r = m_codecvt_facet.in(
  148. m_mbs,
  149. m_input.m_buffer.begin(),
  150. m_input.m_next_available,
  151. input_new_start,
  152. m_output.m_buffer.begin(),
  153. m_output.m_buffer.end(),
  154. next_available
  155. );
  156. BOOST_ASSERT(std::codecvt_base::ok == r);
  157. m_output.m_next_available = next_available;
  158. m_output.m_next = m_output.m_buffer.begin();
  159. // we're done with some of the input so shift left.
  160. m_input.m_next_available = std::copy(
  161. input_new_start,
  162. m_input.m_next_available,
  163. m_input.m_buffer.begin()
  164. );
  165. m_input.m_next = m_input.m_buffer.begin();
  166. }
  167. } // namespace iterators
  168. } // namespace archive
  169. } // namespace boost
  170. #endif // BOOST_ARCHIVE_ITERATORS_WCHAR_FROM_MB_HPP