source: libcfa/src/iostream.cfa@ bc547d3

Last change on this file since bc547d3 was 244335b, checked in by Peter A. Buhr <pabuhr@…>, 11 days ago

update printing of decimal point in floating-point numbers

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