/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock
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
522
523
/*	Copyright (C) 2004 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>
#include <cstddef>
#include <ios>
#include <cctype>
#include <string>

#ifndef __STD_HEADER_OSTREAM_HELPERS
#define __STD_HEADER_OSTREAM_HELPERS 1

#pragma GCC visibility push(default)

namespace std{

	/* We are making the following template class for serveral reasons.  Firstly,
	 * we want to keep the main ostream code neat and tidy.  Secondly, we want it
	 * to be easy to do partial specialization of the ostream code so that it can
	 * be expanded and put into the library.  This will allow us to make application
	 * code smaller at the expense of increased library size.  This is a fair
	 * trade-off when there are multiple applications being compiled.  Also, this
	 * feature will be used optionally via configuration options.  It will also
	 * allow us to keep the code bases in sync, dramatically simplifying the
	 * maintenance required.  We specialized for char because wchar and others
	 * require different scanf functions
	 */



	template <class traits, class charT, class dataType> class _UCXXEXPORT __ostream_printout{
	public:
		static void printout(basic_ostream<charT,traits>& stream, const dataType n);
	};

	template <class traits> class _UCXXEXPORT __ostream_printout<traits, char, signed long int>{
	public:
		static void printout(basic_ostream<char, traits >& stream, const signed long int n)
		{
			char buffer[20];
			const char * c_ld = "%ld";
			const char * c_lo = "%lo";
			const char * c_lX = "%lX";
			const char * c_lx = "%lx";
			const char * c_hashlo = "%#lo";
			const char * c_hashlX = "%#lX";
			const char * c_hashlx = "%#lx";

			const char * formatString=0;

			if( stream.flags() & ios_base::dec){
				formatString = c_ld;
			}else if( stream.flags() & ios_base::oct){
				if( stream.flags() & ios_base::showbase){
					formatString = c_hashlo;
				}else{
					formatString = c_lo;
				}
			}else if (stream.flags() & ios_base::hex){
				if(stream.flags() & ios_base::showbase){
					if(stream.flags() & ios_base::uppercase){
						formatString = c_hashlX;
					}else{
						formatString = c_hashlx;
					}
				}else{
					if(stream.flags() & ios_base::uppercase){
						formatString = c_lX;
					}else{
						formatString = c_lx;
					}
				}
			}

			stream.printout(buffer, snprintf(buffer, 20, formatString, n) );

			if(stream.flags() & ios_base::unitbuf){
				stream.flush();
			}

		}
	};

	template <class traits> class _UCXXEXPORT __ostream_printout<traits, char, unsigned long int>{
	public:
		static void printout(basic_ostream<char, traits >& stream, const unsigned long int n)
		{
			char buffer[20];
			const char * c_lo = "%lo";
			const char * c_lu = "%lu";
			const char * c_lX = "%lX";
			const char * c_lx = "%lx";
			const char * c_hashlo = "%#lo";
			const char * c_hashlX = "%#lX";
			const char * c_hashlx = "%#lx";
			const char * formatString=0;

			if( stream.flags() & ios_base::dec){
				formatString = c_lu;
			}else if( stream.flags() & ios_base::oct){
				if( stream.flags() & ios_base::showbase){
					formatString = c_hashlo;
				}else{
					formatString = c_lo;
				}
			}else if (stream.flags() & ios_base::hex){
				if(stream.flags() & ios_base::showbase){
					if(stream.flags() & ios_base::uppercase){
						formatString = c_hashlX;
					}else{
						formatString = c_hashlx;
					}
				}else{
					if(stream.flags() & ios_base::uppercase){
						formatString = c_lX;
					}else{
						formatString = c_lx;
					}
				}
			}

			stream.printout(buffer, snprintf(buffer, 20, formatString, n));
			if(stream.flags() & ios_base::unitbuf){
				stream.flush();
			}
		}
	};

#ifndef __STRICT_ANSI__

	template <class traits> class _UCXXEXPORT __ostream_printout<traits, char, signed long long int>{
	public:
		static void printout(basic_ostream<char, traits >& stream, const signed long long int n)
		{
			char buffer[28];
			const char * lld = "%lld";
			const char * llo = "%llo";
			const char * llX = "%llX";
			const char * llx = "%llx";
			const char * hashllo = "%#llo";
			const char * hashllX = "%#llX";
			const char * hashllx = "%#llx";
			const char * formatString=0;

			if( stream.flags() & ios_base::dec){
				formatString = lld;
			}else if( stream.flags() & ios_base::oct){
				if( stream.flags() & ios_base::showbase){
					formatString = hashllo;
				}else{
					formatString = llo;
				}
			}else if (stream.flags() & ios_base::hex){
				if(stream.flags() & ios_base::showbase){
					if(stream.flags() & ios_base::uppercase){
						formatString = hashllX;
					}else{
						formatString = hashllx;
					}
				}else{
					if(stream.flags() & ios_base::uppercase){
						formatString = llX;
					}else{
						formatString = llx;
					}
				}
			}

			stream.printout(buffer, snprintf(buffer, 27, formatString, n) );

			if(stream.flags() & ios_base::unitbuf){
				stream.flush();
			}
		}
	};

	template <class traits> class _UCXXEXPORT __ostream_printout<traits, char, unsigned long long int>{
	public:
		static void printout(basic_ostream<char, traits >& stream, const unsigned long long int n)
		{
			char buffer[28];
			const char * llo = "%llo";
			const char * llu = "%llu";
			const char * llX = "%llX";
			const char * llx = "%llx";
			const char * hashllo = "%#llo";
			const char * hashllX = "%#llX";
			const char * hashllx = "%#llx";
			const char * formatString=0;

			if( stream.flags() & ios_base::dec){
				formatString = llu;
			}else if( stream.flags() & ios_base::oct){
				if( stream.flags() & ios_base::showbase){
					formatString = hashllo;
				}else{
					formatString = llo;
				}
			}else if (stream.flags() & ios_base::hex){
				if(stream.flags() & ios_base::showbase){
					if(stream.flags() & ios_base::uppercase){
						formatString = hashllX;
					}else{
						formatString = hashllx;
					}
				}else{
					if(stream.flags() & ios_base::uppercase){
						formatString = llX;
					}else{
						formatString = llx;
					}
				}
			}

			stream.printout(buffer, snprintf(buffer, 27, formatString, n) );

			if(stream.flags() & ios_base::unitbuf){
				stream.flush();
			}
		}
	};


#endif	//__STRICT_ANSI__

	template <class traits> class _UCXXEXPORT __ostream_printout<traits, char, float>{
	public:
		static void printout(basic_ostream<char, traits >& stream, const float f)
		{
			char buffer[32];
			int length;
			
			//length = snprintf(buffer, 32, "%*.*f",static_cast<int>(stream.width()),static_cast<int>(stream.precision()), f);
			length = strlen(dtostrf(f, static_cast<int>(stream.width()), static_cast<int>(stream.precision()), buffer));
					
			stream.printout(buffer, length);
			if(stream.flags() & ios_base::unitbuf){
				stream.flush();
			}
		}
	};

	template <class traits> class _UCXXEXPORT __ostream_printout<traits, char, double>{
	public:
		static void printout(basic_ostream<char, traits >& stream, const double f)
		{
			char buffer[32];
			int length;
			if(stream.flags() & ios_base::scientific){
				if(stream.flags() & ios_base::uppercase){
#if defined( __AVR__ )
					length = strlen(dtostre(f, buffer, static_cast<int>(stream.precision()),  0x04 ));
#else		
					length = snprintf(buffer, 32, "%*.*E", static_cast<int>(stream.width()),static_cast<int>(stream.precision()), f);
#endif
				}else{
#if defined( __AVR__ )
					length = strlen(dtostre(f, buffer, static_cast<int>(stream.precision()), 0));
#else				
					length = snprintf(buffer, 32, "%*.*e", static_cast<int>(stream.width()),static_cast<int>(stream.precision()), f);
#endif					
				}
			} else if(stream.flags() & ios_base::fixed){
#if defined( __AVR__ )
				length = strlen(dtostrf(f, static_cast<int>(stream.width()), static_cast<int>(stream.precision()), buffer));
#else			
				length = snprintf(buffer, 32, "%*.*f",static_cast<int>(stream.width()),static_cast<int>(stream.precision()), f);
#endif				
			} else {
#if defined( __AVR__ )
				length = strlen(dtostrf(f, static_cast<int>(stream.width()), static_cast<int>(stream.precision()), buffer));
#else			
				length = snprintf(buffer, 32, "%*.*g",static_cast<int>(stream.width()),static_cast<int>(stream.precision()), f);
#endif				
			}
			stream.printout(buffer, length);
			if(stream.flags() & ios_base::unitbuf){
				stream.flush();
			}
		}
	};

	template <class traits> class _UCXXEXPORT __ostream_printout<traits, char, long double>{
	public:
		static void printout(basic_ostream<char, traits >& stream, const long double f)
		{
			char buffer[32];
			int length;
			if(stream.flags() & ios_base::scientific){
				if(stream.flags() & ios_base::uppercase){
					length = snprintf(buffer, 32, "%*.*LE", static_cast<int>(stream.width()), static_cast<int>(stream.precision()), f);
				}else{
					length = snprintf(buffer, 32, "%*.*Le", static_cast<int>(stream.width()), static_cast<int>(stream.precision()), f);
				}
			} else if(stream.flags() & ios_base::fixed){
				length = snprintf(buffer, 32, "%*.*Lf", static_cast<int>(stream.width()), static_cast<int>(stream.precision()), f);
			} else {
				length = snprintf(buffer, 32, "%*.*Lg", static_cast<int>(stream.width()), static_cast<int>(stream.precision()), f);
			}
			stream.printout(buffer, length);
			if(stream.flags() & ios_base::unitbuf){
				stream.flush();
			}

		}
	};

#ifdef __UCLIBCXX_HAS_WCHAR__
	template <class traits> class _UCXXEXPORT __ostream_printout<traits, wchar_t, signed long int>{
	public:
		static void printout(basic_ostream<wchar_t, traits >& stream, const signed long int n)
		{
			wchar_t buffer[20];
			if( stream.flags() & ios_base::dec){
				stream.printout(buffer, swprintf(buffer, 20, L"%ld", n));
			}else if( stream.flags() & ios_base::oct){
				if( stream.flags() & ios_base::showbase){
					stream.printout(buffer, swprintf(buffer, 20, L"%#lo", n));
				}else{
					stream.printout(buffer, swprintf(buffer, 20, L"%lo", n) );
				}
			}else if (stream.flags() & ios_base::hex){
				if(stream.flags() & ios_base::showbase){
					if(stream.flags() & ios_base::uppercase){
						stream.printout(buffer, swprintf(buffer, 20, L"%#lX", n) );
					}else{
						stream.printout(buffer, swprintf(buffer, 20, L"%#lx", n) );
					}
				}else{
					if(stream.flags() & ios_base::uppercase){
						stream.printout(buffer, swprintf(buffer, 20, L"%lX", n) );
					}else{
						stream.printout(buffer, swprintf(buffer, 20, L"%lx", n) );
					}
				}
			}
			if(stream.flags() & ios_base::unitbuf){
				stream.flush();
			}
		}
	};

	template <class traits> class _UCXXEXPORT __ostream_printout<traits, wchar_t, unsigned long int>{
	public:
		static void printout(basic_ostream<wchar_t, traits >& stream, const unsigned long int n)
		{
			wchar_t buffer[20];
			if( stream.flags() & ios_base::dec){
				stream.printout(buffer, swprintf(buffer, 20, L"%lu", n));
			}else if( stream.flags() & ios_base::oct){
				if( stream.flags() & ios_base::showbase){
					stream.printout(buffer, swprintf(buffer, 20, L"%#lo", n));
				}else{
					stream.printout(buffer, swprintf(buffer, 20, L"%lo", n) );
				}
			}else if (stream.flags() & ios_base::hex){
				if(stream.flags() & ios_base::showbase){
					if(stream.flags() & ios_base::uppercase){
						stream.printout(buffer, swprintf(buffer, 20, L"%#lX", n) );
					}else{
						stream.printout(buffer, swprintf(buffer, 20, L"%#lx", n) );
					}
				}else{
					if(stream.flags() & ios_base::uppercase){
						stream.printout(buffer, swprintf(buffer, 20, L"%lX", n) );
					}else{
						stream.printout(buffer, swprintf(buffer, 20, L"%lx", n) );
					}
				}
			}
			if(stream.flags() & ios_base::unitbuf){
				stream.flush();
			}
		}
	};

#ifndef __STRICT_ANSI__

	template <class traits> class _UCXXEXPORT __ostream_printout<traits, wchar_t, signed long long int>{
	public:
		static void printout(basic_ostream<wchar_t, traits >& stream, const signed long long int n)
		{
			wchar_t buffer[28];
			if( stream.flags() & ios_base::dec){
				stream.printout(buffer, swprintf(buffer, 27, L"%lld", n));
			}else if( stream.flags() & ios_base::oct){
				if( stream.flags() & ios_base::showbase){
					stream.printout(buffer, swprintf(buffer, 27, L"%#llo", n));
				}else{
					stream.printout(buffer, swprintf(buffer, 27, L"%llo", n) );
				}
			}else if (stream.flags() & ios_base::hex){
				if(stream.flags() & ios_base::showbase){
					if(stream.flags() & ios_base::uppercase){
						stream.printout(buffer, swprintf(buffer, 27, L"%#llX", n) );
					}else{
						stream.printout(buffer, swprintf(buffer, 27, L"%#llx", n) );
					}
				}else{
					if(stream.flags() & ios_base::uppercase){
						stream.printout(buffer, swprintf(buffer, 27, L"%llX", n) );
					}else{
						stream.printout(buffer, swprintf(buffer, 27, L"%llx", n) );
					}
				}
			}
			if(stream.flags() & ios_base::unitbuf){
				stream.flush();
			}
		}
	};

	template <class traits> class _UCXXEXPORT __ostream_printout<traits, wchar_t, unsigned long long int>{
	public:
		static void printout(basic_ostream<wchar_t, traits >& stream, const unsigned long long int n)
		{
			wchar_t buffer[28];
			if( stream.flags() & ios_base::dec){
				stream.printout(buffer, swprintf(buffer, 27, L"%llu", n));
			}else if( stream.flags() & ios_base::oct){
				if( stream.flags() & ios_base::showbase){
					stream.printout(buffer, swprintf(buffer, 27, L"%#llo", n));
				}else{
					stream.printout(buffer, swprintf(buffer, 27, L"%llo", n) );
				}
			}else if (stream.flags() & ios_base::hex){
				if(stream.flags() & ios_base::showbase){
					if(stream.flags() & ios_base::uppercase){
						stream.printout(buffer, swprintf(buffer, 27, L"%#llX", n) );
					}else{
						stream.printout(buffer, swprintf(buffer, 27, L"%#llx", n) );
					}
				}else{
					if(stream.flags() & ios_base::uppercase){
						stream.printout(buffer, swprintf(buffer, 27, L"%llX", n) );
					}else{
						stream.printout(buffer, swprintf(buffer, 27, L"%llx", n) );
					}
				}
			}
			if(stream.flags() & ios_base::unitbuf){
				stream.flush();
			}
		}
	};


#endif	//__STRICT_ANSI__

	template <class traits> class _UCXXEXPORT __ostream_printout<traits, wchar_t, double>{
	public:
		static void printout(basic_ostream<wchar_t, traits >& stream, const double f)
		{
			wchar_t buffer[32];
			wchar_t format_string[32];
			if(stream.flags() & ios_base::scientific){
				if(stream.flags() & ios_base::uppercase){
					swprintf(format_string, 32, L"%%%u.%uE", static_cast<int>(stream.width()), static_cast<unsigned int>(stream.precision()));
				}else{
					swprintf(format_string, 32, L"%%%u.%ue", static_cast<int>(stream.width()), static_cast<unsigned int>(stream.precision()));
				}
			} else if(stream.flags() & ios_base::fixed){
				swprintf(format_string, 32, L"%%%u.%uf", static_cast<int>(stream.width()), static_cast<unsigned int>(stream.precision()));
			} else {
				swprintf(format_string, 32, L"%%%u.%ug", static_cast<int>(stream.width()), static_cast<unsigned int>(stream.precision()));
			}
			stream.printout(buffer, swprintf(buffer, 32, format_string, f) );
			if(stream.flags() & ios_base::unitbuf){
				stream.flush();
			}
		}
	};

	template <class traits> class _UCXXEXPORT __ostream_printout<traits, wchar_t, long double>{
	public:
		static void printout(basic_ostream<wchar_t, traits >& stream, const long double f)
		{
			wchar_t buffer[32];
			wchar_t format_string[32];
			if(stream.flags() & ios_base::scientific){
				if(stream.flags() & ios_base::uppercase){
					swprintf(format_string, 32, L"%%%u.%uLE", static_cast<unsigned int>(stream.width()), static_cast<unsigned int>(stream.precision()));
				}else{
					swprintf(format_string, 32, L"%%%u.%uLe", static_cast<unsigned int>(stream.width()), static_cast<unsigned int>(stream.precision()));
				}
			} else if(stream.flags() & ios_base::fixed){
				swprintf(format_string, 32, L"%%%u.%uLf", static_cast<unsigned int>(stream.width()), static_cast<unsigned int>(stream.precision()));
			} else {
				swprintf(format_string, 32, L"%%%u.%uLg", static_cast<unsigned int>(stream.width()), static_cast<unsigned int>(stream.precision()));
			}
			stream.printout(buffer, swprintf(buffer, 32, format_string, f) );
			if(stream.flags() & ios_base::unitbuf){
				stream.flush();
			}
		}
	};

#endif //__UCLIBCXX_HAS_WCHAR__

}

#pragma GCC visibility pop

#endif