Chris@16
|
1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
|
Chris@16
|
2 // (C) Copyright 2005-2007 Jonathan Turkanis
|
Chris@16
|
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
|
Chris@16
|
5
|
Chris@16
|
6 // See http://www.boost.org/libs/iostreams for documentation.
|
Chris@16
|
7
|
Chris@16
|
8 #ifndef BOOST_IOSTREAMS_READ_HPP_INCLUDED
|
Chris@16
|
9 #define BOOST_IOSTREAMS_READ_HPP_INCLUDED
|
Chris@16
|
10
|
Chris@16
|
11 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
Chris@16
|
12 # pragma once
|
Chris@16
|
13 #endif
|
Chris@16
|
14
|
Chris@16
|
15 #include <boost/config.hpp> // DEDUCED_TYPENAME, MSVC.
|
Chris@16
|
16 #include <boost/detail/workaround.hpp>
|
Chris@16
|
17 #include <boost/iostreams/char_traits.hpp>
|
Chris@16
|
18 #include <boost/iostreams/detail/char_traits.hpp>
|
Chris@16
|
19 #include <boost/iostreams/detail/dispatch.hpp>
|
Chris@16
|
20 #include <boost/iostreams/detail/ios.hpp> // streamsize.
|
Chris@16
|
21 #include <boost/iostreams/detail/streambuf.hpp>
|
Chris@16
|
22 #include <boost/iostreams/detail/wrap_unwrap.hpp>
|
Chris@16
|
23 #include <boost/iostreams/operations_fwd.hpp>
|
Chris@16
|
24 #include <boost/mpl/if.hpp>
|
Chris@16
|
25
|
Chris@16
|
26 // Must come last.
|
Chris@16
|
27 #include <boost/iostreams/detail/config/disable_warnings.hpp>
|
Chris@16
|
28
|
Chris@16
|
29 #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) //-----------------------------------//
|
Chris@16
|
30 # include <boost/iostreams/detail/vc6/read.hpp>
|
Chris@16
|
31 #else // #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) //--------------------------//
|
Chris@16
|
32
|
Chris@16
|
33 namespace boost { namespace iostreams {
|
Chris@16
|
34
|
Chris@16
|
35 namespace detail {
|
Chris@16
|
36
|
Chris@16
|
37 template<typename T>
|
Chris@16
|
38 struct read_device_impl;
|
Chris@16
|
39
|
Chris@16
|
40 template<typename T>
|
Chris@16
|
41 struct read_filter_impl;
|
Chris@16
|
42
|
Chris@16
|
43 } // End namespace detail.
|
Chris@16
|
44
|
Chris@16
|
45 template<typename T>
|
Chris@16
|
46 typename int_type_of<T>::type get(T& t)
|
Chris@16
|
47 { return detail::read_device_impl<T>::get(detail::unwrap(t)); }
|
Chris@16
|
48
|
Chris@16
|
49 template<typename T>
|
Chris@16
|
50 inline std::streamsize
|
Chris@16
|
51 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
|
Chris@16
|
52 { return detail::read_device_impl<T>::read(detail::unwrap(t), s, n); }
|
Chris@16
|
53
|
Chris@16
|
54 template<typename T, typename Source>
|
Chris@16
|
55 std::streamsize
|
Chris@16
|
56 read(T& t, Source& src, typename char_type_of<T>::type* s, std::streamsize n)
|
Chris@16
|
57 { return detail::read_filter_impl<T>::read(detail::unwrap(t), src, s, n); }
|
Chris@16
|
58
|
Chris@16
|
59 template<typename T>
|
Chris@16
|
60 bool putback(T& t, typename char_type_of<T>::type c)
|
Chris@16
|
61 { return detail::read_device_impl<T>::putback(detail::unwrap(t), c); }
|
Chris@16
|
62
|
Chris@16
|
63 //----------------------------------------------------------------------------//
|
Chris@16
|
64
|
Chris@16
|
65 namespace detail {
|
Chris@16
|
66
|
Chris@16
|
67 // Helper function for adding -1 as EOF indicator.
|
Chris@16
|
68 inline std::streamsize check_eof(std::streamsize n) { return n != 0 ? n : -1; }
|
Chris@16
|
69
|
Chris@16
|
70 // Helper templates for reading from streambufs.
|
Chris@16
|
71 template<bool IsLinked>
|
Chris@16
|
72 struct true_eof_impl;
|
Chris@16
|
73
|
Chris@16
|
74 template<>
|
Chris@16
|
75 struct true_eof_impl<true> {
|
Chris@16
|
76 template<typename T>
|
Chris@16
|
77 static bool true_eof(T& t) { return t.true_eof(); }
|
Chris@16
|
78 };
|
Chris@16
|
79
|
Chris@16
|
80 template<>
|
Chris@16
|
81 struct true_eof_impl<false> {
|
Chris@16
|
82 template<typename T>
|
Chris@16
|
83 static bool true_eof(T&) { return true; }
|
Chris@16
|
84 };
|
Chris@16
|
85
|
Chris@16
|
86 template<typename T>
|
Chris@16
|
87 inline bool true_eof(T& t)
|
Chris@16
|
88 {
|
Chris@16
|
89 const bool linked = is_linked<T>::value;
|
Chris@16
|
90 return true_eof_impl<linked>::true_eof(t);
|
Chris@16
|
91 }
|
Chris@16
|
92
|
Chris@16
|
93 //------------------Definition of read_device_impl----------------------------//
|
Chris@16
|
94
|
Chris@16
|
95 template<typename T>
|
Chris@16
|
96 struct read_device_impl
|
Chris@16
|
97 : mpl::if_<
|
Chris@16
|
98 detail::is_custom<T>,
|
Chris@16
|
99 operations<T>,
|
Chris@16
|
100 read_device_impl<
|
Chris@16
|
101 BOOST_DEDUCED_TYPENAME
|
Chris@16
|
102 detail::dispatch<
|
Chris@16
|
103 T, istream_tag, streambuf_tag, input
|
Chris@16
|
104 >::type
|
Chris@16
|
105 >
|
Chris@16
|
106 >::type
|
Chris@16
|
107 { };
|
Chris@16
|
108
|
Chris@16
|
109 template<>
|
Chris@16
|
110 struct read_device_impl<istream_tag> {
|
Chris@16
|
111 template<typename T>
|
Chris@16
|
112 static typename int_type_of<T>::type get(T& t)
|
Chris@16
|
113 { return t.get(); }
|
Chris@16
|
114
|
Chris@16
|
115 template<typename T>
|
Chris@16
|
116 static std::streamsize
|
Chris@16
|
117 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
|
Chris@16
|
118 { return check_eof(t.rdbuf()->sgetn(s, n)); }
|
Chris@16
|
119
|
Chris@16
|
120 template<typename T>
|
Chris@16
|
121 static bool putback(T& t, typename char_type_of<T>::type c)
|
Chris@16
|
122 {
|
Chris@16
|
123 typedef typename char_type_of<T>::type char_type;
|
Chris@16
|
124 typedef BOOST_IOSTREAMS_CHAR_TRAITS(char_type) traits_type;
|
Chris@16
|
125 return !traits_type::eq_int_type( t.rdbuf()->sputbackc(c),
|
Chris@16
|
126 traits_type::eof() );
|
Chris@16
|
127 }
|
Chris@16
|
128 };
|
Chris@16
|
129
|
Chris@16
|
130 template<>
|
Chris@16
|
131 struct read_device_impl<streambuf_tag> {
|
Chris@16
|
132 template<typename T>
|
Chris@16
|
133 static typename int_type_of<T>::type
|
Chris@16
|
134 get(T& t)
|
Chris@16
|
135 { // gcc 2.95 needs namespace qualification for char_traits.
|
Chris@16
|
136 typedef typename char_type_of<T>::type char_type;
|
Chris@16
|
137 typedef iostreams::char_traits<char_type> traits_type;
|
Chris@16
|
138 typename int_type_of<T>::type c;
|
Chris@16
|
139 return !traits_type::is_eof(c = t.sbumpc()) ||
|
Chris@16
|
140 detail::true_eof(t)
|
Chris@16
|
141 ?
|
Chris@16
|
142 c : traits_type::would_block();
|
Chris@16
|
143 }
|
Chris@16
|
144
|
Chris@16
|
145 template<typename T>
|
Chris@16
|
146 static std::streamsize
|
Chris@16
|
147 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
|
Chris@16
|
148 {
|
Chris@16
|
149 std::streamsize amt;
|
Chris@16
|
150 return (amt = t.sgetn(s, n)) != 0 ?
|
Chris@16
|
151 amt :
|
Chris@16
|
152 detail::true_eof(t) ?
|
Chris@16
|
153 -1 :
|
Chris@16
|
154 0;
|
Chris@16
|
155 }
|
Chris@16
|
156
|
Chris@16
|
157 template<typename T>
|
Chris@16
|
158 static bool putback(T& t, typename char_type_of<T>::type c)
|
Chris@16
|
159 { // gcc 2.95 needs namespace qualification for char_traits.
|
Chris@16
|
160 typedef typename char_type_of<T>::type char_type;
|
Chris@16
|
161 typedef iostreams::char_traits<char_type> traits_type;
|
Chris@16
|
162 return !traits_type::is_eof(t.sputbackc(c));
|
Chris@16
|
163 }
|
Chris@16
|
164 };
|
Chris@16
|
165
|
Chris@16
|
166 template<>
|
Chris@16
|
167 struct read_device_impl<input> {
|
Chris@16
|
168 template<typename T>
|
Chris@16
|
169 static typename int_type_of<T>::type
|
Chris@16
|
170 get(T& t)
|
Chris@16
|
171 { // gcc 2.95 needs namespace qualification for char_traits.
|
Chris@16
|
172 typedef typename char_type_of<T>::type char_type;
|
Chris@16
|
173 typedef iostreams::char_traits<char_type> traits_type;
|
Chris@16
|
174 char_type c;
|
Chris@16
|
175 std::streamsize amt;
|
Chris@16
|
176 return (amt = t.read(&c, 1)) == 1 ?
|
Chris@16
|
177 traits_type::to_int_type(c) :
|
Chris@16
|
178 amt == -1 ?
|
Chris@16
|
179 traits_type::eof() :
|
Chris@16
|
180 traits_type::would_block();
|
Chris@16
|
181 }
|
Chris@16
|
182
|
Chris@16
|
183 template<typename T>
|
Chris@16
|
184 static std::streamsize
|
Chris@16
|
185 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
|
Chris@16
|
186 { return t.read(s, n); }
|
Chris@16
|
187
|
Chris@16
|
188 template<typename T>
|
Chris@16
|
189 static bool putback(T& t, typename char_type_of<T>::type c)
|
Chris@16
|
190 { // T must be Peekable.
|
Chris@16
|
191 return t.putback(c);
|
Chris@16
|
192 }
|
Chris@16
|
193 };
|
Chris@16
|
194
|
Chris@16
|
195 //------------------Definition of read_filter_impl----------------------------//
|
Chris@16
|
196
|
Chris@16
|
197 template<typename T>
|
Chris@16
|
198 struct read_filter_impl
|
Chris@16
|
199 : mpl::if_<
|
Chris@16
|
200 detail::is_custom<T>,
|
Chris@16
|
201 operations<T>,
|
Chris@16
|
202 read_filter_impl<
|
Chris@16
|
203 BOOST_DEDUCED_TYPENAME
|
Chris@16
|
204 detail::dispatch<
|
Chris@16
|
205 T, multichar_tag, any_tag
|
Chris@16
|
206 >::type
|
Chris@16
|
207 >
|
Chris@16
|
208 >::type
|
Chris@16
|
209 { };
|
Chris@16
|
210
|
Chris@16
|
211 template<>
|
Chris@16
|
212 struct read_filter_impl<multichar_tag> {
|
Chris@16
|
213 template<typename T, typename Source>
|
Chris@16
|
214 static std::streamsize read
|
Chris@16
|
215 (T& t, Source& src, typename char_type_of<T>::type* s, std::streamsize n)
|
Chris@16
|
216 { return t.read(src, s, n); }
|
Chris@16
|
217 };
|
Chris@16
|
218
|
Chris@16
|
219 template<>
|
Chris@16
|
220 struct read_filter_impl<any_tag> {
|
Chris@16
|
221 template<typename T, typename Source>
|
Chris@16
|
222 static std::streamsize read
|
Chris@16
|
223 (T& t, Source& src, typename char_type_of<T>::type* s, std::streamsize n)
|
Chris@16
|
224 { // gcc 2.95 needs namespace qualification for char_traits.
|
Chris@16
|
225 typedef typename char_type_of<T>::type char_type;
|
Chris@16
|
226 typedef iostreams::char_traits<char_type> traits_type;
|
Chris@16
|
227 for (std::streamsize off = 0; off < n; ++off) {
|
Chris@16
|
228 typename traits_type::int_type c = t.get(src);
|
Chris@16
|
229 if (traits_type::is_eof(c))
|
Chris@16
|
230 return check_eof(off);
|
Chris@16
|
231 if (traits_type::would_block(c))
|
Chris@16
|
232 return off;
|
Chris@16
|
233 s[off] = traits_type::to_char_type(c);
|
Chris@16
|
234 }
|
Chris@16
|
235 return n;
|
Chris@16
|
236 }
|
Chris@16
|
237 };
|
Chris@16
|
238
|
Chris@16
|
239 } // End namespace detail.
|
Chris@16
|
240
|
Chris@16
|
241 } } // End namespaces iostreams, boost.
|
Chris@16
|
242
|
Chris@16
|
243 #endif // #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) //-------------------------//
|
Chris@16
|
244
|
Chris@16
|
245 #include <boost/iostreams/detail/config/enable_warnings.hpp>
|
Chris@16
|
246
|
Chris@16
|
247 #endif // #ifndef BOOST_IOSTREAMS_READ_HPP_INCLUDED
|