Ignore:
Timestamp:
Dec 7, 2017, 1:10:51 PM (4 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
5e1adb5
Parents:
92494fd
Message:

complete conversion of iostream/fstream to use references

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/fstream.c

    r92494fd r09687aa  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jul 20 15:20:49 2017
    13 // Update Count     : 252
     12// Last Modified On : Thu Dec  7 08:35:01 2017
     13// Update Count     : 270
    1414//
    1515
     
    2727#define IO_MSG "I/O error: "
    2828
    29 void ?{}( ofstream & this, void * file, _Bool sepDefault, _Bool sepOnOff, const char * separator, const char * tupleSeparator ) {
    30         this.file = file;
    31         this.sepDefault = sepDefault;
    32         this.sepOnOff = sepOnOff;
    33         sepSet( &this, separator );
    34         sepSetCur( &this, sepGet( &this ) );
    35         sepSetTuple( &this, tupleSeparator );
     29void ?{}( ofstream & os, void * file, _Bool sepDefault, _Bool sepOnOff, const char * separator, const char * tupleSeparator ) {
     30        os.file = file;
     31        os.sepDefault = sepDefault;
     32        os.sepOnOff = sepOnOff;
     33        sepSet( os, separator );
     34        sepSetCur( os, sepGet( os ) );
     35        sepSetTuple( os, tupleSeparator );
    3636}
    3737
    3838// private
    39 _Bool sepPrt( ofstream * os ) { setNL( os, false ); return os->sepOnOff; }
    40 void sepReset( ofstream * os ) { os->sepOnOff = os->sepDefault; }
    41 void sepReset( ofstream * os, _Bool reset ) { os->sepDefault = reset; os->sepOnOff = os->sepDefault; }
    42 const char * sepGetCur( ofstream * os ) { return os->sepCur; }
    43 void sepSetCur( ofstream * os, const char * sepCur ) { os->sepCur = sepCur; }
    44 _Bool getNL( ofstream * os ) { return os->sawNL; }
    45 void setNL( ofstream * os, _Bool state ) { os->sawNL = state; }
     39_Bool sepPrt( ofstream & os ) { setNL( os, false ); return os.sepOnOff; }
     40void sepReset( ofstream & os ) { os.sepOnOff = os.sepDefault; }
     41void sepReset( ofstream & os, _Bool reset ) { os.sepDefault = reset; os.sepOnOff = os.sepDefault; }
     42const char * sepGetCur( ofstream & os ) { return os.sepCur; }
     43void sepSetCur( ofstream & os, const char * sepCur ) { os.sepCur = sepCur; }
     44_Bool getNL( ofstream & os ) { return os.sawNL; }
     45void setNL( ofstream & os, _Bool state ) { os.sawNL = state; }
    4646
    4747// public
    48 void sepOn( ofstream * os ) { os->sepOnOff = ! getNL( os ); }
    49 void sepOff( ofstream * os ) { os->sepOnOff = false; }
    50 
    51 _Bool sepDisable( ofstream *os ) {
    52         _Bool temp = os->sepDefault;
    53         os->sepDefault = false;
     48void ?{}( ofstream & os ) {}
     49
     50void ?{}( ofstream & os, const char * name, const char * mode ) {
     51        open( os, name, mode );
     52}
     53
     54void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); }
     55void sepOff( ofstream & os ) { os.sepOnOff = false; }
     56
     57_Bool sepDisable( ofstream & os ) {
     58        _Bool temp = os.sepDefault;
     59        os.sepDefault = false;
    5460        sepReset( os );
    5561        return temp;
    5662} // sepDisable
    5763
    58 _Bool sepEnable( ofstream *os ) {
    59         _Bool temp = os->sepDefault;
    60         os->sepDefault = true;
    61         if ( os->sepOnOff ) sepReset( os );                                     // start of line ?
     64_Bool sepEnable( ofstream & os ) {
     65        _Bool temp = os.sepDefault;
     66        os.sepDefault = true;
     67        if ( os.sepOnOff ) sepReset( os );                                      // start of line ?
    6268        return temp;
    6369} // sepEnable
    6470
    65 const char * sepGet( ofstream * os ) { return os->separator; }
    66 void sepSet( ofstream * os, const char * s ) {
     71const char * sepGet( ofstream & os ) { return os.separator; }
     72void sepSet( ofstream & os, const char * s ) {
    6773        assert( s );
    68         strncpy( os->separator, s, sepSize - 1 );
    69         os->separator[sepSize - 1] = '\0';
     74        strncpy( os.separator, s, sepSize - 1 );
     75        os.separator[sepSize - 1] = '\0';
    7076} // sepSet
    7177
    72 const char * sepGetTuple( ofstream * os ) { return os->tupleSeparator; }
    73 void sepSetTuple( ofstream * os, const char * s ) {
     78const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator; }
     79void sepSetTuple( ofstream & os, const char * s ) {
    7480        assert( s );
    75         strncpy( os->tupleSeparator, s, sepSize - 1 );
    76         os->tupleSeparator[sepSize - 1] = '\0';
     81        strncpy( os.tupleSeparator, s, sepSize - 1 );
     82        os.tupleSeparator[sepSize - 1] = '\0';
    7783} // sepSet
    7884
    79 int fail( ofstream * os ) {
    80         return ferror( (FILE *)(os->file) );
     85int fail( ofstream & os ) {
     86        return ferror( (FILE *)(os.file) );
    8187} // fail
    8288
    83 int flush( ofstream * os ) {
    84         return fflush( (FILE *)(os->file) );
     89int flush( ofstream & os ) {
     90        return fflush( (FILE *)(os.file) );
    8591} // flush
    8692
    87 void open( ofstream * os, const char * name, const char * mode ) {
     93void open( ofstream & os, const char * name, const char * mode ) {
    8894        FILE *file = fopen( name, mode );
    8995        if ( file == 0 ) {                                                                      // do not change unless successful
     
    9298                exit( EXIT_FAILURE );
    9399        } // if
    94         ?{}( *os, file, true, false, " ", ", " );
     100        (os){ file, true, false, " ", ", " };
    95101} // open
    96102
    97 void close( ofstream * os ) {
    98         if ( (FILE *)(os->file) == stdout || (FILE *)(os->file) == stderr ) return;
    99 
    100         if ( fclose( (FILE *)(os->file) ) == EOF ) {
     103void close( ofstream & os ) {
     104        if ( (FILE *)(os.file) == stdout || (FILE *)(os.file) == stderr ) return;
     105
     106        if ( fclose( (FILE *)(os.file) ) == EOF ) {
    101107                perror( IO_MSG "close output" );
    102108        } // if
    103109} // close
    104110
    105 ofstream * write( ofstream * os, const char * data, unsigned long int size ) {
     111ofstream & write( ofstream & os, const char * data, unsigned long int size ) {
    106112        if ( fail( os ) ) {
    107113                fprintf( stderr, "attempt write I/O on failed stream\n" );
     
    109115        } // if
    110116
    111         if ( fwrite( data, 1, size, (FILE *)(os->file) ) != size ) {
     117        if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) {
    112118                perror( IO_MSG "write" );
    113119                exit( EXIT_FAILURE );
     
    116122} // write
    117123
    118 int fmt( ofstream * os, const char format[], ... ) {
     124int fmt( ofstream & os, const char format[], ... ) {
    119125        va_list args;
    120126        va_start( args, format );
    121         int len = vfprintf( (FILE *)(os->file), format, args );
     127        int len = vfprintf( (FILE *)(os.file), format, args );
    122128        if ( len == EOF ) {
    123                 if ( ferror( (FILE *)(os->file) ) ) {
     129                if ( ferror( (FILE *)(os.file) ) ) {
    124130                        fprintf( stderr, "invalid write\n" );
    125131                        exit( EXIT_FAILURE );
     
    133139
    134140static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), true, false, " ", ", " };
    135 ofstream *sout = &soutFile;
     141ofstream & sout = soutFile;
    136142static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), true, false, " ", ", " };
    137 ofstream *serr = &serrFile;
     143ofstream & serr = serrFile;
    138144
    139145
    140146//---------------------------------------
    141147
    142 
    143 int fail( ifstream * is ) {
    144         return ferror( (FILE *)(is->file) );
     148// private
     149void ?{}( ifstream & is, void * file ) {
     150        is.file = file;
     151}
     152
     153// public
     154void ?{}( ifstream & is ) {}
     155
     156void ?{}( ifstream & is, const char * name ) {
     157        open( is, name );
     158}
     159
     160int fail( ifstream & is ) {
     161        return ferror( (FILE *)(is.file) );
    145162} // fail
    146163
    147 int eof( ifstream * is ) {
    148         return feof( (FILE *)(is->file) );
     164int eof( ifstream & is ) {
     165        return feof( (FILE *)(is.file) );
    149166} // eof
    150167
    151 void open( ifstream * is, const char * name, const char * mode ) {
    152         FILE *file = fopen( name, mode );
     168void open( ifstream & is, const char * name ) {
     169        FILE *file = fopen( name, "r" );
    153170        if ( file == 0 ) {                                                                      // do not change unless successful
    154171                fprintf( stderr, IO_MSG "open input file \"%s\", ", name );
     
    156173                exit( EXIT_FAILURE );
    157174        } // if
    158         is->file = file;
     175        is.file = file;
    159176} // open
    160177
    161 void close( ifstream * is ) {
    162         if ( (FILE *)(is->file) == stdin ) return;
    163 
    164         if ( fclose( (FILE *)(is->file) ) == EOF ) {
     178void close( ifstream & is ) {
     179        if ( (FILE *)(is.file) == stdin ) return;
     180
     181        if ( fclose( (FILE *)(is.file) ) == EOF ) {
    165182                perror( IO_MSG "close input" );
    166183        } // if
    167184} // close
    168185
    169 ifstream * read( ifstream * is, char * data, unsigned long int size ) {
     186ifstream & read( ifstream & is, char * data, unsigned long int size ) {
    170187        if ( fail( is ) ) {
    171188                fprintf( stderr, "attempt read I/O on failed stream\n" );
     
    173190        } // if
    174191
    175         if ( fread( data, size, 1, (FILE *)(is->file) ) == 0 ) {
     192        if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
    176193                perror( IO_MSG "read" );
    177194                exit( EXIT_FAILURE );
     
    180197} // read
    181198
    182 ifstream *ungetc( ifstream * is, char c ) {
     199ifstream &ungetc( ifstream & is, char c ) {
    183200        if ( fail( is ) ) {
    184201                fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
     
    186203        } // if
    187204
    188         if ( ungetc( c, (FILE *)(is->file) ) == EOF ) {
     205        if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
    189206                perror( IO_MSG "ungetc" );
    190207                exit( EXIT_FAILURE );
     
    193210} // ungetc
    194211
    195 int fmt( ifstream * is, const char format[], ... ) {
     212int fmt( ifstream & is, const char format[], ... ) {
    196213        va_list args;
    197214
    198215        va_start( args, format );
    199         int len = vfscanf( (FILE *)(is->file), format, args );
     216        int len = vfscanf( (FILE *)(is.file), format, args );
    200217        if ( len == EOF ) {
    201                 if ( ferror( (FILE *)(is->file) ) ) {
     218                if ( ferror( (FILE *)(is.file) ) ) {
    202219                        fprintf( stderr, "invalid read\n" );
    203220                        exit( EXIT_FAILURE );
     
    210227
    211228static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
    212 ifstream *sin = &sinFile;
     229ifstream & sin = sinFile;
    213230
    214231// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.