Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4680 | right-hear | 1 | // Streambuf iterators |
2 | |||
3 | // Copyright (C) 1997-2001 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 | #pragma GCC system_header |
||
36 | |||
37 | namespace std |
||
38 | { |
||
39 | template |
||
40 | class ostreambuf_iterator |
||
41 | : public iterator |
||
42 | { |
||
43 | public: |
||
44 | // Types: |
||
45 | typedef _CharT char_type; |
||
46 | typedef _Traits traits_type; |
||
47 | typedef basic_streambuf<_CharT, _Traits> streambuf_type; |
||
48 | typedef basic_ostream<_CharT, _Traits> ostream_type; |
||
49 | |||
50 | private: |
||
51 | streambuf_type* _M_sbuf; |
||
52 | bool _M_failed; |
||
53 | |||
54 | public: |
||
55 | inline |
||
56 | ostreambuf_iterator(ostream_type& __s) throw () |
||
57 | : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { } |
||
58 | |||
59 | ostreambuf_iterator(streambuf_type* __s) throw () |
||
60 | : _M_sbuf(__s), _M_failed(!_M_sbuf) { } |
||
61 | |||
62 | ostreambuf_iterator& |
||
63 | operator=(_CharT __c); |
||
64 | |||
65 | ostreambuf_iterator& |
||
66 | operator*() throw() |
||
67 | { return *this; } |
||
68 | |||
69 | ostreambuf_iterator& |
||
70 | operator++(int) throw() |
||
71 | { return *this; } |
||
72 | |||
73 | ostreambuf_iterator& |
||
74 | operator++() throw() |
||
75 | { return *this; } |
||
76 | |||
77 | bool |
||
78 | failed() const throw() |
||
79 | { return _M_failed; } |
||
80 | }; |
||
81 | |||
82 | template |
||
83 | inline ostreambuf_iterator<_CharT, _Traits>& |
||
84 | ostreambuf_iterator<_CharT, _Traits>::operator=(_CharT __c) |
||
85 | { |
||
86 | if (!_M_failed && |
||
87 | _Traits::eq_int_type(_M_sbuf->sputc(__c),_Traits::eof())) |
||
88 | _M_failed = true; |
||
89 | return *this; |
||
90 | } |
||
91 | |||
92 | |||
93 | // 24.5.3 Template class istreambuf_iterator |
||
94 | template |
||
95 | class istreambuf_iterator |
||
96 | : public iterator |
||
97 | _CharT*, _CharT&> |
||
98 | { |
||
99 | public: |
||
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 | private: |
||
110 | // 24.5.3 istreambuf_iterator |
||
111 | // p 1 |
||
112 | // If the end of stream is reached (streambuf_type::sgetc() |
||
113 | // returns traits_type::eof()), the iterator becomes equal to |
||
114 | // the "end of stream" iterator value. |
||
115 | // NB: This implementation assumes the "end of stream" value |
||
116 | // is EOF, or -1. |
||
117 | streambuf_type* _M_sbuf; |
||
118 | int_type _M_c; |
||
119 | |||
120 | public: |
||
121 | istreambuf_iterator() throw() |
||
122 | : _M_sbuf(NULL), _M_c(-2) { } |
||
123 | |||
124 | istreambuf_iterator(istream_type& __s) throw() |
||
125 | : _M_sbuf(__s.rdbuf()), _M_c(-2) { } |
||
126 | |||
127 | istreambuf_iterator(streambuf_type* __s) throw() |
||
128 | : _M_sbuf(__s), _M_c(-2) { } |
||
129 | |||
130 | // NB: This should really have an int_type return |
||
131 | // value, so "end of stream" postion can be checked without |
||
132 | // hacking. |
||
133 | char_type |
||
134 | operator*() const |
||
135 | { |
||
136 | // The result of operator*() on an end of stream is undefined. |
||
137 | char_type __ret; |
||
138 | if (_M_sbuf && _M_c != static_cast |
||
139 | __ret = _M_c; |
||
140 | else if (_M_sbuf) |
||
141 | __ret = traits_type::to_char_type(_M_sbuf->sgetc()); |
||
142 | else |
||
143 | __ret = static_cast |
||
144 | return __ret; |
||
145 | } |
||
146 | |||
147 | __istreambufiter_type& |
||
148 | operator++() |
||
149 | { |
||
150 | if (_M_sbuf) |
||
151 | _M_sbuf->sbumpc(); |
||
152 | _M_c = -2; |
||
153 | return *this; |
||
154 | } |
||
155 | |||
156 | __istreambufiter_type |
||
157 | operator++(int) |
||
158 | { |
||
159 | __istreambufiter_type __old = *this; |
||
160 | if (_M_sbuf) |
||
161 | __old._M_c = _M_sbuf->sbumpc(); |
||
162 | _M_c = -2; |
||
163 | return __old; |
||
164 | } |
||
165 | |||
166 | bool |
||
167 | equal(const __istreambufiter_type& __b) |
||
168 | { |
||
169 | int_type __eof = traits_type::eof(); |
||
170 | bool __thiseof = !_M_sbuf || _M_sbuf->sgetc() == __eof; |
||
171 | bool __beof = !__b._M_sbuf |
||
172 | || __b._M_sbuf->sgetc() == __eof; |
||
173 | return (__thiseof && __beof || (!__thiseof && !__beof)); |
||
174 | } |
||
175 | |||
176 | #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS |
||
177 | // 110 istreambuf_iterator::equal not const |
||
178 | // NB: there is also number 111 pending on this function. |
||
179 | bool |
||
180 | equal(const __istreambufiter_type& __b) const |
||
181 | { |
||
182 | int_type __eof = traits_type::eof(); |
||
183 | bool __thiseof = !_M_sbuf || _M_sbuf->sgetc() == __eof; |
||
184 | bool __beof = !__b._M_sbuf |
||
185 | || __b._M_sbuf->sgetc() == __eof; |
||
186 | return (__thiseof && __beof || (!__thiseof && !__beof)); |
||
187 | } |
||
188 | #endif |
||
189 | }; |
||
190 | |||
191 | template |
||
192 | inline bool |
||
193 | operator==(const istreambuf_iterator<_CharT, _Traits>& __a, |
||
194 | const istreambuf_iterator<_CharT, _Traits>& __b) |
||
195 | { return __a.equal(__b); } |
||
196 | |||
197 | template |
||
198 | inline bool |
||
199 | operator!=(const istreambuf_iterator<_CharT, _Traits>& __a, |
||
200 | const istreambuf_iterator<_CharT, _Traits>& __b) |
||
201 | { return !__a.equal(__b); } |
||
202 | } // namespace std |
||
203 | #endif |