Chris@16
|
1 #ifndef BOOST_ARCHIVE_ITERATORS_MB_FROM_WCHAR_HPP
|
Chris@16
|
2 #define BOOST_ARCHIVE_ITERATORS_MB_FROM_WCHAR_HPP
|
Chris@16
|
3
|
Chris@16
|
4 // MS compatible compilers support #pragma once
|
Chris@101
|
5 #if defined(_MSC_VER)
|
Chris@16
|
6 # pragma once
|
Chris@16
|
7 #endif
|
Chris@16
|
8
|
Chris@16
|
9 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
Chris@16
|
10 // mb_from_wchar.hpp
|
Chris@16
|
11
|
Chris@16
|
12 // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
|
Chris@16
|
13 // Use, modification and distribution is subject to the Boost Software
|
Chris@16
|
14 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
15 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
16
|
Chris@16
|
17 // See http://www.boost.org for updates, documentation, and revision history.
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/assert.hpp>
|
Chris@16
|
20 #include <cstddef> // size_t
|
Chris@16
|
21 #include <cstdlib> // for wctomb()
|
Chris@16
|
22
|
Chris@101
|
23 #include <boost/config.hpp>
|
Chris@16
|
24 #if defined(BOOST_NO_STDC_NAMESPACE)
|
Chris@16
|
25 namespace std{
|
Chris@16
|
26 using ::size_t;
|
Chris@16
|
27 using ::wctomb;
|
Chris@16
|
28 } // namespace std
|
Chris@16
|
29 #endif
|
Chris@16
|
30
|
Chris@16
|
31 #include <boost/serialization/pfto.hpp>
|
Chris@16
|
32 #include <boost/iterator/iterator_adaptor.hpp>
|
Chris@16
|
33
|
Chris@16
|
34 namespace boost {
|
Chris@16
|
35 namespace archive {
|
Chris@16
|
36 namespace iterators {
|
Chris@16
|
37
|
Chris@16
|
38 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
Chris@16
|
39 // class used by text archives to translate wide strings and to char
|
Chris@16
|
40 // strings of the currently selected locale
|
Chris@16
|
41 template<class Base> // the input iterator
|
Chris@16
|
42 class mb_from_wchar
|
Chris@16
|
43 : public boost::iterator_adaptor<
|
Chris@16
|
44 mb_from_wchar<Base>,
|
Chris@16
|
45 Base,
|
Chris@16
|
46 wchar_t,
|
Chris@16
|
47 single_pass_traversal_tag,
|
Chris@16
|
48 char
|
Chris@16
|
49 >
|
Chris@16
|
50 {
|
Chris@16
|
51 friend class boost::iterator_core_access;
|
Chris@16
|
52
|
Chris@101
|
53 typedef typename boost::iterator_adaptor<
|
Chris@16
|
54 mb_from_wchar<Base>,
|
Chris@16
|
55 Base,
|
Chris@16
|
56 wchar_t,
|
Chris@16
|
57 single_pass_traversal_tag,
|
Chris@16
|
58 char
|
Chris@16
|
59 > super_t;
|
Chris@16
|
60
|
Chris@16
|
61 typedef mb_from_wchar<Base> this_t;
|
Chris@16
|
62
|
Chris@16
|
63 char dereference_impl() {
|
Chris@16
|
64 if(! m_full){
|
Chris@16
|
65 fill();
|
Chris@16
|
66 m_full = true;
|
Chris@16
|
67 }
|
Chris@16
|
68 return m_buffer[m_bnext];
|
Chris@16
|
69 }
|
Chris@16
|
70 char dereference() const {
|
Chris@16
|
71 return (const_cast<this_t *>(this))->dereference_impl();
|
Chris@16
|
72 }
|
Chris@16
|
73
|
Chris@16
|
74 // test for iterator equality
|
Chris@16
|
75 bool equal(const mb_from_wchar<Base> & rhs) const {
|
Chris@16
|
76 // once the value is filled, the base_reference has been incremented
|
Chris@16
|
77 // so don't permit comparison anymore.
|
Chris@16
|
78 return
|
Chris@16
|
79 0 == m_bend
|
Chris@16
|
80 && 0 == m_bnext
|
Chris@16
|
81 && this->base_reference() == rhs.base_reference()
|
Chris@16
|
82 ;
|
Chris@16
|
83 }
|
Chris@16
|
84
|
Chris@16
|
85 void fill(){
|
Chris@16
|
86 wchar_t value = * this->base_reference();
|
Chris@16
|
87 #if (defined(__MINGW32__) && ((__MINGW32_MAJOR_VERSION > 3) \
|
Chris@16
|
88 || ((__MINGW32_MAJOR_VERSION == 3) && (__MINGW32_MINOR_VERSION >= 8))))
|
Chris@16
|
89 m_bend = std::wcrtomb(m_buffer, value, 0);
|
Chris@16
|
90 #else
|
Chris@16
|
91 m_bend = std::wctomb(m_buffer, value);
|
Chris@16
|
92 #endif
|
Chris@16
|
93 BOOST_ASSERT(-1 != m_bend);
|
Chris@16
|
94 BOOST_ASSERT((std::size_t)m_bend <= sizeof(m_buffer));
|
Chris@16
|
95 BOOST_ASSERT(m_bend > 0);
|
Chris@16
|
96 m_bnext = 0;
|
Chris@16
|
97 }
|
Chris@16
|
98
|
Chris@16
|
99 void increment(){
|
Chris@16
|
100 if(++m_bnext < m_bend)
|
Chris@16
|
101 return;
|
Chris@16
|
102 m_bend =
|
Chris@16
|
103 m_bnext = 0;
|
Chris@16
|
104 ++(this->base_reference());
|
Chris@16
|
105 m_full = false;
|
Chris@16
|
106 }
|
Chris@16
|
107
|
Chris@16
|
108 // buffer to handle pending characters
|
Chris@16
|
109 int m_bend;
|
Chris@16
|
110 int m_bnext;
|
Chris@16
|
111 char m_buffer[9];
|
Chris@16
|
112 bool m_full;
|
Chris@16
|
113
|
Chris@16
|
114 public:
|
Chris@16
|
115 // make composible buy using templated constructor
|
Chris@16
|
116 template<class T>
|
Chris@16
|
117 mb_from_wchar(BOOST_PFTO_WRAPPER(T) start) :
|
Chris@16
|
118 super_t(Base(BOOST_MAKE_PFTO_WRAPPER(static_cast< T >(start)))),
|
Chris@16
|
119 m_bend(0),
|
Chris@16
|
120 m_bnext(0),
|
Chris@16
|
121 m_full(false)
|
Chris@16
|
122 {}
|
Chris@16
|
123 // intel 7.1 doesn't like default copy constructor
|
Chris@16
|
124 mb_from_wchar(const mb_from_wchar & rhs) :
|
Chris@16
|
125 super_t(rhs.base_reference()),
|
Chris@16
|
126 m_bend(rhs.m_bend),
|
Chris@16
|
127 m_bnext(rhs.m_bnext),
|
Chris@16
|
128 m_full(rhs.m_full)
|
Chris@16
|
129 {}
|
Chris@16
|
130 };
|
Chris@16
|
131
|
Chris@16
|
132 } // namespace iterators
|
Chris@16
|
133 } // namespace archive
|
Chris@16
|
134 } // namespace boost
|
Chris@16
|
135
|
Chris@16
|
136 #endif // BOOST_ARCHIVE_ITERATORS_MB_FROM_WCHAR_HPP
|