]>
Commit | Line | Data |
---|---|---|
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 |
37 | namespace std |
38 | { | |
39 | ||
40 | template<typename _CharT, typename _Traits> | |
41 | class ostreambuf_iterator | |
30a20a1e | 42 | #if 1 // XXX this is standard: |
725dc051 BK |
43 | : public iterator<output_iterator_tag, _CharT, void, void, void> |
44 | #else | |
45 | : public output_iterator | |
46 | #endif | |
47 | { | |
48 | public: | |
49 | ||
50 | // Types: | |
51 | typedef _CharT char_type; | |
52 | typedef _Traits traits_type; | |
53 | typedef basic_streambuf<_CharT, _Traits> streambuf_type; | |
54 | typedef basic_ostream<_CharT, _Traits> ostream_type; | |
55 | ||
56 | inline | |
57 | ostreambuf_iterator(ostream_type& __s) throw () | |
58 | : _M_sbuf(__s.rdbuf()), _M_failed(false) { } | |
59 | ||
60 | ostreambuf_iterator(streambuf_type* __s) throw () | |
61 | : _M_sbuf(__s), _M_failed(false) { } | |
62 | ||
63 | ostreambuf_iterator& | |
64 | operator=(_CharT __c); | |
65 | ||
66 | ostreambuf_iterator& | |
67 | operator*() throw() | |
68 | { return *this; } | |
69 | ||
70 | ostreambuf_iterator& | |
71 | operator++(int) throw() | |
72 | { return *this; } | |
73 | ||
74 | ostreambuf_iterator& | |
75 | operator++() throw() | |
76 | { return *this; } | |
77 | ||
78 | bool | |
79 | failed() const throw() | |
80 | { return _M_failed; } | |
81 | ||
82 | private: | |
83 | streambuf_type* _M_sbuf; | |
84 | bool _M_failed; | |
85 | ||
86 | #if 0 | |
87 | template<> | |
88 | friend char const* | |
89 | copy(char const* __first, char const* __last, | |
90 | ostreambuf_iterator<char,char_traits<char> > __to); | |
91 | template<> | |
92 | friend wchar_t const* | |
93 | copy(wchar_t const* __first, wchar_t const* __last, | |
94 | ostreambuf_iterator<wchar_t,char_traits<wchar_t> > __to); | |
95 | #endif | |
96 | }; | |
97 | ||
98 | template<typename _CharT, typename _Traits> | |
99 | inline ostreambuf_iterator<_CharT, _Traits>& | |
100 | ostreambuf_iterator<_CharT, _Traits>::operator=(_CharT __c) | |
101 | { | |
102 | if (!_M_failed && | |
103 | _Traits::eq_int_type(_M_sbuf->sputc(__c),_Traits::eof())) | |
104 | _M_failed = true; | |
105 | return *this; | |
106 | } | |
107 | ||
108 | ||
109 | #if 0 | |
110 | // Optimized specializations of standard algorithms | |
111 | // These are specialized only for standard types | |
112 | // (with no unbound arguments) to avoid creating | |
113 | // overload problems with user specializations. | |
114 | ||
115 | template<> | |
116 | char const* | |
117 | copy(char const* __first, char const* __last, | |
118 | ostreambuf_iterator<char,char_traits<char> > __to) | |
119 | { | |
120 | if (!__to._M_failed) | |
121 | __to._M_sbuf->sputn(__first, __last-__first); | |
122 | return __last; | |
123 | } | |
124 | ||
125 | template<> | |
126 | wchar_t const* | |
127 | copy(wchar_t const* __first, wchar_t const* __last, | |
128 | ostreambuf_iterator<whar_t,char_traits<wchar_t> > __to) | |
129 | { | |
130 | if (!__to._M_failed) | |
131 | __to._M_sbuf->sputn(__first, __last-__first); | |
132 | return __last; | |
133 | } | |
134 | #endif | |
135 | ||
136 | // 24.5.3 Template class istreambuf_iterator | |
137 | template<class _CharT, class _Traits> | |
138 | class istreambuf_iterator | |
139 | : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type, | |
140 | _CharT*, _CharT&> | |
141 | { | |
142 | public: | |
143 | ||
144 | // Types: | |
145 | typedef _CharT char_type; | |
146 | typedef _Traits traits_type; | |
147 | typedef typename _Traits::int_type int_type; | |
148 | typedef basic_streambuf<_CharT, _Traits> streambuf_type; | |
149 | typedef basic_istream<_CharT, _Traits> istream_type; | |
150 | // Non-standard Types: | |
151 | typedef istreambuf_iterator<_CharT, _Traits> __istreambufiter_type; | |
152 | ||
153 | istreambuf_iterator() throw() | |
154 | : _M_istreambuf(NULL), _M_c(-2) { } | |
155 | ||
156 | istreambuf_iterator(istream_type& __s) throw() | |
157 | : _M_istreambuf(__s.rdbuf()), _M_c(-2) { } | |
158 | ||
159 | istreambuf_iterator(streambuf_type* __s) throw() | |
160 | : _M_istreambuf(__s), _M_c(-2) { } | |
161 | ||
162 | // NB: This should really have an int_type return | |
163 | // value, so "end of stream" postion can be checked without | |
164 | // hacking. | |
165 | char_type | |
166 | operator*() const | |
167 | { | |
168 | // The result of operator*() on an end of stream is undefined. | |
169 | char_type __ret; | |
170 | if (_M_istreambuf && _M_c != static_cast<int_type>(-2)) | |
171 | __ret = _M_c; | |
172 | else if (_M_istreambuf) | |
173 | __ret = traits_type::to_char_type(_M_istreambuf->sgetc()); | |
174 | else | |
175 | __ret = static_cast<char_type>(traits_type::eof()); | |
176 | return __ret; | |
177 | } | |
178 | ||
179 | __istreambufiter_type& | |
180 | operator++() | |
181 | { | |
182 | if (_M_istreambuf) | |
183 | _M_istreambuf->sbumpc(); | |
184 | _M_c = -2; | |
185 | return *this; | |
186 | } | |
187 | ||
188 | #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS | |
189 | // 14882 says return a proxy object. It should be a const | |
190 | // proxy object, but since this class is not mandated, it | |
191 | // should allow this signature: | |
192 | const __istreambufiter_type | |
193 | operator++(int) | |
194 | { | |
195 | if (_M_istreambuf) | |
196 | _M_c = _M_istreambuf->sbumpc(); | |
197 | return *this; | |
198 | } | |
199 | #endif | |
200 | ||
201 | bool | |
202 | equal(const __istreambufiter_type& __b) | |
203 | { | |
204 | int_type __eof = traits_type::eof(); | |
205 | bool __thiseof = !_M_istreambuf || _M_istreambuf->sgetc() == __eof; | |
206 | bool __beof = !__b._M_istreambuf | |
207 | || __b._M_istreambuf->sgetc() == __eof; | |
208 | return (__thiseof && __beof || (!__thiseof && !__beof)); | |
209 | } | |
210 | ||
211 | #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS | |
212 | // 110 istreambuf_iterator::equal not const | |
213 | // NB: there is also number 111 pending on this function. | |
214 | bool | |
215 | equal(const __istreambufiter_type& __b) const | |
216 | { | |
217 | int_type __eof = traits_type::eof(); | |
218 | bool __thiseof = !_M_istreambuf || _M_istreambuf->sgetc() == __eof; | |
219 | bool __beof = !__b._M_istreambuf | |
220 | || __b._M_istreambuf->sgetc() == __eof; | |
221 | return (__thiseof && __beof || (!__thiseof && !__beof)); | |
222 | } | |
223 | #endif | |
224 | ||
225 | private: | |
226 | // 24.5.3 istreambuf_iterator | |
227 | // p 1 | |
228 | // If the end of stream is reached (streambuf_type::sgetc() | |
229 | // returns traits_type::eof()), the iterator becomes equal to | |
230 | // the "end of stream" iterator value. | |
231 | // NB: This implementation assumes the "end of stream" value | |
232 | // is EOF, or -1. | |
233 | streambuf_type* _M_istreambuf; | |
234 | int_type _M_c; | |
235 | }; | |
236 | ||
237 | template<typename _CharT, typename _Traits> | |
238 | inline bool | |
239 | operator==(const istreambuf_iterator<_CharT, _Traits>& __a, | |
240 | const istreambuf_iterator<_CharT, _Traits>& __b) | |
241 | { return __a.equal(__b); } | |
242 | ||
243 | template<typename _CharT, typename _Traits> | |
244 | inline bool | |
245 | operator!=(const istreambuf_iterator<_CharT, _Traits>& __a, | |
246 | const istreambuf_iterator<_CharT, _Traits>& __b) | |
247 | { return !__a.equal(__b); } | |
248 | ||
249 | } // std:: | |
250 | ||
251 | #endif /* _CPP_BITS_SBUF_ITER_H */ | |
252 |