]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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) | |
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 <boost/assert.hpp> | |
20 | #include <cstddef> // size_t | |
21 | #include <cwchar> // mbstate_t | |
22 | ||
23 | #include <boost/config.hpp> | |
24 | #if defined(BOOST_NO_STDC_NAMESPACE) | |
25 | namespace std{ | |
26 | using ::mbstate_t; | |
27 | } // namespace std | |
28 | #endif | |
29 | #include <boost/archive/detail/utf8_codecvt_facet.hpp> | |
30 | #include <boost/iterator/iterator_adaptor.hpp> | |
31 | ||
32 | namespace boost { | |
33 | namespace archive { | |
34 | namespace iterators { | |
35 | ||
36 | /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 | |
37 | // class used by text archives to translate wide strings and to char | |
38 | // strings of the currently selected locale | |
39 | template<class Base> // the input iterator | |
40 | class mb_from_wchar | |
41 | : public boost::iterator_adaptor< | |
42 | mb_from_wchar<Base>, | |
43 | Base, | |
44 | wchar_t, | |
45 | single_pass_traversal_tag, | |
46 | char | |
47 | > | |
48 | { | |
49 | friend class boost::iterator_core_access; | |
50 | ||
51 | typedef typename boost::iterator_adaptor< | |
52 | mb_from_wchar<Base>, | |
53 | Base, | |
54 | wchar_t, | |
55 | single_pass_traversal_tag, | |
56 | char | |
57 | > super_t; | |
58 | ||
59 | typedef mb_from_wchar<Base> this_t; | |
60 | ||
61 | char dereference_impl() { | |
62 | if(! m_full){ | |
63 | fill(); | |
64 | m_full = true; | |
65 | } | |
66 | return m_buffer[m_bnext]; | |
67 | } | |
68 | ||
69 | char dereference() const { | |
70 | return (const_cast<this_t *>(this))->dereference_impl(); | |
71 | } | |
72 | // test for iterator equality | |
73 | bool equal(const mb_from_wchar<Base> & rhs) const { | |
74 | // once the value is filled, the base_reference has been incremented | |
75 | // so don't permit comparison anymore. | |
76 | return | |
77 | 0 == m_bend | |
78 | && 0 == m_bnext | |
79 | && this->base_reference() == rhs.base_reference() | |
80 | ; | |
81 | } | |
82 | ||
83 | void fill(){ | |
84 | wchar_t value = * this->base_reference(); | |
85 | const wchar_t *wend; | |
86 | char *bend; | |
87 | std::codecvt_base::result r = m_codecvt_facet.out( | |
88 | m_mbs, | |
89 | & value, & value + 1, wend, | |
90 | m_buffer, m_buffer + sizeof(m_buffer), bend | |
91 | ); | |
92 | BOOST_ASSERT(std::codecvt_base::ok == r); | |
93 | m_bnext = 0; | |
94 | m_bend = bend - m_buffer; | |
95 | } | |
96 | ||
97 | void increment(){ | |
98 | if(++m_bnext < m_bend) | |
99 | return; | |
100 | m_bend = | |
101 | m_bnext = 0; | |
102 | ++(this->base_reference()); | |
103 | m_full = false; | |
104 | } | |
105 | ||
106 | boost::archive::detail::utf8_codecvt_facet m_codecvt_facet; | |
107 | std::mbstate_t m_mbs; | |
108 | // buffer to handle pending characters | |
109 | char m_buffer[9 /* MB_CUR_MAX */]; | |
110 | std::size_t m_bend; | |
111 | std::size_t m_bnext; | |
112 | bool m_full; | |
113 | ||
114 | public: | |
115 | // make composible buy using templated constructor | |
116 | template<class T> | |
117 | mb_from_wchar(T start) : | |
118 | super_t(Base(static_cast< T >(start))), | |
119 | m_mbs(std::mbstate_t()), | |
120 | m_bend(0), | |
121 | m_bnext(0), | |
122 | m_full(false) | |
123 | {} | |
124 | // intel 7.1 doesn't like default copy constructor | |
125 | mb_from_wchar(const mb_from_wchar & rhs) : | |
126 | super_t(rhs.base_reference()), | |
127 | m_bend(rhs.m_bend), | |
128 | m_bnext(rhs.m_bnext), | |
129 | m_full(rhs.m_full) | |
130 | {} | |
131 | }; | |
132 | ||
133 | } // namespace iterators | |
134 | } // namespace archive | |
135 | } // namespace boost | |
136 | ||
137 | #endif // BOOST_ARCHIVE_ITERATORS_MB_FROM_WCHAR_HPP |