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