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