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