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
11 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
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 #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) //-----------------------------------//
30 # include <boost/iostreams/detail/vc6/read.hpp>
31 #else // #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) //--------------------------//
33 namespace boost { namespace iostreams {
38 struct read_device_impl;
41 struct read_filter_impl;
43 } // End namespace detail.
46 typename int_type_of<T>::type get(T& t)
47 { return detail::read_device_impl<T>::get(detail::unwrap(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); }
54 template<typename T, typename Source>
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); }
60 bool putback(T& t, typename char_type_of<T>::type c)
61 { return detail::read_device_impl<T>::putback(detail::unwrap(t), c); }
63 //----------------------------------------------------------------------------//
67 // Helper function for adding -1 as EOF indicator.
68 inline std::streamsize check_eof(std::streamsize n) { return n != 0 ? n : -1; }
70 // Helper templates for reading from streambufs.
71 template<bool IsLinked>
75 struct true_eof_impl<true> {
77 static bool true_eof(T& t) { return t.true_eof(); }
81 struct true_eof_impl<false> {
83 static bool true_eof(T&) { return true; }
87 inline bool true_eof(T& t)
89 const bool linked = is_linked<T>::value;
90 return true_eof_impl<linked>::true_eof(t);
93 //------------------Definition of read_device_impl----------------------------//
96 struct read_device_impl
101 BOOST_DEDUCED_TYPENAME
103 T, istream_tag, streambuf_tag, input
110 struct read_device_impl<istream_tag> {
112 static typename int_type_of<T>::type get(T& 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)); }
121 static bool putback(T& t, typename char_type_of<T>::type c)
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() );
131 struct read_device_impl<streambuf_tag> {
133 static typename int_type_of<T>::type
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()) ||
142 c : traits_type::would_block();
146 static std::streamsize
147 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
150 return (amt = t.sgetn(s, n)) != 0 ?
152 detail::true_eof(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));
167 struct read_device_impl<input> {
169 static typename int_type_of<T>::type
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;
176 return (amt = t.read(&c, 1)) == 1 ?
177 traits_type::to_int_type(c) :
180 traits_type::would_block();
184 static std::streamsize
185 read(T& t, typename char_type_of<T>::type* s, std::streamsize n)
186 { return t.read(s, n); }
189 static bool putback(T& t, typename char_type_of<T>::type c)
190 { // T must be Peekable.
195 //------------------Definition of read_filter_impl----------------------------//
198 struct read_filter_impl
200 detail::is_custom<T>,
203 BOOST_DEDUCED_TYPENAME
205 T, multichar_tag, any_tag
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); }
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(off);
231 if (traits_type::would_block(c))
233 s[off] = traits_type::to_char_type(c);
239 } // End namespace detail.
241 } } // End namespaces iostreams, boost.
243 #endif // #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) //-------------------------//
245 #include <boost/iostreams/detail/config/enable_warnings.hpp>
247 #endif // #ifndef BOOST_IOSTREAMS_READ_HPP_INCLUDED