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