/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
/*	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 <ios>
#include <cctype>

#include <string>

#ifndef __STD_HEADER_ISTREAM_HELPERS
#define __STD_HEADER_ISTREAM_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 istream code neat and tidy.  Secondly, we want it
	 * to be easy to do partial specialization of the istream 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 C, class traits> _UCXXEXPORT 
		basic_string<C, traits> _readToken(basic_istream<C, traits>& stream)
	{
		basic_string<C, traits> temp;
		typename traits::int_type c;
		while(true){
			c = stream.rdbuf()->sgetc();
			if(c != traits::eof() && isspace(c) == false){
				stream.rdbuf()->sbumpc();
				temp.append(1, traits::to_char_type(c));
			}else{
				break;
			}
		}
		if (temp.size() == 0)
			stream.setstate(ios_base::eofbit|ios_base::failbit);

		return temp;
	}

	template <class C, class traits> _UCXXEXPORT 
		basic_string<C, traits> _readTokenDecimal(basic_istream<C, traits>& stream)
	{
		basic_string<C, traits> temp;
		typename traits::int_type c;
		while(true){
			c = stream.rdbuf()->sgetc();
			if(c != traits::eof() && isspace(c) == false && (
				isdigit(c) ||
				c == '.' ||
				c == ',' ||
				((c == '-' || c == '+') && temp.size() == 0) )
			){
				stream.rdbuf()->sbumpc();
				temp.append(1, traits::to_char_type(c));
			}else{
				break;
			}
		}
		if (temp.size() == 0)
			stream.setstate(ios_base::eofbit|ios_base::failbit);

		return temp;
	}

#ifdef __UCLIBCXX_EXPAND_ISTREAM_CHAR__

	template <> _UCXXEXPORT string _readToken<char, char_traits<char> >(istream & stream);

#endif


	template <class traits, class charT, class dataType> class _UCXXEXPORT __istream_readin{
	public:
		static void readin(basic_istream<charT,traits>& stream, dataType & var);
	};

	template <class traits> class _UCXXEXPORT __istream_readin<traits, char, bool>{
	public:
		inline static void readin(basic_istream<char, traits >& stream, bool & var)
		{
			basic_string<char, traits > temp;
			temp = _readToken( stream);
			if(temp == "true" || temp == "True" || temp == "TRUE" || temp == "1"){
				var = true;
			}else{
				var = false;
			}
		}
	};


	template <class traits> class _UCXXEXPORT __istream_readin<traits, char, short>{
	public:
		inline static void readin(basic_istream<char, traits >& stream, short & var)
		{
			basic_string<char, traits > temp;

			if(stream.flags() & ios_base::dec){
				temp = _readTokenDecimal( stream);
				sscanf(temp.c_str(), "%hd", &var );
			}else{
				temp = _readToken( stream);
				if( stream.flags() & ios_base::oct){
					sscanf(temp.c_str(), "%ho", (unsigned short int *)(&var) );
				}else if(stream.flags() & ios_base::hex){
					if(stream.flags() & ios_base::uppercase){
						sscanf(temp.c_str(), "%hX", (unsigned short int *)(&var) );
					}else{
						sscanf(temp.c_str(), "%hx", (unsigned short int *)(&var) );
					}
				}else{
					sscanf(temp.c_str(), "%hi", &var);
		
				}
			}
		}
	};

	template <class traits> class _UCXXEXPORT __istream_readin<traits, char, unsigned short>{
	public:
		inline static void readin(basic_istream<char, traits >& stream, unsigned short & var)
		{
			basic_string<char, traits > temp;

			if(stream.flags() & ios_base::dec){
				temp = _readTokenDecimal( stream);
				sscanf(temp.c_str(), "%hu", &var );
			}else{
				temp = _readToken( stream);
				if( stream.flags() & ios_base::oct){
					sscanf(temp.c_str(), "%ho", &var);
				}else if(stream.flags() & ios_base::hex){
					if(stream.flags() & ios_base::uppercase){
						sscanf(temp.c_str(), "%hX", &var );
					}else{
						sscanf(temp.c_str(), "%hx", &var);
					}
				}else{
					sscanf(temp.c_str(), "%hi", (signed short int*)(&var) );
				}
			}
		}
	};

	template <class traits> class _UCXXEXPORT __istream_readin<traits, char, int>{
	public:
		inline static void readin(basic_istream<char, traits >& stream, int & var)
		{
			basic_string<char, traits > temp;

			if(stream.flags() & ios_base::dec){
				temp = _readTokenDecimal( stream);
				sscanf(temp.c_str(), "%d", &var );
			}else{
				temp = _readToken( stream);
				if( stream.flags() & ios_base::oct){
					sscanf(temp.c_str(), "%o", (unsigned int *)(&var) );
				}else if(stream.flags() & ios_base::hex){
					if(stream.flags() & ios_base::uppercase){
						sscanf(temp.c_str(), "%X", (unsigned int *)(&var) );
					}else{
						sscanf(temp.c_str(), "%x", (unsigned int *)(&var) );
					}
				}else{
					sscanf(temp.c_str(), "%i", &var);
				}
			}
		}
	};

	template <class traits> class _UCXXEXPORT __istream_readin<traits, char, unsigned int>{
	public:
		inline static void readin(basic_istream<char, traits >& stream, unsigned int & var)
		{
			basic_string<char, traits > temp;

			if(stream.flags() & ios_base::dec){
				temp = _readTokenDecimal( stream);
				sscanf(temp.c_str(), "%u", &var );
			}else{
				temp = _readToken( stream);
				if( stream.flags() & ios_base::oct){
					sscanf(temp.c_str(), "%o", (unsigned int *)(&var) );
				}else if(stream.flags() & ios_base::hex){
					if(stream.flags() & ios_base::uppercase){
						sscanf(temp.c_str(), "%X", (unsigned int *)(&var) );
					}else{
						sscanf(temp.c_str(), "%x", (unsigned int *)(&var) );
					}
				}else{
					sscanf(temp.c_str(), "%i", (int *)(&var) );
				}
			}	

		}
	};


	template <class traits> class _UCXXEXPORT __istream_readin<traits, char, long int>{
	public:
		inline static void readin(basic_istream<char, traits >& stream, long int & var)
		{
			basic_string<char, traits > temp;

			if(stream.flags() & ios_base::dec){
				temp = _readTokenDecimal( stream);
				sscanf(temp.c_str(), "%ld", &var );
			}else{
				temp = _readToken( stream);
				if( stream.flags() & ios_base::oct){
					sscanf(temp.c_str(), "%lo", (unsigned long int *)(&var) );
				}else if(stream.flags() & ios_base::hex){
					if(stream.flags() & ios_base::uppercase){
						sscanf(temp.c_str(), "%lX", (unsigned long int *)(&var) );
					}else{
						sscanf(temp.c_str(), "%lx", (unsigned long int *)(&var) );
					}
				}else{
					sscanf(temp.c_str(), "%li", (long int *)(&var) );
				}
			}

		}
	};


	template <class traits> class _UCXXEXPORT __istream_readin<traits, char, unsigned long int>{
	public:
		inline static void readin(basic_istream<char, traits >& stream, unsigned long int & var)
		{
			basic_string<char, traits > temp;

			if(stream.flags() & ios_base::dec){
				temp = _readTokenDecimal( stream);
				sscanf(temp.c_str(), "%lu", &var );
			}else{
				temp = _readToken( stream);
				if( stream.flags() & ios_base::oct){
					sscanf(temp.c_str(), "%lo", &var );
				}else if(stream.flags() & ios_base::hex){
					if(stream.flags() & ios_base::uppercase){
						sscanf(temp.c_str(), "%lX", &var );
					}else{
						sscanf(temp.c_str(), "%lx", &var);
					}
				}else{
					sscanf(temp.c_str(), "%li", (long int *)(&var) );
				}
			}
		}
	};


#ifdef __UCLIBCXX_HAS_FLOATS__

	template <class traits> class _UCXXEXPORT __istream_readin<traits, char, float>{
	public:
		inline static void readin(basic_istream<char, traits >& stream, float & var)
		{
			basic_string<char, traits > temp;
			temp = _readTokenDecimal( stream);
#if defined (__AVR__) 
			var = strtod(temp.c_str(), NULL);
#else
			sscanf(temp.c_str(), "%g", &var);
#endif
			
		}
	};

	template <class traits> class _UCXXEXPORT __istream_readin<traits, char, double>{
	public:
		inline static void readin(basic_istream<char, traits >& stream, double & var)
		{
			basic_string<char, traits > temp;
			temp = _readTokenDecimal( stream);
#if defined (__AVR__)
			var = strtod(temp.c_str(), NULL);
#else			
			sscanf(temp.c_str(), "%lg", &var);
#endif			
		}
	};

	template <class traits> class _UCXXEXPORT __istream_readin<traits, char, long double>{
	public:
		inline static void readin(basic_istream<char, traits >& stream, long double & var)
		{
			basic_string<char, traits > temp;
			temp = _readTokenDecimal( stream);
#if defined (__AVR__)
			var = strtod(temp.c_str(), NULL);
#else			
			sscanf(temp.c_str(), "%Lg", &var);
#endif			
		}
	};

#endif	// ifdef __UCLIBCXX_HAS_FLOATS__

	template <class traits> class _UCXXEXPORT __istream_readin<traits, char, void*>{
	public:
		inline static void readin(basic_istream<char, traits >& stream, void* & var)
		{
			basic_string<char, traits > temp;
			temp = _readToken( stream);
			sscanf(temp.c_str(), "%p", &var);
		}
	};


	template<class charT, class traits> void __skipws(basic_istream<charT,traits>& is){
		const typename basic_istream<charT,traits>::int_type eof = traits::eof();
		typename basic_istream<charT,traits>::int_type c;
		//While the next character normally read doesn't equal eof
		//and that character is a space, advance to the next read position
		//Thus itterating through all whitespace until we get to the meaty stuff
		while (
			!traits::eq_int_type((c = is.rdbuf()->sgetc()),	eof)
			&& isspace(c)
                      )
		{
			is.rdbuf()->sbumpc();
		}
		if(traits::eq_int_type(c, eof)){
			is.setstate(ios_base::eofbit);
		}
	}
}

#pragma GCC visibility pop

#endif