1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // iostream.cfa -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Wed May 27 17:56:53 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Thu Feb 20 15:30:58 2020 |
---|
13 | // Update Count : 827 |
---|
14 | // |
---|
15 | |
---|
16 | #include "iostream.hfa" |
---|
17 | |
---|
18 | extern "C" { |
---|
19 | #include <stdio.h> |
---|
20 | #include <stdbool.h> // true/false |
---|
21 | //#include <string.h> // strlen, strcmp |
---|
22 | extern size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1))); |
---|
23 | extern int strcmp (const char *__s1, const char *__s2) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2))); |
---|
24 | extern char *strcpy (char *__restrict __dest, const char *__restrict __src) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))); |
---|
25 | extern void *memcpy (void *__restrict __dest, const void *__restrict __src, size_t __n) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__nonnull__ (1, 2))); |
---|
26 | #include <float.h> // DBL_DIG, LDBL_DIG |
---|
27 | #include <math.h> // isfinite |
---|
28 | #include <complex.h> // creal, cimag |
---|
29 | } // extern "C" |
---|
30 | |
---|
31 | |
---|
32 | //*********************************** ostream *********************************** |
---|
33 | |
---|
34 | |
---|
35 | forall( dtype ostype | ostream( ostype ) ) { |
---|
36 | ostype & ?|?( ostype & os, zero_t ) { |
---|
37 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
38 | fmt( os, "%d", 0n ); |
---|
39 | return os; |
---|
40 | } // ?|? |
---|
41 | void ?|?( ostype & os, zero_t z ) { |
---|
42 | (ostype &)(os | z); ends( os ); |
---|
43 | } // ?|? |
---|
44 | |
---|
45 | ostype & ?|?( ostype & os, one_t ) { |
---|
46 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
47 | fmt( os, "%d", 1n ); |
---|
48 | return os; |
---|
49 | } // ?|? |
---|
50 | void ?|?( ostype & os, one_t o ) { |
---|
51 | (ostype &)(os | o); ends( os ); |
---|
52 | } // ?|? |
---|
53 | |
---|
54 | ostype & ?|?( ostype & os, bool b ) { |
---|
55 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
56 | fmt( os, "%s", b ? "true" : "false" ); |
---|
57 | return os; |
---|
58 | } // ?|? |
---|
59 | void ?|?( ostype & os, bool b ) { |
---|
60 | (ostype &)(os | b); ends( os ); |
---|
61 | } // ?|? |
---|
62 | |
---|
63 | ostype & ?|?( ostype & os, char c ) { |
---|
64 | fmt( os, "%c", c ); |
---|
65 | if ( c == '\n' ) $setNL( os, true ); |
---|
66 | return sepOff( os ); |
---|
67 | } // ?|? |
---|
68 | void ?|?( ostype & os, char c ) { |
---|
69 | (ostype &)(os | c); ends( os ); |
---|
70 | } // ?|? |
---|
71 | |
---|
72 | ostype & ?|?( ostype & os, signed char sc ) { |
---|
73 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
74 | fmt( os, "%hhd", sc ); |
---|
75 | return os; |
---|
76 | } // ?|? |
---|
77 | void ?|?( ostype & os, signed char sc ) { |
---|
78 | (ostype &)(os | sc); ends( os ); |
---|
79 | } // ?|? |
---|
80 | |
---|
81 | ostype & ?|?( ostype & os, unsigned char usc ) { |
---|
82 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
83 | fmt( os, "%hhu", usc ); |
---|
84 | return os; |
---|
85 | } // ?|? |
---|
86 | void ?|?( ostype & os, unsigned char usc ) { |
---|
87 | (ostype &)(os | usc); ends( os ); |
---|
88 | } // ?|? |
---|
89 | |
---|
90 | ostype & ?|?( ostype & os, short int si ) { |
---|
91 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
92 | fmt( os, "%hd", si ); |
---|
93 | return os; |
---|
94 | } // ?|? |
---|
95 | void & ?|?( ostype & os, short int si ) { |
---|
96 | (ostype &)(os | si); ends( os ); |
---|
97 | } // ?|? |
---|
98 | |
---|
99 | ostype & ?|?( ostype & os, unsigned short int usi ) { |
---|
100 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
101 | fmt( os, "%hu", usi ); |
---|
102 | return os; |
---|
103 | } // ?|? |
---|
104 | void & ?|?( ostype & os, unsigned short int usi ) { |
---|
105 | (ostype &)(os | usi); ends( os ); |
---|
106 | } // ?|? |
---|
107 | |
---|
108 | ostype & ?|?( ostype & os, int i ) { |
---|
109 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
110 | fmt( os, "%d", i ); |
---|
111 | return os; |
---|
112 | } // ?|? |
---|
113 | void & ?|?( ostype & os, int i ) { |
---|
114 | (ostype &)(os | i); ends( os ); |
---|
115 | } // ?|? |
---|
116 | |
---|
117 | ostype & ?|?( ostype & os, unsigned int ui ) { |
---|
118 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
119 | fmt( os, "%u", ui ); |
---|
120 | return os; |
---|
121 | } // ?|? |
---|
122 | void & ?|?( ostype & os, unsigned int ui ) { |
---|
123 | (ostype &)(os | ui); ends( os ); |
---|
124 | } // ?|? |
---|
125 | |
---|
126 | ostype & ?|?( ostype & os, long int li ) { |
---|
127 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
128 | fmt( os, "%ld", li ); |
---|
129 | return os; |
---|
130 | } // ?|? |
---|
131 | void & ?|?( ostype & os, long int li ) { |
---|
132 | (ostype &)(os | li); ends( os ); |
---|
133 | } // ?|? |
---|
134 | |
---|
135 | ostype & ?|?( ostype & os, unsigned long int uli ) { |
---|
136 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
137 | fmt( os, "%lu", uli ); |
---|
138 | return os; |
---|
139 | } // ?|? |
---|
140 | void & ?|?( ostype & os, unsigned long int uli ) { |
---|
141 | (ostype &)(os | uli); ends( os ); |
---|
142 | } // ?|? |
---|
143 | |
---|
144 | ostype & ?|?( ostype & os, long long int lli ) { |
---|
145 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
146 | fmt( os, "%lld", lli ); |
---|
147 | return os; |
---|
148 | } // ?|? |
---|
149 | void & ?|?( ostype & os, long long int lli ) { |
---|
150 | (ostype &)(os | lli); ends( os ); |
---|
151 | } // ?|? |
---|
152 | |
---|
153 | ostype & ?|?( ostype & os, unsigned long long int ulli ) { |
---|
154 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
155 | fmt( os, "%llu", ulli ); |
---|
156 | return os; |
---|
157 | } // ?|? |
---|
158 | void & ?|?( ostype & os, unsigned long long int ulli ) { |
---|
159 | (ostype &)(os | ulli); ends( os ); |
---|
160 | } // ?|? |
---|
161 | |
---|
162 | #if defined( __SIZEOF_INT128__ ) |
---|
163 | // UINT64_MAX 18_446_744_073_709_551_615_ULL |
---|
164 | #define P10_UINT64 10_000_000_000_000_000_000_ULL // 19 zeroes |
---|
165 | |
---|
166 | static void base10_128( ostype & os, unsigned int128 val ) { |
---|
167 | if ( val > UINT64_MAX ) { |
---|
168 | base10_128( os, val / P10_UINT64 ); // recursive |
---|
169 | fmt( os, "%.19lu", (uint64_t)(val % P10_UINT64) ); |
---|
170 | } else { |
---|
171 | fmt( os, "%lu", (uint64_t)val ); |
---|
172 | } // if |
---|
173 | } // base10_128 |
---|
174 | |
---|
175 | static void base10_128( ostype & os, int128 val ) { |
---|
176 | if ( val < 0 ) { |
---|
177 | fmt( os, "-" ); // leading negative sign |
---|
178 | val = -val; |
---|
179 | } // if |
---|
180 | base10_128( os, (unsigned int128)val ); // print zero/positive value |
---|
181 | } // base10_128 |
---|
182 | |
---|
183 | ostype & ?|?( ostype & os, int128 llli ) { |
---|
184 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
185 | base10_128( os, llli ); |
---|
186 | return os; |
---|
187 | } // ?|? |
---|
188 | void & ?|?( ostype & os, int128 llli ) { |
---|
189 | (ostype &)(os | llli); ends( os ); |
---|
190 | } // ?|? |
---|
191 | |
---|
192 | ostype & ?|?( ostype & os, unsigned int128 ullli ) { |
---|
193 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); |
---|
194 | base10_128( os, ullli ); |
---|
195 | return os; |
---|
196 | } // ?|? |
---|
197 | void & ?|?( ostype & os, unsigned int128 ullli ) { |
---|
198 | (ostype &)(os | ullli); ends( os ); |
---|
199 | } // ?|? |
---|
200 | #endif // __SIZEOF_INT128__ |
---|
201 | |
---|
202 | #define PrintWithDP( os, format, val, ... ) \ |
---|
203 | { \ |
---|
204 | enum { size = 48 }; \ |
---|
205 | char buf[size]; \ |
---|
206 | int len = snprintf( buf, size, format, ##__VA_ARGS__, val ); \ |
---|
207 | fmt( os, "%s", buf ); \ |
---|
208 | if ( isfinite( val ) ) { /* if number, always print decimal point */ \ |
---|
209 | for ( int i = 0;; i += 1 ) { \ |
---|
210 | if ( i == len ) { fmt( os, "." ); break; } \ |
---|
211 | if ( buf[i] == '.' ) break; \ |
---|
212 | } /* for */ \ |
---|
213 | } /* if */ \ |
---|
214 | } |
---|
215 | |
---|
216 | ostype & ?|?( ostype & os, float f ) { |
---|
217 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
218 | PrintWithDP( os, "%g", f ); |
---|
219 | return os; |
---|
220 | } // ?|? |
---|
221 | void & ?|?( ostype & os, float f ) { |
---|
222 | (ostype &)(os | f); ends( os ); |
---|
223 | } // ?|? |
---|
224 | |
---|
225 | ostype & ?|?( ostype & os, double d ) { |
---|
226 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
227 | PrintWithDP( os, "%.*lg", d, DBL_DIG ); |
---|
228 | return os; |
---|
229 | } // ?|? |
---|
230 | void & ?|?( ostype & os, double d ) { |
---|
231 | (ostype &)(os | d); ends( os ); |
---|
232 | } // ?|? |
---|
233 | |
---|
234 | ostype & ?|?( ostype & os, long double ld ) { |
---|
235 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
236 | PrintWithDP( os, "%.*Lg", ld, LDBL_DIG ); |
---|
237 | return os; |
---|
238 | } // ?|? |
---|
239 | void & ?|?( ostype & os, long double ld ) { |
---|
240 | (ostype &)(os | ld); ends( os ); |
---|
241 | } // ?|? |
---|
242 | |
---|
243 | ostype & ?|?( ostype & os, float _Complex fc ) { |
---|
244 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
245 | // os | crealf( fc ) | nonl; |
---|
246 | PrintWithDP( os, "%g", crealf( fc ) ); |
---|
247 | PrintWithDP( os, "%+g", cimagf( fc ) ); |
---|
248 | fmt( os, "i" ); |
---|
249 | return os; |
---|
250 | } // ?|? |
---|
251 | void & ?|?( ostype & os, float _Complex fc ) { |
---|
252 | (ostype &)(os | fc); ends( os ); |
---|
253 | } // ?|? |
---|
254 | |
---|
255 | ostype & ?|?( ostype & os, double _Complex dc ) { |
---|
256 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
257 | // os | creal( dc ) | nonl; |
---|
258 | PrintWithDP( os, "%.*lg", creal( dc ), DBL_DIG ); |
---|
259 | PrintWithDP( os, "%+.*lg", cimag( dc ), DBL_DIG ); |
---|
260 | fmt( os, "i" ); |
---|
261 | return os; |
---|
262 | } // ?|? |
---|
263 | void & ?|?( ostype & os, double _Complex dc ) { |
---|
264 | (ostype &)(os | dc); ends( os ); |
---|
265 | } // ?|? |
---|
266 | |
---|
267 | ostype & ?|?( ostype & os, long double _Complex ldc ) { |
---|
268 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
269 | // os | creall( ldc ) || nonl; |
---|
270 | PrintWithDP( os, "%.*Lg", creall( ldc ), LDBL_DIG ); |
---|
271 | PrintWithDP( os, "%+.*Lg", cimagl( ldc ), LDBL_DIG ); |
---|
272 | fmt( os, "i" ); |
---|
273 | return os; |
---|
274 | } // ?|? |
---|
275 | void & ?|?( ostype & os, long double _Complex ldc ) { |
---|
276 | (ostype &)(os | ldc); ends( os ); |
---|
277 | } // ?|? |
---|
278 | |
---|
279 | ostype & ?|?( ostype & os, const char str[] ) { |
---|
280 | enum { Open = 1, Close, OpenClose }; |
---|
281 | static const unsigned char mask[256] @= { |
---|
282 | // opening delimiters, no space after |
---|
283 | ['('] : Open, ['['] : Open, ['{'] : Open, |
---|
284 | ['='] : Open, ['$'] : Open, [(unsigned char)'£'] : Open, [(unsigned char)'¥'] : Open, |
---|
285 | [(unsigned char)'¡'] : Open, [(unsigned char)'¿'] : Open, [(unsigned char)'«'] : Open, |
---|
286 | // closing delimiters, no space before |
---|
287 | [','] : Close, ['.'] : Close, [';'] : Close, ['!'] : Close, ['?'] : Close, |
---|
288 | ['%'] : Close, [(unsigned char)'¢'] : Close, [(unsigned char)'»'] : Close, |
---|
289 | [')'] : Close, [']'] : Close, ['}'] : Close, |
---|
290 | // opening-closing delimiters, no space before or after |
---|
291 | ['\''] : OpenClose, ['`'] : OpenClose, ['"'] : OpenClose, [':'] : OpenClose, |
---|
292 | [' '] : OpenClose, ['\f'] : OpenClose, ['\n'] : OpenClose, ['\r'] : OpenClose, ['\t'] : OpenClose, ['\v'] : OpenClose, // isspace |
---|
293 | }; // mask |
---|
294 | |
---|
295 | if ( str[0] == '\0' ) { sepOff( os ); return os; } // null string => no separator |
---|
296 | |
---|
297 | // first character IS NOT spacing or closing punctuation => add left separator |
---|
298 | unsigned char ch = str[0]; // must make unsigned |
---|
299 | if ( $sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) { |
---|
300 | fmt( os, "%s", $sepGetCur( os ) ); |
---|
301 | } // if |
---|
302 | |
---|
303 | // if string starts line, must reset to determine open state because separator is off |
---|
304 | $sepReset( os ); // reset separator |
---|
305 | |
---|
306 | // last character IS spacing or opening punctuation => turn off separator for next item |
---|
307 | size_t len = strlen( str ); |
---|
308 | ch = str[len - 1]; // must make unsigned |
---|
309 | if ( $sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) { |
---|
310 | sepOn( os ); |
---|
311 | } else { |
---|
312 | sepOff( os ); |
---|
313 | } // if |
---|
314 | if ( ch == '\n' ) $setNL( os, true ); // check *AFTER* $sepPrt call above as it resets NL flag |
---|
315 | return write( os, str, len ); |
---|
316 | } // ?|? |
---|
317 | |
---|
318 | void ?|?( ostype & os, const char str[] ) { |
---|
319 | (ostype &)(os | str); ends( os ); |
---|
320 | } // ?|? |
---|
321 | |
---|
322 | // ostype & ?|?( ostype & os, const char16_t * str ) { |
---|
323 | // if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
324 | // fmt( os, "%ls", str ); |
---|
325 | // return os; |
---|
326 | // } // ?|? |
---|
327 | |
---|
328 | // #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous |
---|
329 | // ostype & ?|?( ostype & os, const char32_t * str ) { |
---|
330 | // if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
331 | // fmt( os, "%ls", str ); |
---|
332 | // return os; |
---|
333 | // } // ?|? |
---|
334 | // #endif // ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) |
---|
335 | |
---|
336 | // ostype & ?|?( ostype & os, const wchar_t * str ) { |
---|
337 | // if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
338 | // fmt( os, "%ls", str ); |
---|
339 | // return os; |
---|
340 | // } // ?|? |
---|
341 | |
---|
342 | ostype & ?|?( ostype & os, const void * p ) { |
---|
343 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
344 | fmt( os, "%p", p ); |
---|
345 | return os; |
---|
346 | } // ?|? |
---|
347 | void ?|?( ostype & os, const void * p ) { |
---|
348 | (ostype &)(os | p); ends( os ); |
---|
349 | } // ?|? |
---|
350 | |
---|
351 | // manipulators |
---|
352 | ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) { |
---|
353 | (ostype &)(manip( os )); |
---|
354 | return os; |
---|
355 | } // ?|? |
---|
356 | void ?|?( ostype & os, ostype & (* manip)( ostype & ) ) { |
---|
357 | (ostype &)(manip( os )); |
---|
358 | if ( $getPrt( os ) ) ends( os ); // something printed ? |
---|
359 | $setPrt( os, false ); // turn off |
---|
360 | } // ?|? |
---|
361 | |
---|
362 | ostype & sep( ostype & os ) { |
---|
363 | return (ostype &)(os | sepGet( os )); |
---|
364 | } // sep |
---|
365 | |
---|
366 | ostype & sepTuple( ostype & os ) { |
---|
367 | return os | sepGetTuple( os ); |
---|
368 | } // sepTuple |
---|
369 | |
---|
370 | ostype & nl( ostype & os ) { |
---|
371 | (ostype &)(os | '\n'); |
---|
372 | $setPrt( os, false ); // turn off |
---|
373 | $setNL( os, true ); |
---|
374 | flush( os ); |
---|
375 | return sepOff( os ); // prepare for next line |
---|
376 | } // nl |
---|
377 | |
---|
378 | ostype & nonl( ostype & os ) { |
---|
379 | $setPrt( os, false ); // turn off |
---|
380 | return os; |
---|
381 | } // nonl |
---|
382 | |
---|
383 | ostype & sepOn( ostype & os ) { |
---|
384 | sepOn( os ); // call void returning |
---|
385 | return os; |
---|
386 | } // sepOn |
---|
387 | |
---|
388 | ostype & sepOff( ostype & os ) { |
---|
389 | sepOff( os ); // call void returning |
---|
390 | return os; |
---|
391 | } // sepOff |
---|
392 | |
---|
393 | ostype & sepEnable( ostype & os ) { |
---|
394 | sepEnable( os ); // call void returning |
---|
395 | return os; |
---|
396 | } // sepEnable |
---|
397 | |
---|
398 | ostype & sepDisable( ostype & os ) { |
---|
399 | sepDisable( os ); // call void returning |
---|
400 | return os; |
---|
401 | } // sepDisable |
---|
402 | |
---|
403 | ostype & nlOn( ostype & os ) { |
---|
404 | nlOn( os ); // call void returning |
---|
405 | return os; |
---|
406 | } // nlOn |
---|
407 | |
---|
408 | ostype & nlOff( ostype & os ) { |
---|
409 | nlOff( os ); // call void returning |
---|
410 | return os; |
---|
411 | } // nlOff |
---|
412 | } // distribution |
---|
413 | |
---|
414 | // tuples |
---|
415 | forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) { |
---|
416 | ostype & ?|?( ostype & os, T arg, Params rest ) { |
---|
417 | (ostype &)(os | arg); // print first argument |
---|
418 | $sepSetCur( os, sepGetTuple( os ) ); // switch to tuple separator |
---|
419 | (ostype &)(os | rest); // print remaining arguments |
---|
420 | $sepSetCur( os, sepGet( os ) ); // switch to regular separator |
---|
421 | return os; |
---|
422 | } // ?|? |
---|
423 | void ?|?( ostype & os, T arg, Params rest ) { |
---|
424 | // (ostype &)(?|?( os, arg, rest )); ends( os ); |
---|
425 | (ostype &)(os | arg); // print first argument |
---|
426 | $sepSetCur( os, sepGetTuple( os ) ); // switch to tuple separator |
---|
427 | (ostype &)(os | rest); // print remaining arguments |
---|
428 | $sepSetCur( os, sepGet( os ) ); // switch to regular separator |
---|
429 | ends( os ); |
---|
430 | } // ?|? |
---|
431 | } // distribution |
---|
432 | |
---|
433 | // writes the range [begin, end) to the given stream |
---|
434 | forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) ) { |
---|
435 | void write( iterator_type begin, iterator_type end, ostype & os ) { |
---|
436 | void print( elt_type i ) { os | i; } |
---|
437 | for_each( begin, end, print ); |
---|
438 | } // ?|? |
---|
439 | |
---|
440 | void write_reverse( iterator_type begin, iterator_type end, ostype & os ) { |
---|
441 | void print( elt_type i ) { os | i; } |
---|
442 | for_each_reverse( begin, end, print ); |
---|
443 | } // ?|? |
---|
444 | } // distribution |
---|
445 | |
---|
446 | //*********************************** manipulators *********************************** |
---|
447 | |
---|
448 | //*********************************** integral *********************************** |
---|
449 | |
---|
450 | static const char * shortbin[] = { "0", "1", "10", "11", "100", "101", "110", "111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; |
---|
451 | static const char * longbin[] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; |
---|
452 | |
---|
453 | // Default prefix for non-decimal prints is 0b, 0, 0x. |
---|
454 | #define IntegralFMTImpl( T, CODE, IFMTNP, IFMTP ) \ |
---|
455 | forall( dtype ostype | ostream( ostype ) ) { \ |
---|
456 | ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \ |
---|
457 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \ |
---|
458 | \ |
---|
459 | if ( f.base == 'b' || f.base == 'B' ) { /* bespoke binary format */ \ |
---|
460 | int bits; \ |
---|
461 | if ( f.val == (T){0} ) bits = 1; /* force at least one bit to print */ \ |
---|
462 | else bits = sizeof(long long int) * 8 - __builtin_clzll( f.val ); /* position of most significant bit */ \ |
---|
463 | bits = bits > sizeof(f.val) * 8 ? sizeof(f.val) * 8 : bits; \ |
---|
464 | int spaces = f.wd - bits; /* can be negative */ \ |
---|
465 | if ( ! f.flags.nobsdp ) { spaces -= 2; } /* base prefix takes space */ \ |
---|
466 | /* printf( "%d %d\n", bits, spaces ); */ \ |
---|
467 | if ( ! f.flags.left ) { /* right justified ? */ \ |
---|
468 | /* Note, base prefix then zero padding or spacing then prefix. */ \ |
---|
469 | if ( f.flags.pad0 || f.flags.pc ) { \ |
---|
470 | if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \ |
---|
471 | if ( f.flags.pc ) spaces = f.pc - bits; \ |
---|
472 | if ( spaces > 0 ) fmt( os, "%0*d", spaces, 0 ); /* zero pad */ \ |
---|
473 | } else { \ |
---|
474 | if ( spaces > 0 ) fmt( os, "%*s", spaces, " " ); /* space pad */ \ |
---|
475 | if ( ! f.flags.nobsdp ) { fmt( os, "0%c", f.base ); } \ |
---|
476 | } /* if */ \ |
---|
477 | } else if ( ! f.flags.nobsdp ) { \ |
---|
478 | fmt( os, "0%c", f.base ); \ |
---|
479 | } /* if */ \ |
---|
480 | int shift = (bits - 1) / 4 * 4; /* floor( bits - 1, 4 ) */ \ |
---|
481 | typeof( f.val ) temp = f.val; \ |
---|
482 | fmt( os, "%s", shortbin[(temp >> shift) & 0xf] ); \ |
---|
483 | for () { \ |
---|
484 | shift -= 4; \ |
---|
485 | if ( shift < 0 ) break; \ |
---|
486 | temp = f.val; \ |
---|
487 | fmt( os, "%s", longbin[(temp >> shift) & 0xf] ); \ |
---|
488 | } /* for */ \ |
---|
489 | if ( f.flags.left && spaces > 0 ) fmt( os, "%*s", spaces, " " ); \ |
---|
490 | return os; \ |
---|
491 | } /* if */ \ |
---|
492 | \ |
---|
493 | char fmtstr[sizeof(IFMTP)]; /* sizeof includes '\0' */ \ |
---|
494 | if ( ! f.flags.pc ) memcpy( &fmtstr, IFMTNP, sizeof(IFMTNP) ); \ |
---|
495 | else memcpy( &fmtstr, IFMTP, sizeof(IFMTP) ); \ |
---|
496 | int star = 4; /* position before first '*' */ \ |
---|
497 | \ |
---|
498 | /* Insert flags into spaces before '*', from right to left. */ \ |
---|
499 | if ( ! f.flags.nobsdp ) { fmtstr[star] = '#'; star -= 1; } \ |
---|
500 | if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } \ |
---|
501 | if ( f.flags.sign && f.base == CODE ) { fmtstr[star] = '+'; star -= 1; } \ |
---|
502 | if ( f.flags.pad0 && ! f.flags.pc ) { fmtstr[star] = '0'; star -= 1; } \ |
---|
503 | fmtstr[star] = '%'; \ |
---|
504 | \ |
---|
505 | if ( ! f.flags.pc ) { /* no precision */ \ |
---|
506 | fmtstr[sizeof(IFMTNP)-2] = f.base; /* sizeof includes '\0' */ \ |
---|
507 | /* printf( "%s %c %c\n", &fmtstr[star], f.base, CODE ); */ \ |
---|
508 | fmt( os, &fmtstr[star], f.wd, f.val ); \ |
---|
509 | } else { /* precision */ \ |
---|
510 | fmtstr[sizeof(IFMTP)-2] = f.base; /* sizeof includes '\0' */ \ |
---|
511 | /* printf( "%s %c %c\n", &fmtstr[star], f.base, CODE ); */ \ |
---|
512 | fmt( os, &fmtstr[star], f.wd, f.pc, f.val ); \ |
---|
513 | } /* if */ \ |
---|
514 | return os; \ |
---|
515 | } /* ?|? */ \ |
---|
516 | void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \ |
---|
517 | } // distribution |
---|
518 | |
---|
519 | IntegralFMTImpl( signed char, 'd', "% *hh ", "% *.*hh " ) |
---|
520 | IntegralFMTImpl( unsigned char, 'u', "% *hh ", "% *.*hh " ) |
---|
521 | IntegralFMTImpl( signed short int, 'd', "% *h ", "% *.*h " ) |
---|
522 | IntegralFMTImpl( unsigned short int, 'u', "% *h ", "% *.*h " ) |
---|
523 | IntegralFMTImpl( signed int, 'd', "% * ", "% *.* " ) |
---|
524 | IntegralFMTImpl( unsigned int, 'u', "% * ", "% *.* " ) |
---|
525 | IntegralFMTImpl( signed long int, 'd', "% *l ", "% *.*l " ) |
---|
526 | IntegralFMTImpl( unsigned long int, 'u', "% *l ", "% *.*l " ) |
---|
527 | IntegralFMTImpl( signed long long int, 'd', "% *ll ", "% *.*ll " ) |
---|
528 | IntegralFMTImpl( unsigned long long int, 'u', "% *ll ", "% *.*ll " ) |
---|
529 | |
---|
530 | |
---|
531 | #if defined( __SIZEOF_INT128__ ) |
---|
532 | // Default prefix for non-decimal prints is 0b, 0, 0x. |
---|
533 | #define IntegralFMTImpl128( T, SIGNED, CODE, IFMTNP, IFMTP ) \ |
---|
534 | forall( dtype ostype | ostream( ostype ) ) \ |
---|
535 | static void base10_128( ostype & os, _Ostream_Manip(T) fmt ) { \ |
---|
536 | if ( fmt.val > UINT64_MAX ) { \ |
---|
537 | fmt.val /= P10_UINT64; \ |
---|
538 | base10_128( os, fmt ); /* recursive */ \ |
---|
539 | _Ostream_Manip(unsigned long long int) fmt2 @= { (uint64_t)(fmt.val % P10_UINT64), 0, 19, 'u', { .all : 0 } }; \ |
---|
540 | fmt2.flags.nobsdp = true; \ |
---|
541 | printf( "fmt2 %c %lld %d\n", fmt2.base, fmt2.val, fmt2.all ); \ |
---|
542 | sepOff( os ); \ |
---|
543 | (ostype &)(os | fmt2); \ |
---|
544 | } else { \ |
---|
545 | printf( "fmt %c %lld %d\n", fmt.base, fmt.val, fmt.all ); \ |
---|
546 | (ostype &)(os | fmt); \ |
---|
547 | } /* if */ \ |
---|
548 | } /* base10_128 */ \ |
---|
549 | forall( dtype ostype | ostream( ostype ) ) { \ |
---|
550 | ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \ |
---|
551 | if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); \ |
---|
552 | \ |
---|
553 | if ( f.base == 'b' | f.base == 'o' | f.base == 'x' | f.base == 'X' ) { \ |
---|
554 | unsigned long long int msig = (unsigned long long int)(f.val >> 64); \ |
---|
555 | unsigned long long int lsig = (unsigned long long int)(f.val); \ |
---|
556 | _Ostream_Manip(SIGNED long long int) fmt @= { msig, f.wd, f.pc, f.base, { .all : f.all } }; \ |
---|
557 | _Ostream_Manip(unsigned long long int) fmt2 @= { lsig, 0, 0, f.base, { .all : 0 } }; \ |
---|
558 | if ( msig == 0 ) { \ |
---|
559 | fmt.val = lsig; \ |
---|
560 | (ostype &)(os | fmt); \ |
---|
561 | } else { \ |
---|
562 | fmt2.flags.pad0 = fmt2.flags.nobsdp = true; \ |
---|
563 | if ( f.base == 'b' ) { \ |
---|
564 | if ( f.wd > 64 ) fmt.wd = f.wd - 64; \ |
---|
565 | fmt2.wd = 64; \ |
---|
566 | (ostype &)(os | fmt | "" | fmt2); \ |
---|
567 | } else if ( f.base == 'o' ) { \ |
---|
568 | fmt.val = (unsigned long long int)fmt.val >> 2; \ |
---|
569 | if ( f.wd > 21 ) fmt.wd = f.wd - 21; \ |
---|
570 | fmt2.wd = 1; \ |
---|
571 | fmt2.val = ((msig & 0x3) << 1) + 1; \ |
---|
572 | (ostype &)(os | fmt | "" | fmt2); \ |
---|
573 | sepOff( os ); \ |
---|
574 | fmt2.wd = 21; \ |
---|
575 | fmt2.val = lsig & 0x7fffffffffffffff; \ |
---|
576 | (ostype &)(os | fmt2); \ |
---|
577 | } else { \ |
---|
578 | if ( f.flags.left ) { \ |
---|
579 | if ( f.wd > 16 ) fmt2.wd = f.wd - 16; \ |
---|
580 | fmt.wd = 16; \ |
---|
581 | } else { \ |
---|
582 | if ( f.wd > 16 ) fmt.wd = f.wd - 16; \ |
---|
583 | fmt2.wd = 16; \ |
---|
584 | } /* if */ \ |
---|
585 | (ostype &)(os | fmt | "" | fmt2); \ |
---|
586 | } /* if */ \ |
---|
587 | } /* if */ \ |
---|
588 | } else { \ |
---|
589 | base10_128( os, f ); \ |
---|
590 | } /* if */ \ |
---|
591 | return os; \ |
---|
592 | } /* ?|? */ \ |
---|
593 | void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \ |
---|
594 | } // distribution |
---|
595 | |
---|
596 | IntegralFMTImpl128( int128, signed, 'd', "% *ll ", "% *.*ll " ) |
---|
597 | IntegralFMTImpl128( unsigned int128, unsigned, 'u', "% *ll ", "% *.*ll " ) |
---|
598 | #endif // __SIZEOF_INT128__ |
---|
599 | |
---|
600 | //*********************************** floating point *********************************** |
---|
601 | |
---|
602 | #define PrintWithDP2( os, format, val, ... ) \ |
---|
603 | { \ |
---|
604 | enum { size = 48 }; \ |
---|
605 | char buf[size]; \ |
---|
606 | int bufbeg = 0, i, len = snprintf( buf, size, format, ##__VA_ARGS__, val ); \ |
---|
607 | if ( isfinite( val ) && (f.base != 'g' || f.pc != 0) ) { /* if number, print decimal point */ \ |
---|
608 | for ( i = 0; i < len && buf[i] != '.' && buf[i] != 'e' && buf[i] != 'E'; i += 1 ); /* decimal point or scientific ? */ \ |
---|
609 | if ( i == len && ! f.flags.nobsdp ) { \ |
---|
610 | if ( ! f.flags.left ) { \ |
---|
611 | buf[i] = '.'; buf[i + 1] = '\0'; \ |
---|
612 | if ( buf[0] == ' ' ) bufbeg = 1; /* decimal point within width */ \ |
---|
613 | } else { \ |
---|
614 | for ( i = 0; i < len && buf[i] != ' '; i += 1 ); /* trailing blank ? */ \ |
---|
615 | buf[i] = '.'; \ |
---|
616 | if ( i == len ) buf[i + 1] = '\0'; \ |
---|
617 | } /* if */ \ |
---|
618 | } /* if */ \ |
---|
619 | } /* if */ \ |
---|
620 | fmt( os, "%s", &buf[bufbeg] ); \ |
---|
621 | } |
---|
622 | |
---|
623 | #define FloatingPointFMTImpl( T, DFMTNP, DFMTP ) \ |
---|
624 | forall( dtype ostype | ostream( ostype ) ) { \ |
---|
625 | ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \ |
---|
626 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \ |
---|
627 | char fmtstr[sizeof(DFMTP)]; /* sizeof includes '\0' */ \ |
---|
628 | if ( ! f.flags.pc ) memcpy( &fmtstr, DFMTNP, sizeof(DFMTNP) ); \ |
---|
629 | else memcpy( &fmtstr, DFMTP, sizeof(DFMTP) ); \ |
---|
630 | int star = 4; /* position before first '*' */ \ |
---|
631 | \ |
---|
632 | /* Insert flags into spaces before '*', from right to left. */ \ |
---|
633 | if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } \ |
---|
634 | if ( f.flags.sign ) { fmtstr[star] = '+'; star -= 1; } \ |
---|
635 | if ( f.flags.pad0 ) { fmtstr[star] = '0'; star -= 1; } \ |
---|
636 | fmtstr[star] = '%'; \ |
---|
637 | \ |
---|
638 | if ( ! f.flags.pc ) { /* no precision */ \ |
---|
639 | fmtstr[sizeof(DFMTNP)-2] = f.base; /* sizeof includes '\0' */ \ |
---|
640 | /* printf( "%g %d %s\n", f.val, f.wd, &fmtstr[star]); */ \ |
---|
641 | PrintWithDP2( os, &fmtstr[star], f.val, f.wd ) \ |
---|
642 | } else { /* precision */ \ |
---|
643 | fmtstr[sizeof(DFMTP)-2] = f.base; /* sizeof includes '\0' */ \ |
---|
644 | /* printf( "%g %d %d %s\n", f.val, f.wd, f.pc, &fmtstr[star] ); */ \ |
---|
645 | PrintWithDP2( os, &fmtstr[star], f.val, f.wd, f.pc ) \ |
---|
646 | } /* if */ \ |
---|
647 | return os; \ |
---|
648 | } /* ?|? */ \ |
---|
649 | \ |
---|
650 | void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \ |
---|
651 | } // distribution |
---|
652 | |
---|
653 | FloatingPointFMTImpl( double, "% * ", "% *.* " ) |
---|
654 | FloatingPointFMTImpl( long double, "% *L ", "% *.*L " ) |
---|
655 | |
---|
656 | //*********************************** character *********************************** |
---|
657 | |
---|
658 | forall( dtype ostype | ostream( ostype ) ) { |
---|
659 | ostype & ?|?( ostype & os, _Ostream_Manip(char) f ) { |
---|
660 | if ( f.base != 'c' ) { // bespoke binary/octal/hex format |
---|
661 | _Ostream_Manip(unsigned char) fmtuc @= { f.val, f.wd, f.pc, f.base, {'\0'} }; |
---|
662 | fmtuc.flags.pc = f.flags.pc; |
---|
663 | fmtuc.flags.nobsdp = f.flags.nobsdp; |
---|
664 | // os | fmtuc | nonl; |
---|
665 | (ostype &)(os | fmtuc); |
---|
666 | return os; |
---|
667 | } // if |
---|
668 | |
---|
669 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
670 | |
---|
671 | #define CFMTNP "% * " |
---|
672 | char fmtstr[sizeof(CFMTNP)]; // sizeof includes '\0' |
---|
673 | memcpy( &fmtstr, CFMTNP, sizeof(CFMTNP) ); |
---|
674 | int star = 1; // position before first '*' |
---|
675 | |
---|
676 | // Insert flags into spaces before '*', from right to left. |
---|
677 | if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } |
---|
678 | fmtstr[star] = '%'; |
---|
679 | |
---|
680 | fmtstr[sizeof(CFMTNP)-2] = f.base; // sizeof includes '\0' |
---|
681 | // printf( "%d %s\n", f.wd, &fmtstr[star] ); |
---|
682 | fmt( os, &fmtstr[star], f.wd, f.val ); |
---|
683 | return os; |
---|
684 | } // ?|? |
---|
685 | |
---|
686 | void ?|?( ostype & os, _Ostream_Manip(char) f ) { (ostype &)(os | f); ends( os ); } |
---|
687 | } // distribution |
---|
688 | |
---|
689 | //*********************************** C string *********************************** |
---|
690 | |
---|
691 | forall( dtype ostype | ostream( ostype ) ) { |
---|
692 | ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f ) { |
---|
693 | if ( ! f.val ) return os; // null pointer ? |
---|
694 | |
---|
695 | if ( f.base != 's' ) { // bespoke binary/octal/hex format |
---|
696 | _Ostream_Manip(unsigned char) fmtuc @= { 0, f.wd, f.pc, f.base, {'\0'} }; |
---|
697 | fmtuc.flags.pc = f.flags.pc; |
---|
698 | fmtuc.flags.nobsdp = f.flags.nobsdp; |
---|
699 | for ( unsigned int i = 0; f.val[i] != '\0'; i += 1 ) { |
---|
700 | fmtuc.val = f.val[i]; |
---|
701 | // os | fmtuc | nonl; |
---|
702 | (ostype &)(os | fmtuc); |
---|
703 | } // for |
---|
704 | return os; |
---|
705 | } // if |
---|
706 | |
---|
707 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); |
---|
708 | |
---|
709 | #define SFMTNP "% * " |
---|
710 | #define SFMTP "% *.* " |
---|
711 | char fmtstr[sizeof(SFMTP)]; // sizeof includes '\0' |
---|
712 | if ( ! f.flags.pc ) memcpy( &fmtstr, SFMTNP, sizeof(SFMTNP) ); |
---|
713 | else memcpy( &fmtstr, SFMTP, sizeof(SFMTP) ); |
---|
714 | int star = 1; // position before first '*' |
---|
715 | |
---|
716 | // Insert flags into spaces before '*', from right to left. |
---|
717 | if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } |
---|
718 | fmtstr[star] = '%'; |
---|
719 | |
---|
720 | if ( ! f.flags.pc ) { // no precision |
---|
721 | // printf( "%d %s\n", f.wd, &fmtstr[star] ); |
---|
722 | fmtstr[sizeof(SFMTNP)-2] = f.base; // sizeof includes '\0' |
---|
723 | fmt( os, &fmtstr[star], f.wd, f.val ); |
---|
724 | } else { // precision |
---|
725 | fmtstr[sizeof(SFMTP)-2] = f.base; // sizeof includes '\0' |
---|
726 | // printf( "%d %d %s\n", f.wd, f.pc, &fmtstr[star] ); |
---|
727 | fmt( os, &fmtstr[star], f.wd, f.pc, f.val ); |
---|
728 | } // if |
---|
729 | return os; |
---|
730 | } // ?|? |
---|
731 | |
---|
732 | void ?|?( ostype & os, _Ostream_Manip(const char *) f ) { (ostype &)(os | f); ends( os ); } |
---|
733 | } // distribution |
---|
734 | |
---|
735 | |
---|
736 | //*********************************** istream *********************************** |
---|
737 | |
---|
738 | |
---|
739 | forall( dtype istype | istream( istype ) ) { |
---|
740 | istype & ?|?( istype & is, bool & b ) { |
---|
741 | char val[6]; |
---|
742 | fmt( is, "%5s", val ); |
---|
743 | if ( strcmp( val, "true" ) == 0 ) b = true; |
---|
744 | else if ( strcmp( val, "false" ) == 0 ) b = false; |
---|
745 | else { |
---|
746 | fprintf( stderr, "invalid Boolean constant\n" ); |
---|
747 | abort(); // cannot use abort stream |
---|
748 | } // if |
---|
749 | return is; |
---|
750 | } // ?|? |
---|
751 | |
---|
752 | istype & ?|?( istype & is, char & c ) { |
---|
753 | char temp; |
---|
754 | for () { |
---|
755 | fmt( is, "%c", &temp ); // must pass pointer through varg to fmt |
---|
756 | // do not overwrite parameter with newline unless appropriate |
---|
757 | if ( temp != '\n' || getANL( is ) ) { c = temp; break; } |
---|
758 | if ( eof( is ) ) break; |
---|
759 | } // for |
---|
760 | return is; |
---|
761 | } // ?|? |
---|
762 | |
---|
763 | istype & ?|?( istype & is, signed char & sc ) { |
---|
764 | fmt( is, "%hhi", &sc ); |
---|
765 | return is; |
---|
766 | } // ?|? |
---|
767 | |
---|
768 | istype & ?|?( istype & is, unsigned char & usc ) { |
---|
769 | fmt( is, "%hhi", &usc ); |
---|
770 | return is; |
---|
771 | } // ?|? |
---|
772 | |
---|
773 | istype & ?|?( istype & is, short int & si ) { |
---|
774 | fmt( is, "%hi", &si ); |
---|
775 | return is; |
---|
776 | } // ?|? |
---|
777 | |
---|
778 | istype & ?|?( istype & is, unsigned short int & usi ) { |
---|
779 | fmt( is, "%hi", &usi ); |
---|
780 | return is; |
---|
781 | } // ?|? |
---|
782 | |
---|
783 | istype & ?|?( istype & is, int & i ) { |
---|
784 | fmt( is, "%i", &i ); |
---|
785 | return is; |
---|
786 | } // ?|? |
---|
787 | |
---|
788 | istype & ?|?( istype & is, unsigned int & ui ) { |
---|
789 | fmt( is, "%i", &ui ); |
---|
790 | return is; |
---|
791 | } // ?|? |
---|
792 | |
---|
793 | istype & ?|?( istype & is, long int & li ) { |
---|
794 | fmt( is, "%li", &li ); |
---|
795 | return is; |
---|
796 | } // ?|? |
---|
797 | |
---|
798 | istype & ?|?( istype & is, unsigned long int & ulli ) { |
---|
799 | fmt( is, "%li", &ulli ); |
---|
800 | return is; |
---|
801 | } // ?|? |
---|
802 | |
---|
803 | istype & ?|?( istype & is, long long int & lli ) { |
---|
804 | fmt( is, "%lli", &lli ); |
---|
805 | return is; |
---|
806 | } // ?|? |
---|
807 | |
---|
808 | istype & ?|?( istype & is, unsigned long long int & ulli ) { |
---|
809 | fmt( is, "%lli", &ulli ); |
---|
810 | return is; |
---|
811 | } // ?|? |
---|
812 | |
---|
813 | |
---|
814 | istype & ?|?( istype & is, float & f ) { |
---|
815 | fmt( is, "%f", &f ); |
---|
816 | return is; |
---|
817 | } // ?|? |
---|
818 | |
---|
819 | istype & ?|?( istype & is, double & d ) { |
---|
820 | fmt( is, "%lf", &d ); |
---|
821 | return is; |
---|
822 | } // ?|? |
---|
823 | |
---|
824 | istype & ?|?( istype & is, long double & ld ) { |
---|
825 | fmt( is, "%Lf", &ld ); |
---|
826 | return is; |
---|
827 | } // ?|? |
---|
828 | |
---|
829 | |
---|
830 | istype & ?|?( istype & is, float _Complex & fc ) { |
---|
831 | float re, im; |
---|
832 | fmt( is, "%f%fi", &re, &im ); |
---|
833 | fc = re + im * _Complex_I; |
---|
834 | return is; |
---|
835 | } // ?|? |
---|
836 | |
---|
837 | istype & ?|?( istype & is, double _Complex & dc ) { |
---|
838 | double re, im; |
---|
839 | fmt( is, "%lf%lfi", &re, &im ); |
---|
840 | dc = re + im * _Complex_I; |
---|
841 | return is; |
---|
842 | } // ?|? |
---|
843 | |
---|
844 | istype & ?|?( istype & is, long double _Complex & ldc ) { |
---|
845 | long double re, im; |
---|
846 | fmt( is, "%Lf%Lfi", &re, &im ); |
---|
847 | ldc = re + im * _Complex_I; |
---|
848 | return is; |
---|
849 | } // ?|? |
---|
850 | |
---|
851 | // istype & ?|?( istype & is, const char fmt[] ) { |
---|
852 | // fmt( is, fmt, "" ); |
---|
853 | // return is; |
---|
854 | // } // ?|? |
---|
855 | |
---|
856 | istype & ?|?( istype & is, char * s ) { |
---|
857 | fmt( is, "%s", s ); |
---|
858 | return is; |
---|
859 | } // ?|? |
---|
860 | |
---|
861 | // manipulators |
---|
862 | istype & ?|?( istype & is, istype & (* manip)( istype & ) ) { |
---|
863 | return manip( is ); |
---|
864 | } // ?|? |
---|
865 | |
---|
866 | istype & nl( istype & is ) { |
---|
867 | fmt( is, "%*[^\n]" ); // ignore characters to newline |
---|
868 | return is; |
---|
869 | } // nl |
---|
870 | |
---|
871 | istype & nlOn( istype & is ) { |
---|
872 | nlOn( is ); // call void returning |
---|
873 | return is; |
---|
874 | } // nlOn |
---|
875 | |
---|
876 | istype & nlOff( istype & is ) { |
---|
877 | nlOff( is ); // call void returning |
---|
878 | return is; |
---|
879 | } // nlOff |
---|
880 | } // distribution |
---|
881 | |
---|
882 | //*********************************** manipulators *********************************** |
---|
883 | |
---|
884 | forall( dtype istype | istream( istype ) ) |
---|
885 | istype & ?|?( istype & is, _Istream_Cstr f ) { |
---|
886 | // skip xxx |
---|
887 | if ( ! f.s ) { |
---|
888 | // printf( "skip %s %d\n", f.scanset, f.wd ); |
---|
889 | if ( f.wd == -1 ) fmt( is, f.scanset, "" ); // no input arguments |
---|
890 | else for ( f.wd ) fmt( is, "%*c" ); |
---|
891 | return is; |
---|
892 | } // if |
---|
893 | size_t len = 0; |
---|
894 | if ( f.scanset ) len = strlen( f.scanset ); |
---|
895 | char fmtstr[len + 16]; |
---|
896 | int start = 1; |
---|
897 | fmtstr[0] = '%'; |
---|
898 | if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; } |
---|
899 | if ( f.wd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.wd ); } |
---|
900 | // cstr %s, %*s, %ws, %*ws |
---|
901 | if ( ! f.scanset ) { |
---|
902 | fmtstr[start] = 's'; fmtstr[start + 1] = '\0'; |
---|
903 | // printf( "cstr %s\n", fmtstr ); |
---|
904 | fmt( is, fmtstr, f.s ); |
---|
905 | return is; |
---|
906 | } // if |
---|
907 | // incl %[xxx], %*[xxx], %w[xxx], %*w[xxx] |
---|
908 | // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx] |
---|
909 | fmtstr[start] = '['; start += 1; |
---|
910 | if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; } |
---|
911 | strcpy( &fmtstr[start], f.scanset ); // copy includes '\0' |
---|
912 | len += start; |
---|
913 | fmtstr[len] = ']'; fmtstr[len + 1] = '\0'; |
---|
914 | // printf( "incl/excl %s\n", fmtstr ); |
---|
915 | fmt( is, fmtstr, f.s ); |
---|
916 | return is; |
---|
917 | } // ?|? |
---|
918 | |
---|
919 | forall( dtype istype | istream( istype ) ) |
---|
920 | istype & ?|?( istype & is, _Istream_Char f ) { |
---|
921 | fmt( is, "%*c" ); // argument variable unused |
---|
922 | return is; |
---|
923 | } // ?|? |
---|
924 | |
---|
925 | #define InputFMTImpl( T, CODE ) \ |
---|
926 | forall( dtype istype | istream( istype ) ) \ |
---|
927 | istype & ?|?( istype & is, _Istream_Manip(T) f ) { \ |
---|
928 | enum { size = 16 }; \ |
---|
929 | char fmtstr[size]; \ |
---|
930 | if ( f.wd == -1 ) { \ |
---|
931 | snprintf( fmtstr, size, "%%%s%s", f.ignore ? "*" : "", CODE ); \ |
---|
932 | } else { \ |
---|
933 | snprintf( fmtstr, size, "%%%s%d%s", f.ignore ? "*" : "", f.wd, CODE ); \ |
---|
934 | } /* if */ \ |
---|
935 | /* printf( "%d %s %p\n", f.wd, fmtstr, &f.val ); */ \ |
---|
936 | fmt( is, fmtstr, &f.val ); \ |
---|
937 | return is; \ |
---|
938 | } // ?|? |
---|
939 | |
---|
940 | InputFMTImpl( signed char, "hhi" ) |
---|
941 | InputFMTImpl( unsigned char, "hhi" ) |
---|
942 | InputFMTImpl( signed short int, "hi" ) |
---|
943 | InputFMTImpl( unsigned short int, "hi" ) |
---|
944 | InputFMTImpl( signed int, "i" ) |
---|
945 | InputFMTImpl( unsigned int, "i" ) |
---|
946 | InputFMTImpl( signed long int, "li" ) |
---|
947 | InputFMTImpl( unsigned long int, "li" ) |
---|
948 | InputFMTImpl( signed long long int, "lli" ) |
---|
949 | InputFMTImpl( unsigned long long int, "lli" ) |
---|
950 | |
---|
951 | InputFMTImpl( float, "f" ) |
---|
952 | InputFMTImpl( double, "lf" ) |
---|
953 | InputFMTImpl( long double, "Lf" ) |
---|
954 | |
---|
955 | forall( dtype istype | istream( istype ) ) |
---|
956 | istype & ?|?( istype & is, _Istream_Manip(float _Complex) fc ) { |
---|
957 | float re, im; |
---|
958 | _Istream_Manip(float) fmtuc @= { re, fc.wd, fc.ignore }; |
---|
959 | is | fmtuc; |
---|
960 | &fmtuc.val = &im; |
---|
961 | is | fmtuc; |
---|
962 | if ( ! fc.ignore ) fc.val = re + im * _Complex_I; // re/im are uninitialized for ignore |
---|
963 | return is; |
---|
964 | } // ?|? |
---|
965 | |
---|
966 | forall( dtype istype | istream( istype ) ) |
---|
967 | istype & ?|?( istype & is, _Istream_Manip(double _Complex) dc ) { |
---|
968 | double re, im; |
---|
969 | _Istream_Manip(double) fmtuc @= { re, dc.wd, dc.ignore }; |
---|
970 | is | fmtuc; |
---|
971 | &fmtuc.val = &im; |
---|
972 | is | fmtuc; |
---|
973 | if ( ! dc.ignore ) dc.val = re + im * _Complex_I; // re/im are uninitialized for ignore |
---|
974 | return is; |
---|
975 | } // ?|? |
---|
976 | |
---|
977 | forall( dtype istype | istream( istype ) ) |
---|
978 | istype & ?|?( istype & is, _Istream_Manip(long double _Complex) ldc ) { |
---|
979 | long double re, im; |
---|
980 | _Istream_Manip(long double) fmtuc @= { re, ldc.wd, ldc.ignore }; |
---|
981 | is | fmtuc; |
---|
982 | &fmtuc.val = &im; |
---|
983 | is | fmtuc; |
---|
984 | if ( ! ldc.ignore ) ldc.val = re + im * _Complex_I; // re/im are uninitialized for ignore |
---|
985 | return is; |
---|
986 | } // ?|? |
---|
987 | |
---|
988 | // Local Variables: // |
---|
989 | // tab-width: 4 // |
---|
990 | // compile-command: "cfa iostream.cfa" // |
---|
991 | // End: // |
---|