Ignore:
Timestamp:
Jan 7, 2021, 2:55:57 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
58fe85a
Parents:
bdfc032 (diff), 44e37ef (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into dkobets-vector

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/fstream.cfa

    rbdfc032 reef8dfb  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Nov 29 06:56:46 2019
    13 // Update Count     : 355
     12// Last Modified On : Fri Jun 19 16:24:54 2020
     13// Update Count     : 384
    1414//
    1515
     
    2626
    2727
    28 //*********************************** ofstream ***********************************
     28// *********************************** ofstream ***********************************
    2929
    3030
     
    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;
     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 ) );
    4041        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 = 0; }
    59 
    60 void ?{}( ofstream & os, const char * name, const char * mode ) {
     58void ?{}( ofstream & os ) { os.$file = 0p; }
     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} // ?{}
     
    7070} // ^?{}
    7171
    72 void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); }
    73 void sepOff( ofstream & os ) { os.sepOnOff = false; }
     72void sepOn( ofstream & os ) { os.$sepOnOff = ! $getNL( os ); }
     73void sepOff( ofstream & os ) { os.$sepOnOff = false; }
    7474
    7575bool sepDisable( ofstream & os ) {
    76         bool temp = os.sepDefault;
    77         os.sepDefault = false;
    78         sepReset( os );
     76        bool temp = os.$sepDefault;
     77        os.$sepDefault = false;
     78        $sepReset( os );
    7979        return temp;
    8080} // sepDisable
    8181
    8282bool sepEnable( ofstream & os ) {
    83         bool temp = os.sepDefault;
    84         os.sepDefault = true;
    85         if ( os.sepOnOff ) sepReset( os );                                      // start of line ?
     83        bool temp = os.$sepDefault;
     84        os.$sepDefault = true;
     85        if ( os.$sepOnOff ) $sepReset( os );                            // start of line ?
    8686        return temp;
    8787} // sepEnable
    8888
    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 ) {
     89void nlOn( ofstream & os ) { os.$nlOnOff = true; }
     90void nlOff( ofstream & os ) { os.$nlOnOff = false; }
     91
     92const char * sepGet( ofstream & os ) { return os.$separator; }
     93void sepSet( ofstream & os, const char s[] ) {
    9494        assert( s );
    95         strncpy( os.separator, s, sepSize - 1 );
    96         os.separator[sepSize - 1] = '\0';
     95        strncpy( os.$separator, s, sepSize - 1 );
     96        os.$separator[sepSize - 1] = '\0';
    9797} // sepSet
    9898
    99 const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator; }
    100 void sepSetTuple( ofstream & os, const char * s ) {
     99const char * sepGetTuple( ofstream & os ) { return os.$tupleSeparator; }
     100void sepSetTuple( ofstream & os, const char s[] ) {
    101101        assert( s );
    102         strncpy( os.tupleSeparator, s, sepSize - 1 );
    103         os.tupleSeparator[sepSize - 1] = '\0';
     102        strncpy( os.$tupleSeparator, s, sepSize - 1 );
     103        os.$tupleSeparator[sepSize - 1] = '\0';
    104104} // sepSet
    105105
    106106void ends( ofstream & os ) {
    107         if ( getANL( os ) ) nl( os );
    108         else setPrt( os, false );                                                       // turn off
     107        if ( $getANL( os ) ) nl( os );
     108        else $setPrt( os, false );                                                      // turn off
    109109        if ( &os == &exit ) exit( EXIT_FAILURE );
    110110        if ( &os == &abort ) abort();
     
    112112
    113113int fail( ofstream & os ) {
    114         return os.file == 0 || ferror( (FILE *)(os.file) );
     114        return os.$file == 0 || ferror( (FILE *)(os.$file) );
    115115} // fail
    116116
    117117int flush( ofstream & os ) {
    118         return fflush( (FILE *)(os.file) );
     118        return fflush( (FILE *)(os.$file) );
    119119} // flush
    120120
    121 void open( ofstream & os, const char * name, const char * mode ) {
     121void open( ofstream & os, const char name[], const char mode[] ) {
    122122        FILE * file = fopen( name, mode );
    123123        #ifdef __CFA_DEBUG__
    124         if ( file == 0 ) {
    125                 abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno );
     124        if ( file == 0p ) {
     125                throw (Open_Failure){ os };
     126                // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno );
    126127        } // if
    127128        #endif // __CFA_DEBUG__
     
    129130} // open
    130131
    131 void open( ofstream & os, const char * name ) {
     132void open( ofstream & os, const char name[] ) {
    132133        open( os, name, "w" );
    133134} // open
    134135
    135136void close( ofstream & os ) {
    136         if ( (FILE *)(os.file) == stdout || (FILE *)(os.file) == stderr ) return;
    137 
    138         if ( fclose( (FILE *)(os.file) ) == EOF ) {
     137  if ( (FILE *)(os.$file) == 0p ) return;
     138  if ( (FILE *)(os.$file) == (FILE *)stdout || (FILE *)(os.$file) == (FILE *)stderr ) return;
     139
     140        if ( fclose( (FILE *)(os.$file) ) == EOF ) {
    139141                abort | IO_MSG "close output" | nl | strerror( errno );
    140142        } // if
     143        os.$file = 0p;
    141144} // close
    142145
    143 ofstream & write( ofstream & os, const char * data, size_t size ) {
     146ofstream & write( ofstream & os, const char data[], size_t size ) {
    144147        if ( fail( os ) ) {
    145148                abort | IO_MSG "attempt write I/O on failed stream";
    146149        } // if
    147150
    148         if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) {
     151        if ( fwrite( data, 1, size, (FILE *)(os.$file) ) != size ) {
    149152                abort | IO_MSG "write" | nl | strerror( errno );
    150153        } // if
     
    155158        va_list args;
    156159        va_start( args, format );
    157         int len = vfprintf( (FILE *)(os.file), format, args );
     160        int len = vfprintf( (FILE *)(os.$file), format, args );
    158161        if ( len == EOF ) {
    159                 if ( ferror( (FILE *)(os.file) ) ) {
     162                if ( ferror( (FILE *)(os.$file) ) ) {
    160163                        abort | IO_MSG "invalid write";
    161164                } // if
     
    163166        va_end( args );
    164167
    165         setPrt( os, true );                                                                     // called in output cascade
    166         sepReset( os );                                                                         // reset separator
     168        $setPrt( os, true );                                                            // called in output cascade
     169        $sepReset( os );                                                                        // reset separator
    167170        return len;
    168171} // fmt
     
    179182
    180183
    181 //*********************************** ifstream ***********************************
     184// *********************************** ifstream ***********************************
    182185
    183186
    184187// private
    185188void ?{}( ifstream & is, void * file ) {
    186         is.file = file;
    187         is.nlOnOff = false;
     189        is.$file = file;
     190        is.$nlOnOff = false;
    188191} // ?{}
    189192
    190193// public
    191 void ?{}( ifstream & is ) {     is.file = 0; }
    192 
    193 void ?{}( ifstream & is, const char * name, const char * mode ) {
     194void ?{}( ifstream & is ) { is.$file = 0p; }
     195
     196void ?{}( ifstream & is, const char name[], const char mode[] ) {
    194197        open( is, name, mode );
    195198} // ?{}
    196199
    197 void ?{}( ifstream & is, const char * name ) {
     200void ?{}( ifstream & is, const char name[] ) {
    198201        open( is, name, "r" );
    199202} // ?{}
     
    203206} // ^?{}
    204207
    205 void nlOn( ifstream & os ) { os.nlOnOff = true; }
    206 void nlOff( ifstream & os ) { os.nlOnOff = false; }
    207 bool getANL( ifstream & os ) { return os.nlOnOff; }
     208void nlOn( ifstream & os ) { os.$nlOnOff = true; }
     209void nlOff( ifstream & os ) { os.$nlOnOff = false; }
     210bool getANL( ifstream & os ) { return os.$nlOnOff; }
    208211
    209212int fail( ifstream & is ) {
    210         return is.file == 0 || ferror( (FILE *)(is.file) );
     213        return is.$file == 0p || ferror( (FILE *)(is.$file) );
    211214} // fail
    212215
    213216int eof( ifstream & is ) {
    214         return feof( (FILE *)(is.file) );
     217        return feof( (FILE *)(is.$file) );
    215218} // eof
    216219
    217 void open( ifstream & is, const char * name, const char * mode ) {
     220void open( ifstream & is, const char name[], const char mode[] ) {
    218221        FILE * file = fopen( name, mode );
    219222        #ifdef __CFA_DEBUG__
    220         if ( file == 0 ) {
    221                 abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
     223        if ( file == 0p ) {
     224                throw (Open_Failure){ is };
     225                // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
    222226        } // if
    223227        #endif // __CFA_DEBUG__
    224         is.file = file;
     228        is.$file = file;
    225229} // open
    226230
    227 void open( ifstream & is, const char * name ) {
     231void open( ifstream & is, const char name[] ) {
    228232        open( is, name, "r" );
    229233} // open
    230234
    231235void close( ifstream & is ) {
    232         if ( (FILE *)(is.file) == stdin ) return;
    233 
    234         if ( fclose( (FILE *)(is.file) ) == EOF ) {
     236  if ( (FILE *)(is.$file) == 0p ) return;
     237  if ( (FILE *)(is.$file) == (FILE *)stdin ) return;
     238
     239        if ( fclose( (FILE *)(is.$file) ) == EOF ) {
    235240                abort | IO_MSG "close input" | nl | strerror( errno );
    236241        } // if
     242        is.$file = 0p;
    237243} // close
    238244
     
    242248        } // if
    243249
    244         if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
     250        if ( fread( data, size, 1, (FILE *)(is.$file) ) == 0 ) {
    245251                abort | IO_MSG "read" | nl | strerror( errno );
    246252        } // if
     
    253259        } // if
    254260
    255         if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
     261        if ( ungetc( c, (FILE *)(is.$file) ) == EOF ) {
    256262                abort | IO_MSG "ungetc" | nl | strerror( errno );
    257263        } // if
     
    263269
    264270        va_start( args, format );
    265         int len = vfscanf( (FILE *)(is.file), format, args );
     271        int len = vfscanf( (FILE *)(is.$file), format, args );
    266272        if ( len == EOF ) {
    267                 if ( ferror( (FILE *)(is.file) ) ) {
     273                if ( ferror( (FILE *)(is.$file) ) ) {
    268274                        abort | IO_MSG "invalid read";
    269275                } // if
     
    276282ifstream & sin = sinFile, & stdin = sinFile;
    277283
     284
     285// *********************************** exceptions ***********************************
     286
     287
     288void ?{}( Open_Failure & this, ofstream & ostream ) {
     289        VTABLE_INIT(this, Open_Failure);
     290        this.ostream = &ostream;
     291        this.tag = 1;
     292}
     293void ?{}( Open_Failure & this, ifstream & istream ) {
     294        VTABLE_INIT(this, Open_Failure);
     295        this.istream = &istream;
     296        this.tag = 0;
     297}
     298const char * Open_Failure_msg(Open_Failure * this) {
     299        return "Open_Failure";
     300}
     301VTABLE_INSTANCE(Open_Failure)(Open_Failure_msg);
     302void throwOpen_Failure( ofstream & ostream ) {
     303        Open_Failure exc = { ostream };
     304}
     305void throwOpen_Failure( ifstream & istream ) {
     306        Open_Failure exc = { istream };
     307}
     308
    278309// Local Variables: //
    279310// tab-width: 4 //
Note: See TracChangeset for help on using the changeset viewer.