source: libcfa/src/iostream.cfa @ 8d1ad36

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

move flush from basic_ostream to ostream, refactor istream into basic_istream and istream

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