source: libcfa/src/iostream.cfa @ 12b4ab4

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 12b4ab4 was c08c3cf, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

fix conflict

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