Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5496 leency 1
/*	Copyright (C) 2004 Garrett A. Kajmowicz
2
 
3
	This file is part of the uClibc++ Library.
4
 
5
	This library is free software; you can redistribute it and/or
6
	modify it under the terms of the GNU Lesser General Public
7
	License as published by the Free Software Foundation; either
8
	version 2.1 of the License, or (at your option) any later version.
9
 
10
	This library is distributed in the hope that it will be useful,
11
	but WITHOUT ANY WARRANTY; without even the implied warranty of
12
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
	Lesser General Public License for more details.
14
 
15
	You should have received a copy of the GNU Lesser General Public
16
	License along with this library; if not, write to the Free Software
17
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
*/
19
 
20
#include 
21
#include 
22
#include 
23
#include 
24
#include 
25
 
26
 
27
 
28
#ifndef __STD_HEADER_ITERATOR
29
#define __STD_HEADER_ITERATOR 1
30
 
31
#pragma GCC visibility push(default)
32
 
33
namespace std{
34
 
35
  // subclause _lib.stream.iterators_, stream iterators:
36
	template , class Distance = ptrdiff_t> class istream_iterator;
37
	template  bool
38
		operator==(const istream_iterator& x, const istream_iterator& y);
39
	template  bool
40
		operator!=(const istream_iterator& x, const istream_iterator& y);
41
	template  > class ostream_iterator;
42
	template > class istreambuf_iterator;
43
	template  bool
44
		operator==(const istreambuf_iterator& a, const istreambuf_iterator& b);
45
	template  bool
46
		operator!=(const istreambuf_iterator& a, const istreambuf_iterator& b);
47
	template  > class ostreambuf_iterator;
48
 
49
 
50
	template < class T, class charT, class traits, class Distance >	class _UCXXEXPORT istream_iterator
51
		: public iterator
52
	{
53
	public:
54
		typedef charT char_type;
55
		typedef traits traits_type;
56
		typedef basic_istream istream_type;
57
		istream_iterator() : in_stream(0), value(0) {}
58
		istream_iterator(istream_type& s) : in_stream(&s), value() {
59
			*in_stream >> value;
60
		}
61
		istream_iterator(const istream_iterator& x)
62
			: in_stream(x.in_stream), value(x.value)
63
		{ }
64
		~istream_iterator() { }
65
		const T& operator*() const{
66
			return value;
67
		}
68
		const T* operator->() const{
69
			return &value;
70
		}
71
		istream_iterator& operator++() {
72
			*in_stream >> value;
73
			return *this;
74
		}
75
		istream_iterator  operator++(int){
76
			istream_iterator tmp = *this;
77
			*in_stream >> value;
78
			return (tmp);
79
		}
80
		bool m_equal(const istream_iterator& x) const{
81
			return (in_stream == x.in_stream);
82
		}
83
	private:
84
		basic_istream* in_stream;
85
		T value;
86
	};
87
 
88
	template  _UCXXEXPORT
89
		bool operator==(const istream_iterator& x,
90
		const istream_iterator& y)
91
	{
92
		return x.m_equal(y);
93
	}
94
 
95
	template  _UCXXEXPORT
96
		bool operator!=(const istream_iterator& x,
97
		const istream_iterator& y)
98
	{
99
		return !(x == y);
100
	}
101
 
102
	template  class _UCXXEXPORT ostream_iterator
103
		: public iterator
104
	{
105
	public:
106
		typedef charT char_type;
107
		typedef traits traits_type;
108
		typedef basic_ostream ostream_type;
109
 
110
		ostream_iterator(ostream_type& s) : out_stream(&s), delim(0) { }
111
		ostream_iterator(ostream_type& s, const charT* delimiter) : out_stream(&s), delim(delimiter) { }
112
		ostream_iterator(const ostream_iterator& x) : out_stream(x.out_stream), delim(x.delim) { }
113
		~ostream_iterator() { }
114
		ostream_iterator& operator=(const T& value){
115
			*out_stream << value;
116
			if(delim != 0){
117
				*out_stream << delim;
118
			}
119
			return (*this);
120
		}
121
		ostream_iterator& operator*(){ return *this; }
122
		ostream_iterator& operator++() { return *this; }
123
		ostream_iterator operator++(int) { return *this; }
124
	private:
125
		basic_ostream* out_stream;
126
		const char* delim;
127
	};
128
 
129
	template class _UCXXEXPORT istreambuf_iterator :
130
		public iterator
131
	{
132
	public:
133
		typedef charT				char_type;
134
		typedef traits				traits_type;
135
		typedef typename traits::int_type	int_type;
136
		typedef basic_streambuf	streambuf_type;
137
		typedef basic_istream	istream_type;
138
 
139
		class _UCXXEXPORT proxy{
140
			charT val;
141
			basic_streambuf * buf;
142
 
143
			proxy(charT v, basic_streambuf * b) : val(v), buf(b) { }
144
		public:
145
			charT operator*() { return val; }
146
		};
147
 
148
		istreambuf_iterator() throw() : sbuf(0) { }
149
		istreambuf_iterator(istream_type& s) throw() : sbuf(s.rdbuf()) { }
150
		istreambuf_iterator(streambuf_type* s) throw() : sbuf(s) { }
151
		istreambuf_iterator(const proxy& p) throw() : sbuf(&p.buf) { }
152
 
153
		charT operator*() const{
154
			return sbuf->sgetc();
155
		}
156
		istreambuf_iterator& operator++(){
157
			sbuf->sbumpc();
158
			return *this;
159
		}
160
		proxy operator++(int){
161
			istreambuf_iterator tmp = *this;
162
			sbuf->sbumpc();
163
			return(tmp);
164
		}
165
 
166
		bool equal(const istreambuf_iterator& b) const{
167
			return sbuf == b.sbuf || is_eof() && b.is_eof();
168
		}
169
	private:
170
		streambuf_type* sbuf;
171
		inline bool is_eof() const{
172
			return sbuf == 0 || sbuf->sgetc() == traits_type::eof();
173
		}
174
	};
175
 
176
	template  _UCXXEXPORT bool
177
		operator==(const istreambuf_iterator& a,
178
		const istreambuf_iterator& b)
179
	{
180
		return a.equal(b);
181
	}
182
 
183
	template  bool _UCXXEXPORT
184
		operator!=(const istreambuf_iterator& a,
185
		const istreambuf_iterator& b)
186
	{
187
		return !a.equal(b);
188
	}
189
 
190
	template  class _UCXXEXPORT ostreambuf_iterator
191
		: iterator
192
	{
193
	public:
194
		typedef charT                         char_type;
195
		typedef traits                        traits_type;
196
		typedef basic_streambuf streambuf_type;
197
		typedef basic_ostream   ostream_type;
198
	public:
199
		ostreambuf_iterator(ostream_type& s) throw() : sbuf(s.rdbuf()), f(false) { }
200
		ostreambuf_iterator(streambuf_type* s) throw() : sbuf(s), f(false) { }
201
		ostreambuf_iterator& operator=(charT c){
202
			if(failed() == false){
203
				if(sbuf->sputc(c) == traits::eof()){
204
					f = true;
205
				}
206
			}
207
			return *this;
208
		}
209
		ostreambuf_iterator& operator*(){
210
			return *this;
211
		}
212
		ostreambuf_iterator& operator++() { return *this; }
213
		ostreambuf_iterator operator++(int) { return *this; }
214
		bool failed() const throw(){
215
			return f;
216
		}
217
 
218
	private:
219
		streambuf_type* sbuf;
220
		bool f;
221
	};
222
 
223
}
224
 
225
#pragma GCC visibility pop
226
 
227
#endif
228