source: libcfa/src/iostream.cfa @ 09a767e

Last change on this file since 09a767e was 09a767e, checked in by Peter A. Buhr <pabuhr@…>, 9 months ago

fix incorrect location of decimal point for floating-point value in hex, print base (0x) before 0 for hexadecimal

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