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