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)
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 namespace boost { namespace iostreams {
30
31 namespace detail {
32
33 template<typename T>
34 struct read_device_impl;
35
36 template<typename T>
37 struct read_filter_impl;
38
39 } // End namespace detail.
40
41 template<typename T>
get(T & t)42 typename int_type_of<T>::type get(T& t)
43 { return detail::read_device_impl<T>::get(detail::unwrap(t)); }
44
45 template<typename T>
46 inline std::streamsize
read(T & t,typename char_type_of<T>::type * s,std::streamsize n)47 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
48 { return detail::read_device_impl<T>::read(detail::unwrap(t), s, n); }
49
50 template<typename T, typename Source>
51 std::streamsize
read(T & t,Source & src,typename char_type_of<T>::type * s,std::streamsize n)52 read(T& t, Source& src, typename char_type_of<T>::type* s, std::streamsize n)
53 { return detail::read_filter_impl<T>::read(detail::unwrap(t), src, s, n); }
54
55 template<typename T>
putback(T & t,typename char_type_of<T>::type c)56 bool putback(T& t, typename char_type_of<T>::type c)
57 { return detail::read_device_impl<T>::putback(detail::unwrap(t), c); }
58
59 //----------------------------------------------------------------------------//
60
61 namespace detail {
62
63 // Helper function for adding -1 as EOF indicator.
check_eof(std::streamsize n)64 inline std::streamsize check_eof(std::streamsize n) { return n != 0 ? n : -1; }
65
66 // Helper templates for reading from streambufs.
67 template<bool IsLinked>
68 struct true_eof_impl;
69
70 template<>
71 struct true_eof_impl<true> {
72 template<typename T>
true_eofboost::iostreams::detail::true_eof_impl73 static bool true_eof(T& t) { return t.true_eof(); }
74 };
75
76 template<>
77 struct true_eof_impl<false> {
78 template<typename T>
true_eofboost::iostreams::detail::true_eof_impl79 static bool true_eof(T&) { return true; }
80 };
81
82 template<typename T>
true_eof(T & t)83 inline bool true_eof(T& t)
84 {
85 const bool linked = is_linked<T>::value;
86 return true_eof_impl<linked>::true_eof(t);
87 }
88
89 //------------------Definition of read_device_impl----------------------------//
90
91 template<typename T>
92 struct read_device_impl
93 : mpl::if_<
94 detail::is_custom<T>,
95 operations<T>,
96 read_device_impl<
97 BOOST_DEDUCED_TYPENAME
98 detail::dispatch<
99 T, istream_tag, streambuf_tag, input
100 >::type
101 >
102 >::type
103 { };
104
105 template<>
106 struct read_device_impl<istream_tag> {
107 template<typename T>
getboost::iostreams::detail::read_device_impl108 static typename int_type_of<T>::type get(T& t)
109 { return t.get(); }
110
111 template<typename T>
112 static std::streamsize
readboost::iostreams::detail::read_device_impl113 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
114 { return check_eof(t.rdbuf()->sgetn(s, n)); }
115
116 template<typename T>
putbackboost::iostreams::detail::read_device_impl117 static bool putback(T& t, typename char_type_of<T>::type c)
118 {
119 typedef typename char_type_of<T>::type char_type;
120 typedef BOOST_IOSTREAMS_CHAR_TRAITS(char_type) traits_type;
121 return !traits_type::eq_int_type( t.rdbuf()->sputbackc(c),
122 traits_type::eof() );
123 }
124 };
125
126 template<>
127 struct read_device_impl<streambuf_tag> {
128 template<typename T>
129 static typename int_type_of<T>::type
getboost::iostreams::detail::read_device_impl130 get(T& t)
131 { // gcc 2.95 needs namespace qualification for char_traits.
132 typedef typename char_type_of<T>::type char_type;
133 typedef iostreams::char_traits<char_type> traits_type;
134 typename int_type_of<T>::type c;
135 return !traits_type::is_eof(c = t.sbumpc()) ||
136 detail::true_eof(t)
137 ?
138 c : traits_type::would_block();
139 }
140
141 template<typename T>
142 static std::streamsize
readboost::iostreams::detail::read_device_impl143 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
144 {
145 std::streamsize amt;
146 return (amt = t.sgetn(s, n)) != 0 ?
147 amt :
148 detail::true_eof(t) ?
149 -1 :
150 0;
151 }
152
153 template<typename T>
putbackboost::iostreams::detail::read_device_impl154 static bool putback(T& t, typename char_type_of<T>::type c)
155 { // gcc 2.95 needs namespace qualification for char_traits.
156 typedef typename char_type_of<T>::type char_type;
157 typedef iostreams::char_traits<char_type> traits_type;
158 return !traits_type::is_eof(t.sputbackc(c));
159 }
160 };
161
162 template<>
163 struct read_device_impl<input> {
164 template<typename T>
165 static typename int_type_of<T>::type
getboost::iostreams::detail::read_device_impl166 get(T& t)
167 { // gcc 2.95 needs namespace qualification for char_traits.
168 typedef typename char_type_of<T>::type char_type;
169 typedef iostreams::char_traits<char_type> traits_type;
170 char_type c;
171 std::streamsize amt;
172 return (amt = t.read(&c, 1)) == 1 ?
173 traits_type::to_int_type(c) :
174 amt == -1 ?
175 traits_type::eof() :
176 traits_type::would_block();
177 }
178
179 template<typename T>
180 static std::streamsize
readboost::iostreams::detail::read_device_impl181 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
182 { return t.read(s, n); }
183
184 template<typename T>
putbackboost::iostreams::detail::read_device_impl185 static bool putback(T& t, typename char_type_of<T>::type c)
186 { // T must be Peekable.
187 return t.putback(c);
188 }
189 };
190
191 //------------------Definition of read_filter_impl----------------------------//
192
193 template<typename T>
194 struct read_filter_impl
195 : mpl::if_<
196 detail::is_custom<T>,
197 operations<T>,
198 read_filter_impl<
199 BOOST_DEDUCED_TYPENAME
200 detail::dispatch<
201 T, multichar_tag, any_tag
202 >::type
203 >
204 >::type
205 { };
206
207 template<>
208 struct read_filter_impl<multichar_tag> {
209 template<typename T, typename Source>
readboost::iostreams::detail::read_filter_impl210 static std::streamsize read
211 (T& t, Source& src, typename char_type_of<T>::type* s, std::streamsize n)
212 { return t.read(src, s, n); }
213 };
214
215 template<>
216 struct read_filter_impl<any_tag> {
217 template<typename T, typename Source>
readboost::iostreams::detail::read_filter_impl218 static std::streamsize read
219 (T& t, Source& src, typename char_type_of<T>::type* s, std::streamsize n)
220 { // gcc 2.95 needs namespace qualification for char_traits.
221 typedef typename char_type_of<T>::type char_type;
222 typedef iostreams::char_traits<char_type> traits_type;
223 for (std::streamsize off = 0; off < n; ++off) {
224 typename traits_type::int_type c = t.get(src);
225 if (traits_type::is_eof(c))
226 return check_eof(off);
227 if (traits_type::would_block(c))
228 return off;
229 s[off] = traits_type::to_char_type(c);
230 }
231 return n;
232 }
233 };
234
235 } // End namespace detail.
236
237 } } // End namespaces iostreams, boost.
238
239 #include <boost/iostreams/detail/config/enable_warnings.hpp>
240
241 #endif // #ifndef BOOST_IOSTREAMS_READ_HPP_INCLUDED
242