williamr@2
|
1 |
/*
|
williamr@2
|
2 |
*
|
williamr@2
|
3 |
* Copyright (c) 1994
|
williamr@2
|
4 |
* Hewlett-Packard Company
|
williamr@2
|
5 |
*
|
williamr@2
|
6 |
* Copyright (c) 1996-1998
|
williamr@2
|
7 |
* Silicon Graphics Computer Systems, Inc.
|
williamr@2
|
8 |
*
|
williamr@2
|
9 |
* Copyright (c) 1997
|
williamr@2
|
10 |
* Moscow Center for SPARC Technology
|
williamr@2
|
11 |
*
|
williamr@2
|
12 |
* Copyright (c) 1999
|
williamr@2
|
13 |
* Boris Fomitchev
|
williamr@2
|
14 |
*
|
williamr@2
|
15 |
* This material is provided "as is", with absolutely no warranty expressed
|
williamr@2
|
16 |
* or implied. Any use is at your own risk.
|
williamr@2
|
17 |
*
|
williamr@2
|
18 |
* Permission to use or copy this software for any purpose is hereby granted
|
williamr@2
|
19 |
* without fee, provided the above notices are retained on all copies.
|
williamr@2
|
20 |
* Permission to modify the code and to distribute modified code is granted,
|
williamr@2
|
21 |
* provided the above notices are retained, and a notice that the code was
|
williamr@2
|
22 |
* modified is included with the above copyright notice.
|
williamr@2
|
23 |
*
|
williamr@2
|
24 |
*/
|
williamr@2
|
25 |
|
williamr@2
|
26 |
/* NOTE: This is an internal header file, included by other STL headers.
|
williamr@2
|
27 |
* You should not attempt to use it directly.
|
williamr@2
|
28 |
*/
|
williamr@2
|
29 |
|
williamr@2
|
30 |
#if !defined (_STLP_INTERNAL_STREAM_ITERATOR_H) && ! defined (_STLP_USE_NO_IOSTREAMS)
|
williamr@2
|
31 |
#define _STLP_INTERNAL_STREAM_ITERATOR_H
|
williamr@2
|
32 |
|
williamr@2
|
33 |
#ifndef _STLP_INTERNAL_ITERATOR_BASE_H
|
williamr@2
|
34 |
# include <stl/_iterator_base.h>
|
williamr@2
|
35 |
#endif
|
williamr@2
|
36 |
|
williamr@2
|
37 |
// streambuf_iterators predeclarations must appear first
|
williamr@2
|
38 |
#ifndef _STLP_IOSFWD
|
williamr@2
|
39 |
# include <iosfwd>
|
williamr@2
|
40 |
#endif
|
williamr@2
|
41 |
|
williamr@2
|
42 |
#ifndef _STLP_INTERNAL_ALGOBASE_H
|
williamr@2
|
43 |
#include <stl/_algobase.h>
|
williamr@2
|
44 |
#endif
|
williamr@2
|
45 |
|
williamr@2
|
46 |
#if defined (_STLP_OWN_IOSTREAMS)
|
williamr@2
|
47 |
|
williamr@2
|
48 |
#ifndef _STLP_INTERNAL_OSTREAMBUF_ITERATOR_H
|
williamr@2
|
49 |
# include <stl/_ostreambuf_iterator.h>
|
williamr@2
|
50 |
#endif
|
williamr@2
|
51 |
|
williamr@2
|
52 |
#ifndef _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H
|
williamr@2
|
53 |
# include <stl/_istreambuf_iterator.h>
|
williamr@2
|
54 |
#endif
|
williamr@2
|
55 |
|
williamr@2
|
56 |
#ifndef _STLP_INTERNAL_ISTREAM_H
|
williamr@2
|
57 |
# include <stl/_istream.h>
|
williamr@2
|
58 |
#endif
|
williamr@2
|
59 |
#endif /* _STLP_OWN_IOSTREAMS */
|
williamr@2
|
60 |
|
williamr@2
|
61 |
// istream_iterator and ostream_iterator look very different if we're
|
williamr@2
|
62 |
// using new, templatized iostreams than if we're using the old cfront
|
williamr@2
|
63 |
// version.
|
williamr@2
|
64 |
|
williamr@2
|
65 |
# if defined (_STLP_USE_NEW_IOSTREAMS)
|
williamr@2
|
66 |
|
williamr@2
|
67 |
_STLP_BEGIN_NAMESPACE
|
williamr@2
|
68 |
|
williamr@2
|
69 |
# ifndef _STLP_LIMITED_DEFAULT_TEMPLATES
|
williamr@2
|
70 |
template <class _Tp,
|
williamr@2
|
71 |
class _CharT = _STLP_DEFAULTCHAR, class _Traits = char_traits<_CharT>,
|
williamr@2
|
72 |
class _Dist = ptrdiff_t>
|
williamr@2
|
73 |
# define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _CharT, class _Traits, class _Dist
|
williamr@2
|
74 |
# define __ISI_TMPL_ARGUMENTS _Tp, _CharT, _Traits, _Dist
|
williamr@2
|
75 |
class istream_iterator : public iterator<input_iterator_tag, _Tp , _Dist,
|
williamr@2
|
76 |
const _Tp*, const _Tp& > {
|
williamr@2
|
77 |
# else
|
williamr@2
|
78 |
|
williamr@2
|
79 |
# if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (_STLP_DEFAULT_TYPE_PARAM)
|
williamr@2
|
80 |
# define __ISI_TMPL_HEADER_ARGUMENTS class _Tp
|
williamr@2
|
81 |
# define __ISI_TMPL_ARGUMENTS _Tp
|
williamr@2
|
82 |
template <class _Tp>
|
williamr@2
|
83 |
class istream_iterator : public iterator<input_iterator_tag, _Tp , ptrdiff_t,
|
williamr@2
|
84 |
const _Tp*, const _Tp& > {
|
williamr@2
|
85 |
# else
|
williamr@2
|
86 |
# define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _Dist
|
williamr@2
|
87 |
# define __ISI_TMPL_ARGUMENTS _Tp, _Dist
|
williamr@2
|
88 |
template <class _Tp,__DFL_TYPE_PARAM(_Dist, ptrdiff_t)>
|
williamr@2
|
89 |
class istream_iterator : public iterator<input_iterator_tag, _Tp, _Dist ,
|
williamr@2
|
90 |
const _Tp*, const _Tp& > {
|
williamr@2
|
91 |
# endif /* _STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS */
|
williamr@2
|
92 |
|
williamr@2
|
93 |
# endif /* _STLP_LIMITED_DEFAULT_TEMPLATES */
|
williamr@2
|
94 |
|
williamr@2
|
95 |
# ifdef _STLP_LIMITED_DEFAULT_TEMPLATES
|
williamr@2
|
96 |
typedef char _CharT;
|
williamr@2
|
97 |
typedef char_traits<char> _Traits;
|
williamr@2
|
98 |
# if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (_STLP_DEFAULT_TYPE_PARAM)
|
williamr@2
|
99 |
typedef ptrdiff_t _Dist;
|
williamr@2
|
100 |
# endif
|
williamr@2
|
101 |
# endif
|
williamr@2
|
102 |
|
williamr@2
|
103 |
typedef istream_iterator< __ISI_TMPL_ARGUMENTS > _Self;
|
williamr@2
|
104 |
public:
|
williamr@2
|
105 |
typedef _CharT char_type;
|
williamr@2
|
106 |
typedef _Traits traits_type;
|
williamr@2
|
107 |
typedef basic_istream<_CharT, _Traits> istream_type;
|
williamr@2
|
108 |
|
williamr@2
|
109 |
typedef input_iterator_tag iterator_category;
|
williamr@2
|
110 |
typedef _Tp value_type;
|
williamr@2
|
111 |
typedef _Dist difference_type;
|
williamr@2
|
112 |
typedef const _Tp* pointer;
|
williamr@2
|
113 |
typedef const _Tp& reference;
|
williamr@2
|
114 |
|
williamr@2
|
115 |
istream_iterator() : _M_stream(0), _M_ok(false) {}
|
williamr@2
|
116 |
istream_iterator(istream_type& __s) : _M_stream(&__s) { _M_read(); }
|
williamr@2
|
117 |
|
williamr@2
|
118 |
reference operator*() const { return _M_value; }
|
williamr@2
|
119 |
|
williamr@2
|
120 |
_STLP_DEFINE_ARROW_OPERATOR
|
williamr@2
|
121 |
|
williamr@2
|
122 |
_Self& operator++() {
|
williamr@2
|
123 |
_M_read();
|
williamr@2
|
124 |
return *this;
|
williamr@2
|
125 |
}
|
williamr@2
|
126 |
_Self operator++(int) {
|
williamr@2
|
127 |
_Self __tmp = *this;
|
williamr@2
|
128 |
_M_read();
|
williamr@2
|
129 |
return __tmp;
|
williamr@2
|
130 |
}
|
williamr@2
|
131 |
|
williamr@2
|
132 |
bool _M_equal(const _Self& __x) const
|
williamr@2
|
133 |
{ return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
|
williamr@2
|
134 |
|
williamr@2
|
135 |
private:
|
williamr@2
|
136 |
istream_type* _M_stream;
|
williamr@2
|
137 |
_Tp _M_value;
|
williamr@2
|
138 |
bool _M_ok;
|
williamr@2
|
139 |
|
williamr@2
|
140 |
void _M_read() {
|
williamr@2
|
141 |
_M_ok = (_M_stream && *_M_stream) ? true : false;
|
williamr@2
|
142 |
if (_M_ok) {
|
williamr@2
|
143 |
*_M_stream >> _M_value;
|
williamr@2
|
144 |
_M_ok = *_M_stream ? true : false;
|
williamr@2
|
145 |
}
|
williamr@2
|
146 |
}
|
williamr@2
|
147 |
};
|
williamr@2
|
148 |
|
williamr@2
|
149 |
#ifndef _STLP_LIMITED_DEFAULT_TEMPLATES
|
williamr@2
|
150 |
template <class _TpP,
|
williamr@2
|
151 |
class _CharT = _STLP_DEFAULTCHAR, class _Traits = char_traits<_CharT> >
|
williamr@2
|
152 |
#else
|
williamr@2
|
153 |
template <class _TpP>
|
williamr@2
|
154 |
#endif
|
williamr@2
|
155 |
class ostream_iterator: public iterator<output_iterator_tag, void, void, void, void> {
|
williamr@2
|
156 |
# ifdef _STLP_LIMITED_DEFAULT_TEMPLATES
|
williamr@2
|
157 |
typedef char _CharT;
|
williamr@2
|
158 |
typedef char_traits<char> _Traits;
|
williamr@2
|
159 |
typedef ostream_iterator<_TpP> _Self;
|
williamr@2
|
160 |
# else
|
williamr@2
|
161 |
typedef ostream_iterator<_TpP, _CharT, _Traits> _Self;
|
williamr@2
|
162 |
# endif
|
williamr@2
|
163 |
public:
|
williamr@2
|
164 |
typedef _CharT char_type;
|
williamr@2
|
165 |
typedef _Traits traits_type;
|
williamr@2
|
166 |
typedef basic_ostream<_CharT, _Traits> ostream_type;
|
williamr@2
|
167 |
|
williamr@2
|
168 |
typedef output_iterator_tag iterator_category;
|
williamr@2
|
169 |
|
williamr@2
|
170 |
ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
|
williamr@2
|
171 |
ostream_iterator(ostream_type& __s, const _CharT* __c)
|
williamr@2
|
172 |
: _M_stream(&__s), _M_string(__c) {}
|
williamr@2
|
173 |
_Self& operator=(const _TpP& __val) {
|
williamr@2
|
174 |
*_M_stream << __val;
|
williamr@2
|
175 |
if (_M_string) *_M_stream << _M_string;
|
williamr@2
|
176 |
return *this;
|
williamr@2
|
177 |
}
|
williamr@2
|
178 |
_Self& operator*() { return *this; }
|
williamr@2
|
179 |
_Self& operator++() { return *this; }
|
williamr@2
|
180 |
_Self& operator++(int) { return *this; }
|
williamr@2
|
181 |
private:
|
williamr@2
|
182 |
ostream_type* _M_stream;
|
williamr@2
|
183 |
const _CharT* _M_string;
|
williamr@2
|
184 |
};
|
williamr@2
|
185 |
|
williamr@2
|
186 |
# ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
|
williamr@2
|
187 |
# ifdef _STLP_LIMITED_DEFAULT_TEMPLATES
|
williamr@2
|
188 |
template <class _TpP>
|
williamr@2
|
189 |
inline output_iterator_tag _STLP_CALL
|
williamr@2
|
190 |
iterator_category(const ostream_iterator<_TpP>&) { return output_iterator_tag(); }
|
williamr@2
|
191 |
# else
|
williamr@2
|
192 |
template <class _TpP, class _CharT, class _Traits>
|
williamr@2
|
193 |
inline output_iterator_tag _STLP_CALL
|
williamr@2
|
194 |
iterator_category(const ostream_iterator<_TpP, _CharT, _Traits>&) { return output_iterator_tag(); }
|
williamr@2
|
195 |
# endif
|
williamr@2
|
196 |
# endif
|
williamr@2
|
197 |
|
williamr@2
|
198 |
_STLP_END_NAMESPACE
|
williamr@2
|
199 |
|
williamr@2
|
200 |
# elif ! defined(_STLP_USE_NO_IOSTREAMS)
|
williamr@2
|
201 |
|
williamr@2
|
202 |
_STLP_BEGIN_NAMESPACE
|
williamr@2
|
203 |
|
williamr@2
|
204 |
# if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (_STLP_DEFAULT_TYPE_PARAM)
|
williamr@2
|
205 |
# define __ISI_TMPL_HEADER_ARGUMENTS class _Tp
|
williamr@2
|
206 |
# define __ISI_TMPL_ARGUMENTS _Tp
|
williamr@2
|
207 |
template <class _Tp>
|
williamr@2
|
208 |
class istream_iterator : public iterator<input_iterator_tag, _Tp, ptrdiff_t,
|
williamr@2
|
209 |
const _Tp*, const _Tp& > {
|
williamr@2
|
210 |
# else
|
williamr@2
|
211 |
# define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _Dist
|
williamr@2
|
212 |
# define __ISI_TMPL_ARGUMENTS _Tp, _Dist
|
williamr@2
|
213 |
template <class _Tp, __DFL_TYPE_PARAM(_Dist, ptrdiff_t)>
|
williamr@2
|
214 |
class istream_iterator : public iterator<input_iterator_tag, _Tp, _Dist,
|
williamr@2
|
215 |
const _Tp*, const _Tp& > {
|
williamr@2
|
216 |
# endif
|
williamr@2
|
217 |
|
williamr@2
|
218 |
protected:
|
williamr@2
|
219 |
istream* _M_stream;
|
williamr@2
|
220 |
_Tp _M_value;
|
williamr@2
|
221 |
bool _M_end_marker;
|
williamr@2
|
222 |
void _M_read() {
|
williamr@2
|
223 |
_M_end_marker = (*_M_stream) ? true : false;
|
williamr@2
|
224 |
if (_M_end_marker) *_M_stream >> _M_value;
|
williamr@2
|
225 |
_M_end_marker = (*_M_stream) ? true : false;
|
williamr@2
|
226 |
}
|
williamr@2
|
227 |
public:
|
williamr@2
|
228 |
typedef input_iterator_tag iterator_category;
|
williamr@2
|
229 |
typedef _Tp value_type;
|
williamr@2
|
230 |
typedef _Dist difference_type;
|
williamr@2
|
231 |
typedef const _Tp* pointer;
|
williamr@2
|
232 |
typedef const _Tp& reference;
|
williamr@2
|
233 |
|
williamr@2
|
234 |
istream_iterator() : _M_stream(&cin), _M_end_marker(false) {}
|
williamr@2
|
235 |
istream_iterator(istream& __s) : _M_stream(&__s) { _M_read(); }
|
williamr@2
|
236 |
reference operator*() const { return _M_value; }
|
williamr@2
|
237 |
|
williamr@2
|
238 |
_STLP_DEFINE_ARROW_OPERATOR
|
williamr@2
|
239 |
|
williamr@2
|
240 |
istream_iterator< __ISI_TMPL_ARGUMENTS >& operator++() {
|
williamr@2
|
241 |
_M_read();
|
williamr@2
|
242 |
return *this;
|
williamr@2
|
243 |
}
|
williamr@2
|
244 |
istream_iterator< __ISI_TMPL_ARGUMENTS > operator++(int) {
|
williamr@2
|
245 |
istream_iterator< __ISI_TMPL_ARGUMENTS > __tmp = *this;
|
williamr@2
|
246 |
_M_read();
|
williamr@2
|
247 |
return __tmp;
|
williamr@2
|
248 |
}
|
williamr@2
|
249 |
inline bool _M_equal(const istream_iterator< __ISI_TMPL_ARGUMENTS >& __y) const {
|
williamr@2
|
250 |
return (_M_stream == __y._M_stream &&
|
williamr@2
|
251 |
_M_end_marker == __y._M_end_marker) ||
|
williamr@2
|
252 |
_M_end_marker == false && __y._M_end_marker == false;
|
williamr@2
|
253 |
}
|
williamr@2
|
254 |
};
|
williamr@2
|
255 |
|
williamr@2
|
256 |
template <class _Tp>
|
williamr@2
|
257 |
class ostream_iterator {
|
williamr@2
|
258 |
protected:
|
williamr@2
|
259 |
ostream* _M_stream;
|
williamr@2
|
260 |
const char* _M_string;
|
williamr@2
|
261 |
public:
|
williamr@2
|
262 |
typedef output_iterator_tag iterator_category;
|
williamr@2
|
263 |
# ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION
|
williamr@2
|
264 |
typedef void value_type;
|
williamr@2
|
265 |
typedef void difference_type;
|
williamr@2
|
266 |
typedef void pointer;
|
williamr@2
|
267 |
typedef void reference;
|
williamr@2
|
268 |
# endif
|
williamr@2
|
269 |
ostream_iterator(ostream& __s) : _M_stream(&__s), _M_string(0) {}
|
williamr@2
|
270 |
ostream_iterator(ostream& __s, const char* __c)
|
williamr@2
|
271 |
: _M_stream(&__s), _M_string(__c) {}
|
williamr@2
|
272 |
ostream_iterator<_Tp>& operator=(const _Tp& __val) {
|
williamr@2
|
273 |
*_M_stream << __val;
|
williamr@2
|
274 |
if (_M_string) *_M_stream << _M_string;
|
williamr@2
|
275 |
return *this;
|
williamr@2
|
276 |
}
|
williamr@2
|
277 |
ostream_iterator<_Tp>& operator*() { return *this; }
|
williamr@2
|
278 |
ostream_iterator<_Tp>& operator++() { return *this; }
|
williamr@2
|
279 |
ostream_iterator<_Tp>& operator++(int) { return *this; }
|
williamr@2
|
280 |
};
|
williamr@2
|
281 |
|
williamr@2
|
282 |
# ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
|
williamr@2
|
283 |
template <class _Tp> inline output_iterator_tag
|
williamr@2
|
284 |
iterator_category(const ostream_iterator<_Tp>&) { return output_iterator_tag(); }
|
williamr@2
|
285 |
#endif
|
williamr@2
|
286 |
|
williamr@2
|
287 |
_STLP_END_NAMESPACE
|
williamr@2
|
288 |
|
williamr@2
|
289 |
#endif /* _STLP_USE_NEW_IOSTREAMS */
|
williamr@2
|
290 |
|
williamr@2
|
291 |
// form-independent definiotion of stream iterators
|
williamr@2
|
292 |
_STLP_BEGIN_NAMESPACE
|
williamr@2
|
293 |
|
williamr@2
|
294 |
template < __ISI_TMPL_HEADER_ARGUMENTS >
|
williamr@2
|
295 |
inline bool _STLP_CALL
|
williamr@2
|
296 |
operator==(const istream_iterator< __ISI_TMPL_ARGUMENTS >& __x,
|
williamr@2
|
297 |
const istream_iterator< __ISI_TMPL_ARGUMENTS >& __y) {
|
williamr@2
|
298 |
return __x._M_equal(__y);
|
williamr@2
|
299 |
}
|
williamr@2
|
300 |
|
williamr@2
|
301 |
# ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
|
williamr@2
|
302 |
|
williamr@2
|
303 |
template < __ISI_TMPL_HEADER_ARGUMENTS >
|
williamr@2
|
304 |
inline bool _STLP_CALL
|
williamr@2
|
305 |
operator!=(const istream_iterator< __ISI_TMPL_ARGUMENTS >& __x,
|
williamr@2
|
306 |
const istream_iterator< __ISI_TMPL_ARGUMENTS >& __y) {
|
williamr@2
|
307 |
return !__x._M_equal(__y);
|
williamr@2
|
308 |
}
|
williamr@2
|
309 |
|
williamr@2
|
310 |
# endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
|
williamr@2
|
311 |
|
williamr@2
|
312 |
# ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
|
williamr@2
|
313 |
template < __ISI_TMPL_HEADER_ARGUMENTS >
|
williamr@2
|
314 |
inline input_iterator_tag _STLP_CALL
|
williamr@2
|
315 |
iterator_category(const istream_iterator< __ISI_TMPL_ARGUMENTS >&)
|
williamr@2
|
316 |
{ return input_iterator_tag(); }
|
williamr@2
|
317 |
template < __ISI_TMPL_HEADER_ARGUMENTS >
|
williamr@2
|
318 |
inline _Tp* _STLP_CALL
|
williamr@2
|
319 |
value_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (_Tp*) 0; }
|
williamr@2
|
320 |
|
williamr@2
|
321 |
# if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (_STLP_DEFAULT_TYPE_PARAM)
|
williamr@2
|
322 |
template < __ISI_TMPL_HEADER_ARGUMENTS >
|
williamr@2
|
323 |
inline ptrdiff_t* _STLP_CALL
|
williamr@2
|
324 |
distance_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (ptrdiff_t*)0; }
|
williamr@2
|
325 |
# else
|
williamr@2
|
326 |
template < __ISI_TMPL_HEADER_ARGUMENTS >
|
williamr@2
|
327 |
inline _Dist* _STLP_CALL
|
williamr@2
|
328 |
distance_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (_Dist*)0; }
|
williamr@2
|
329 |
# endif /* _STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS */
|
williamr@2
|
330 |
|
williamr@2
|
331 |
# endif
|
williamr@2
|
332 |
|
williamr@2
|
333 |
_STLP_END_NAMESPACE
|
williamr@2
|
334 |
|
williamr@2
|
335 |
# undef __ISI_TMPL_HEADER_ARGUMENTS
|
williamr@2
|
336 |
# undef __ISI_TMPL_ARGUMENTS
|
williamr@2
|
337 |
|
williamr@2
|
338 |
|
williamr@2
|
339 |
#endif /* _STLP_INTERNAL_STREAM_ITERATOR_H */
|
williamr@2
|
340 |
|
williamr@2
|
341 |
// Local Variables:
|
williamr@2
|
342 |
// mode:C++
|
williamr@2
|
343 |
// End:
|