/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-2008 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 <basic_definitions>
21
22
#ifndef STD_HEADER_OSTREAM
23
#define STD_HEADER_OSTREAM 1
24
25
#include <iosfwd>
26
#include <streambuf>
27
#include <cstdio>
28
#include <ostream_helpers>
29
30
#if defined( __AVR__ )
31
#include <avr/pgmspace.h>
32
class __FlashStringHelper;
33
#endif
34
35
#pragma GCC visibility push(default)
36
37
namespace std {
38
	template <class charT, class traits > class basic_ostream;
39
	typedef basic_ostream<char> ostream;
40
41
#ifdef __UCLIBCXX_HAS_WCHAR__
42
	typedef basic_ostream<wchar_t> wostream;
43
#endif
44
45
	template <class charT, class traits> basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
46
	template <class charT, class traits> basic_ostream<charT,traits>& ends(basic_ostream<charT,traits>& os);
47
	template <class charT, class traits> basic_ostream<charT,traits>& flush(basic_ostream<charT,traits>& os);
48
49
	template <class charT, class traits > class _UCXXEXPORT basic_ostream
50
		: virtual public basic_ios<charT,traits>
51
	{
52
	public:
53
54
		typedef charT char_type;
55
		typedef typename traits::int_type int_type;
56
		typedef typename traits::pos_type pos_type;
57
		typedef typename traits::off_type off_type;
58
		typedef traits traits_type;
59
60
61
		_UCXXEXPORT basic_ostream(basic_streambuf<charT,traits>* sb)
62
			: basic_ios<charT, traits>(sb)
63
		{
64
			basic_ios<charT,traits>::init(sb);
65
		}
66
		virtual _UCXXEXPORT ~basic_ostream();
67
68
		class sentry;
69
70
		_UCXXEXPORT basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>& (*pf)(basic_ostream<charT,traits>&)){
71
			return pf(*this);
72
		}
73
		_UCXXEXPORT basic_ostream<charT,traits>& operator<<(basic_ios<charT,traits>& (*pf)(basic_ios<charT,traits>&)){
74
			pf(*this);
75
			return *this;
76
		}
77
		_UCXXEXPORT basic_ostream<charT,traits>& operator<<(ios_base& (*pf)(ios_base&)){
78
			pf(*this);
79
			return *this;
80
		}
81
		basic_ostream<charT,traits>& operator<<(bool n);
82
		basic_ostream<charT,traits>& operator<<(short n);
83
		basic_ostream<charT,traits>& operator<<(unsigned short n);
84
		basic_ostream<charT,traits>& operator<<(int n);
85
		basic_ostream<charT,traits>& operator<<(unsigned int n);
86
		basic_ostream<charT,traits>& operator<<(long n);
87
		basic_ostream<charT,traits>& operator<<(unsigned long n);
88
		basic_ostream<charT,traits>& operator<<(float f);
89
		basic_ostream<charT,traits>& operator<<(double f);
90
		basic_ostream<charT,traits>& operator<<(long double f);
91
		basic_ostream<charT,traits>& operator<<(void* p);
92
		basic_ostream<charT,traits>& operator<<(basic_streambuf<char_type,traits>* sb);
93
#if defined( __AVR__ )
94
		basic_ostream<charT,traits>& operator<<(const __FlashStringHelper* p);
95
#endif
96
97
		_UCXXEXPORT basic_ostream<charT,traits>& put(char_type c){
98
			if(basic_ostream<charT,traits>::traits_type::eq_int_type(
99
				basic_ios<charT, traits>::mstreambuf->sputc(c),
100
				basic_ostream<charT,traits>::traits_type::eof()))
101
			{
102
				basic_ios<charT,traits>::setstate(ios_base::eofbit);
103
			}
104
			return *this;
105
		}
106
		_UCXXEXPORT basic_ostream<charT,traits>& write(const char_type* s, streamsize n){
107
			if(basic_ostream<charT,traits>::traits_type::eq_int_type(
108
				basic_ios<charT, traits>::mstreambuf->sputn(s, n), 
109
				basic_ostream<charT,traits>::traits_type::eof())
110
			){
111
				basic_ios<charT,traits>::setstate(ios_base::eofbit);
112
			}
113
			return *this;
114
		}
115
		_UCXXEXPORT basic_ostream<charT,traits>& flush(){
116
			if(basic_ios<charT, traits>::mstreambuf->pubsync() == -1){
117
				basic_ios<charT,traits>::setstate(ios_base::badbit);
118
			}
119
			return *this;
120
		}
121
		_UCXXEXPORT pos_type tellp(){
122
			if(basic_ios<charT,traits>::fail() != false){
123
				return pos_type(-1);
124
			}
125
			return basic_ios<charT,traits>::rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
126
		}
127
		_UCXXEXPORT basic_ostream<charT,traits>& seekp(pos_type pos){
128
			if( basic_ios<charT,traits>::fail() != true ){
129
				basic_ios<charT,traits>::rdbuf()->pubseekpos(pos);
130
			}
131
			return *this;
132
		}
133
		_UCXXEXPORT basic_ostream<charT,traits>& seekp(off_type off, ios_base::seekdir dir){
134
			if( basic_ios<charT,traits>::fail() != true){
135
				basic_ios<charT,traits>::rdbuf()->pubseekoff(off, dir);
136
			}
137
			return *this;
138
		}
139
140
		_UCXXEXPORT void printout(const char_type* s, streamsize n){
141
			streamsize extra = ios::width() - n;
142
			if ((ios::flags()&ios::adjustfield) == ios::right){
143
				while (extra > 0) {
144
					--extra;
145
					put(ios::fill());
146
				}
147
			}
148
			write(s, n);
149
			if ((ios::flags()&ios::adjustfield) == ios::left) {
150
				while (extra > 0) {
151
					--extra;
152
					put(ios::fill());
153
				}
154
			}
155
			// Width value only applies for the next output operation.  Reset to zero.
156
			ios::width(0);
157
		}
158
159
	protected:
160
		basic_ostream(const basic_ostream<charT,traits> &){ }
161
		basic_ostream<charT,traits> & operator=(const basic_ostream<charT,traits> &){ return *this; }
162
	};
163
164
	//Implementations of template functions.  To allow for partial specialization
165
166
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>::~basic_ostream(){ }
167
	
168
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>& basic_ostream<charT, traits>::operator<<(bool n){
169
		sentry s(*this);
170
		if( basic_ios<charT,traits>::flags() & ios_base::boolalpha){
171
			if(n){
172
				printout("true", 4);
173
			}else{
174
				printout("false", 5);
175
			}
176
		}else{
177
			if(n){
178
				printout("1", 1);
179
			}else{
180
				printout("0", 1);
181
			}
182
		}
183
		if(basic_ios<charT,traits>::flags() & ios_base::unitbuf){
184
			flush();
185
		}
186
		return *this;
187
	}
188
189
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>& 
190
		basic_ostream<charT, traits>::operator<<(unsigned short n){
191
		sentry s(*this);
192
		__ostream_printout<traits, charT, unsigned long int>::printout(*this, n);
193
		return *this;
194
	}
195
196
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>& basic_ostream<charT, traits>::operator<<(short n){
197
		sentry s(*this);
198
		__ostream_printout<traits, charT, long int>::printout(*this, n);
199
		return *this;
200
	}
201
202
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>& basic_ostream<charT, traits>::operator<<(int n){
203
		sentry s(*this);
204
		__ostream_printout<traits, charT, long int>::printout(*this, n);
205
		return *this;
206
	}
207
208
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>& basic_ostream<charT, traits>::operator<<(unsigned int n){
209
		sentry s(*this);
210
		__ostream_printout<traits, charT, unsigned long int>::printout(*this, n);
211
		return *this;
212
	}
213
214
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>& basic_ostream<charT, traits>::operator<<(long n){
215
		sentry s(*this);
216
		__ostream_printout<traits, charT, long >::printout(*this, n);
217
		return *this;
218
	}
219
220
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>& 
221
		basic_ostream<charT, traits>::operator<<(unsigned long n)
222
	{
223
		sentry s(*this);
224
		__ostream_printout<traits, charT, unsigned long >::printout(*this, n);
225
		return *this;
226
	}
227
228
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>& basic_ostream<charT, traits>::operator<<(float f){
229
		sentry s(*this);
230
		__ostream_printout<traits, charT, double >::printout(*this, f);
231
		return *this;
232
	}
233
234
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>& basic_ostream<charT, traits>::operator<<(double f){
235
		sentry s(*this);
236
		__ostream_printout<traits, charT, double >::printout(*this, f);
237
		return *this;
238
	}
239
240
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>& basic_ostream<charT, traits>::operator<<(long double f){
241
		sentry s(*this);
242
		__ostream_printout<traits, charT, long double >::printout(*this, f);
243
		return *this;
244
	}
245
246
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>& basic_ostream<charT, traits>::operator<<(void* p){
247
		sentry s(*this);
248
		char buffer[20];
249
		printout(buffer, snprintf(buffer, 20, "%p", p) );
250
		if(basic_ios<charT,traits>::flags() & ios_base::unitbuf){
251
			flush();
252
		}
253
		return *this;
254
	}
255
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>& 
256
		basic_ostream<charT, traits>::operator<<(basic_streambuf<charT,traits>* sb)
257
	{
258
		sentry s(*this);
259
		if(sb == 0){
260
			basic_ios<charT,traits>::setstate(ios_base::badbit);
261
			return *this;
262
		}
263
264
		typename traits::int_type c;
265
266
		while(basic_ios<charT,traits>::good() && (c = sb->sbumpc()) != traits::eof() ){
267
			put(c);
268
		}
269
270
		if(basic_ios<charT,traits>::flags() & ios_base::unitbuf){
271
			flush();
272
		}
273
		return *this;
274
	}
275
#if defined( __AVR__ )
276
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>& basic_ostream<charT, traits>::operator<<(const __FlashStringHelper* p ){
277
		sentry s(*this);
278
		char buffer[64];
279
		size_t p_len = strlcpy_P(buffer,reinterpret_cast<const char*>(p),sizeof(buffer));
280
		printout(buffer, (p_len>=sizeof(buffer)) ? sizeof(buffer) - 1 : p_len ); 
281
		if(basic_ios<charT,traits>::flags() & ios_base::unitbuf){
282
			flush();
283
		}
284
		return *this;
285
	}
286
#endif
287
288
289
	/*Template Specializations*/
290
291
#ifdef __UCLIBCXX_EXPAND_OSTREAM_CHAR__
292
#ifndef __UCLIBCXX_COMPILE_OSTREAM__
293
294
#ifdef __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__
295
296
	template <> _UCXXEXPORT ostream::~basic_ostream();
297
298
#endif // __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__
299
300
	template <> _UCXXEXPORT ostream & ostream::flush();
301
302
	template <> _UCXXEXPORT ostream & ostream::operator<<(bool n);
303
	template <> _UCXXEXPORT ostream & ostream::operator<<(short int n);
304
	template <> _UCXXEXPORT ostream & ostream::operator<<(unsigned short int n);
305
	template <> _UCXXEXPORT ostream & ostream::operator<<(int n);
306
	template <> _UCXXEXPORT ostream & ostream::operator<<(unsigned int n);
307
	template <> _UCXXEXPORT ostream & ostream::operator<<(long n);
308
	template <> _UCXXEXPORT ostream & ostream::operator<<(unsigned long n);
309
	template <> _UCXXEXPORT ostream & ostream::operator<<(float f);
310
	template <> _UCXXEXPORT ostream & ostream::operator<<(double f);
311
	template <> _UCXXEXPORT ostream & ostream::operator<<(long double f);
312
	template <> _UCXXEXPORT ostream & ostream::operator<<(void* p);
313
	template <> _UCXXEXPORT ostream & ostream::operator<<(basic_streambuf<char, char_traits<char> >* sb);
314
#endif
315
#endif
316
317
	template <class charT,class traits = char_traits<charT> >
318
		class _UCXXEXPORT basic_ostream<charT,traits>::sentry
319
	{
320
		bool ok;
321
	public:
322
		explicit _UCXXEXPORT sentry(basic_ostream<charT,traits>& os): ok(true){
323
			if(os.good() !=0){		//Prepare for output
324
			}
325
326
			//Flush any tied buffer
327
			if(os.tie() !=0 ){
328
				os.tie()->flush();
329
			}
330
		}
331
		_UCXXEXPORT ~sentry() { }
332
		_UCXXEXPORT operator bool() {
333
			return ok;
334
		}
335
	};
336
337
338
#ifdef __UCLIBCXX_EXPAND_OSTREAM_CHAR__
339
#ifndef __UCLIBCXX_COMPILE_OSTREAM__
340
#ifdef __UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__
341
342
	template <> _UCXXEXPORT ostream::sentry::sentry(ostream & os);
343
	template <> _UCXXEXPORT ostream::sentry::~sentry();
344
345
#endif //__UCLIBCXX_EXPAND_CONSTRUCTORS_DESTRUCTORS__
346
#endif
347
#endif
348
349
350
	//Non - class functions
351
352
353
	template<class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>&
354
		operator<<(basic_ostream<charT,traits>& out, charT c)
355
	{
356
		typename basic_ostream<charT,traits>::sentry s(out);
357
		out.put(c);
358
		return out;
359
	}
360
	
361
	template<class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>&
362
		operator<<(basic_ostream<charT,traits>& out, char c)
363
	{
364
		typename basic_ostream<charT,traits>::sentry s(out);
365
		out.put(c);
366
		return out;
367
	}
368
    
369
	template<class traits> _UCXXEXPORT basic_ostream<char,traits>&
370
		operator<<(basic_ostream<char,traits>& out, char c)
371
	{
372
		typename basic_ostream<char,traits>::sentry s(out);
373
		out.put(c);
374
		return out;
375
	}
376
    
377
    // signed and unsigned
378
	template<class traits> _UCXXEXPORT basic_ostream<char,traits>&
379
		operator<<(basic_ostream<char,traits>& out, signed char c)
380
	{
381
		typename basic_ostream<char,traits>::sentry s(out);
382
		out.put(c);
383
		return out;
384
	}
385
	
386
	template<class traits> _UCXXEXPORT basic_ostream<char,traits>&
387
		operator<<(basic_ostream<char,traits>& out, unsigned char c)
388
	{
389
		typename basic_ostream<char,traits>::sentry s(out);
390
		out.put(c);
391
		return out;
392
	}
393
	
394
	template<class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>&
395
		operator<<(basic_ostream<charT,traits>& out, const charT* c)
396
	{
397
		typename basic_ostream<charT,traits>::sentry s(out);
398
		out.printout(c, traits::length(c) );
399
		return out;
400
	}
401
	
402
	template<class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>&
403
		operator<<(basic_ostream<charT,traits>& out, const char* c)
404
	{
405
		typename basic_ostream<charT,traits>::sentry s(out);
406
		out.printout(c, char_traits<char>::length(c) );
407
		return out;
408
	}
409
410
    // partial specializations
411
	template<class traits> _UCXXEXPORT basic_ostream<char,traits>&
412
		operator<<(basic_ostream<char,traits>& out, const char* c)
413
	{
414
		typename basic_ostream<char,traits>::sentry s(out);
415
		out.printout(c, traits::length(c));
416
		return out;
417
	}
418
419
#ifdef __UCLIBCXX_HAS_WCHAR__
420
	template<class traits> _UCXXEXPORT basic_ostream<wchar_t,traits>&
421
		operator<<(basic_ostream<wchar_t,traits>& out, const char* c)
422
	{
423
		typename basic_ostream<wchar_t, traits>::sentry s(out);
424
		size_t numChars = char_traits<char>::length(c);
425
		wchar_t * temp = new wchar_t[numChars];
426
427
		for(size_t i=0; i < numChars; ++i){
428
			temp[i] = out.widen(c[i]);
429
		}
430
431
		out.printout(temp, numChars);
432
		return out;
433
	}
434
#endif
435
    
436
    //  signed and unsigned
437
	template<class traits> _UCXXEXPORT basic_ostream<char,traits>&
438
		operator<<(basic_ostream<char,traits>& out, const signed char* c)
439
	{
440
		typename basic_ostream<char,traits>::sentry s(out);
441
		out.printout(reinterpret_cast<const char *>(c), traits::length( reinterpret_cast<const char *>(c)));
442
		return out;
443
	}
444
	
445
	template<class traits> _UCXXEXPORT basic_ostream<char,traits>&
446
		operator<<(basic_ostream<char,traits>& out, const unsigned char* c)
447
	{
448
		typename basic_ostream<char,traits>::sentry s(out);
449
		out.printout(reinterpret_cast<const char *>(c), traits::length( reinterpret_cast<const char *>(c)));
450
		return out;
451
	}
452
453
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>&
454
		endl(basic_ostream<charT,traits>& os)
455
	{
456
		typename basic_ostream<charT,traits>::sentry s(os);
457
		os.put('\n');
458
		os.flush();
459
		return os;
460
	}
461
462
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>&
463
		ends(basic_ostream<charT,traits>& os)
464
	{
465
		typename basic_ostream<charT,traits>::sentry s(os);
466
		os.put(traits::eos());
467
		return os;
468
	}
469
470
	template <class charT, class traits> _UCXXEXPORT basic_ostream<charT,traits>& flush(basic_ostream<charT,traits>& os){
471
		typename basic_ostream<charT,traits>::sentry s(os);
472
		os.flush();
473
		return os;
474
	}
475
476
477
#ifdef __UCLIBCXX_EXPAND_OSTREAM_CHAR__
478
#ifndef __UCLIBCXX_COMPILE_OSTREAM__
479
	template <> _UCXXEXPORT ostream & endl(ostream & os);
480
	template <> _UCXXEXPORT ostream & flush(ostream & os);
481
	template <> _UCXXEXPORT ostream & operator<<(ostream & out, char c);
482
	template <> _UCXXEXPORT ostream & operator<<(ostream & out, const char* c);
483
	template <> _UCXXEXPORT ostream & operator<<(ostream & out, unsigned char c);
484
	template <> _UCXXEXPORT ostream & operator<<(ostream & out, unsigned const char* c);
485
486
#endif
487
#endif
488
489
490
#ifndef __STRICT_ANSI__
491
492
//Support for output of long long data types
493
494
template<class Ch, class Tr> _UCXXEXPORT basic_ostream<Ch, Tr>& 
495
	operator<<(basic_ostream<Ch, Tr>& os, signed long long int i)
496
{
497
	typename basic_ostream<Ch, Tr>::sentry s(os);
498
	__ostream_printout<Tr, Ch, signed long long int>::printout(os, i);
499
	return os;
500
}
501
502
503
template<class Ch, class Tr> _UCXXEXPORT basic_ostream<Ch, Tr>& 
504
	operator<<(basic_ostream<Ch, Tr>& os, unsigned long long int i)
505
{
506
	typename basic_ostream<Ch, Tr>::sentry s(os);
507
	__ostream_printout<Tr, Ch, unsigned long long int>::printout(os, i);
508
	return os;
509
}
510
511
512
#endif	//__STRICT_ANSI__
513
514
515
516
517
}
518
519
#pragma GCC visibility pop
520
521
#endif
522