source: libcfa/src/iostream.cfa @ 5751a56

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

patch bug causing print of -0 for gcc 5 & 6

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