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 namespace boost { namespace iostreams {
13 struct read_device_impl;
16 struct read_filter_impl;
18 } // End namespace detail.
21 typename int_type_of<T>::type get(T& t)
23 typedef typename detail::unwrapped_type<T>::type unwrapped;
24 return detail::read_device_impl<T>::inner<unwrapped>::get(detail::unwrap(t));
28 inline std::streamsize
29 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
31 typedef typename detail::unwrapped_type<T>::type unwrapped;
32 return detail::read_device_impl<T>::inner<unwrapped>::read(detail::unwrap(t), s, n);
35 template<typename T, typename Source>
37 read(T& t, Source& src, typename char_type_of<T>::type* s, std::streamsize n)
39 typedef typename detail::unwrapped_type<T>::type unwrapped;
40 return detail::read_filter_impl<T>::inner<unwrapped>::read(detail::unwrap(t), src, s, n);
44 bool putback(T& t, typename char_type_of<T>::type c)
46 typedef typename detail::unwrapped_type<T>::type unwrapped;
47 return detail::read_device_impl<T>::inner<unwrapped>::putback(detail::unwrap(t), c);
50 //----------------------------------------------------------------------------//
54 // Helper function for adding -1 as EOF indicator.
55 inline std::streamsize check_eof(std::streamsize n) { return n != 0 ? n : -1; }
57 // Helper templates for reading from streambufs.
58 template<bool IsLinked>
62 struct true_eof_impl<true> {
64 static bool true_eof(T& t) { return t.true_eof(); }
68 struct true_eof_impl<false> {
70 static bool true_eof(T& t) { return true; }
74 inline bool true_eof(T& t)
76 const bool linked = is_linked<T>::value;
77 return true_eof_impl<linked>::true_eof(t);
80 //------------------Definition of read_device_impl----------------------------//
83 struct read_device_impl
88 BOOST_DEDUCED_TYPENAME
90 T, istream_tag, streambuf_tag, input
97 struct read_device_impl<istream_tag> {
100 static typename int_type_of<T>::type get(T& t)
103 static std::streamsize
104 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
105 { return check_eof(t.rdbuf()->sgetn(s, n)); }
107 static bool putback(T& t, typename char_type_of<T>::type c)
109 typedef typename char_type_of<T>::type char_type;
110 typedef BOOST_IOSTREAMS_CHAR_TRAITS(char_type) traits_type;
111 return !traits_type::eq_int_type( t.rdbuf()->sputbackc(c),
112 traits_type::eof() );
118 struct read_device_impl<streambuf_tag> {
121 static typename int_type_of<T>::type
124 typedef typename char_type_of<T>::type char_type;
125 typedef char_traits<char_type> traits_type;
126 typename int_type_of<T>::type c;
127 return !traits_type::is_eof(c = t.sbumpc()) ||
130 c : traits_type::would_block();
133 static std::streamsize
134 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
137 return (amt = t.sgetn(s, n)) != 0 ?
139 detail::true_eof(t) ?
144 static bool putback(T& t, typename char_type_of<T>::type c)
146 typedef typename char_type_of<T>::type char_type;
147 typedef char_traits<char_type> traits_type;
148 return !traits_type::is_eof(t.sputbackc(c));
154 struct read_device_impl<input> {
157 static typename int_type_of<T>::type
160 typedef typename char_type_of<T>::type char_type;
161 typedef char_traits<char_type> traits_type;
164 return (amt = t.read(&c, 1)) == 1 ?
165 traits_type::to_int_type(c) :
168 traits_type::would_block();
172 static std::streamsize
173 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
174 { return t.read(s, n); }
177 static bool putback(T& t, typename char_type_of<T>::type c)
178 { // T must be Peekable.
184 //------------------Definition of read_filter_impl----------------------------//
187 struct read_filter_impl
189 detail::is_custom<T>,
192 BOOST_DEDUCED_TYPENAME
194 T, multichar_tag, any_tag
201 struct read_filter_impl<multichar_tag> {
204 template<typename Source>
205 static std::streamsize read
206 ( T& t, Source& src, typename char_type_of<T>::type* s,
208 { return t.read(src, s, n); }
213 struct read_filter_impl<any_tag> {
216 template<typename Source>
217 static std::streamsize read
218 ( T& t, Source& src, typename char_type_of<T>::type* s,
221 typedef typename char_type_of<T>::type char_type;
222 typedef 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);
236 } // End namespace detail.
238 } } // End namespaces iostreams, boost.