Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/fstream.cfa

    rd1a9ff5 ra87d40b  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Feb  7 19:01:01 2020
    13 // Update Count     : 363
     12// Last Modified On : Mon Jul 15 18:11:26 2019
     13// Update Count     : 349
    1414//
    1515
     
    3232
    3333void ?{}( ofstream & os, void * file ) {
    34         os.$file = file;
    35         os.$sepDefault = true;
    36         os.$sepOnOff = false;
    37         os.$nlOnOff = true;
    38         os.$prt = false;
    39         os.$sawNL = false;
    40         $sepSetCur( os, sepGet( os ) );
     34        os.file = file;
     35        os.sepDefault = true;
     36        os.sepOnOff = false;
     37        os.nlOnOff = true;
     38        os.prt = false;
     39        os.sawNL = false;
    4140        sepSet( os, " " );
     41        sepSetCur( os, sepGet( os ) );
    4242        sepSetTuple( os, ", " );
    4343} // ?{}
    4444
    4545// private
    46 bool $sepPrt( ofstream & os ) { $setNL( os, false ); return os.$sepOnOff; }
    47 void $sepReset( ofstream & os ) { os.$sepOnOff = os.$sepDefault; }
    48 void $sepReset( ofstream & os, bool reset ) { os.$sepDefault = reset; os.$sepOnOff = os.$sepDefault; }
    49 const char * $sepGetCur( ofstream & os ) { return os.$sepCur; }
    50 void $sepSetCur( ofstream & os, const char sepCur[] ) { os.$sepCur = sepCur; }
    51 bool $getNL( ofstream & os ) { return os.$sawNL; }
    52 void $setNL( ofstream & os, bool state ) { os.$sawNL = state; }
    53 bool $getANL( ofstream & os ) { return os.$nlOnOff; }
    54 bool $getPrt( ofstream & os ) { return os.$prt; }
    55 void $setPrt( ofstream & os, bool state ) { os.$prt = state; }
     46bool sepPrt( ofstream & os ) { setNL( os, false ); return os.sepOnOff; }
     47void sepReset( ofstream & os ) { os.sepOnOff = os.sepDefault; }
     48void sepReset( ofstream & os, bool reset ) { os.sepDefault = reset; os.sepOnOff = os.sepDefault; }
     49const char * sepGetCur( ofstream & os ) { return os.sepCur; }
     50void sepSetCur( ofstream & os, const char * sepCur ) { os.sepCur = sepCur; }
     51bool getNL( ofstream & os ) { return os.sawNL; }
     52void setNL( ofstream & os, bool state ) { os.sawNL = state; }
     53bool getANL( ofstream & os ) { return os.nlOnOff; }
     54bool getPrt( ofstream & os ) { return os.prt; }
     55void setPrt( ofstream & os, bool state ) { os.prt = state; }
    5656
    5757// public
    58 void ?{}( ofstream & os ) { os.$file = 0p; }
    59 
    60 void ?{}( ofstream & os, const char name[], const char mode[] ) {
     58void ?{}( ofstream & os ) { os.file = 0; }
     59
     60void ?{}( ofstream & os, const char * name, const char * mode ) {
    6161        open( os, name, mode );
    6262} // ?{}
    6363
    64 void ?{}( ofstream & os, const char name[] ) {
     64void ?{}( ofstream & os, const char * name ) {
    6565        open( os, name, "w" );
    6666} // ?{}
    6767
    68 void ^?{}( ofstream & os ) {
    69         close( os );
    70 } // ^?{}
    71 
    72 void sepOn( ofstream & os ) { os.$sepOnOff = ! $getNL( os ); }
    73 void sepOff( ofstream & os ) { os.$sepOnOff = false; }
     68void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); }
     69void sepOff( ofstream & os ) { os.sepOnOff = false; }
    7470
    7571bool sepDisable( ofstream & os ) {
    76         bool temp = os.$sepDefault;
    77         os.$sepDefault = false;
    78         $sepReset( os );
     72        bool temp = os.sepDefault;
     73        os.sepDefault = false;
     74        sepReset( os );
    7975        return temp;
    8076} // sepDisable
    8177
    8278bool sepEnable( ofstream & os ) {
    83         bool temp = os.$sepDefault;
    84         os.$sepDefault = true;
    85         if ( os.$sepOnOff ) $sepReset( os );                            // start of line ?
     79        bool temp = os.sepDefault;
     80        os.sepDefault = true;
     81        if ( os.sepOnOff ) sepReset( os );                                      // start of line ?
    8682        return temp;
    8783} // sepEnable
    8884
    89 void nlOn( ofstream & os ) { os.$nlOnOff = true; }
    90 void nlOff( ofstream & os ) { os.$nlOnOff = false; }
    91 
    92 const char * sepGet( ofstream & os ) { return os.$separator; }
    93 void sepSet( ofstream & os, const char s[] ) {
     85void nlOn( ofstream & os ) { os.nlOnOff = true; }
     86void nlOff( ofstream & os ) { os.nlOnOff = false; }
     87
     88const char * sepGet( ofstream & os ) { return os.separator; }
     89void sepSet( ofstream & os, const char * s ) {
    9490        assert( s );
    95         strncpy( os.$separator, s, sepSize - 1 );
    96         os.$separator[sepSize - 1] = '\0';
     91        strncpy( os.separator, s, sepSize - 1 );
     92        os.separator[sepSize - 1] = '\0';
    9793} // sepSet
    9894
    99 const char * sepGetTuple( ofstream & os ) { return os.$tupleSeparator; }
    100 void sepSetTuple( ofstream & os, const char s[] ) {
     95const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator; }
     96void sepSetTuple( ofstream & os, const char * s ) {
    10197        assert( s );
    102         strncpy( os.$tupleSeparator, s, sepSize - 1 );
    103         os.$tupleSeparator[sepSize - 1] = '\0';
     98        strncpy( os.tupleSeparator, s, sepSize - 1 );
     99        os.tupleSeparator[sepSize - 1] = '\0';
    104100} // sepSet
    105101
    106102void ends( ofstream & os ) {
    107         if ( $getANL( os ) ) nl( os );
    108         else $setPrt( os, false );                                                      // turn off
     103        if ( getANL( os ) ) nl( os );
     104        else setPrt( os, false );                                                       // turn off
    109105        if ( &os == &exit ) exit( EXIT_FAILURE );
    110106        if ( &os == &abort ) abort();
     
    112108
    113109int fail( ofstream & os ) {
    114         return os.$file == 0 || ferror( (FILE *)(os.$file) );
     110        return os.file == 0 || ferror( (FILE *)(os.file) );
    115111} // fail
    116112
    117113int flush( ofstream & os ) {
    118         return fflush( (FILE *)(os.$file) );
     114        return fflush( (FILE *)(os.file) );
    119115} // flush
    120116
    121 void open( ofstream & os, const char name[], const char mode[] ) {
     117void open( ofstream & os, const char * name, const char * mode ) {
    122118        FILE * file = fopen( name, mode );
    123119        #ifdef __CFA_DEBUG__
    124         if ( file == 0p ) {
     120        if ( file == 0 ) {
    125121                abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno );
    126122        } // if
     
    129125} // open
    130126
    131 void open( ofstream & os, const char name[] ) {
     127void open( ofstream & os, const char * name ) {
    132128        open( os, name, "w" );
    133129} // open
    134130
    135131void close( ofstream & os ) {
    136         if ( (FILE *)(os.$file) == stdout || (FILE *)(os.$file) == stderr ) return;
    137 
    138         if ( fclose( (FILE *)(os.$file) ) == EOF ) {
     132        if ( (FILE *)(os.file) == stdout || (FILE *)(os.file) == stderr ) return;
     133
     134        if ( fclose( (FILE *)(os.file) ) == EOF ) {
    139135                abort | IO_MSG "close output" | nl | strerror( errno );
    140136        } // if
    141137} // close
    142138
    143 ofstream & write( ofstream & os, const char data[], size_t size ) {
     139ofstream & write( ofstream & os, const char * data, size_t size ) {
    144140        if ( fail( os ) ) {
    145141                abort | IO_MSG "attempt write I/O on failed stream";
    146142        } // if
    147143
    148         if ( fwrite( data, 1, size, (FILE *)(os.$file) ) != size ) {
     144        if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) {
    149145                abort | IO_MSG "write" | nl | strerror( errno );
    150146        } // if
     
    155151        va_list args;
    156152        va_start( args, format );
    157         int len = vfprintf( (FILE *)(os.$file), format, args );
     153        int len = vfprintf( (FILE *)(os.file), format, args );
    158154        if ( len == EOF ) {
    159                 if ( ferror( (FILE *)(os.$file) ) ) {
     155                if ( ferror( (FILE *)(os.file) ) ) {
    160156                        abort | IO_MSG "invalid write";
    161157                } // if
     
    163159        va_end( args );
    164160
    165         $setPrt( os, true );                                                            // called in output cascade
    166         $sepReset( os );                                                                        // reset separator
     161        setPrt( os, true );                                                                     // called in output cascade
     162        sepReset( os );                                                                         // reset separator
    167163        return len;
    168164} // fmt
    169165
    170 static ofstream soutFile = { (FILE *)stdout };
     166static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) };
    171167ofstream & sout = soutFile, & stdout = soutFile;
    172 static ofstream serrFile = { (FILE *)stderr };
     168static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };
    173169ofstream & serr = serrFile, & stderr = serrFile;
    174170
    175 static ofstream exitFile = { (FILE *)stdout };
     171static ofstream exitFile = { (FILE *)(&_IO_2_1_stdout_) };
    176172ofstream & exit = exitFile;
    177 static ofstream abortFile = { (FILE *)stderr };
     173static ofstream abortFile = { (FILE *)(&_IO_2_1_stderr_) };
    178174ofstream & abort = abortFile;
    179175
     
    184180// private
    185181void ?{}( ifstream & is, void * file ) {
    186         is.$file = file;
    187         is.$nlOnOff = false;
     182        is.file = file;
     183        is.nlOnOff = false;
    188184} // ?{}
    189185
    190186// public
    191 void ?{}( ifstream & is ) { is.$file = 0p; }
    192 
    193 void ?{}( ifstream & is, const char name[], const char mode[] ) {
     187void ?{}( ifstream & is ) {     is.file = 0; }
     188
     189void ?{}( ifstream & is, const char * name, const char * mode ) {
    194190        open( is, name, mode );
    195191} // ?{}
    196192
    197 void ?{}( ifstream & is, const char name[] ) {
     193void ?{}( ifstream & is, const char * name ) {
    198194        open( is, name, "r" );
    199195} // ?{}
    200196
    201 void ^?{}( ifstream & is ) {
    202         close( is );
    203 } // ^?{}
    204 
    205 void nlOn( ifstream & os ) { os.$nlOnOff = true; }
    206 void nlOff( ifstream & os ) { os.$nlOnOff = false; }
    207 bool getANL( ifstream & os ) { return os.$nlOnOff; }
     197void nlOn( ifstream & os ) { os.nlOnOff = true; }
     198void nlOff( ifstream & os ) { os.nlOnOff = false; }
     199bool getANL( ifstream & os ) { return os.nlOnOff; }
    208200
    209201int fail( ifstream & is ) {
    210         return is.$file == 0p || ferror( (FILE *)(is.$file) );
     202        return is.file == 0 || ferror( (FILE *)(is.file) );
    211203} // fail
    212204
    213205int eof( ifstream & is ) {
    214         return feof( (FILE *)(is.$file) );
     206        return feof( (FILE *)(is.file) );
    215207} // eof
    216208
    217 void open( ifstream & is, const char name[], const char mode[] ) {
     209void open( ifstream & is, const char * name, const char * mode ) {
    218210        FILE * file = fopen( name, mode );
    219211        #ifdef __CFA_DEBUG__
    220         if ( file == 0p ) {
     212        if ( file == 0 ) {
    221213                abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
    222214        } // if
    223215        #endif // __CFA_DEBUG__
    224         is.$file = file;
    225 } // open
    226 
    227 void open( ifstream & is, const char name[] ) {
     216        is.file = file;
     217} // open
     218
     219void open( ifstream & is, const char * name ) {
    228220        open( is, name, "r" );
    229221} // open
    230222
    231223void close( ifstream & is ) {
    232         if ( (FILE *)(is.$file) == stdin ) return;
    233 
    234         if ( fclose( (FILE *)(is.$file) ) == EOF ) {
     224        if ( (FILE *)(is.file) == stdin ) return;
     225
     226        if ( fclose( (FILE *)(is.file) ) == EOF ) {
    235227                abort | IO_MSG "close input" | nl | strerror( errno );
    236228        } // if
     
    242234        } // if
    243235
    244         if ( fread( data, size, 1, (FILE *)(is.$file) ) == 0 ) {
     236        if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
    245237                abort | IO_MSG "read" | nl | strerror( errno );
    246238        } // if
     
    253245        } // if
    254246
    255         if ( ungetc( c, (FILE *)(is.$file) ) == EOF ) {
     247        if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
    256248                abort | IO_MSG "ungetc" | nl | strerror( errno );
    257249        } // if
     
    263255
    264256        va_start( args, format );
    265         int len = vfscanf( (FILE *)(is.$file), format, args );
     257        int len = vfscanf( (FILE *)(is.file), format, args );
    266258        if ( len == EOF ) {
    267                 if ( ferror( (FILE *)(is.$file) ) ) {
     259                if ( ferror( (FILE *)(is.file) ) ) {
    268260                        abort | IO_MSG "invalid read";
    269261                } // if
     
    273265} // fmt
    274266
    275 static ifstream sinFile = { (FILE *)stdin };
     267
     268static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
    276269ifstream & sin = sinFile, & stdin = sinFile;
    277270
Note: See TracChangeset for help on using the changeset viewer.