Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/iostream.cfa

    r852a2f06 r4f37255  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Mar 11 14:35:35 2020
    13 // Update Count     : 860
     12// Last Modified On : Sat Jul 13 08:07:59 2019
     13// Update Count     : 821
    1414//
    1515
     
    1919#include <stdio.h>
    2020#include <stdbool.h>                                                                    // true/false
    21 #include <stdint.h>                                                                             // UINT64_MAX
    2221//#include <string.h>                                                                   // strlen, strcmp
    2322extern size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
     
    3635forall( dtype ostype | ostream( ostype ) ) {
    3736        ostype & ?|?( ostype & os, zero_t ) {
    38                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     37                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    3938                fmt( os, "%d", 0n );
    4039                return os;
     
    4544
    4645        ostype & ?|?( ostype & os, one_t ) {
    47                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     46                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    4847                fmt( os, "%d", 1n );
    4948                return os;
     
    5453
    5554        ostype & ?|?( ostype & os, bool b ) {
    56                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     55                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    5756                fmt( os, "%s", b ? "true" : "false" );
    5857                return os;
     
    6463        ostype & ?|?( ostype & os, char c ) {
    6564                fmt( os, "%c", c );
    66                 if ( c == '\n' ) $setNL( os, true );
     65                if ( c == '\n' ) setNL( os, true );
    6766                return sepOff( os );
    6867        } // ?|?
     
    7271
    7372        ostype & ?|?( ostype & os, signed char sc ) {
    74                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     73                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    7574                fmt( os, "%hhd", sc );
    7675                return os;
     
    8180
    8281        ostype & ?|?( ostype & os, unsigned char usc ) {
    83                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     82                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    8483                fmt( os, "%hhu", usc );
    8584                return os;
     
    9089
    9190        ostype & ?|?( ostype & os, short int si ) {
    92                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     91                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    9392                fmt( os, "%hd", si );
    9493                return os;
     
    9998
    10099        ostype & ?|?( ostype & os, unsigned short int usi ) {
    101                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     100                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    102101                fmt( os, "%hu", usi );
    103102                return os;
     
    108107
    109108        ostype & ?|?( ostype & os, int i ) {
    110                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     109                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    111110                fmt( os, "%d", i );
    112111                return os;
     
    117116
    118117        ostype & ?|?( ostype & os, unsigned int ui ) {
    119                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     118                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    120119                fmt( os, "%u", ui );
    121120                return os;
     
    126125
    127126        ostype & ?|?( ostype & os, long int li ) {
    128                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     127                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    129128                fmt( os, "%ld", li );
    130129                return os;
     
    135134
    136135        ostype & ?|?( ostype & os, unsigned long int uli ) {
    137                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     136                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    138137                fmt( os, "%lu", uli );
    139138                return os;
     
    144143
    145144        ostype & ?|?( ostype & os, long long int lli ) {
    146                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     145                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    147146                fmt( os, "%lld", lli );
    148147                return os;
     
    153152
    154153        ostype & ?|?( ostype & os, unsigned long long int ulli ) {
    155                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     154                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    156155                fmt( os, "%llu", ulli );
    157156                return os;
     
    160159                (ostype &)(os | ulli); ends( os );
    161160        } // ?|?
    162 
    163 #if defined( __SIZEOF_INT128__ )
    164         //      UINT64_MAX 18_446_744_073_709_551_615_ULL
    165         #define P10_UINT64 10_000_000_000_000_000_000_ULL       // 19 zeroes
    166 
    167         static void base10_128( ostype & os, unsigned int128 val ) {
    168                 if ( val > UINT64_MAX ) {
    169                         base10_128( os, val / P10_UINT64 );                     // recursive
    170                         fmt( os, "%.19lu", (uint64_t)(val % P10_UINT64) );
    171                 } else {
    172                         fmt( os, "%lu", (uint64_t)val );
    173                 } // if
    174         } // base10_128
    175 
    176         static void base10_128( ostype & os, int128 val ) {
    177                 if ( val < 0 ) {
    178                         fmt( os, "-" );                                                         // leading negative sign
    179                         val = -val;
    180                 } // if
    181                 base10_128( os, (unsigned int128)val );                 // print zero/positive value
    182         } // base10_128
    183 
    184         ostype & ?|?( ostype & os, int128 llli ) {
    185                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
    186                 base10_128( os, llli );
    187                 return os;
    188         } // ?|?
    189         void & ?|?( ostype & os, int128 llli ) {
    190                 (ostype &)(os | llli); ends( os );
    191         } // ?|?
    192 
    193         ostype & ?|?( ostype & os, unsigned int128 ullli ) {
    194                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
    195                 base10_128( os, ullli );
    196                 return os;
    197         } // ?|?
    198         void & ?|?( ostype & os, unsigned int128 ullli ) {
    199                 (ostype &)(os | ullli); ends( os );
    200         } // ?|?
    201 #endif // __SIZEOF_INT128__
    202161
    203162        #define PrintWithDP( os, format, val, ... ) \
     
    216175
    217176        ostype & ?|?( ostype & os, float f ) {
    218                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     177                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    219178                PrintWithDP( os, "%g", f );
    220179                return os;
     
    225184
    226185        ostype & ?|?( ostype & os, double d ) {
    227                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     186                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    228187                PrintWithDP( os, "%.*lg", d, DBL_DIG );
    229188                return os;
     
    234193
    235194        ostype & ?|?( ostype & os, long double ld ) {
    236                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     195                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    237196                PrintWithDP( os, "%.*Lg", ld, LDBL_DIG );
    238197                return os;
     
    243202
    244203        ostype & ?|?( ostype & os, float _Complex fc ) {
    245                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     204                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    246205//              os | crealf( fc ) | nonl;
    247206                PrintWithDP( os, "%g", crealf( fc ) );
     
    255214
    256215        ostype & ?|?( ostype & os, double _Complex dc ) {
    257                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     216                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    258217//              os | creal( dc ) | nonl;
    259218                PrintWithDP( os, "%.*lg", creal( dc ), DBL_DIG );
     
    267226
    268227        ostype & ?|?( ostype & os, long double _Complex ldc ) {
    269                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     228                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    270229//              os | creall( ldc ) || nonl;
    271230                PrintWithDP( os, "%.*Lg", creall( ldc ), LDBL_DIG );
     
    278237        } // ?|?
    279238
    280         ostype & ?|?( ostype & os, const char str[] ) {
     239        ostype & ?|?( ostype & os, const char * str ) {
    281240                enum { Open = 1, Close, OpenClose };
    282241                static const unsigned char mask[256] @= {
     
    298257                // first character IS NOT spacing or closing punctuation => add left separator
    299258                unsigned char ch = str[0];                                              // must make unsigned
    300                 if ( $sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
    301                         fmt( os, "%s", $sepGetCur( os ) );
     259                if ( sepPrt( os ) && mask[ ch ] != Close && mask[ ch ] != OpenClose ) {
     260                        fmt( os, "%s", sepGetCur( os ) );
    302261                } // if
    303262
    304263                // if string starts line, must reset to determine open state because separator is off
    305                 $sepReset( os );                                                                // reset separator
     264                sepReset( os );                                                                 // reset separator
    306265
    307266                // last character IS spacing or opening punctuation => turn off separator for next item
    308267                size_t len = strlen( str );
    309268                ch = str[len - 1];                                                              // must make unsigned
    310                 if ( $sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
     269                if ( sepPrt( os ) && mask[ ch ] != Open && mask[ ch ] != OpenClose ) {
    311270                        sepOn( os );
    312271                } else {
    313272                        sepOff( os );
    314273                } // if
    315                 if ( ch == '\n' ) $setNL( os, true );                   // check *AFTER* $sepPrt call above as it resets NL flag
     274                if ( ch == '\n' ) setNL( os, true );                    // check *AFTER* sepPrt call above as it resets NL flag
    316275                return write( os, str, len );
    317276        } // ?|?
    318 
    319         void ?|?( ostype & os, const char str[] ) {
     277        void ?|?( ostype & os, const char * str ) {
    320278                (ostype &)(os | str); ends( os );
    321279        } // ?|?
    322280
    323281//      ostype & ?|?( ostype & os, const char16_t * str ) {
    324 //              if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     282//              if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    325283//              fmt( os, "%ls", str );
    326284//              return os;
     
    329287// #if ! ( __ARM_ARCH_ISA_ARM == 1 && __ARM_32BIT_STATE == 1 ) // char32_t == wchar_t => ambiguous
    330288//      ostype & ?|?( ostype & os, const char32_t * str ) {
    331 //              if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     289//              if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    332290//              fmt( os, "%ls", str );
    333291//              return os;
     
    336294
    337295//      ostype & ?|?( ostype & os, const wchar_t * str ) {
    338 //              if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     296//              if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    339297//              fmt( os, "%ls", str );
    340298//              return os;
     
    342300
    343301        ostype & ?|?( ostype & os, const void * p ) {
    344                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     302                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    345303                fmt( os, "%p", p );
    346304                return os;
     
    357315        void ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
    358316                (ostype &)(manip( os ));
    359                 if ( $getPrt( os ) ) ends( os );                                // something printed ?
    360                 $setPrt( os, false );                                                   // turn off
     317                if ( getPrt( os ) ) ends( os );                                 // something printed ?
     318                setPrt( os, false );                                                    // turn off
    361319        } // ?|?
    362320
     
    371329        ostype & nl( ostype & os ) {
    372330                (ostype &)(os | '\n');
    373                 $setPrt( os, false );                                                   // turn off
    374                 $setNL( os, true );
     331                setPrt( os, false );                                                    // turn off
     332                setNL( os, true );
    375333                flush( os );
    376334                return sepOff( os );                                                    // prepare for next line
     
    378336
    379337        ostype & nonl( ostype & os ) {
    380                 $setPrt( os, false );                                                   // turn off
     338                setPrt( os, false );                                                    // turn off
    381339                return os;
    382340        } // nonl
     
    417375        ostype & ?|?( ostype & os, T arg, Params rest ) {
    418376                (ostype &)(os | arg);                                                   // print first argument
    419                 $sepSetCur( os, sepGetTuple( os ) );                    // switch to tuple separator
     377                sepSetCur( os, sepGetTuple( os ) );                             // switch to tuple separator
    420378                (ostype &)(os | rest);                                                  // print remaining arguments
    421                 $sepSetCur( os, sepGet( os ) );                                 // switch to regular separator
     379                sepSetCur( os, sepGet( os ) );                                  // switch to regular separator
    422380                return os;
    423381        } // ?|?
     
    425383                // (ostype &)(?|?( os, arg, rest )); ends( os );
    426384                (ostype &)(os | arg);                                                   // print first argument
    427                 $sepSetCur( os, sepGetTuple( os ) );                    // switch to tuple separator
     385                sepSetCur( os, sepGetTuple( os ) );                             // switch to tuple separator
    428386                (ostype &)(os | rest);                                                  // print remaining arguments
    429                 $sepSetCur( os, sepGet( os ) );                                 // switch to regular separator
     387                sepSetCur( os, sepGet( os ) );                                  // switch to regular separator
    430388                ends( os );
    431389        } // ?|?
     
    456414forall( dtype ostype | ostream( ostype ) ) { \
    457415        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
    458                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \
     416                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); \
    459417\
    460418                if ( f.base == 'b' || f.base == 'B' ) {                 /* bespoke binary format */ \
     
    505463\
    506464                if ( ! f.flags.pc ) {                                                   /* no precision */ \
     465                        /* printf( "%s\n", &fmtstr[star] ); */ \
    507466                        fmtstr[sizeof(IFMTNP)-2] = f.base;                      /* sizeof includes '\0' */ \
    508                         /* printf( "%s %c %c\n", &fmtstr[star], f.base, CODE ); */ \
    509467                        fmt( os, &fmtstr[star], f.wd, f.val ); \
    510468                } else {                                                                                /* precision */ \
    511469                        fmtstr[sizeof(IFMTP)-2] = f.base;                       /* sizeof includes '\0' */ \
    512                         /* printf( "%s %c %c\n", &fmtstr[star], f.base, CODE ); */ \
     470                        /* printf( "%s\n", &fmtstr[star] ); */ \
    513471                        fmt( os, &fmtstr[star], f.wd, f.pc, f.val ); \
    514472                } /* if */ \
     
    528486IntegralFMTImpl( signed long long int, 'd', "%    *ll ", "%    *.*ll " )
    529487IntegralFMTImpl( unsigned long long int, 'u', "%    *ll ", "%    *.*ll " )
    530 
    531 
    532 #if defined( __SIZEOF_INT128__ )
    533 // Default prefix for non-decimal prints is 0b, 0, 0x.
    534 #define IntegralFMTImpl128( T, SIGNED, CODE, IFMTNP, IFMTP ) \
    535 forall( dtype ostype | ostream( ostype ) ) \
    536 static void base10_128( ostype & os, _Ostream_Manip(T) f ) { \
    537         if ( f.val > UINT64_MAX ) { \
    538                 unsigned long long int lsig = f.val % P10_UINT64; \
    539                 f.val /= P10_UINT64; /* msig */ \
    540                 base10_128( os, f ); /* recursion */ \
    541                 _Ostream_Manip(unsigned long long int) fmt @= { lsig, 0, 19, 'u', { .all : 0 } }; \
    542                 fmt.flags.nobsdp = true; \
    543                 /* printf( "fmt1 %c %lld %d\n", fmt.base, fmt.val, fmt.all ); */ \
    544                 sepOff( os ); \
    545                 (ostype &)(os | fmt); \
    546         } else { \
    547                 /* printf( "fmt2 %c %lld %d\n", f.base, (unsigned long long int)f.val, f.all ); */ \
    548                 _Ostream_Manip(SIGNED long long int) fmt @= { (SIGNED long long int)f.val, f.wd, f.pc, f.base, { .all : f.all } }; \
    549                 (ostype &)(os | fmt); \
    550         } /* if */ \
    551 } /* base10_128 */ \
    552 forall( dtype ostype | ostream( ostype ) ) { \
    553         ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
    554                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \
    555 \
    556                 if ( f.base == 'b' | f.base == 'B' | f.base == 'o' | f.base == 'x' | f.base == 'X' ) { \
    557                         unsigned long long int msig = (unsigned long long int)(f.val >> 64); \
    558                         unsigned long long int lsig = (unsigned long long int)(f.val); \
    559                         _Ostream_Manip(SIGNED long long int) fmt @= { msig, f.wd, f.pc, f.base, { .all : f.all } }; \
    560                         _Ostream_Manip(unsigned long long int) fmt2 @= { lsig, 0, 0, f.base, { .all : 0 } }; \
    561                         if ( msig == 0 ) { \
    562                                 fmt.val = lsig; \
    563                                 (ostype &)(os | fmt); \
    564                         } else { \
    565                                 fmt2.flags.pad0 = fmt2.flags.nobsdp = true;     \
    566                                 if ( f.base == 'b' | f.base == 'B' ) { \
    567                                         if ( f.wd > 64 ) fmt.wd = f.wd - 64; \
    568                                         if ( f.flags.pc && f.pc > 64 ) fmt.pc = f.pc - 64; \
    569                                         fmt2.wd = 64; \
    570                                         (ostype &)(os | fmt | "" | fmt2); \
    571                                 } else if ( f.base == 'o' ) { \
    572                                         fmt.val = (unsigned long long int)fmt.val >> 2; \
    573                                         if ( f.wd > 21 ) fmt.wd = f.wd - 21; \
    574                                         if ( f.flags.pc && f.pc > 21 ) fmt.pc = f.pc - 21; \
    575                                         fmt2.wd = 1; \
    576                                         fmt2.val = ((msig & 0x3) << 1) + 1; \
    577                                         (ostype &)(os | fmt | "" | fmt2); \
    578                                         sepOff( os ); \
    579                                         fmt2.wd = 21; \
    580                                         fmt2.val = lsig & 0x7fffffffffffffff; \
    581                                         (ostype &)(os | fmt2); \
    582                                 } else { \
    583                                         if ( f.flags.left ) { \
    584                                                 if ( f.wd > 16 ) fmt2.wd = f.wd - 16; \
    585                                                 fmt.wd = 16; \
    586                                         } else { \
    587                                                 if ( f.wd > 16 ) fmt.wd = f.wd - 16; \
    588                                                 if ( f.flags.pc && f.pc > 16 ) fmt.pc = f.pc - 16; \
    589                                                 fmt2.wd = 16; \
    590                                         } /* if */ \
    591                                         (ostype &)(os | fmt | "" | fmt2); \
    592                                 } /* if */ \
    593                         } /* if */ \
    594                 } else { \
    595                         if ( CODE == 'd' ) { \
    596                                 if ( f.val < 0 )  { fmt( os, "-" ); sepOff( os ); f.val = -f.val; f.flags.sign = false; } \
    597                         } /* if */ \
    598                         base10_128( os, f ); \
    599                 } /* if */ \
    600                 return os; \
    601         } /* ?|? */ \
    602         void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \
    603 } // distribution
    604 
    605 IntegralFMTImpl128( int128, signed, 'd', "%    *ll ", "%    *.*ll " )
    606 IntegralFMTImpl128( unsigned int128, unsigned, 'u', "%    *ll ", "%    *.*ll " )
    607 #endif // __SIZEOF_INT128__
    608488
    609489//*********************************** floating point ***********************************
     
    633513forall( dtype ostype | ostream( ostype ) ) { \
    634514        ostype & ?|?( ostype & os, _Ostream_Manip(T) f ) { \
    635                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) ); \
     515                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) ); \
    636516                char fmtstr[sizeof(DFMTP)];                                             /* sizeof includes '\0' */ \
    637517                if ( ! f.flags.pc ) memcpy( &fmtstr, DFMTNP, sizeof(DFMTNP) ); \
     
    656536                return os; \
    657537        } /* ?|? */ \
    658 \
    659538        void ?|?( ostype & os, _Ostream_Manip(T) f ) { (ostype &)(os | f); ends( os ); } \
    660539} // distribution
     
    676555                } // if
    677556
    678                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     557                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    679558
    680559                #define CFMTNP "% * "
     
    692571                return os;
    693572        } // ?|?
    694 
    695573        void ?|?( ostype & os, _Ostream_Manip(char) f ) { (ostype &)(os | f); ends( os ); }
    696574} // distribution
     
    714592                } // if
    715593
    716                 if ( $sepPrt( os ) ) fmt( os, "%s", $sepGetCur( os ) );
     594                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    717595
    718596                #define SFMTNP "% * "
     
    738616                return os;
    739617        } // ?|?
    740 
    741618        void ?|?( ostype & os, _Ostream_Manip(const char *) f ) { (ostype &)(os | f); ends( os ); }
    742619} // distribution
     
    858735        } // ?|?
    859736
    860         // istype & ?|?( istype & is, const char fmt[] ) {
     737        // istype & ?|?( istype & is, const char * fmt ) {
    861738        //      fmt( is, fmt, "" );
    862739        //      return is;
Note: See TracChangeset for help on using the changeset viewer.