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