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