source: libcfa/src/iostream.cfa@ 15a0f6f

Last change on this file since 15a0f6f was 15a0f6f, checked in by Peter A. Buhr <pabuhr@…>, 4 days ago

fix problem with printing 0 for largest int_128 values

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