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