source: libcfa/src/iostream.cfa @ 6c5d92f

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

for private variables and functions, move prefix $ to suffix $

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