Chris@16
|
1 #ifndef BOOST_ARCHIVE_ITERATORS_WCHAR_FROM_MB_HPP
|
Chris@16
|
2 #define BOOST_ARCHIVE_ITERATORS_WCHAR_FROM_MB_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 // wchar_from_mb.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 <cctype>
|
Chris@16
|
21 #include <cstddef> // size_t
|
Chris@16
|
22 #include <cstdlib> // mblen
|
Chris@16
|
23
|
Chris@101
|
24 #include <boost/config.hpp>
|
Chris@16
|
25 #if defined(BOOST_NO_STDC_NAMESPACE)
|
Chris@16
|
26 namespace std{
|
Chris@16
|
27 using ::mblen;
|
Chris@16
|
28 using ::mbtowc;
|
Chris@16
|
29 } // namespace std
|
Chris@16
|
30 #endif
|
Chris@16
|
31
|
Chris@16
|
32 #include <boost/serialization/throw_exception.hpp>
|
Chris@16
|
33 #include <boost/serialization/pfto.hpp>
|
Chris@16
|
34
|
Chris@16
|
35 #include <boost/iterator/iterator_adaptor.hpp>
|
Chris@16
|
36 #include <boost/archive/iterators/dataflow_exception.hpp>
|
Chris@16
|
37
|
Chris@16
|
38 namespace boost {
|
Chris@16
|
39 namespace archive {
|
Chris@16
|
40 namespace iterators {
|
Chris@16
|
41
|
Chris@16
|
42 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
|
Chris@16
|
43 // class used by text archives to translate char strings to wchar_t
|
Chris@16
|
44 // strings of the currently selected locale
|
Chris@16
|
45 template<class Base>
|
Chris@16
|
46 class wchar_from_mb
|
Chris@16
|
47 : public boost::iterator_adaptor<
|
Chris@16
|
48 wchar_from_mb<Base>,
|
Chris@16
|
49 Base,
|
Chris@16
|
50 wchar_t,
|
Chris@16
|
51 single_pass_traversal_tag,
|
Chris@16
|
52 wchar_t
|
Chris@16
|
53 >
|
Chris@16
|
54 {
|
Chris@16
|
55 friend class boost::iterator_core_access;
|
Chris@101
|
56 typedef typename boost::iterator_adaptor<
|
Chris@16
|
57 wchar_from_mb<Base>,
|
Chris@16
|
58 Base,
|
Chris@16
|
59 wchar_t,
|
Chris@16
|
60 single_pass_traversal_tag,
|
Chris@16
|
61 wchar_t
|
Chris@16
|
62 > super_t;
|
Chris@16
|
63
|
Chris@16
|
64 typedef wchar_from_mb<Base> this_t;
|
Chris@16
|
65
|
Chris@16
|
66 wchar_t drain();
|
Chris@16
|
67
|
Chris@16
|
68 wchar_t dereference_impl() {
|
Chris@16
|
69 if(! m_full){
|
Chris@16
|
70 m_current_value = drain();
|
Chris@16
|
71 m_full = true;
|
Chris@16
|
72 }
|
Chris@16
|
73 return m_current_value;
|
Chris@16
|
74 }
|
Chris@16
|
75
|
Chris@16
|
76 wchar_t dereference() const {
|
Chris@16
|
77 return const_cast<this_t *>(this)->dereference_impl();
|
Chris@16
|
78 }
|
Chris@16
|
79
|
Chris@16
|
80 void increment(){
|
Chris@16
|
81 dereference_impl();
|
Chris@16
|
82 m_full = false;
|
Chris@16
|
83 ++(this->base_reference());
|
Chris@16
|
84 };
|
Chris@16
|
85
|
Chris@16
|
86 wchar_t m_current_value;
|
Chris@16
|
87 bool m_full;
|
Chris@16
|
88
|
Chris@16
|
89 public:
|
Chris@16
|
90 // make composible buy using templated constructor
|
Chris@16
|
91 template<class T>
|
Chris@16
|
92 wchar_from_mb(BOOST_PFTO_WRAPPER(T) start) :
|
Chris@16
|
93 super_t(Base(BOOST_MAKE_PFTO_WRAPPER(static_cast< T >(start)))),
|
Chris@16
|
94 m_full(false)
|
Chris@16
|
95 {}
|
Chris@16
|
96 // intel 7.1 doesn't like default copy constructor
|
Chris@16
|
97 wchar_from_mb(const wchar_from_mb & rhs) :
|
Chris@16
|
98 super_t(rhs.base_reference()),
|
Chris@16
|
99 m_full(rhs.m_full)
|
Chris@16
|
100 {}
|
Chris@16
|
101 };
|
Chris@16
|
102
|
Chris@16
|
103 template<class Base>
|
Chris@16
|
104 wchar_t wchar_from_mb<Base>::drain(){
|
Chris@16
|
105 char buffer[9];
|
Chris@16
|
106 char * bptr = buffer;
|
Chris@16
|
107 char val;
|
Chris@16
|
108 for(std::size_t i = 0; i++ < (unsigned)MB_CUR_MAX;){
|
Chris@16
|
109 val = * this->base_reference();
|
Chris@16
|
110 *bptr++ = val;
|
Chris@16
|
111 int result = std::mblen(buffer, i);
|
Chris@16
|
112 if(-1 != result)
|
Chris@16
|
113 break;
|
Chris@16
|
114 ++(this->base_reference());
|
Chris@16
|
115 }
|
Chris@16
|
116 wchar_t retval;
|
Chris@16
|
117 int result = std::mbtowc(& retval, buffer, MB_CUR_MAX);
|
Chris@16
|
118 if(0 >= result)
|
Chris@16
|
119 boost::serialization::throw_exception(iterators::dataflow_exception(
|
Chris@16
|
120 iterators::dataflow_exception::invalid_conversion
|
Chris@16
|
121 ));
|
Chris@16
|
122 return retval;
|
Chris@16
|
123 }
|
Chris@16
|
124
|
Chris@16
|
125 } // namespace iterators
|
Chris@16
|
126 } // namespace archive
|
Chris@16
|
127 } // namespace boost
|
Chris@16
|
128
|
Chris@16
|
129 #endif // BOOST_ARCHIVE_ITERATORS_WCHAR_FROM_MB_HPP
|