source: libcfa/src/iostream.cfa @ 93829cb

ADTast-experimentalenumpthread-emulationqualifiedEnum
Last change on this file since 93829cb was 6a33e40, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

print 0p for NULL char * pointer

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