/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock
57 by edam
added ulibc
1
/*	Copyright (C) 2004 Garrett A. Kajmowicz
2
3
	This file is part of the uClibc C++ Library.  This library is free
4
	software; you can redistribute it and/or modify it under the
5
	terms of the GNU General Public License as published by the
6
	Free Software Foundation; either version 2, or (at your option)
7
	any later version.
8
9
	This library is distributed in the hope that it will be useful,
10
	but WITHOUT ANY WARRANTY; without even the implied warranty of
11
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
	GNU General Public License for more details.
13
14
	You should have received a copy of the GNU General Public License along
15
	with this library; see the file COPYING.  If not, write to the Free
16
	Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17
	USA.
18
*/
19
20
#include <ios>
21
#include <cctype>
22
#include <streambuf>
23
#include <istream_helpers>
24
#include <ostream>
25
26
#ifndef __STD_HEADER_ISTREAM
27
#define __STD_HEADER_ISTREAM 1
28
29
#pragma GCC visibility push(default)
30
31
namespace std{
32
33
	typedef basic_istream<char> istream;
34
35
#ifdef __UCLIBCXX_HAS_WCHAR__
36
	typedef basic_istream<wchar_t> wistream;
37
#endif
38
39
	template <class charT, class traits> basic_istream<charT,traits>& ws(basic_istream<charT,traits>& is);
40
41
	template <class charT, class traits> class _UCXXEXPORT basic_istream :
42
		virtual public basic_ios<charT,traits>
43
	{
44
	public:
45
	
46
		typedef charT                     char_type;
47
		typedef typename traits::int_type int_type;
48
		typedef typename traits::pos_type pos_type;
49
		typedef typename traits::off_type off_type;
50
		typedef basic_streambuf<charT,traits>  streambuf_type;
51
		typedef traits                    traits_type;
52
    	
53
		explicit basic_istream(basic_streambuf<charT,traits>* sb)
54
			: basic_ios<charT, traits>(sb), count_last_ufmt_input(0)
55
		{
56
			basic_ios<charT, traits>::init(sb);
57
		}
58
		virtual ~basic_istream() { }
59
	
60
		class sentry;
61
	
62
		basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>& (*pf)(basic_istream<charT,traits>&));
63
		basic_istream<charT,traits>& operator>>(basic_ios<charT,traits>& (*pf)(basic_ios<charT,traits>&));
64
		basic_istream<charT,traits>& operator>>(ios_base& (*pf)(ios_base&));
65
		basic_istream<charT,traits>& operator>>(bool& n);
66
		basic_istream<charT,traits>& operator>>(short& n);
67
		basic_istream<charT,traits>& operator>>(unsigned short& n);
68
		basic_istream<charT,traits>& operator>>(int& n);
69
		basic_istream<charT,traits>& operator>>(unsigned int& n);
70
		basic_istream<charT,traits>& operator>>(long& n);
71
		basic_istream<charT,traits>& operator>>(unsigned long& n);
72
		basic_istream<charT,traits>& operator>>(void*& p);
73
		basic_istream<charT,traits>& operator>>(basic_streambuf<char_type,traits>* sb);
74
75
#ifdef __UCLIBCXX_HAS_FLOATS__
76
		basic_istream<charT,traits>& operator>>(float& f);
77
		basic_istream<charT,traits>& operator>>(double& f);
78
		basic_istream<charT,traits>& operator>>(long double& f);
79
#endif
80
	
81
		_UCXXEXPORT streamsize gcount() const{
82
			return count_last_ufmt_input;
83
		}
84
		
85
		_UCXXEXPORT int_type get();		//below
86
		_UCXXEXPORT basic_istream<charT,traits>& get(char_type& c); 	//Below
87
		
88
		_UCXXEXPORT basic_istream<charT,traits>& get(char_type* s, streamsize n){
89
			return get(s, n, basic_ios<charT,traits>::widen('\n'));
90
		}
91
		
92
		_UCXXEXPORT basic_istream<charT,traits>& get(char_type* s, streamsize n, char_type delim){
93
			sentry(*this, true);
94
			streamsize i = 0;
95
			int_type c;
96
			for(i=0;i<n-1;++i){
97
				c = basic_ios<charT, traits>::mstreambuf->sgetc();
98
				basic_ios<charT, traits>::mstreambuf->sbumpc();
99
				if(c == traits::eof() ){
100
					if(i==0){
101
						basic_ios<charT,traits>::setstate(ios_base::failbit);
102
					}else{
103
						basic_ios<charT,traits>::setstate(ios_base::eofbit);
104
					}
105
					break;
106
				}
107
				if(c == delim){
108
					if(i==0){
109
						basic_ios<charT,traits>::setstate(ios_base::failbit);
110
					}
111
					basic_ios<charT, traits>::mstreambuf->sputbackc(c);
112
					break;
113
				}
114
				s[i] = c;
115
			}
116
			s[i] = traits::eos();
117
			count_last_ufmt_input = i;
118
			return *this;
119
		}
120
		
121
		_UCXXEXPORT basic_istream<charT,traits>& get(basic_streambuf<char_type,traits>& sb){
122
			return get(sb, basic_ios<charT,traits>::widen('\n'));
123
		}
124
		
125
		_UCXXEXPORT basic_istream<charT,traits>& get(basic_streambuf<char_type,traits>& sb, char_type delim){
126
			sentry(*this, true);
127
			streamsize i = 0;
128
			int_type c;
129
			while(1){		//We will exit internally based upon error conditions
130
				c = basic_ios<charT, traits>::mstreambuf->sgetc();
131
				if(c == traits::eof()){
132
					if(i==0){
133
						basic_ios<charT,traits>::setstate(ios_base::failbit);
134
					}else{
135
						basic_ios<charT,traits>::setstate(ios_base::eofbit);
136
					}
137
					count_last_ufmt_input = i;
138
					return *this;
139
				}
140
				if(c == delim){
141
					if(i==0){
142
						basic_ios<charT,traits>::setstate(ios_base::failbit);
143
					}
144
					count_last_ufmt_input = i;
145
					return *this;
146
				}
147
				if(sb.sputc(c) != c){	//Error doing output
148
					count_last_ufmt_input = i;
149
					return *this;
150
				}
151
				++i;
152
				basic_ios<charT, traits>::mstreambuf->sbumpc();
153
			}		
154
		}
155
		
156
		_UCXXEXPORT basic_istream<charT,traits>& getline(char_type* s, streamsize n){
157
			return getline(s, n, basic_ios<charT,traits>::widen('\n'));
158
		}
159
		
160
		_UCXXEXPORT basic_istream<charT,traits>& getline(char_type* s, streamsize n, char_type delim){
161
			sentry(*this, true);
162
			streamsize i = 0;
163
			int_type c;
164
			for(i=0;i<n-1;++i){
165
				c = basic_ios<charT, traits>::mstreambuf->sgetc();
166
				if(c == traits::eof() ){
167
					if( basic_ios<charT,traits>::eof() ){
168
						basic_ios<charT,traits>::setstate(ios_base::failbit);
169
					}else{
170
						basic_ios<charT,traits>::setstate(ios_base::eofbit);
171
					}
172
					count_last_ufmt_input = i;
173
					s[i] = traits::eos();
174
					return *this;
175
				}
176
				if(basic_ios<charT, traits>::mstreambuf->sbumpc()==traits::eof() ){
177
					basic_ios<charT,traits>::setstate(ios_base::eofbit);
178
				}
179
				if(c == delim){
180
					count_last_ufmt_input = i+1;
181
					s[i] = traits::eos();
182
					return *this;
183
				}
184
				s[i] = c;
185
			}
186
			s[n-1] = traits::eos();
187
			return *this;
188
		}
189
		
190
		_UCXXEXPORT basic_istream<charT,traits>& ignore (streamsize n = 1, int_type delim = traits::eof()){
191
			sentry(*this, true);
192
			streamsize i;
193
			int_type c;
194
			for(i=0;i<n;++i){
195
				c = basic_ios<charT, traits>::mstreambuf->sgetc();
196
				if(c == traits::eof()){
197
					basic_ios<charT,traits>::setstate(ios_base::eofbit);
198
					return *this;
199
				}
200
				basic_ios<charT, traits>::mstreambuf->sbumpc();
201
				if(c == delim){
202
					return *this;
203
				}
204
			}
205
			return *this;
206
		}
207
		
208
		_UCXXEXPORT int_type peek(){
209
			if(basic_ios<charT,traits>::good() == false){
210
				return traits::eof();
211
			}else{
212
				int_type c = basic_ios<charT, traits>::mstreambuf->sgetc();
213
				if(c == traits::eof()){
214
					basic_ios<charT,traits>::setstate(ios_base::eofbit);
215
				}
216
				return basic_ios<charT, traits>::mstreambuf->sgetc();
217
			}
218
		}
219
		
220
		_UCXXEXPORT basic_istream<charT,traits>& read (char_type* s, streamsize n){
221
			sentry(*this, true);
222
			streamsize i;
223
			int_type c;
224
			for(i=0;i<n;++i){
225
				c = basic_ios<charT, traits>::mstreambuf->sgetc();
226
227
				if(c == traits::eof()){
228
					basic_ios<charT,traits>::setstate(ios_base::failbit);
229
					basic_ios<charT,traits>::setstate(ios_base::eofbit);
230
					count_last_ufmt_input = i;
231
					return *this;
232
				}
233
				basic_ios<charT, traits>::mstreambuf->sbumpc();
234
				s[i] = c;
235
			}
236
			count_last_ufmt_input = n;
237
			return *this;
238
		}
239
		
240
		_UCXXEXPORT streamsize readsome(char_type* s, streamsize n){
241
			sentry(*this, true);
242
			if(!basic_ios<charT,traits>::good()){
243
				count_last_ufmt_input = 0;
244
				basic_ios<charT,traits>::setstate(ios_base::failbit);
245
				return 0;
246
			}
247
			
248
			if( basic_ios<charT, traits>::mstreambuf->in_avail()  ==  -1){
249
				count_last_ufmt_input=0;
250
				basic_ios<charT,traits>::setstate(ios_base::eofbit);
251
				return 0;
252
			}
253
			
254
			if(n > basic_ios<charT, traits>::mstreambuf->in_avail() ){
255
				n = basic_ios<charT, traits>::mstreambuf->in_avail();
256
			}
257
			
258
			streamsize i;
259
			int_type c;
260
			
261
			for(i=0;i<n;++i){
262
				c = basic_ios<charT, traits>::mstreambuf->sgetc();
263
				basic_ios<charT, traits>::mstreambuf->sbumpc();
264
				s[i] = c;
265
			}
266
			count_last_ufmt_input = n;
267
			return n;
268
		}
269
		
270
		_UCXXEXPORT basic_istream<charT,traits>& putback(char_type c){
271
			sentry(*this, true);
272
			if(!basic_ios<charT,traits>::good()){
273
				basic_ios<charT,traits>::setstate(ios_base::failbit);
274
				return *this;
275
			}
276
			if(basic_ios<charT, traits>::mstreambuf == 0){
277
				basic_ios<charT,traits>::setstate(ios_base::badbit);
278
				return *this;
279
			}
280
			if(basic_ios<charT, traits>::mstreambuf->sputbackc(c) == traits::eof()){
281
				basic_ios<charT,traits>::setstate(ios_base::badbit);
282
				return *this;
283
			}
284
			return *this;
285
		}
286
		
287
		_UCXXEXPORT basic_istream<charT,traits>& unget(){
288
			sentry(*this, true);
289
			if(!basic_ios<charT,traits>::good()){
290
				basic_ios<charT,traits>::setstate(ios_base::failbit);
291
				return *this;
292
			}
293
			if(basic_ios<charT, traits>::mstreambuf == 0){
294
				basic_ios<charT,traits>::setstate(ios_base::failbit);
295
				return *this;
296
			}
297
			if(basic_ios<charT, traits>::mstreambuf->sungetc() == traits::eof()){
298
				basic_ios<charT,traits>::setstate(ios_base::failbit);
299
			}
300
			return *this;
301
		}
302
		
303
		_UCXXEXPORT int sync(){
304
			sentry(*this, true);
305
			if(basic_ios<charT, traits>::mstreambuf == 0){
306
				return -1;
307
			}
308
			if(basic_ios<charT, traits>::mstreambuf->pubsync() == -1){
309
				basic_ios<charT,traits>::setstate(ios_base::badbit);
310
				return traits::eof();
311
			}
312
			return 0;
313
		}
314
	
315
		_UCXXEXPORT pos_type tellg(){
316
			if(basic_ios<charT,traits>::fail() !=false){
317
				return pos_type(-1);
318
			}
319
			return basic_ios<charT, traits>::mstreambuf->pubseekoff(0, ios_base::cur, ios_base::in);
320
		}
321
		
322
		_UCXXEXPORT basic_istream<charT,traits>& seekg(pos_type pos){
323
			 if(basic_ios<charT,traits>::fail() !=true){
324
			 	basic_ios<charT, traits>::mstreambuf->pubseekpos(pos);
325
			 }
326
			return *this;		
327
		}
328
		
329
		_UCXXEXPORT basic_istream<charT,traits>& seekg(off_type off, ios_base::seekdir dir){
330
			if(basic_ios<charT,traits>::fail() !=true){
331
				basic_ios<charT, traits>::mstreambuf->pubseekoff(off, dir);
332
			}
333
			return *this;
334
		}
335
		
336
	protected:
337
		_UCXXEXPORT basic_istream(const basic_istream<charT,traits> &): basic_ios<charT, traits>() { }
338
		_UCXXEXPORT basic_istream<charT,traits> & operator=(const basic_istream<charT,traits> &){ return *this; }
339
		streamsize count_last_ufmt_input;
340
341
	};
342
	
343
	template <class charT,class traits = char_traits<charT> > class _UCXXEXPORT basic_istream<charT,traits>::sentry {
344
		bool ok;
345
	public:
346
		explicit _UCXXEXPORT sentry(basic_istream<charT,traits>& os, bool noskipws = false){
347
			if(os.good() !=0){		//Prepare for output
348
			}
349
350
			//Flush any tied buffer
351
			if(os.tie() != 0){
352
				os.tie()->flush();
353
			}
354
			if(!noskipws){
355
				__skipws(os);
356
			}
357
358
			ok = true;
359
		}
360
		_UCXXEXPORT ~sentry() { }
361
		_UCXXEXPORT operator bool() {
362
			return ok;
363
		}
364
	};
365
	
366
	//Template implementations of basic_istream functions which may be partially specialized
367
	//For code reduction
368
369
	template <class charT, class traits>
370
	_UCXXEXPORT typename basic_istream<charT,traits>::int_type basic_istream<charT,traits>::get(){
371
		sentry(*this, true);
372
		int_type retval = basic_ios<charT, traits>::mstreambuf->sgetc();
373
		if(retval == traits::eof()){
374
			count_last_ufmt_input = 0;
375
			basic_ios<charT,traits>::setstate(ios_base::eofbit);
376
		}else{
377
			count_last_ufmt_input = 1;
378
			basic_ios<charT, traits>::mstreambuf->sbumpc();
379
		}
380
		return retval;
381
	}
382
383
	template <class charT, class traits>
384
	_UCXXEXPORT basic_istream<charT,traits>& basic_istream<charT,traits>::get(char_type& c){
385
		sentry(*this, true);
386
		int_type retval = basic_ios<charT, traits>::mstreambuf->sgetc();
387
		if(retval == traits::eof()){
388
			count_last_ufmt_input = 0;
389
			basic_ios<charT,traits>::setstate(ios_base::eofbit);
390
			basic_ios<charT,traits>::setstate(ios_base::failbit);
391
		}else{
392
			count_last_ufmt_input = 1;
393
			c = traits::to_char_type(retval);
394
			basic_ios<charT, traits>::mstreambuf->sbumpc();
395
		}
396
		return *this;
397
	}
398
		
399
400
	template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>& 
401
		basic_istream<charT,traits>::operator>>(bool& n)
402
	{
403
		sentry(*this);
404
		__istream_readin<traits, charT, bool>::readin(*this, n);
405
		return *this;
406
	}
407
408
	template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>& 
409
		basic_istream<charT,traits>::operator>>(short& n)
410
	{
411
		sentry(*this);
412
		__istream_readin<traits, charT, short>::readin(*this, n);
413
		return *this;
414
	}
415
416
	template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>& 
417
		basic_istream<charT,traits>::operator>>(unsigned short& n)
418
	{
419
		sentry(*this);
420
		__istream_readin<traits, charT, unsigned short>::readin(*this, n);
421
		return *this;
422
	}
423
424
	template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>& basic_istream<charT,traits>::operator>>(int& n){
425
		sentry(*this);
426
		__istream_readin<traits, charT, int>::readin(*this, n);
427
		return *this;
428
	}
429
430
	template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>& basic_istream<charT,traits>::operator>>(unsigned int& n){
431
		sentry(*this);
432
		__istream_readin<traits, charT, unsigned int>::readin(*this, n);
433
		return *this;
434
	}
435
436
	template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>& basic_istream<charT,traits>::operator>>(long int& n){
437
		sentry(*this);
438
		__istream_readin<traits, charT, long int>::readin(*this, n);
439
		return *this;
440
	}
441
442
	template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>& 
443
		basic_istream<charT,traits>::operator>>(unsigned long int& n)
444
	{
445
		sentry(*this);
446
		__istream_readin<traits, charT, unsigned long int>::readin(*this, n);
447
		return *this;
448
	}
449
450
#ifdef __UCLIBCXX_HAS_FLOATS__
451
	template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>& 
452
		basic_istream<charT,traits>::operator>>(float& n)
453
	{
454
		sentry(*this);
455
		__istream_readin<traits, charT, float>::readin(*this, n);
456
		return *this;
457
	}
458
	template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>& 
459
		basic_istream<charT,traits>::operator>>(double& n)
460
	{
461
		sentry(*this);
462
		__istream_readin<traits, charT, double>::readin(*this, n);
463
		return *this;
464
	}
465
	template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>& 
466
		basic_istream<charT,traits>::operator>>(long double& n)
467
	{
468
		sentry(*this);
469
		__istream_readin<traits, charT, long double>::readin(*this, n);
470
		return *this;
471
	}
472
#endif
473
	template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>& 
474
		basic_istream<charT,traits>::operator>>(void *& n)
475
	{
476
		sentry(*this);
477
		__istream_readin<traits, charT, void*>::readin(*this, n);
478
		return *this;
479
	}
480
481
	template<class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>&
482
		operator>>(basic_istream<charT,traits>& is, charT& c)
483
	{
484
		typename basic_istream<charT,traits>::sentry s(is);
485
		is.get(c);
486
		return is;
487
	}
488
489
	template<class traits> _UCXXEXPORT basic_istream<char,traits>&
490
		operator>>(basic_istream<char,traits>& is, unsigned char& c)
491
	{
492
		typename basic_istream<char,traits>::sentry s(is);
493
		char b;
494
		is.get(b);
495
		c = b;
496
		return is;
497
	}
498
	template<class traits> _UCXXEXPORT basic_istream<char,traits>&
499
		operator>>(basic_istream<char,traits>& is, signed char& c)
500
	{
501
		typename basic_istream<char,traits>::sentry s(is);
502
		is.get(c);
503
		return is;
504
	}
505
506
	template<class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>&
507
		operator>>(basic_istream<charT,traits>& is, charT* c)
508
	{
509
		typename basic_istream<charT,traits>::sentry s(is);
510
		int n = is.width();
511
		if(n == 0){
512
			n = __STRING_MAX_UNITS;
513
		}
514
		is.get(c, n);
515
		return is;
516
517
	}
518
	template<class traits> _UCXXEXPORT basic_istream<char,traits>&
519
		operator>>(basic_istream<char,traits>& is, unsigned char* c)
520
	{
521
		typename basic_istream<char,traits>::sentry s(is);
522
		int n = is.width();
523
		if(n == 0){
524
			n = __STRING_MAX_UNITS;
525
		}
526
		is.get(c, n);
527
		return is;
528
	}
529
	template<class traits> _UCXXEXPORT basic_istream<char,traits>&
530
		operator>>(basic_istream<char,traits>& is, signed char* c)
531
	{
532
		typename basic_istream<char,traits>::sentry s(is);
533
		int n = is.width();
534
		if(n == 0){
535
			n = __STRING_MAX_UNITS;
536
		}
537
		is.get(c, n);
538
		return is;
539
	}
540
541
	template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>& 
542
		basic_istream<charT,traits>::operator>>(basic_istream<charT,traits>& (*pf)(basic_istream<charT,traits>&))
543
	{
544
		sentry(*this);
545
		pf(*this);
546
		return *this;
547
	}
548
549
	template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>& 
550
		basic_istream<charT,traits>::operator>>(basic_ios<charT,traits>& (*pf)(basic_ios<charT,traits>&))
551
	{
552
		sentry(*this);
553
		pf(*this);
554
		return *this;
555
	}
556
557
	template <class charT, class traits> _UCXXEXPORT basic_istream<charT,traits>&
558
		ws(basic_istream<charT,traits>& is)
559
	{
560
		__skipws(is);
561
		return is;
562
	}
563
564
565
#ifdef __UCLIBCXX_EXPAND_ISTREAM_CHAR__
566
#ifndef __UCLIBCXX_COMPILE_ISTREAM__
567
568
569
	template <> _UCXXEXPORT istream & basic_istream<char, char_traits<char> >::get(char & c);
570
	template <> _UCXXEXPORT istream::int_type basic_istream<char, char_traits<char> >::get();
571
572
	template <> _UCXXEXPORT istream & istream::operator>>(bool &n);
573
	template <> _UCXXEXPORT istream & istream::operator>>(short &n);
574
	template <> _UCXXEXPORT istream & istream::operator>>(unsigned short &n);
575
	template <> _UCXXEXPORT istream & istream::operator>>(int &n);
576
	template <> _UCXXEXPORT istream & istream::operator>>(unsigned int &n);
577
	template <> _UCXXEXPORT istream & istream::operator>>(long unsigned &n);
578
	template <> _UCXXEXPORT istream & istream::operator>>(long int &n);
579
	template <> _UCXXEXPORT istream & istream::operator>>(void *& p);
580
581
#ifdef __UCLIBCXX_HAS_FLOATS__
582
	template <> _UCXXEXPORT istream & istream::operator>>(float &f);
583
	template <> _UCXXEXPORT istream & istream::operator>>(double &f);
584
	template <> _UCXXEXPORT istream & istream::operator>>(long double &f);
585
#endif
586
587
	template <> _UCXXEXPORT istream & operator>>(istream & is, char & c);
588
589
	template <> _UCXXEXPORT void __skipws(basic_istream<char,char_traits<char> >& is);
590
591
#endif
592
#endif
593
	
594
595
596
}
597
598
#pragma GCC visibility pop
599
600
#endif
601