source: libcfa/src/iostream.cfa@ c1dafea

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation pthread-emulation qualifiedEnum
Last change on this file since c1dafea was b1e614f4, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

check for log10(0) in output manipulator eng

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