]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/streambuf_iterator.h
gcov.c (no_data_file): New flag.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / streambuf_iterator.h
CommitLineData
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
PC
45
46 template<typename _CharT>
47 typename __enable_if<ostreambuf_iterator<_CharT>,
48 __is_char<_CharT>::__value>::__type
49 copy(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>,
50 ostreambuf_iterator<_CharT>);
3cbc7af0 51
725dc051 52 // 24.5.3 Template class istreambuf_iterator
ffcec5c8 53 /// Provides input iterator semantics for streambufs.
39003c99 54 template<typename _CharT, typename _Traits>
725dc051
BK
55 class istreambuf_iterator
56 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
ed6814f7 57 _CharT*, _CharT&>
725dc051
BK
58 {
59 public:
725dc051 60 // Types:
ffcec5c8
JQ
61 //@{
62 /// Public typedefs
ed6814f7
BI
63 typedef _CharT char_type;
64 typedef _Traits traits_type;
65 typedef typename _Traits::int_type int_type;
66 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
67 typedef basic_istream<_CharT, _Traits> istream_type;
ffcec5c8 68 //@}
725dc051 69
0002d5d2
PC
70 template<typename _CharT2>
71 friend typename __enable_if<ostreambuf_iterator<_CharT2>,
72 __is_char<_CharT2>::__value>::__type
73 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
74 ostreambuf_iterator<_CharT2>);
75
76 template<typename _CharT2>
77 friend typename __enable_if<_CharT2*,
78 __is_char<_CharT2>::__value>::__type
79 __copy_aux(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
80 _CharT2*);
81
82 template<typename _CharT2>
83 friend typename __enable_if<istreambuf_iterator<_CharT2>,
84 __is_char<_CharT2>::__value>::__type
85 find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
86 _CharT2);
87
39003c99 88 private:
ed6814f7
BI
89 // 24.5.3 istreambuf_iterator
90 // p 1
39003c99
BK
91 // If the end of stream is reached (streambuf_type::sgetc()
92 // returns traits_type::eof()), the iterator becomes equal to
93 // the "end of stream" iterator value.
94 // NB: This implementation assumes the "end of stream" value
95 // is EOF, or -1.
ed6814f7 96 mutable streambuf_type* _M_sbuf;
15fb0dbe 97 mutable int_type _M_c;
39003c99
BK
98
99 public:
ffcec5c8 100 /// Construct end of input stream iterator.
ed6814f7 101 istreambuf_iterator() throw()
f13a69ec 102 : _M_sbuf(0), _M_c(traits_type::eof()) { }
ed6814f7 103
ffcec5c8 104 /// Construct start of input stream iterator.
725dc051 105 istreambuf_iterator(istream_type& __s) throw()
f13a69ec 106 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
725dc051 107
ffcec5c8 108 /// Construct start of streambuf iterator.
725dc051 109 istreambuf_iterator(streambuf_type* __s) throw()
f13a69ec 110 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
ed6814f7 111
ffcec5c8
JQ
112 /// Return the current character pointed to by iterator. This returns
113 /// streambuf.sgetc(). It cannot be assigned. NB: The result of
114 /// operator*() on an end of stream is undefined.
ed6814f7 115 char_type
725dc051 116 operator*() const
ed6814f7 117 {
285b36d6
BK
118#ifdef _GLIBCXX_DEBUG_PEDANTIC
119 // Dereferencing a past-the-end istreambuf_iterator is a
120 // libstdc++ extension
121 __glibcxx_requires_cond(!_M_at_eof(),
122 _M_message(__gnu_debug::__msg_deref_istreambuf)
ed6814f7 123 ._M_iterator(*this));
285b36d6 124#endif
ed6814f7 125 return traits_type::to_char_type(_M_get());
285b36d6 126 }
ffcec5c8
JQ
127
128 /// Advance the iterator. Calls streambuf.sbumpc().
ed6814f7 129 istreambuf_iterator&
725dc051 130 operator++()
ed6814f7 131 {
285b36d6
BK
132 __glibcxx_requires_cond(!_M_at_eof(),
133 _M_message(__gnu_debug::__msg_inc_istreambuf)
ed6814f7 134 ._M_iterator(*this));
eacf72d3
NM
135 if (_M_sbuf)
136 {
137 _M_sbuf->sbumpc();
138 _M_c = traits_type::eof();
139 }
ed6814f7 140 return *this;
725dc051
BK
141 }
142
ffcec5c8 143 /// Advance the iterator. Calls streambuf.sbumpc().
b581eaf7 144 istreambuf_iterator
725dc051
BK
145 operator++(int)
146 {
285b36d6
BK
147 __glibcxx_requires_cond(!_M_at_eof(),
148 _M_message(__gnu_debug::__msg_inc_istreambuf)
ed6814f7 149 ._M_iterator(*this));
285b36d6 150
b581eaf7 151 istreambuf_iterator __old = *this;
eacf72d3
NM
152 if (_M_sbuf)
153 {
154 __old._M_c = _M_sbuf->sbumpc();
155 _M_c = traits_type::eof();
156 }
ed6814f7 157 return __old;
725dc051 158 }
725dc051 159
f5677b15 160 // _GLIBCXX_RESOLVE_LIB_DEFECTS
725dc051 161 // 110 istreambuf_iterator::equal not const
77cd227e 162 // NB: there is also number 111 (NAD, Future) pending on this function.
ffcec5c8 163 /// Return true both iterators are end or both are not end.
ed6814f7 164 bool
b581eaf7 165 equal(const istreambuf_iterator& __b) const
725dc051 166 {
22352844
PC
167 const bool __thiseof = _M_at_eof();
168 const bool __beof = __b._M_at_eof();
725dc051
BK
169 return (__thiseof && __beof || (!__thiseof && !__beof));
170 }
9875ea05
BK
171
172 private:
ed6814f7 173 int_type
9875ea05 174 _M_get() const
ed6814f7 175 {
f13a69ec
BK
176 const int_type __eof = traits_type::eof();
177 int_type __ret = __eof;
9875ea05 178 if (_M_sbuf)
ed6814f7 179 {
50922820 180 if (!traits_type::eq_int_type(_M_c, __eof))
9875ea05 181 __ret = _M_c;
50922820
PC
182 else if (!traits_type::eq_int_type((__ret = _M_sbuf->sgetc()),
183 __eof))
184 _M_c = __ret;
15fb0dbe 185 else
15d72060 186 _M_sbuf = 0;
9875ea05
BK
187 }
188 return __ret;
189 }
285b36d6 190
ed6814f7 191 bool
285b36d6
BK
192 _M_at_eof() const
193 {
194 const int_type __eof = traits_type::eof();
195 return traits_type::eq_int_type(_M_get(), __eof);
196 }
725dc051
BK
197 };
198
199 template<typename _CharT, typename _Traits>
ed6814f7 200 inline bool
725dc051
BK
201 operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
202 const istreambuf_iterator<_CharT, _Traits>& __b)
203 { return __a.equal(__b); }
204
205 template<typename _CharT, typename _Traits>
ed6814f7 206 inline bool
725dc051
BK
207 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
208 const istreambuf_iterator<_CharT, _Traits>& __b)
209 { return !__a.equal(__b); }
5c352a47 210
ffcec5c8 211 /// Provides output iterator semantics for streambufs.
5c352a47
BK
212 template<typename _CharT, typename _Traits>
213 class ostreambuf_iterator
214 : public iterator<output_iterator_tag, void, void, void, void>
215 {
216 public:
217 // Types:
ffcec5c8
JQ
218 //@{
219 /// Public typedefs
5c352a47
BK
220 typedef _CharT char_type;
221 typedef _Traits traits_type;
222 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
223 typedef basic_ostream<_CharT, _Traits> ostream_type;
ffcec5c8 224 //@}
5c352a47 225
0002d5d2
PC
226 template<typename _CharT2>
227 friend typename __enable_if<ostreambuf_iterator<_CharT2>,
228 __is_char<_CharT2>::__value>::__type
229 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
230 ostreambuf_iterator<_CharT2>);
231
5c352a47 232 private:
ed6814f7
BI
233 streambuf_type* _M_sbuf;
234 bool _M_failed;
5c352a47
BK
235
236 public:
ffcec5c8 237 /// Construct output iterator from ostream.
5c352a47
BK
238 ostreambuf_iterator(ostream_type& __s) throw ()
239 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
ed6814f7 240
ffcec5c8 241 /// Construct output iterator from streambuf.
5c352a47
BK
242 ostreambuf_iterator(streambuf_type* __s) throw ()
243 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
244
ffcec5c8 245 /// Write character to streambuf. Calls streambuf.sputc().
ed6814f7 246 ostreambuf_iterator&
2e2a38cd
BK
247 operator=(_CharT __c)
248 {
ed6814f7 249 if (!_M_failed &&
2e2a38cd
BK
250 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
251 _M_failed = true;
252 return *this;
253 }
5c352a47 254
ffcec5c8 255 /// Return *this.
ed6814f7 256 ostreambuf_iterator&
e0ec69c9 257 operator*()
5c352a47
BK
258 { return *this; }
259
ffcec5c8 260 /// Return *this.
ed6814f7 261 ostreambuf_iterator&
e0ec69c9 262 operator++(int)
5c352a47
BK
263 { return *this; }
264
ffcec5c8 265 /// Return *this.
ed6814f7 266 ostreambuf_iterator&
e0ec69c9 267 operator++()
5c352a47
BK
268 { return *this; }
269
ffcec5c8 270 /// Return true if previous operator=() failed.
ed6814f7 271 bool
5c352a47
BK
272 failed() const throw()
273 { return _M_failed; }
5c352a47 274
ed6814f7 275 ostreambuf_iterator&
2e2a38cd
BK
276 _M_put(const _CharT* __ws, streamsize __len)
277 {
53279c10 278 if (__builtin_expect(!_M_failed, true)
15d72060
PC
279 && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len,
280 false))
53279c10 281 _M_failed = true;
2e2a38cd
BK
282 return *this;
283 }
284 };
3cbc7af0 285
0002d5d2
PC
286 // Overloads for streambuf iterators.
287 template<typename _CharT>
288 typename __enable_if<ostreambuf_iterator<_CharT>,
289 __is_char<_CharT>::__value>::__type
290 copy(istreambuf_iterator<_CharT> __first,
291 istreambuf_iterator<_CharT> __last,
292 ostreambuf_iterator<_CharT> __result)
293 {
294 if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed)
295 {
296 bool __ineof;
297 __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof);
298 if (!__ineof)
299 __result._M_failed = true;
300 }
301 return __result;
302 }
303
304 template<typename _CharT>
305 typename __enable_if<ostreambuf_iterator<_CharT>,
306 __is_char<_CharT>::__value>::__type
307 __copy_aux(_CharT* __first, _CharT* __last,
308 ostreambuf_iterator<_CharT> __result)
309 {
310 const streamsize __num = __last - __first;
311 if (__num > 0)
312 __result._M_put(__first, __num);
313 return __result;
314 }
315
316 template<typename _CharT>
317 typename __enable_if<ostreambuf_iterator<_CharT>,
318 __is_char<_CharT>::__value>::__type
319 __copy_aux(const _CharT* __first, const _CharT* __last,
320 ostreambuf_iterator<_CharT> __result)
321 {
322 const streamsize __num = __last - __first;
323 if (__num > 0)
324 __result._M_put(__first, __num);
325 return __result;
326 }
327
328 template<typename _CharT>
329 typename __enable_if<_CharT*, __is_char<_CharT>::__value>::__type
330 __copy_aux(istreambuf_iterator<_CharT> __first,
331 istreambuf_iterator<_CharT> __last, _CharT* __result)
332 {
333 typedef istreambuf_iterator<_CharT> __is_iterator_type;
334 typedef typename __is_iterator_type::traits_type traits_type;
335 typedef typename __is_iterator_type::streambuf_type streambuf_type;
336 typedef typename traits_type::int_type int_type;
337
338 if (__first._M_sbuf && !__last._M_sbuf)
339 {
340 streambuf_type* __sb = __first._M_sbuf;
341 int_type __c = __sb->sgetc();
342 while (!traits_type::eq_int_type(__c, traits_type::eof()))
343 {
344 const streamsize __n = __sb->egptr() - __sb->gptr();
345 if (__n > 1)
346 {
347 traits_type::copy(__result, __sb->gptr(), __n);
348 __sb->gbump(__n);
349 __result += __n;
350 __c = __sb->underflow();
351 }
352 else
353 {
354 *__result++ = traits_type::to_char_type(__c);
355 __c = __sb->snextc();
356 }
357 }
358 }
359 return __result;
360 }
361
362 template<typename _CharT>
363 typename __enable_if<istreambuf_iterator<_CharT>,
364 __is_char<_CharT>::__value>::__type
365 find(istreambuf_iterator<_CharT> __first,
366 istreambuf_iterator<_CharT> __last, _CharT __val)
367 {
368 typedef istreambuf_iterator<_CharT> __is_iterator_type;
369 typedef typename __is_iterator_type::traits_type traits_type;
370 typedef typename __is_iterator_type::streambuf_type streambuf_type;
371 typedef typename traits_type::int_type int_type;
372
373 if (__first._M_sbuf && !__last._M_sbuf)
374 {
375 const int_type __ival = traits_type::to_int_type(__val);
376 streambuf_type* __sb = __first._M_sbuf;
377 int_type __c = __sb->sgetc();
378 while (!traits_type::eq_int_type(__c, traits_type::eof())
379 && !traits_type::eq_int_type(__c, __ival))
380 {
381 streamsize __n = __sb->egptr() - __sb->gptr();
382 if (__n > 1)
383 {
384 const _CharT* __p = traits_type::find(__sb->gptr(),
385 __n, __val);
386 if (__p)
387 __n = __p - __sb->gptr();
388 __sb->gbump(__n);
389 __c = __sb->sgetc();
390 }
391 else
392 __c = __sb->snextc();
393 }
394
395 if (!traits_type::eq_int_type(__c, traits_type::eof()))
396 __first._M_c = __c;
397 else
398 __first._M_sbuf = 0;
399 }
400 return __first;
401 }
402
3cbc7af0
BK
403_GLIBCXX_END_NAMESPACE
404
b85381b9 405#endif