]>
Commit | Line | Data |
---|---|---|
725dc051 BK |
1 | // Streambuf iterators |
2 | ||
0002d5d2 | 3 | // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 |
5c352a47 | 4 | // Free Software Foundation, Inc. |
725dc051 BK |
5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
9 | // Free Software Foundation; either version 2, or (at your option) | |
10 | // any later version. | |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
17 | // You should have received a copy of the GNU General Public License along | |
18 | // with this library; see the file COPYING. If not, write to the Free | |
83f51799 | 19 | // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
725dc051 BK |
20 | // USA. |
21 | ||
22 | // As a special exception, you may use this file as part of a free software | |
23 | // library without restriction. Specifically, if other files instantiate | |
24 | // templates or use macros or inline functions from this file, or you compile | |
25 | // this file and link it with other files to produce an executable, this | |
26 | // file does not by itself cause the resulting executable to be covered by | |
27 | // the GNU General Public License. This exception does not however | |
28 | // invalidate any other reasons why the executable file might be covered by | |
29 | // the GNU General Public License. | |
30 | ||
729e3d3f PE |
31 | /** @file streambuf_iterator.h |
32 | * This is an internal header file, included by other library headers. | |
33 | * You should not attempt to use it directly. | |
34 | */ | |
35 | ||
3d7c150e BK |
36 | #ifndef _STREAMBUF_ITERATOR_H |
37 | #define _STREAMBUF_ITERATOR_H 1 | |
725dc051 | 38 | |
b0a85b86 GDR |
39 | #pragma GCC system_header |
40 | ||
2e2a38cd | 41 | #include <streambuf> |
285b36d6 | 42 | #include <debug/debug.h> |
2e2a38cd | 43 | |
3cbc7af0 | 44 | _GLIBCXX_BEGIN_NAMESPACE(std) |
0002d5d2 | 45 | |
725dc051 | 46 | // 24.5.3 Template class istreambuf_iterator |
ffcec5c8 | 47 | /// Provides input iterator semantics for streambufs. |
39003c99 | 48 | template<typename _CharT, typename _Traits> |
725dc051 BK |
49 | class istreambuf_iterator |
50 | : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type, | |
ed6814f7 | 51 | _CharT*, _CharT&> |
725dc051 BK |
52 | { |
53 | public: | |
725dc051 | 54 | // Types: |
ffcec5c8 JQ |
55 | //@{ |
56 | /// Public typedefs | |
ed6814f7 BI |
57 | typedef _CharT char_type; |
58 | typedef _Traits traits_type; | |
59 | typedef typename _Traits::int_type int_type; | |
60 | typedef basic_streambuf<_CharT, _Traits> streambuf_type; | |
61 | typedef basic_istream<_CharT, _Traits> istream_type; | |
ffcec5c8 | 62 | //@} |
725dc051 | 63 | |
0002d5d2 | 64 | template<typename _CharT2> |
105c6331 BK |
65 | friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, |
66 | ostreambuf_iterator<_CharT2> >::__type | |
0002d5d2 PC |
67 | copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>, |
68 | ostreambuf_iterator<_CharT2>); | |
69 | ||
70 | template<typename _CharT2> | |
105c6331 BK |
71 | friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, |
72 | _CharT2*>::__type | |
0002d5d2 PC |
73 | __copy_aux(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>, |
74 | _CharT2*); | |
75 | ||
76 | template<typename _CharT2> | |
105c6331 BK |
77 | friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, |
78 | istreambuf_iterator<_CharT2> >::__type | |
0002d5d2 | 79 | find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>, |
976e25f4 | 80 | const _CharT2&); |
0002d5d2 | 81 | |
39003c99 | 82 | private: |
ed6814f7 BI |
83 | // 24.5.3 istreambuf_iterator |
84 | // p 1 | |
39003c99 BK |
85 | // If the end of stream is reached (streambuf_type::sgetc() |
86 | // returns traits_type::eof()), the iterator becomes equal to | |
87 | // the "end of stream" iterator value. | |
88 | // NB: This implementation assumes the "end of stream" value | |
89 | // is EOF, or -1. | |
ed6814f7 | 90 | mutable streambuf_type* _M_sbuf; |
15fb0dbe | 91 | mutable int_type _M_c; |
39003c99 BK |
92 | |
93 | public: | |
ffcec5c8 | 94 | /// Construct end of input stream iterator. |
ed6814f7 | 95 | istreambuf_iterator() throw() |
f13a69ec | 96 | : _M_sbuf(0), _M_c(traits_type::eof()) { } |
ed6814f7 | 97 | |
ffcec5c8 | 98 | /// Construct start of input stream iterator. |
725dc051 | 99 | istreambuf_iterator(istream_type& __s) throw() |
f13a69ec | 100 | : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { } |
725dc051 | 101 | |
ffcec5c8 | 102 | /// Construct start of streambuf iterator. |
725dc051 | 103 | istreambuf_iterator(streambuf_type* __s) throw() |
f13a69ec | 104 | : _M_sbuf(__s), _M_c(traits_type::eof()) { } |
ed6814f7 | 105 | |
ffcec5c8 JQ |
106 | /// Return the current character pointed to by iterator. This returns |
107 | /// streambuf.sgetc(). It cannot be assigned. NB: The result of | |
108 | /// operator*() on an end of stream is undefined. | |
ed6814f7 | 109 | char_type |
725dc051 | 110 | operator*() const |
ed6814f7 | 111 | { |
285b36d6 BK |
112 | #ifdef _GLIBCXX_DEBUG_PEDANTIC |
113 | // Dereferencing a past-the-end istreambuf_iterator is a | |
114 | // libstdc++ extension | |
115 | __glibcxx_requires_cond(!_M_at_eof(), | |
116 | _M_message(__gnu_debug::__msg_deref_istreambuf) | |
ed6814f7 | 117 | ._M_iterator(*this)); |
285b36d6 | 118 | #endif |
ed6814f7 | 119 | return traits_type::to_char_type(_M_get()); |
285b36d6 | 120 | } |
ffcec5c8 JQ |
121 | |
122 | /// Advance the iterator. Calls streambuf.sbumpc(). | |
ed6814f7 | 123 | istreambuf_iterator& |
725dc051 | 124 | operator++() |
ed6814f7 | 125 | { |
285b36d6 BK |
126 | __glibcxx_requires_cond(!_M_at_eof(), |
127 | _M_message(__gnu_debug::__msg_inc_istreambuf) | |
ed6814f7 | 128 | ._M_iterator(*this)); |
eacf72d3 NM |
129 | if (_M_sbuf) |
130 | { | |
131 | _M_sbuf->sbumpc(); | |
132 | _M_c = traits_type::eof(); | |
133 | } | |
ed6814f7 | 134 | return *this; |
725dc051 BK |
135 | } |
136 | ||
ffcec5c8 | 137 | /// Advance the iterator. Calls streambuf.sbumpc(). |
b581eaf7 | 138 | istreambuf_iterator |
725dc051 BK |
139 | operator++(int) |
140 | { | |
285b36d6 BK |
141 | __glibcxx_requires_cond(!_M_at_eof(), |
142 | _M_message(__gnu_debug::__msg_inc_istreambuf) | |
ed6814f7 | 143 | ._M_iterator(*this)); |
285b36d6 | 144 | |
b581eaf7 | 145 | istreambuf_iterator __old = *this; |
eacf72d3 NM |
146 | if (_M_sbuf) |
147 | { | |
148 | __old._M_c = _M_sbuf->sbumpc(); | |
149 | _M_c = traits_type::eof(); | |
150 | } | |
ed6814f7 | 151 | return __old; |
725dc051 | 152 | } |
725dc051 | 153 | |
f5677b15 | 154 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
725dc051 | 155 | // 110 istreambuf_iterator::equal not const |
77cd227e | 156 | // NB: there is also number 111 (NAD, Future) pending on this function. |
ffcec5c8 | 157 | /// Return true both iterators are end or both are not end. |
ed6814f7 | 158 | bool |
b581eaf7 | 159 | equal(const istreambuf_iterator& __b) const |
6bfcbf0d | 160 | { return _M_at_eof() == __b._M_at_eof(); } |
9875ea05 BK |
161 | |
162 | private: | |
ed6814f7 | 163 | int_type |
9875ea05 | 164 | _M_get() const |
ed6814f7 | 165 | { |
f13a69ec BK |
166 | const int_type __eof = traits_type::eof(); |
167 | int_type __ret = __eof; | |
9875ea05 | 168 | if (_M_sbuf) |
ed6814f7 | 169 | { |
50922820 | 170 | if (!traits_type::eq_int_type(_M_c, __eof)) |
9875ea05 | 171 | __ret = _M_c; |
50922820 PC |
172 | else if (!traits_type::eq_int_type((__ret = _M_sbuf->sgetc()), |
173 | __eof)) | |
174 | _M_c = __ret; | |
15fb0dbe | 175 | else |
15d72060 | 176 | _M_sbuf = 0; |
9875ea05 BK |
177 | } |
178 | return __ret; | |
179 | } | |
285b36d6 | 180 | |
ed6814f7 | 181 | bool |
285b36d6 BK |
182 | _M_at_eof() const |
183 | { | |
184 | const int_type __eof = traits_type::eof(); | |
185 | return traits_type::eq_int_type(_M_get(), __eof); | |
186 | } | |
725dc051 BK |
187 | }; |
188 | ||
189 | template<typename _CharT, typename _Traits> | |
ed6814f7 | 190 | inline bool |
725dc051 BK |
191 | operator==(const istreambuf_iterator<_CharT, _Traits>& __a, |
192 | const istreambuf_iterator<_CharT, _Traits>& __b) | |
193 | { return __a.equal(__b); } | |
194 | ||
195 | template<typename _CharT, typename _Traits> | |
ed6814f7 | 196 | inline bool |
725dc051 BK |
197 | operator!=(const istreambuf_iterator<_CharT, _Traits>& __a, |
198 | const istreambuf_iterator<_CharT, _Traits>& __b) | |
199 | { return !__a.equal(__b); } | |
5c352a47 | 200 | |
ffcec5c8 | 201 | /// Provides output iterator semantics for streambufs. |
5c352a47 BK |
202 | template<typename _CharT, typename _Traits> |
203 | class ostreambuf_iterator | |
204 | : public iterator<output_iterator_tag, void, void, void, void> | |
205 | { | |
206 | public: | |
207 | // Types: | |
ffcec5c8 JQ |
208 | //@{ |
209 | /// Public typedefs | |
5c352a47 BK |
210 | typedef _CharT char_type; |
211 | typedef _Traits traits_type; | |
212 | typedef basic_streambuf<_CharT, _Traits> streambuf_type; | |
213 | typedef basic_ostream<_CharT, _Traits> ostream_type; | |
ffcec5c8 | 214 | //@} |
5c352a47 | 215 | |
0002d5d2 | 216 | template<typename _CharT2> |
105c6331 BK |
217 | friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, |
218 | ostreambuf_iterator<_CharT2> >::__type | |
0002d5d2 PC |
219 | copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>, |
220 | ostreambuf_iterator<_CharT2>); | |
221 | ||
5c352a47 | 222 | private: |
ed6814f7 BI |
223 | streambuf_type* _M_sbuf; |
224 | bool _M_failed; | |
5c352a47 BK |
225 | |
226 | public: | |
ffcec5c8 | 227 | /// Construct output iterator from ostream. |
5c352a47 BK |
228 | ostreambuf_iterator(ostream_type& __s) throw () |
229 | : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { } | |
ed6814f7 | 230 | |
ffcec5c8 | 231 | /// Construct output iterator from streambuf. |
5c352a47 BK |
232 | ostreambuf_iterator(streambuf_type* __s) throw () |
233 | : _M_sbuf(__s), _M_failed(!_M_sbuf) { } | |
234 | ||
ffcec5c8 | 235 | /// Write character to streambuf. Calls streambuf.sputc(). |
ed6814f7 | 236 | ostreambuf_iterator& |
2e2a38cd BK |
237 | operator=(_CharT __c) |
238 | { | |
ed6814f7 | 239 | if (!_M_failed && |
2e2a38cd BK |
240 | _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof())) |
241 | _M_failed = true; | |
242 | return *this; | |
243 | } | |
5c352a47 | 244 | |
ffcec5c8 | 245 | /// Return *this. |
ed6814f7 | 246 | ostreambuf_iterator& |
e0ec69c9 | 247 | operator*() |
5c352a47 BK |
248 | { return *this; } |
249 | ||
ffcec5c8 | 250 | /// Return *this. |
ed6814f7 | 251 | ostreambuf_iterator& |
e0ec69c9 | 252 | operator++(int) |
5c352a47 BK |
253 | { return *this; } |
254 | ||
ffcec5c8 | 255 | /// Return *this. |
ed6814f7 | 256 | ostreambuf_iterator& |
e0ec69c9 | 257 | operator++() |
5c352a47 BK |
258 | { return *this; } |
259 | ||
ffcec5c8 | 260 | /// Return true if previous operator=() failed. |
ed6814f7 | 261 | bool |
5c352a47 BK |
262 | failed() const throw() |
263 | { return _M_failed; } | |
5c352a47 | 264 | |
ed6814f7 | 265 | ostreambuf_iterator& |
2e2a38cd BK |
266 | _M_put(const _CharT* __ws, streamsize __len) |
267 | { | |
53279c10 | 268 | if (__builtin_expect(!_M_failed, true) |
15d72060 PC |
269 | && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len, |
270 | false)) | |
53279c10 | 271 | _M_failed = true; |
2e2a38cd BK |
272 | return *this; |
273 | } | |
274 | }; | |
3cbc7af0 | 275 | |
0002d5d2 PC |
276 | // Overloads for streambuf iterators. |
277 | template<typename _CharT> | |
105c6331 BK |
278 | typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, |
279 | ostreambuf_iterator<_CharT> >::__type | |
0002d5d2 PC |
280 | copy(istreambuf_iterator<_CharT> __first, |
281 | istreambuf_iterator<_CharT> __last, | |
282 | ostreambuf_iterator<_CharT> __result) | |
283 | { | |
284 | if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed) | |
285 | { | |
286 | bool __ineof; | |
287 | __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof); | |
288 | if (!__ineof) | |
289 | __result._M_failed = true; | |
290 | } | |
291 | return __result; | |
292 | } | |
293 | ||
294 | template<typename _CharT> | |
105c6331 BK |
295 | typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, |
296 | ostreambuf_iterator<_CharT> >::__type | |
0002d5d2 PC |
297 | __copy_aux(_CharT* __first, _CharT* __last, |
298 | ostreambuf_iterator<_CharT> __result) | |
299 | { | |
300 | const streamsize __num = __last - __first; | |
301 | if (__num > 0) | |
302 | __result._M_put(__first, __num); | |
303 | return __result; | |
304 | } | |
305 | ||
306 | template<typename _CharT> | |
105c6331 BK |
307 | typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, |
308 | ostreambuf_iterator<_CharT> >::__type | |
0002d5d2 PC |
309 | __copy_aux(const _CharT* __first, const _CharT* __last, |
310 | ostreambuf_iterator<_CharT> __result) | |
311 | { | |
312 | const streamsize __num = __last - __first; | |
313 | if (__num > 0) | |
314 | __result._M_put(__first, __num); | |
315 | return __result; | |
316 | } | |
317 | ||
318 | template<typename _CharT> | |
105c6331 BK |
319 | typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, |
320 | _CharT*>::__type | |
0002d5d2 PC |
321 | __copy_aux(istreambuf_iterator<_CharT> __first, |
322 | istreambuf_iterator<_CharT> __last, _CharT* __result) | |
323 | { | |
324 | typedef istreambuf_iterator<_CharT> __is_iterator_type; | |
325 | typedef typename __is_iterator_type::traits_type traits_type; | |
326 | typedef typename __is_iterator_type::streambuf_type streambuf_type; | |
327 | typedef typename traits_type::int_type int_type; | |
328 | ||
329 | if (__first._M_sbuf && !__last._M_sbuf) | |
330 | { | |
331 | streambuf_type* __sb = __first._M_sbuf; | |
332 | int_type __c = __sb->sgetc(); | |
333 | while (!traits_type::eq_int_type(__c, traits_type::eof())) | |
334 | { | |
335 | const streamsize __n = __sb->egptr() - __sb->gptr(); | |
336 | if (__n > 1) | |
337 | { | |
338 | traits_type::copy(__result, __sb->gptr(), __n); | |
339 | __sb->gbump(__n); | |
340 | __result += __n; | |
341 | __c = __sb->underflow(); | |
342 | } | |
343 | else | |
344 | { | |
345 | *__result++ = traits_type::to_char_type(__c); | |
346 | __c = __sb->snextc(); | |
347 | } | |
348 | } | |
349 | } | |
350 | return __result; | |
351 | } | |
352 | ||
353 | template<typename _CharT> | |
105c6331 BK |
354 | typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, |
355 | istreambuf_iterator<_CharT> >::__type | |
0002d5d2 | 356 | find(istreambuf_iterator<_CharT> __first, |
976e25f4 | 357 | istreambuf_iterator<_CharT> __last, const _CharT& __val) |
0002d5d2 PC |
358 | { |
359 | typedef istreambuf_iterator<_CharT> __is_iterator_type; | |
360 | typedef typename __is_iterator_type::traits_type traits_type; | |
361 | typedef typename __is_iterator_type::streambuf_type streambuf_type; | |
362 | typedef typename traits_type::int_type int_type; | |
363 | ||
364 | if (__first._M_sbuf && !__last._M_sbuf) | |
365 | { | |
366 | const int_type __ival = traits_type::to_int_type(__val); | |
367 | streambuf_type* __sb = __first._M_sbuf; | |
368 | int_type __c = __sb->sgetc(); | |
369 | while (!traits_type::eq_int_type(__c, traits_type::eof()) | |
370 | && !traits_type::eq_int_type(__c, __ival)) | |
371 | { | |
372 | streamsize __n = __sb->egptr() - __sb->gptr(); | |
373 | if (__n > 1) | |
374 | { | |
375 | const _CharT* __p = traits_type::find(__sb->gptr(), | |
376 | __n, __val); | |
377 | if (__p) | |
378 | __n = __p - __sb->gptr(); | |
379 | __sb->gbump(__n); | |
380 | __c = __sb->sgetc(); | |
381 | } | |
382 | else | |
383 | __c = __sb->snextc(); | |
384 | } | |
385 | ||
386 | if (!traits_type::eq_int_type(__c, traits_type::eof())) | |
387 | __first._M_c = __c; | |
388 | else | |
389 | __first._M_sbuf = 0; | |
390 | } | |
391 | return __first; | |
392 | } | |
393 | ||
3cbc7af0 BK |
394 | _GLIBCXX_END_NAMESPACE |
395 | ||
b85381b9 | 396 | #endif |