source: libcfa/src/iostream.cfa @ 5454d77

Last change on this file since 5454d77 was 94d2544, checked in by Peter A. Buhr <pabuhr@…>, 12 months ago

formatting, create macros OSTYPE_VOID, OSTYPE_VOID_IMPL. ISTYPE_VOID, ISTYPE_VOID_IMPL to create companion void print routine

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