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.)
6 // See http://www.boost.org/libs/iostreams for documentation.
8 #ifndef BOOST_IOSTREAMS_READ_HPP_INCLUDED
9 #define BOOST_IOSTREAMS_READ_HPP_INCLUDED
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>
27 #include <boost/iostreams/detail/config/disable_warnings.hpp>
29 namespace boost { namespace iostreams {
34 struct read_device_impl;
37 struct read_filter_impl;
39 } // End namespace detail.
42 typename int_type_of<T>::type get(T& t)
43 { return detail::read_device_impl<T>::get(detail::unwrap(t)); }
46 inline std::streamsize
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); }
50 template<typename T, typename Source>
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); }
56 bool putback(T& t, typename char_type_of<T>::type c)
57 { return detail::read_device_impl<T>::putback(detail::unwrap(t), c); }
59 //----------------------------------------------------------------------------//
63 // Helper function for adding -1 as EOF indicator.
64 inline std::streamsize check_eof(std::streamsize n) { return n != 0 ? n : -1; }
66 // Helper templates for reading from streambufs.
67 template<bool IsLinked>
71 struct true_eof_impl<true> {
73 static bool true_eof(T& t) { return t.true_eof(); }
77 struct true_eof_impl<false> {
79 static bool true_eof(T&) { return true; }
83 inline bool true_eof(T& t)
85 const bool linked = is_linked<T>::value;
86 return true_eof_impl<linked>::true_eof(t);
89 //------------------Definition of read_device_impl----------------------------//
92 struct read_device_impl
97 BOOST_DEDUCED_TYPENAME
99 T, istream_tag, streambuf_tag, input
106 struct read_device_impl<istream_tag> {
108 static typename int_type_of<T>::type get(T& t)
112 static std::streamsize
113 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
114 { return check_eof(t.rdbuf()->sgetn(s, n)); }
117 static bool putback(T& t, typename char_type_of<T>::type c)
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() );
127 struct read_device_impl<streambuf_tag> {
129 static typename int_type_of<T>::type
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()) ||
138 c : traits_type::would_block();
142 static std::streamsize
143 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
146 return (amt = t.sgetn(s, n)) != 0 ?
148 detail::true_eof(t) ?
154 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));
163 struct read_device_impl<input> {
165 static typename int_type_of<T>::type
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;
172 return (amt = t.read(&c, 1)) == 1 ?
173 traits_type::to_int_type(c) :
176 traits_type::would_block();
180 static std::streamsize
181 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
182 { return t.read(s, n); }
185 static bool putback(T& t, typename char_type_of<T>::type c)
186 { // T must be Peekable.
191 //------------------Definition of read_filter_impl----------------------------//
194 struct read_filter_impl
196 detail::is_custom<T>,
199 BOOST_DEDUCED_TYPENAME
201 T, multichar_tag, any_tag
208 struct read_filter_impl<multichar_tag> {
209 template<typename T, typename Source>
210 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); }
216 struct read_filter_impl<any_tag> {
217 template<typename T, typename Source>
218 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))
229 s[off] = traits_type::to_char_type(c);
235 } // End namespace detail.
237 } } // End namespaces iostreams, boost.
239 #include <boost/iostreams/detail/config/enable_warnings.hpp>
241 #endif // #ifndef BOOST_IOSTREAMS_READ_HPP_INCLUDED