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 : Wed Mar 11 14:35:35 2020
|
---|
13 | // Update Count : 860
|
---|
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) f ) { \
|
---|
537 | if ( f.val > UINT64_MAX ) { \
|
---|
538 | unsigned long long int lsig = f.val % P10_UINT64; \
|
---|
539 | f.val /= P10_UINT64; /* msig */ \
|
---|
540 | base10_128( os, f ); /* recursion */ \
|
---|
541 | _Ostream_Manip(unsigned long long int) fmt @= { lsig, 0, 19, 'u', { .all : 0 } }; \
|
---|
542 | fmt.flags.nobsdp = true; \
|
---|
543 | /* printf( "fmt1 %c %lld %d\n", fmt.base, fmt.val, fmt.all ); */ \
|
---|
544 | sepOff( os ); \
|
---|
545 | (ostype &)(os | fmt); \
|
---|
546 | } else { \
|
---|
547 | /* printf( "fmt2 %c %lld %d\n", f.base, (unsigned long long int)f.val, f.all ); */ \
|
---|
548 | _Ostream_Manip(SIGNED long long int) fmt @= { (SIGNED long long int)f.val, f.wd, f.pc, f.base, { .all : f.all } }; \
|
---|
549 | (ostype &)(os | fmt); \
|
---|
550 | } /* if */ \
|
---|
551 | } /* base10_128 */ \
|
---|
552 | forall( dtype ostype | ostream( ostype ) ) { \
|
---|
553 | ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
|
---|
554 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \
|
---|
555 | \
|
---|
556 | if ( f.base == 'b' | f.base == 'B' | f.base == 'o' | f.base == 'x' | f.base == 'X' ) { \
|
---|
557 | unsigned long long int msig = (unsigned long long int)(f.val >> 64); \
|
---|
558 | unsigned long long int lsig = (unsigned long long int)(f.val); \
|
---|
559 | _Ostream_Manip(SIGNED long long int) fmt @= { msig, f.wd, f.pc, f.base, { .all : f.all } }; \
|
---|
560 | _Ostream_Manip(unsigned long long int) fmt2 @= { lsig, 0, 0, f.base, { .all : 0 } }; \
|
---|
561 | if ( msig == 0 ) { \
|
---|
562 | fmt.val = lsig; \
|
---|
563 | (ostype &)(os | fmt); \
|
---|
564 | } else { \
|
---|
565 | fmt2.flags.pad0 = fmt2.flags.nobsdp = true; \
|
---|
566 | if ( f.base == 'b' | f.base == 'B' ) { \
|
---|
567 | if ( f.wd > 64 ) fmt.wd = f.wd - 64; \
|
---|
568 | if ( f.flags.pc && f.pc > 64 ) fmt.pc = f.pc - 64; \
|
---|
569 | fmt2.wd = 64; \
|
---|
570 | (ostype &)(os | fmt | "" | fmt2); \
|
---|
571 | } else if ( f.base == 'o' ) { \
|
---|
572 | fmt.val = (unsigned long long int)fmt.val >> 2; \
|
---|
573 | if ( f.wd > 21 ) fmt.wd = f.wd - 21; \
|
---|
574 | if ( f.flags.pc && f.pc > 21 ) fmt.pc = f.pc - 21; \
|
---|
575 | fmt2.wd = 1; \
|
---|
576 | fmt2.val = ((msig & 0x3) << 1) + 1; \
|
---|
577 | (ostype &)(os | fmt | "" | fmt2); \
|
---|
578 | sepOff( os ); \
|
---|
579 | fmt2.wd = 21; \
|
---|
580 | fmt2.val = lsig & 0x7fffffffffffffff; \
|
---|
581 | (ostype &)(os | fmt2); \
|
---|
582 | } else { \
|
---|
583 | if ( f.flags.left ) { \
|
---|
584 | if ( f.wd > 16 ) fmt2.wd = f.wd - 16; \
|
---|
585 | fmt.wd = 16; \
|
---|
586 | } else { \
|
---|
587 | if ( f.wd > 16 ) fmt.wd = f.wd - 16; \
|
---|
588 | if ( f.flags.pc && f.pc > 16 ) fmt.pc = f.pc - 16; \
|
---|
589 | fmt2.wd = 16; \
|
---|
590 | } /* if */ \
|
---|
591 | (ostype &)(os | fmt | "" | fmt2); \
|
---|
592 | } /* if */ \
|
---|
593 | } /* if */ \
|
---|
594 | } else { \
|
---|
595 | if ( CODE == 'd' ) { \
|
---|
596 | if ( f.val < 0 ) { fmt( os, "-" ); sepOff( os ); f.val = -f.val; f.flags.sign = false; } \
|
---|
597 | } /* if */ \
|
---|
598 | base10_128( os, f ); \
|
---|
599 | } /* if */ \
|
---|
600 | return os; \
|
---|
601 | } /* ?|? */ \
|
---|
602 | void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \
|
---|
603 | } // distribution
|
---|
604 |
|
---|
605 | IntegralFMTImpl128( int128, signed, 'd', "% *ll ", "% *.*ll " )
|
---|
606 | IntegralFMTImpl128( unsigned int128, unsigned, 'u', "% *ll ", "% *.*ll " )
|
---|
607 | #endif // __SIZEOF_INT128__
|
---|
608 |
|
---|
609 | //*********************************** floating point ***********************************
|
---|
610 |
|
---|
611 | #define PrintWithDP2( os, format, val, ... ) \
|
---|
612 | { \
|
---|
613 | enum { size = 48 }; \
|
---|
614 | char buf[size]; \
|
---|
615 | int bufbeg = 0, i, len = snprintf( buf, size, format, ##__VA_ARGS__, val ); \
|
---|
616 | if ( isfinite( val ) && (f.base != 'g' || f.pc != 0) ) { /* if number, print decimal point */ \
|
---|
617 | for ( i = 0; i < len && buf[i] != '.' && buf[i] != 'e' && buf[i] != 'E'; i += 1 ); /* decimal point or scientific ? */ \
|
---|
618 | if ( i == len && ! f.flags.nobsdp ) { \
|
---|
619 | if ( ! f.flags.left ) { \
|
---|
620 | buf[i] = '.'; buf[i + 1] = '\0'; \
|
---|
621 | if ( buf[0] == ' ' ) bufbeg = 1; /* decimal point within width */ \
|
---|
622 | } else { \
|
---|
623 | for ( i = 0; i < len && buf[i] != ' '; i += 1 ); /* trailing blank ? */ \
|
---|
624 | buf[i] = '.'; \
|
---|
625 | if ( i == len ) buf[i + 1] = '\0'; \
|
---|
626 | } /* if */ \
|
---|
627 | } /* if */ \
|
---|
628 | } /* if */ \
|
---|
629 | fmt( os, "%s", &buf[bufbeg] ); \
|
---|
630 | }
|
---|
631 |
|
---|
632 | #define FloatingPointFMTImpl( T, DFMTNP, DFMTP ) \
|
---|
633 | forall( dtype ostype | ostream( ostype ) ) { \
|
---|
634 | ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
|
---|
635 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \
|
---|
636 | char fmtstr[sizeof(DFMTP)]; /* sizeof includes '\0' */ \
|
---|
637 | if ( ! f.flags.pc ) memcpy( &fmtstr, DFMTNP, sizeof(DFMTNP) ); \
|
---|
638 | else memcpy( &fmtstr, DFMTP, sizeof(DFMTP) ); \
|
---|
639 | int star = 4; /* position before first '*' */ \
|
---|
640 | \
|
---|
641 | /* Insert flags into spaces before '*', from right to left. */ \
|
---|
642 | if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; } \
|
---|
643 | if ( f.flags.sign ) { fmtstr[star] = '+'; star -= 1; } \
|
---|
644 | if ( f.flags.pad0 ) { fmtstr[star] = '0'; star -= 1; } \
|
---|
645 | fmtstr[star] = '%'; \
|
---|
646 | \
|
---|
647 | if ( ! f.flags.pc ) { /* no precision */ \
|
---|
648 | fmtstr[sizeof(DFMTNP)-2] = f.base; /* sizeof includes '\0' */ \
|
---|
649 | /* printf( "%g %d %s\n", f.val, f.wd, &fmtstr[star]); */ \
|
---|
650 | PrintWithDP2( os, &fmtstr[star], f.val, f.wd ) \
|
---|
651 | } else { /* precision */ \
|
---|
652 | fmtstr[sizeof(DFMTP)-2] = f.base; /* sizeof includes '\0' */ \
|
---|
653 | /* printf( "%g %d %d %s\n", f.val, f.wd, f.pc, &fmtstr[star] ); */ \
|
---|
654 | PrintWithDP2( os, &fmtstr[star], f.val, f.wd, f.pc ) \
|
---|
655 | } /* if */ \
|
---|
656 | return os; \
|
---|
657 | } /* ?|? */ \
|
---|
658 | \
|
---|
659 | void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \
|
---|
660 | } // distribution
|
---|
661 |
|
---|
662 | FloatingPointFMTImpl( double, "% * ", "% *.* " )
|
---|
663 | FloatingPointFMTImpl( long double, "% *L ", "% *.*L " )
|
---|
664 |
|
---|
665 | //*********************************** character ***********************************
|
---|
666 |
|
---|
667 | forall( dtype ostype | ostream( ostype ) ) {
|
---|
668 | ostype & ?|?( ostype & os, _Ostream_Manip(char) f ) {
|
---|
669 | if ( f.base != 'c' ) { // bespoke binary/octal/hex format
|
---|
670 | _Ostream_Manip(unsigned char) fmtuc @= { f.val, f.wd, f.pc, f.base, {'\0'} };
|
---|
671 | fmtuc.flags.pc = f.flags.pc;
|
---|
672 | fmtuc.flags.nobsdp = f.flags.nobsdp;
|
---|
673 | // os | fmtuc | nonl;
|
---|
674 | (ostype &)(os | fmtuc);
|
---|
675 | return os;
|
---|
676 | } // if
|
---|
677 |
|
---|
678 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
|
---|
679 |
|
---|
680 | #define CFMTNP "% * "
|
---|
681 | char fmtstr[sizeof(CFMTNP)]; // sizeof includes '\0'
|
---|
682 | memcpy( &fmtstr, CFMTNP, sizeof(CFMTNP) );
|
---|
683 | int star = 1; // position before first '*'
|
---|
684 |
|
---|
685 | // Insert flags into spaces before '*', from right to left.
|
---|
686 | if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; }
|
---|
687 | fmtstr[star] = '%';
|
---|
688 |
|
---|
689 | fmtstr[sizeof(CFMTNP)-2] = f.base; // sizeof includes '\0'
|
---|
690 | // printf( "%d %s\n", f.wd, &fmtstr[star] );
|
---|
691 | fmt( os, &fmtstr[star], f.wd, f.val );
|
---|
692 | return os;
|
---|
693 | } // ?|?
|
---|
694 |
|
---|
695 | void ?|?( ostype & os, _Ostream_Manip(char) f ) { (ostype &)(os | f); ends( os ); }
|
---|
696 | } // distribution
|
---|
697 |
|
---|
698 | //*********************************** C string ***********************************
|
---|
699 |
|
---|
700 | forall( dtype ostype | ostream( ostype ) ) {
|
---|
701 | ostype & ?|?( ostype & os, _Ostream_Manip(const char *) f ) {
|
---|
702 | if ( ! f.val ) return os; // null pointer ?
|
---|
703 |
|
---|
704 | if ( f.base != 's' ) { // bespoke binary/octal/hex format
|
---|
705 | _Ostream_Manip(unsigned char) fmtuc @= { 0, f.wd, f.pc, f.base, {'\0'} };
|
---|
706 | fmtuc.flags.pc = f.flags.pc;
|
---|
707 | fmtuc.flags.nobsdp = f.flags.nobsdp;
|
---|
708 | for ( unsigned int i = 0; f.val[i] != '\0'; i += 1 ) {
|
---|
709 | fmtuc.val = f.val[i];
|
---|
710 | // os | fmtuc | nonl;
|
---|
711 | (ostype &)(os | fmtuc);
|
---|
712 | } // for
|
---|
713 | return os;
|
---|
714 | } // if
|
---|
715 |
|
---|
716 | if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
|
---|
717 |
|
---|
718 | #define SFMTNP "% * "
|
---|
719 | #define SFMTP "% *.* "
|
---|
720 | char fmtstr[sizeof(SFMTP)]; // sizeof includes '\0'
|
---|
721 | if ( ! f.flags.pc ) memcpy( &fmtstr, SFMTNP, sizeof(SFMTNP) );
|
---|
722 | else memcpy( &fmtstr, SFMTP, sizeof(SFMTP) );
|
---|
723 | int star = 1; // position before first '*'
|
---|
724 |
|
---|
725 | // Insert flags into spaces before '*', from right to left.
|
---|
726 | if ( f.flags.left ) { fmtstr[star] = '-'; star -= 1; }
|
---|
727 | fmtstr[star] = '%';
|
---|
728 |
|
---|
729 | if ( ! f.flags.pc ) { // no precision
|
---|
730 | // printf( "%d %s\n", f.wd, &fmtstr[star] );
|
---|
731 | fmtstr[sizeof(SFMTNP)-2] = f.base; // sizeof includes '\0'
|
---|
732 | fmt( os, &fmtstr[star], f.wd, f.val );
|
---|
733 | } else { // precision
|
---|
734 | fmtstr[sizeof(SFMTP)-2] = f.base; // sizeof includes '\0'
|
---|
735 | // printf( "%d %d %s\n", f.wd, f.pc, &fmtstr[star] );
|
---|
736 | fmt( os, &fmtstr[star], f.wd, f.pc, f.val );
|
---|
737 | } // if
|
---|
738 | return os;
|
---|
739 | } // ?|?
|
---|
740 |
|
---|
741 | void ?|?( ostype & os, _Ostream_Manip(const char *) f ) { (ostype &)(os | f); ends( os ); }
|
---|
742 | } // distribution
|
---|
743 |
|
---|
744 |
|
---|
745 | //*********************************** istream ***********************************
|
---|
746 |
|
---|
747 |
|
---|
748 | forall( dtype istype | istream( istype ) ) {
|
---|
749 | istype & ?|?( istype & is, bool & b ) {
|
---|
750 | char val[6];
|
---|
751 | fmt( is, "%5s", val );
|
---|
752 | if ( strcmp( val, "true" ) == 0 ) b = true;
|
---|
753 | else if ( strcmp( val, "false" ) == 0 ) b = false;
|
---|
754 | else {
|
---|
755 | fprintf( stderr, "invalid Boolean constant\n" );
|
---|
756 | abort(); // cannot use abort stream
|
---|
757 | } // if
|
---|
758 | return is;
|
---|
759 | } // ?|?
|
---|
760 |
|
---|
761 | istype & ?|?( istype & is, char & c ) {
|
---|
762 | char temp;
|
---|
763 | for () {
|
---|
764 | fmt( is, "%c", &temp ); // must pass pointer through varg to fmt
|
---|
765 | // do not overwrite parameter with newline unless appropriate
|
---|
766 | if ( temp != '\n' || getANL( is ) ) { c = temp; break; }
|
---|
767 | if ( eof( is ) ) break;
|
---|
768 | } // for
|
---|
769 | return is;
|
---|
770 | } // ?|?
|
---|
771 |
|
---|
772 | istype & ?|?( istype & is, signed char & sc ) {
|
---|
773 | fmt( is, "%hhi", &sc );
|
---|
774 | return is;
|
---|
775 | } // ?|?
|
---|
776 |
|
---|
777 | istype & ?|?( istype & is, unsigned char & usc ) {
|
---|
778 | fmt( is, "%hhi", &usc );
|
---|
779 | return is;
|
---|
780 | } // ?|?
|
---|
781 |
|
---|
782 | istype & ?|?( istype & is, short int & si ) {
|
---|
783 | fmt( is, "%hi", &si );
|
---|
784 | return is;
|
---|
785 | } // ?|?
|
---|
786 |
|
---|
787 | istype & ?|?( istype & is, unsigned short int & usi ) {
|
---|
788 | fmt( is, "%hi", &usi );
|
---|
789 | return is;
|
---|
790 | } // ?|?
|
---|
791 |
|
---|
792 | istype & ?|?( istype & is, int & i ) {
|
---|
793 | fmt( is, "%i", &i );
|
---|
794 | return is;
|
---|
795 | } // ?|?
|
---|
796 |
|
---|
797 | istype & ?|?( istype & is, unsigned int & ui ) {
|
---|
798 | fmt( is, "%i", &ui );
|
---|
799 | return is;
|
---|
800 | } // ?|?
|
---|
801 |
|
---|
802 | istype & ?|?( istype & is, long int & li ) {
|
---|
803 | fmt( is, "%li", &li );
|
---|
804 | return is;
|
---|
805 | } // ?|?
|
---|
806 |
|
---|
807 | istype & ?|?( istype & is, unsigned long int & ulli ) {
|
---|
808 | fmt( is, "%li", &ulli );
|
---|
809 | return is;
|
---|
810 | } // ?|?
|
---|
811 |
|
---|
812 | istype & ?|?( istype & is, long long int & lli ) {
|
---|
813 | fmt( is, "%lli", &lli );
|
---|
814 | return is;
|
---|
815 | } // ?|?
|
---|
816 |
|
---|
817 | istype & ?|?( istype & is, unsigned long long int & ulli ) {
|
---|
818 | fmt( is, "%lli", &ulli );
|
---|
819 | return is;
|
---|
820 | } // ?|?
|
---|
821 |
|
---|
822 |
|
---|
823 | istype & ?|?( istype & is, float & f ) {
|
---|
824 | fmt( is, "%f", &f );
|
---|
825 | return is;
|
---|
826 | } // ?|?
|
---|
827 |
|
---|
828 | istype & ?|?( istype & is, double & d ) {
|
---|
829 | fmt( is, "%lf", &d );
|
---|
830 | return is;
|
---|
831 | } // ?|?
|
---|
832 |
|
---|
833 | istype & ?|?( istype & is, long double & ld ) {
|
---|
834 | fmt( is, "%Lf", &ld );
|
---|
835 | return is;
|
---|
836 | } // ?|?
|
---|
837 |
|
---|
838 |
|
---|
839 | istype & ?|?( istype & is, float _Complex & fc ) {
|
---|
840 | float re, im;
|
---|
841 | fmt( is, "%f%fi", &re, &im );
|
---|
842 | fc = re + im * _Complex_I;
|
---|
843 | return is;
|
---|
844 | } // ?|?
|
---|
845 |
|
---|
846 | istype & ?|?( istype & is, double _Complex & dc ) {
|
---|
847 | double re, im;
|
---|
848 | fmt( is, "%lf%lfi", &re, &im );
|
---|
849 | dc = re + im * _Complex_I;
|
---|
850 | return is;
|
---|
851 | } // ?|?
|
---|
852 |
|
---|
853 | istype & ?|?( istype & is, long double _Complex & ldc ) {
|
---|
854 | long double re, im;
|
---|
855 | fmt( is, "%Lf%Lfi", &re, &im );
|
---|
856 | ldc = re + im * _Complex_I;
|
---|
857 | return is;
|
---|
858 | } // ?|?
|
---|
859 |
|
---|
860 | // istype & ?|?( istype & is, const char fmt[] ) {
|
---|
861 | // fmt( is, fmt, "" );
|
---|
862 | // return is;
|
---|
863 | // } // ?|?
|
---|
864 |
|
---|
865 | istype & ?|?( istype & is, char * s ) {
|
---|
866 | fmt( is, "%s", s );
|
---|
867 | return is;
|
---|
868 | } // ?|?
|
---|
869 |
|
---|
870 | // manipulators
|
---|
871 | istype & ?|?( istype & is, istype & (* manip)( istype & ) ) {
|
---|
872 | return manip( is );
|
---|
873 | } // ?|?
|
---|
874 |
|
---|
875 | istype & nl( istype & is ) {
|
---|
876 | fmt( is, "%*[^\n]" ); // ignore characters to newline
|
---|
877 | return is;
|
---|
878 | } // nl
|
---|
879 |
|
---|
880 | istype & nlOn( istype & is ) {
|
---|
881 | nlOn( is ); // call void returning
|
---|
882 | return is;
|
---|
883 | } // nlOn
|
---|
884 |
|
---|
885 | istype & nlOff( istype & is ) {
|
---|
886 | nlOff( is ); // call void returning
|
---|
887 | return is;
|
---|
888 | } // nlOff
|
---|
889 | } // distribution
|
---|
890 |
|
---|
891 | //*********************************** manipulators ***********************************
|
---|
892 |
|
---|
893 | forall( dtype istype | istream( istype ) )
|
---|
894 | istype & ?|?( istype & is, _Istream_Cstr f ) {
|
---|
895 | // skip xxx
|
---|
896 | if ( ! f.s ) {
|
---|
897 | // printf( "skip %s %d\n", f.scanset, f.wd );
|
---|
898 | if ( f.wd == -1 ) fmt( is, f.scanset, "" ); // no input arguments
|
---|
899 | else for ( f.wd ) fmt( is, "%*c" );
|
---|
900 | return is;
|
---|
901 | } // if
|
---|
902 | size_t len = 0;
|
---|
903 | if ( f.scanset ) len = strlen( f.scanset );
|
---|
904 | char fmtstr[len + 16];
|
---|
905 | int start = 1;
|
---|
906 | fmtstr[0] = '%';
|
---|
907 | if ( f.flags.ignore ) { fmtstr[1] = '*'; start += 1; }
|
---|
908 | if ( f.wd != -1 ) { start += sprintf( &fmtstr[start], "%d", f.wd ); }
|
---|
909 | // cstr %s, %*s, %ws, %*ws
|
---|
910 | if ( ! f.scanset ) {
|
---|
911 | fmtstr[start] = 's'; fmtstr[start + 1] = '\0';
|
---|
912 | // printf( "cstr %s\n", fmtstr );
|
---|
913 | fmt( is, fmtstr, f.s );
|
---|
914 | return is;
|
---|
915 | } // if
|
---|
916 | // incl %[xxx], %*[xxx], %w[xxx], %*w[xxx]
|
---|
917 | // excl %[^xxx], %*[^xxx], %w[^xxx], %*w[^xxx]
|
---|
918 | fmtstr[start] = '['; start += 1;
|
---|
919 | if ( f.flags.inex ) { fmtstr[start] = '^'; start += 1; }
|
---|
920 | strcpy( &fmtstr[start], f.scanset ); // copy includes '\0'
|
---|
921 | len += start;
|
---|
922 | fmtstr[len] = ']'; fmtstr[len + 1] = '\0';
|
---|
923 | // printf( "incl/excl %s\n", fmtstr );
|
---|
924 | fmt( is, fmtstr, f.s );
|
---|
925 | return is;
|
---|
926 | } // ?|?
|
---|
927 |
|
---|
928 | forall( dtype istype | istream( istype ) )
|
---|
929 | istype & ?|?( istype & is, _Istream_Char f ) {
|
---|
930 | fmt( is, "%*c" ); // argument variable unused
|
---|
931 | return is;
|
---|
932 | } // ?|?
|
---|
933 |
|
---|
934 | #define InputFMTImpl( T, CODE ) \
|
---|
935 | forall( dtype istype | istream( istype ) ) \
|
---|
936 | istype & ?|?( istype & is, _Istream_Manip(T) f ) { \
|
---|
937 | enum { size = 16 }; \
|
---|
938 | char fmtstr[size]; \
|
---|
939 | if ( f.wd == -1 ) { \
|
---|
940 | snprintf( fmtstr, size, "%%%s%s", f.ignore ? "*" : "", CODE ); \
|
---|
941 | } else { \
|
---|
942 | snprintf( fmtstr, size, "%%%s%d%s", f.ignore ? "*" : "", f.wd, CODE ); \
|
---|
943 | } /* if */ \
|
---|
944 | /* printf( "%d %s %p\n", f.wd, fmtstr, &f.val ); */ \
|
---|
945 | fmt( is, fmtstr, &f.val ); \
|
---|
946 | return is; \
|
---|
947 | } // ?|?
|
---|
948 |
|
---|
949 | InputFMTImpl( signed char, "hhi" )
|
---|
950 | InputFMTImpl( unsigned char, "hhi" )
|
---|
951 | InputFMTImpl( signed short int, "hi" )
|
---|
952 | InputFMTImpl( unsigned short int, "hi" )
|
---|
953 | InputFMTImpl( signed int, "i" )
|
---|
954 | InputFMTImpl( unsigned int, "i" )
|
---|
955 | InputFMTImpl( signed long int, "li" )
|
---|
956 | InputFMTImpl( unsigned long int, "li" )
|
---|
957 | InputFMTImpl( signed long long int, "lli" )
|
---|
958 | InputFMTImpl( unsigned long long int, "lli" )
|
---|
959 |
|
---|
960 | InputFMTImpl( float, "f" )
|
---|
961 | InputFMTImpl( double, "lf" )
|
---|
962 | InputFMTImpl( long double, "Lf" )
|
---|
963 |
|
---|
964 | forall( dtype istype | istream( istype ) )
|
---|
965 | istype & ?|?( istype & is, _Istream_Manip(float _Complex) fc ) {
|
---|
966 | float re, im;
|
---|
967 | _Istream_Manip(float) fmtuc @= { re, fc.wd, fc.ignore };
|
---|
968 | is | fmtuc;
|
---|
969 | &fmtuc.val = &im;
|
---|
970 | is | fmtuc;
|
---|
971 | if ( ! fc.ignore ) fc.val = re + im * _Complex_I; // re/im are uninitialized for ignore
|
---|
972 | return is;
|
---|
973 | } // ?|?
|
---|
974 |
|
---|
975 | forall( dtype istype | istream( istype ) )
|
---|
976 | istype & ?|?( istype & is, _Istream_Manip(double _Complex) dc ) {
|
---|
977 | double re, im;
|
---|
978 | _Istream_Manip(double) fmtuc @= { re, dc.wd, dc.ignore };
|
---|
979 | is | fmtuc;
|
---|
980 | &fmtuc.val = &im;
|
---|
981 | is | fmtuc;
|
---|
982 | if ( ! dc.ignore ) dc.val = re + im * _Complex_I; // re/im are uninitialized for ignore
|
---|
983 | return is;
|
---|
984 | } // ?|?
|
---|
985 |
|
---|
986 | forall( dtype istype | istream( istype ) )
|
---|
987 | istype & ?|?( istype & is, _Istream_Manip(long double _Complex) ldc ) {
|
---|
988 | long double re, im;
|
---|
989 | _Istream_Manip(long double) fmtuc @= { re, ldc.wd, ldc.ignore };
|
---|
990 | is | fmtuc;
|
---|
991 | &fmtuc.val = &im;
|
---|
992 | is | fmtuc;
|
---|
993 | if ( ! ldc.ignore ) ldc.val = re + im * _Complex_I; // re/im are uninitialized for ignore
|
---|
994 | return is;
|
---|
995 | } // ?|?
|
---|
996 |
|
---|
997 | // Local Variables: //
|
---|
998 | // tab-width: 4 //
|
---|
999 | // compile-command: "cfa iostream.cfa" //
|
---|
1000 | // End: //
|
---|