]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/sbuf_iter.h
sbuf_iter.h (istreambuf_iterator): Correct.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / sbuf_iter.h
CommitLineData
725dc051
BK
1// Streambuf iterators
2
b0a85b86 3// Copyright (C) 1997-2001 Free Software Foundation, Inc.
725dc051
BK
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING. If not, write to the Free
18// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19// USA.
20
21// As a special exception, you may use this file as part of a free software
22// library without restriction. Specifically, if other files instantiate
23// templates or use macros or inline functions from this file, or you compile
24// this file and link it with other files to produce an executable, this
25// file does not by itself cause the resulting executable to be covered by
26// the GNU General Public License. This exception does not however
27// invalidate any other reasons why the executable file might be covered by
28// the GNU General Public License.
29
30// XXX Should specialize copy, find algorithms for streambuf iterators.
31
32#ifndef _CPP_BITS_SBUF_ITER_H
33#define _CPP_BITS_SBUF_ITER_H 1
34
b0a85b86
GDR
35#pragma GCC system_header
36
725dc051
BK
37namespace std
38{
725dc051
BK
39 template<typename _CharT, typename _Traits>
40 class ostreambuf_iterator
725dc051 41 : public iterator<output_iterator_tag, _CharT, void, void, void>
725dc051
BK
42 {
43 public:
725dc051 44 // Types:
b85381b9 45 typedef _CharT char_type;
725dc051
BK
46 typedef _Traits traits_type;
47 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
48 typedef basic_ostream<_CharT, _Traits> ostream_type;
49
50 inline
51 ostreambuf_iterator(ostream_type& __s) throw ()
b85381b9 52 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
725dc051
BK
53
54 ostreambuf_iterator(streambuf_type* __s) throw ()
b85381b9 55 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
725dc051
BK
56
57 ostreambuf_iterator&
58 operator=(_CharT __c);
59
60 ostreambuf_iterator&
61 operator*() throw()
62 { return *this; }
63
64 ostreambuf_iterator&
65 operator++(int) throw()
66 { return *this; }
67
68 ostreambuf_iterator&
69 operator++() throw()
70 { return *this; }
71
72 bool
73 failed() const throw()
74 { return _M_failed; }
75
76 private:
77 streambuf_type* _M_sbuf;
78 bool _M_failed;
725dc051
BK
79 };
80
81 template<typename _CharT, typename _Traits>
82 inline ostreambuf_iterator<_CharT, _Traits>&
83 ostreambuf_iterator<_CharT, _Traits>::operator=(_CharT __c)
84 {
b85381b9 85 if (!_M_failed &&
725dc051
BK
86 _Traits::eq_int_type(_M_sbuf->sputc(__c),_Traits::eof()))
87 _M_failed = true;
88 return *this;
89 }
90
91
725dc051
BK
92 // 24.5.3 Template class istreambuf_iterator
93 template<class _CharT, class _Traits>
94 class istreambuf_iterator
95 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
96 _CharT*, _CharT&>
97 {
98 public:
99
100 // Types:
101 typedef _CharT char_type;
102 typedef _Traits traits_type;
103 typedef typename _Traits::int_type int_type;
104 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
105 typedef basic_istream<_CharT, _Traits> istream_type;
106 // Non-standard Types:
107 typedef istreambuf_iterator<_CharT, _Traits> __istreambufiter_type;
108
109 istreambuf_iterator() throw()
b85381b9 110 : _M_sbuf(NULL), _M_c(-2) { }
725dc051
BK
111
112 istreambuf_iterator(istream_type& __s) throw()
b85381b9 113 : _M_sbuf(__s.rdbuf()), _M_c(-2) { }
725dc051
BK
114
115 istreambuf_iterator(streambuf_type* __s) throw()
b85381b9 116 : _M_sbuf(__s), _M_c(-2) { }
725dc051
BK
117
118 // NB: This should really have an int_type return
119 // value, so "end of stream" postion can be checked without
120 // hacking.
121 char_type
122 operator*() const
123 {
124 // The result of operator*() on an end of stream is undefined.
125 char_type __ret;
b85381b9 126 if (_M_sbuf && _M_c != static_cast<int_type>(-2))
725dc051 127 __ret = _M_c;
b85381b9
BK
128 else if (_M_sbuf)
129 __ret = traits_type::to_char_type(_M_sbuf->sgetc());
725dc051
BK
130 else
131 __ret = static_cast<char_type>(traits_type::eof());
132 return __ret;
133 }
134
135 __istreambufiter_type&
136 operator++()
137 {
b85381b9
BK
138 if (_M_sbuf)
139 _M_sbuf->sbumpc();
725dc051
BK
140 _M_c = -2;
141 return *this;
142 }
143
a85afd69 144 __istreambufiter_type
725dc051
BK
145 operator++(int)
146 {
a85afd69 147 __istreambufiter_type __old = *this;
b85381b9 148 if (_M_sbuf)
a85afd69
BK
149 __old._M_c = _M_sbuf->sbumpc();
150 _M_c = -2;
151 return __old;
725dc051 152 }
725dc051
BK
153
154 bool
155 equal(const __istreambufiter_type& __b)
156 {
157 int_type __eof = traits_type::eof();
b85381b9
BK
158 bool __thiseof = !_M_sbuf || _M_sbuf->sgetc() == __eof;
159 bool __beof = !__b._M_sbuf
160 || __b._M_sbuf->sgetc() == __eof;
725dc051
BK
161 return (__thiseof && __beof || (!__thiseof && !__beof));
162 }
163
164#ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
165 // 110 istreambuf_iterator::equal not const
166 // NB: there is also number 111 pending on this function.
167 bool
168 equal(const __istreambufiter_type& __b) const
169 {
170 int_type __eof = traits_type::eof();
b85381b9
BK
171 bool __thiseof = !_M_sbuf || _M_sbuf->sgetc() == __eof;
172 bool __beof = !__b._M_sbuf
173 || __b._M_sbuf->sgetc() == __eof;
725dc051
BK
174 return (__thiseof && __beof || (!__thiseof && !__beof));
175 }
176#endif
177
178 private:
179 // 24.5.3 istreambuf_iterator
180 // p 1
181 // If the end of stream is reached (streambuf_type::sgetc()
182 // returns traits_type::eof()), the iterator becomes equal to
183 // the "end of stream" iterator value.
184 // NB: This implementation assumes the "end of stream" value
185 // is EOF, or -1.
b85381b9 186 streambuf_type* _M_sbuf;
725dc051
BK
187 int_type _M_c;
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
b85381b9 202} // namespace std
725dc051 203
b85381b9 204#endif
a85afd69
BK
205
206
207