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