/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++ 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
#include <cstddef>
22
#include <ios>
23
#include <cctype>
24
#include <string>
25
26
#ifndef __STD_HEADER_OSTREAM_HELPERS
27
#define __STD_HEADER_OSTREAM_HELPERS 1
28
29
#pragma GCC visibility push(default)
30
31
namespace std{
32
33
	/* We are making the following template class for serveral reasons.  Firstly,
34
	 * we want to keep the main ostream code neat and tidy.  Secondly, we want it
35
	 * to be easy to do partial specialization of the ostream code so that it can
36
	 * be expanded and put into the library.  This will allow us to make application
37
	 * code smaller at the expense of increased library size.  This is a fair
38
	 * trade-off when there are multiple applications being compiled.  Also, this
39
	 * feature will be used optionally via configuration options.  It will also
40
	 * allow us to keep the code bases in sync, dramatically simplifying the
41
	 * maintenance required.  We specialized for char because wchar and others
42
	 * require different scanf functions
43
	 */
44
45
46
47
	template <class traits, class charT, class dataType> class _UCXXEXPORT __ostream_printout{
48
	public:
49
		static void printout(basic_ostream<charT,traits>& stream, const dataType n);
50
	};
51
52
	template <class traits> class _UCXXEXPORT __ostream_printout<traits, char, signed long int>{
53
	public:
54
		static void printout(basic_ostream<char, traits >& stream, const signed long int n)
55
		{
56
			char buffer[20];
57
			const char * c_ld = "%ld";
58
			const char * c_lo = "%lo";
59
			const char * c_lX = "%lX";
60
			const char * c_lx = "%lx";
61
			const char * c_hashlo = "%#lo";
62
			const char * c_hashlX = "%#lX";
63
			const char * c_hashlx = "%#lx";
64
65
			const char * formatString=0;
66
67
			if( stream.flags() & ios_base::dec){
68
				formatString = c_ld;
69
			}else if( stream.flags() & ios_base::oct){
70
				if( stream.flags() & ios_base::showbase){
71
					formatString = c_hashlo;
72
				}else{
73
					formatString = c_lo;
74
				}
75
			}else if (stream.flags() & ios_base::hex){
76
				if(stream.flags() & ios_base::showbase){
77
					if(stream.flags() & ios_base::uppercase){
78
						formatString = c_hashlX;
79
					}else{
80
						formatString = c_hashlx;
81
					}
82
				}else{
83
					if(stream.flags() & ios_base::uppercase){
84
						formatString = c_lX;
85
					}else{
86
						formatString = c_lx;
87
					}
88
				}
89
			}
90
91
			stream.printout(buffer, snprintf(buffer, 20, formatString, n) );
92
93
			if(stream.flags() & ios_base::unitbuf){
94
				stream.flush();
95
			}
96
97
		}
98
	};
99
100
	template <class traits> class _UCXXEXPORT __ostream_printout<traits, char, unsigned long int>{
101
	public:
102
		static void printout(basic_ostream<char, traits >& stream, const unsigned long int n)
103
		{
104
			char buffer[20];
105
			const char * c_lo = "%lo";
106
			const char * c_lu = "%lu";
107
			const char * c_lX = "%lX";
108
			const char * c_lx = "%lx";
109
			const char * c_hashlo = "%#lo";
110
			const char * c_hashlX = "%#lX";
111
			const char * c_hashlx = "%#lx";
112
			const char * formatString=0;
113
114
			if( stream.flags() & ios_base::dec){
115
				formatString = c_lu;
116
			}else if( stream.flags() & ios_base::oct){
117
				if( stream.flags() & ios_base::showbase){
118
					formatString = c_hashlo;
119
				}else{
120
					formatString = c_lo;
121
				}
122
			}else if (stream.flags() & ios_base::hex){
123
				if(stream.flags() & ios_base::showbase){
124
					if(stream.flags() & ios_base::uppercase){
125
						formatString = c_hashlX;
126
					}else{
127
						formatString = c_hashlx;
128
					}
129
				}else{
130
					if(stream.flags() & ios_base::uppercase){
131
						formatString = c_lX;
132
					}else{
133
						formatString = c_lx;
134
					}
135
				}
136
			}
137
138
			stream.printout(buffer, snprintf(buffer, 20, formatString, n));
139
			if(stream.flags() & ios_base::unitbuf){
140
				stream.flush();
141
			}
142
		}
143
	};
144
145
#ifndef __STRICT_ANSI__
146
147
	template <class traits> class _UCXXEXPORT __ostream_printout<traits, char, signed long long int>{
148
	public:
149
		static void printout(basic_ostream<char, traits >& stream, const signed long long int n)
150
		{
151
			char buffer[28];
152
			const char * lld = "%lld";
153
			const char * llo = "%llo";
154
			const char * llX = "%llX";
155
			const char * llx = "%llx";
156
			const char * hashllo = "%#llo";
157
			const char * hashllX = "%#llX";
158
			const char * hashllx = "%#llx";
159
			const char * formatString=0;
160
161
			if( stream.flags() & ios_base::dec){
162
				formatString = lld;
163
			}else if( stream.flags() & ios_base::oct){
164
				if( stream.flags() & ios_base::showbase){
165
					formatString = hashllo;
166
				}else{
167
					formatString = llo;
168
				}
169
			}else if (stream.flags() & ios_base::hex){
170
				if(stream.flags() & ios_base::showbase){
171
					if(stream.flags() & ios_base::uppercase){
172
						formatString = hashllX;
173
					}else{
174
						formatString = hashllx;
175
					}
176
				}else{
177
					if(stream.flags() & ios_base::uppercase){
178
						formatString = llX;
179
					}else{
180
						formatString = llx;
181
					}
182
				}
183
			}
184
185
			stream.printout(buffer, snprintf(buffer, 27, formatString, n) );
186
187
			if(stream.flags() & ios_base::unitbuf){
188
				stream.flush();
189
			}
190
		}
191
	};
192
193
	template <class traits> class _UCXXEXPORT __ostream_printout<traits, char, unsigned long long int>{
194
	public:
195
		static void printout(basic_ostream<char, traits >& stream, const unsigned long long int n)
196
		{
197
			char buffer[28];
198
			const char * llo = "%llo";
199
			const char * llu = "%llu";
200
			const char * llX = "%llX";
201
			const char * llx = "%llx";
202
			const char * hashllo = "%#llo";
203
			const char * hashllX = "%#llX";
204
			const char * hashllx = "%#llx";
205
			const char * formatString=0;
206
207
			if( stream.flags() & ios_base::dec){
208
				formatString = llu;
209
			}else if( stream.flags() & ios_base::oct){
210
				if( stream.flags() & ios_base::showbase){
211
					formatString = hashllo;
212
				}else{
213
					formatString = llo;
214
				}
215
			}else if (stream.flags() & ios_base::hex){
216
				if(stream.flags() & ios_base::showbase){
217
					if(stream.flags() & ios_base::uppercase){
218
						formatString = hashllX;
219
					}else{
220
						formatString = hashllx;
221
					}
222
				}else{
223
					if(stream.flags() & ios_base::uppercase){
224
						formatString = llX;
225
					}else{
226
						formatString = llx;
227
					}
228
				}
229
			}
230
231
			stream.printout(buffer, snprintf(buffer, 27, formatString, n) );
232
233
			if(stream.flags() & ios_base::unitbuf){
234
				stream.flush();
235
			}
236
		}
237
	};
238
239
240
#endif	//__STRICT_ANSI__
241
242
	template <class traits> class _UCXXEXPORT __ostream_printout<traits, char, float>{
243
	public:
244
		static void printout(basic_ostream<char, traits >& stream, const float f)
245
		{
246
			char buffer[32];
247
			int length;
248
			
249
			//length = snprintf(buffer, 32, "%*.*f",static_cast<int>(stream.width()),static_cast<int>(stream.precision()), f);
250
			length = strlen(dtostrf(f, static_cast<int>(stream.width()), static_cast<int>(stream.precision()), buffer));
251
					
252
			stream.printout(buffer, length);
253
			if(stream.flags() & ios_base::unitbuf){
254
				stream.flush();
255
			}
256
		}
257
	};
258
259
	template <class traits> class _UCXXEXPORT __ostream_printout<traits, char, double>{
260
	public:
261
		static void printout(basic_ostream<char, traits >& stream, const double f)
262
		{
263
			char buffer[32];
264
			int length;
265
			if(stream.flags() & ios_base::scientific){
266
				if(stream.flags() & ios_base::uppercase){
267
#if defined( __AVR__ )
268
					length = strlen(dtostre(f, buffer, static_cast<int>(stream.precision()),  0x04 ));
269
#else		
270
					length = snprintf(buffer, 32, "%*.*E", static_cast<int>(stream.width()),static_cast<int>(stream.precision()), f);
271
#endif
272
				}else{
273
#if defined( __AVR__ )
274
					length = strlen(dtostre(f, buffer, static_cast<int>(stream.precision()), 0));
275
#else				
276
					length = snprintf(buffer, 32, "%*.*e", static_cast<int>(stream.width()),static_cast<int>(stream.precision()), f);
277
#endif					
278
				}
279
			} else if(stream.flags() & ios_base::fixed){
280
#if defined( __AVR__ )
281
				length = strlen(dtostrf(f, static_cast<int>(stream.width()), static_cast<int>(stream.precision()), buffer));
282
#else			
283
				length = snprintf(buffer, 32, "%*.*f",static_cast<int>(stream.width()),static_cast<int>(stream.precision()), f);
284
#endif				
285
			} else {
286
#if defined( __AVR__ )
287
				length = strlen(dtostrf(f, static_cast<int>(stream.width()), static_cast<int>(stream.precision()), buffer));
288
#else			
289
				length = snprintf(buffer, 32, "%*.*g",static_cast<int>(stream.width()),static_cast<int>(stream.precision()), f);
290
#endif				
291
			}
292
			stream.printout(buffer, length);
293
			if(stream.flags() & ios_base::unitbuf){
294
				stream.flush();
295
			}
296
		}
297
	};
298
299
	template <class traits> class _UCXXEXPORT __ostream_printout<traits, char, long double>{
300
	public:
301
		static void printout(basic_ostream<char, traits >& stream, const long double f)
302
		{
303
			char buffer[32];
304
			int length;
305
			if(stream.flags() & ios_base::scientific){
306
				if(stream.flags() & ios_base::uppercase){
307
					length = snprintf(buffer, 32, "%*.*LE", static_cast<int>(stream.width()), static_cast<int>(stream.precision()), f);
308
				}else{
309
					length = snprintf(buffer, 32, "%*.*Le", static_cast<int>(stream.width()), static_cast<int>(stream.precision()), f);
310
				}
311
			} else if(stream.flags() & ios_base::fixed){
312
				length = snprintf(buffer, 32, "%*.*Lf", static_cast<int>(stream.width()), static_cast<int>(stream.precision()), f);
313
			} else {
314
				length = snprintf(buffer, 32, "%*.*Lg", static_cast<int>(stream.width()), static_cast<int>(stream.precision()), f);
315
			}
316
			stream.printout(buffer, length);
317
			if(stream.flags() & ios_base::unitbuf){
318
				stream.flush();
319
			}
320
321
		}
322
	};
323
324
#ifdef __UCLIBCXX_HAS_WCHAR__
325
	template <class traits> class _UCXXEXPORT __ostream_printout<traits, wchar_t, signed long int>{
326
	public:
327
		static void printout(basic_ostream<wchar_t, traits >& stream, const signed long int n)
328
		{
329
			wchar_t buffer[20];
330
			if( stream.flags() & ios_base::dec){
331
				stream.printout(buffer, swprintf(buffer, 20, L"%ld", n));
332
			}else if( stream.flags() & ios_base::oct){
333
				if( stream.flags() & ios_base::showbase){
334
					stream.printout(buffer, swprintf(buffer, 20, L"%#lo", n));
335
				}else{
336
					stream.printout(buffer, swprintf(buffer, 20, L"%lo", n) );
337
				}
338
			}else if (stream.flags() & ios_base::hex){
339
				if(stream.flags() & ios_base::showbase){
340
					if(stream.flags() & ios_base::uppercase){
341
						stream.printout(buffer, swprintf(buffer, 20, L"%#lX", n) );
342
					}else{
343
						stream.printout(buffer, swprintf(buffer, 20, L"%#lx", n) );
344
					}
345
				}else{
346
					if(stream.flags() & ios_base::uppercase){
347
						stream.printout(buffer, swprintf(buffer, 20, L"%lX", n) );
348
					}else{
349
						stream.printout(buffer, swprintf(buffer, 20, L"%lx", n) );
350
					}
351
				}
352
			}
353
			if(stream.flags() & ios_base::unitbuf){
354
				stream.flush();
355
			}
356
		}
357
	};
358
359
	template <class traits> class _UCXXEXPORT __ostream_printout<traits, wchar_t, unsigned long int>{
360
	public:
361
		static void printout(basic_ostream<wchar_t, traits >& stream, const unsigned long int n)
362
		{
363
			wchar_t buffer[20];
364
			if( stream.flags() & ios_base::dec){
365
				stream.printout(buffer, swprintf(buffer, 20, L"%lu", n));
366
			}else if( stream.flags() & ios_base::oct){
367
				if( stream.flags() & ios_base::showbase){
368
					stream.printout(buffer, swprintf(buffer, 20, L"%#lo", n));
369
				}else{
370
					stream.printout(buffer, swprintf(buffer, 20, L"%lo", n) );
371
				}
372
			}else if (stream.flags() & ios_base::hex){
373
				if(stream.flags() & ios_base::showbase){
374
					if(stream.flags() & ios_base::uppercase){
375
						stream.printout(buffer, swprintf(buffer, 20, L"%#lX", n) );
376
					}else{
377
						stream.printout(buffer, swprintf(buffer, 20, L"%#lx", n) );
378
					}
379
				}else{
380
					if(stream.flags() & ios_base::uppercase){
381
						stream.printout(buffer, swprintf(buffer, 20, L"%lX", n) );
382
					}else{
383
						stream.printout(buffer, swprintf(buffer, 20, L"%lx", n) );
384
					}
385
				}
386
			}
387
			if(stream.flags() & ios_base::unitbuf){
388
				stream.flush();
389
			}
390
		}
391
	};
392
393
#ifndef __STRICT_ANSI__
394
395
	template <class traits> class _UCXXEXPORT __ostream_printout<traits, wchar_t, signed long long int>{
396
	public:
397
		static void printout(basic_ostream<wchar_t, traits >& stream, const signed long long int n)
398
		{
399
			wchar_t buffer[28];
400
			if( stream.flags() & ios_base::dec){
401
				stream.printout(buffer, swprintf(buffer, 27, L"%lld", n));
402
			}else if( stream.flags() & ios_base::oct){
403
				if( stream.flags() & ios_base::showbase){
404
					stream.printout(buffer, swprintf(buffer, 27, L"%#llo", n));
405
				}else{
406
					stream.printout(buffer, swprintf(buffer, 27, L"%llo", n) );
407
				}
408
			}else if (stream.flags() & ios_base::hex){
409
				if(stream.flags() & ios_base::showbase){
410
					if(stream.flags() & ios_base::uppercase){
411
						stream.printout(buffer, swprintf(buffer, 27, L"%#llX", n) );
412
					}else{
413
						stream.printout(buffer, swprintf(buffer, 27, L"%#llx", n) );
414
					}
415
				}else{
416
					if(stream.flags() & ios_base::uppercase){
417
						stream.printout(buffer, swprintf(buffer, 27, L"%llX", n) );
418
					}else{
419
						stream.printout(buffer, swprintf(buffer, 27, L"%llx", n) );
420
					}
421
				}
422
			}
423
			if(stream.flags() & ios_base::unitbuf){
424
				stream.flush();
425
			}
426
		}
427
	};
428
429
	template <class traits> class _UCXXEXPORT __ostream_printout<traits, wchar_t, unsigned long long int>{
430
	public:
431
		static void printout(basic_ostream<wchar_t, traits >& stream, const unsigned long long int n)
432
		{
433
			wchar_t buffer[28];
434
			if( stream.flags() & ios_base::dec){
435
				stream.printout(buffer, swprintf(buffer, 27, L"%llu", n));
436
			}else if( stream.flags() & ios_base::oct){
437
				if( stream.flags() & ios_base::showbase){
438
					stream.printout(buffer, swprintf(buffer, 27, L"%#llo", n));
439
				}else{
440
					stream.printout(buffer, swprintf(buffer, 27, L"%llo", n) );
441
				}
442
			}else if (stream.flags() & ios_base::hex){
443
				if(stream.flags() & ios_base::showbase){
444
					if(stream.flags() & ios_base::uppercase){
445
						stream.printout(buffer, swprintf(buffer, 27, L"%#llX", n) );
446
					}else{
447
						stream.printout(buffer, swprintf(buffer, 27, L"%#llx", n) );
448
					}
449
				}else{
450
					if(stream.flags() & ios_base::uppercase){
451
						stream.printout(buffer, swprintf(buffer, 27, L"%llX", n) );
452
					}else{
453
						stream.printout(buffer, swprintf(buffer, 27, L"%llx", n) );
454
					}
455
				}
456
			}
457
			if(stream.flags() & ios_base::unitbuf){
458
				stream.flush();
459
			}
460
		}
461
	};
462
463
464
#endif	//__STRICT_ANSI__
465
466
	template <class traits> class _UCXXEXPORT __ostream_printout<traits, wchar_t, double>{
467
	public:
468
		static void printout(basic_ostream<wchar_t, traits >& stream, const double f)
469
		{
470
			wchar_t buffer[32];
471
			wchar_t format_string[32];
472
			if(stream.flags() & ios_base::scientific){
473
				if(stream.flags() & ios_base::uppercase){
474
					swprintf(format_string, 32, L"%%%u.%uE", static_cast<int>(stream.width()), static_cast<unsigned int>(stream.precision()));
475
				}else{
476
					swprintf(format_string, 32, L"%%%u.%ue", static_cast<int>(stream.width()), static_cast<unsigned int>(stream.precision()));
477
				}
478
			} else if(stream.flags() & ios_base::fixed){
479
				swprintf(format_string, 32, L"%%%u.%uf", static_cast<int>(stream.width()), static_cast<unsigned int>(stream.precision()));
480
			} else {
481
				swprintf(format_string, 32, L"%%%u.%ug", static_cast<int>(stream.width()), static_cast<unsigned int>(stream.precision()));
482
			}
483
			stream.printout(buffer, swprintf(buffer, 32, format_string, f) );
484
			if(stream.flags() & ios_base::unitbuf){
485
				stream.flush();
486
			}
487
		}
488
	};
489
490
	template <class traits> class _UCXXEXPORT __ostream_printout<traits, wchar_t, long double>{
491
	public:
492
		static void printout(basic_ostream<wchar_t, traits >& stream, const long double f)
493
		{
494
			wchar_t buffer[32];
495
			wchar_t format_string[32];
496
			if(stream.flags() & ios_base::scientific){
497
				if(stream.flags() & ios_base::uppercase){
498
					swprintf(format_string, 32, L"%%%u.%uLE", static_cast<unsigned int>(stream.width()), static_cast<unsigned int>(stream.precision()));
499
				}else{
500
					swprintf(format_string, 32, L"%%%u.%uLe", static_cast<unsigned int>(stream.width()), static_cast<unsigned int>(stream.precision()));
501
				}
502
			} else if(stream.flags() & ios_base::fixed){
503
				swprintf(format_string, 32, L"%%%u.%uLf", static_cast<unsigned int>(stream.width()), static_cast<unsigned int>(stream.precision()));
504
			} else {
505
				swprintf(format_string, 32, L"%%%u.%uLg", static_cast<unsigned int>(stream.width()), static_cast<unsigned int>(stream.precision()));
506
			}
507
			stream.printout(buffer, swprintf(buffer, 32, format_string, f) );
508
			if(stream.flags() & ios_base::unitbuf){
509
				stream.flush();
510
			}
511
		}
512
	};
513
514
#endif //__UCLIBCXX_HAS_WCHAR__
515
516
}
517
518
#pragma GCC visibility pop
519
520
#endif
521
522
523