Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/iostream.cfa

    r9d362a0 r58b6d1b  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Dec 24 18:33:40 2018
    13 // Update Count     : 589
     12// Last Modified On : Sat Jun  2 08:24:56 2018
     13// Update Count     : 471
    1414//
    1515
     
    1919#include <stdio.h>
    2020#include <stdbool.h>                                                                    // true/false
    21 //#include <string.h>                                                                   // strlen, strcmp
     21//#include <string.h>                                                                           // strlen, strcmp
    2222extern int strcmp (const char *__s1, const char *__s2) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
    2323extern size_t strlen (const char *__s) __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
     
    2727
    2828forall( dtype ostype | ostream( ostype ) ) {
    29         ostype & ?|?( ostype & os, bool b ) {
     29        ostype & ?|?( ostype & os, _Bool b ) {
    3030                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    3131                fmt( os, "%s", b ? "true" : "false" );
    3232                return os;
    3333        } // ?|?
    34         void ?|?( ostype & os, bool b ) {
    35                 (ostype &)(os | b); nl( os );
    36         } // ?|?
    37 
    38         ostype & ?|?( ostype & os, char c ) {
    39                 fmt( os, "%c", c );
    40                 if ( c == '\n' ) setNL( os, true );
    41                 return sepOff( os );
    42         } // ?|?
    43         void ?|?( ostype & os, char c ) {
    44                 (ostype &)(os | c); nl( os );
    45         } // ?|?
    46 
    47         ostype & ?|?( ostype & os, signed char sc ) {
    48                 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    49                 fmt( os, "%hhd", sc );
    50                 return os;
    51         } // ?|?
    52         void ?|?( ostype & os, signed char sc ) {
    53                 (ostype &)(os | sc); nl( os );
    54         } // ?|?
    55 
    56         ostype & ?|?( ostype & os, unsigned char usc ) {
    57                 if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
    58                 fmt( os, "%hhu", usc );
    59                 return os;
    60         } // ?|?
    61         void ?|?( ostype & os, unsigned char usc ) {
    62                 (ostype &)(os | usc); nl( os );
     34
     35        ostype & ?|?( ostype & os, char ch ) {
     36                fmt( os, "%c", ch );
     37                if ( ch == '\n' ) setNL( os, true );
     38                sepOff( os );
     39                return os;
     40        } // ?|?
     41
     42        ostype & ?|?( ostype & os, signed char c ) {
     43                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     44                fmt( os, "%hhd", c );
     45                return os;
     46        } // ?|?
     47
     48        ostype & ?|?( ostype & os, unsigned char c ) {
     49                if ( sepPrt( os ) ) fmt( os, "%s", sepGetCur( os ) );
     50                fmt( os, "%hhu", c );
     51                return os;
    6352        } // ?|?
    6453
     
    6857                return os;
    6958        } // ?|?
    70         void & ?|?( ostype & os, short int si ) {
    71                 (ostype &)(os | si); nl( os );
    72         } // ?|?
    7359
    7460        ostype & ?|?( ostype & os, unsigned short int usi ) {
     
    7763                return os;
    7864        } // ?|?
    79         void & ?|?( ostype & os, unsigned short int usi ) {
    80                 (ostype &)(os | usi); nl( os );
    81         } // ?|?
    8265
    8366        ostype & ?|?( ostype & os, int i ) {
     
    8669                return os;
    8770        } // ?|?
    88         void & ?|?( ostype & os, int i ) {
    89                 (ostype &)(os | i); nl( os );
    90         } // ?|?
    9171
    9272        ostype & ?|?( ostype & os, unsigned int ui ) {
     
    9575                return os;
    9676        } // ?|?
    97         void & ?|?( ostype & os, unsigned int ui ) {
    98                 (ostype &)(os | ui); nl( os );
    99         } // ?|?
    10077
    10178        ostype & ?|?( ostype & os, long int li ) {
     
    10481                return os;
    10582        } // ?|?
    106         void & ?|?( ostype & os, long int li ) {
    107                 (ostype &)(os | li); nl( os );
    108         } // ?|?
    10983
    11084        ostype & ?|?( ostype & os, unsigned long int uli ) {
     
    11387                return os;
    11488        } // ?|?
    115         void & ?|?( ostype & os, unsigned long int uli ) {
    116                 (ostype &)(os | uli); nl( os );
    117         } // ?|?
    11889
    11990        ostype & ?|?( ostype & os, long long int lli ) {
     
    12293                return os;
    12394        } // ?|?
    124         void & ?|?( ostype & os, long long int lli ) {
    125                 (ostype &)(os | lli); nl( os );
    126         } // ?|?
    12795
    12896        ostype & ?|?( ostype & os, unsigned long long int ulli ) {
     
    13199                return os;
    132100        } // ?|?
    133         void & ?|?( ostype & os, unsigned long long int ulli ) {
    134                 (ostype &)(os | ulli); nl( os );
    135         } // ?|?
    136101
    137102        ostype & ?|?( ostype & os, float f ) {
     
    140105                return os;
    141106        } // ?|?
    142         void & ?|?( ostype & os, float f ) {
    143                 (ostype &)(os | f); nl( os );
    144         } // ?|?
    145107
    146108        ostype & ?|?( ostype & os, double d ) {
     
    149111                return os;
    150112        } // ?|?
    151         void & ?|?( ostype & os, double d ) {
    152                 (ostype &)(os | d); nl( os );
    153         } // ?|?
    154113
    155114        ostype & ?|?( ostype & os, long double ld ) {
     
    158117                return os;
    159118        } // ?|?
    160         void & ?|?( ostype & os, long double ld ) {
    161                 (ostype &)(os | ld); nl( os );
    162         } // ?|?
    163119
    164120        ostype & ?|?( ostype & os, float _Complex fc ) {
     
    167123                return os;
    168124        } // ?|?
    169         void & ?|?( ostype & os, float _Complex fc ) {
    170                 (ostype &)(os | fc); nl( os );
    171         } // ?|?
    172125
    173126        ostype & ?|?( ostype & os, double _Complex dc ) {
     
    176129                return os;
    177130        } // ?|?
    178         void & ?|?( ostype & os, double _Complex dc ) {
    179                 (ostype &)(os | dc); nl( os );
    180         } // ?|?
    181131
    182132        ostype & ?|?( ostype & os, long double _Complex ldc ) {
     
    184134                fmt( os, "%.*Lg%+.*Lgi", LDBL_DIG, creall( ldc ), LDBL_DIG, cimagl( ldc ) );
    185135                return os;
    186         } // ?|?
    187         void & ?|?( ostype & os, long double _Complex ldc ) {
    188                 (ostype &)(os | ldc); nl( os );
    189136        } // ?|?
    190137
     
    227174                return write( os, str, len );
    228175        } // ?|?
    229         void ?|?( ostype & os, const char * str ) {
    230                 (ostype &)(os | str); nl( os );
    231         } // ?|?
    232176
    233177//      ostype & ?|?( ostype & os, const char16_t * str ) {
     
    256200                return os;
    257201        } // ?|?
    258         void ?|?( ostype & os, const void * p ) {
    259                 (ostype &)(os | p); nl( os );
    260         } // ?|?
     202
    261203
    262204        // manipulators
    263205        ostype & ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
    264                 (ostype &)(manip( os ));
    265                 return os;
    266         } // ?|?
    267         void ?|?( ostype & os, ostype & (* manip)( ostype & ) ) {
    268                 (ostype &)(manip( os ));
    269                 if ( getPrt( os ) ) nl( os );                                   // something printed ?
    270                 setPrt( os, false );                                                    // turn off
     206                return manip( os );
    271207        } // ?|?
    272208
    273209        ostype & sep( ostype & os ) {
    274                 return (ostype &)(os | sepGet( os ));
     210                os | sepGet( os );
     211                return os;
    275212        } // sep
    276213
    277214        ostype & sepTuple( ostype & os ) {
    278                 return os | sepGetTuple( os );
     215                os | sepGetTuple( os );
     216                return os;
    279217        } // sepTuple
    280218
    281         ostype & nl( ostype & os ) {
    282                 (ostype &)(os | '\n');
    283                 setPrt( os, false );                                                    // turn off
     219        ostype & endl( ostype & os ) {
     220                os | '\n';
    284221                setNL( os, true );
    285222                flush( os );
    286                 return sepOff( os );                                                    // prepare for next line
    287         } // nl
    288 
    289         void nl( ostype & os ) {
    290                 if ( getANL( os ) ) (ostype &)(nl( os ));               // implementation only
    291                 else setPrt( os, false );                                               // turn off
    292         } // nl
    293 
    294         ostype & nonl( ostype & os ) {
    295                 setPrt( os, false );                                                    // turn off
    296                 return os;
    297         } // nonl
     223                sepOff( os );                                                                   // prepare for next line
     224                return os;
     225        } // endl
    298226
    299227        ostype & sepOn( ostype & os ) {
    300                 sepOn( os );                                                                    // call void returning
     228                sepOn( os );
    301229                return os;
    302230        } // sepOn
    303231
    304232        ostype & sepOff( ostype & os ) {
    305                 sepOff( os );                                                                   // call void returning
     233                sepOff( os );
    306234                return os;
    307235        } // sepOff
    308236
    309237        ostype & sepEnable( ostype & os ) {
    310                 sepEnable( os );                                                                // call void returning
     238                sepEnable( os );
    311239                return os;
    312240        } // sepEnable
    313241
    314242        ostype & sepDisable( ostype & os ) {
    315                 sepDisable( os );                                                               // call void returning
     243                sepDisable( os );
    316244                return os;
    317245        } // sepDisable
    318 
    319         ostype & nlOn( ostype & os ) {
    320                 nlOn( os );                                                                             // call void returning
    321                 return os;
    322         } // nlOn
    323 
    324         ostype & nlOff( ostype & os ) {
    325                 nlOff( os );                                                                    // call void returning
    326                 return os;
    327         } // nlOff
    328246} // distribution
    329247
     248
    330249// tuples
    331 forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } ) {
    332         ostype & ?|?( ostype & os, T arg, Params rest ) {
    333                 (ostype &)(os | arg);                                                   // print first argument
    334                 sepSetCur( os, sepGetTuple( os ) );                             // switch to tuple separator
    335                 (ostype &)(os | rest);                                                  // print remaining arguments
    336                 sepSetCur( os, sepGet( os ) );                                  // switch to regular separator
    337                 return os;
    338         } // ?|?
    339         void ?|?( ostype & os, T arg, Params rest ) {
    340                 // (ostype &)(?|?( os, arg, rest )); nl( os );
    341                 (ostype &)(os | arg);                                                   // print first argument
    342                 sepSetCur( os, sepGetTuple( os ) );                             // switch to tuple separator
    343                 (ostype &)(os | rest);                                                  // print remaining arguments
    344                 sepSetCur( os, sepGet( os ) );                                  // switch to regular separator
    345                 nl( os );
    346         } // ?|?
    347 } // distribution
     250forall( dtype ostype, otype T, ttype Params | writeable( T, ostype ) | { ostype & ?|?( ostype &, Params ); } )
     251ostype & ?|?( ostype & os, T arg, Params rest ) {
     252        os | arg;                                                                                       // print first argument
     253        sepSetCur( os, sepGetTuple( os ) );                                     // switch to tuple separator
     254        os | rest;                                                                                      // print remaining arguments
     255        sepSetCur( os, sepGet( os ) );                                          // switch to regular separator
     256        return os;
     257} // ?|?
    348258
    349259//---------------------------------------
    350260
    351261// writes the range [begin, end) to the given stream
    352 forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) ) {
    353         void write( iterator_type begin, iterator_type end, ostype & os ) {
    354                 void print( elt_type i ) { os | i; }
    355                 for_each( begin, end, print );
    356         } // ?|?
    357 
    358         void write_reverse( iterator_type begin, iterator_type end, ostype & os ) {
    359                 void print( elt_type i ) { os | i; }
    360                 for_each_reverse( begin, end, print );
    361         } // ?|?
    362 } // distribution
     262forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) )
     263void write( iterator_type begin, iterator_type end, ostype & os ) {
     264        void print( elt_type i ) { os | i; }
     265        for_each( begin, end, print );
     266} // ?|?
     267
     268forall( dtype ostype, otype elt_type | writeable( elt_type, ostype ), otype iterator_type | iterator( iterator_type, elt_type ) )
     269void write_reverse( iterator_type begin, iterator_type end, ostype & os ) {
     270        void print( elt_type i ) { os | i; }
     271        for_each_reverse( begin, end, print );
     272} // ?|?
    363273
    364274//---------------------------------------
    365275
    366276forall( dtype istype | istream( istype ) ) {
    367         istype & ?|?( istype & is, bool & b ) {
     277        istype & ?|?( istype & is, _Bool & b ) {
    368278                char val[6];
    369279                fmt( is, "%5s", val );
     
    371281                else if ( strcmp( val, "false" ) == 0 ) b = false;
    372282                else {
    373                         fprintf( stderr, "invalid Boolean constant\n" );
     283                        fprintf( stderr, "invalid _Bool constant\n" );
    374284                        abort();
    375285                } // if
     
    476386        } // ?|?
    477387
    478         istype & nl( istype & is ) {
     388        istype & endl( istype & is ) {
    479389                fmt( is, "%*[ \t\f\n\r\v]" );                                   // ignore whitespace
    480390                return is;
    481         } // nl
     391        } // endl
    482392} // distribution
    483393
     
    500410// Local Variables: //
    501411// tab-width: 4 //
    502 // compile-command: "cfa iostream.cfa" //
     412// compile-command: "cfa iostream.c" //
    503413// End: //
Note: See TracChangeset for help on using the changeset viewer.