Changeset 3bf3b6b


Ignore:
Timestamp:
Sep 21, 2021, 9:59:03 PM (3 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, master, pthread-emulation, qualifiedEnum
Children:
cf78319
Parents:
6cc87c0
Message:

clean code, add fix that might deal with the I/O acquire timeout

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/fstream.cfa

    r6cc87c0 r3bf3b6b  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jul 29 22:34:10 2021
    13 // Update Count     : 454
     12// Last Modified On : Tue Sep 21 21:51:38 2021
     13// Update Count     : 460
    1414//
    1515
     
    2828#define IO_MSG "I/O error: "
    2929
    30 void ?{}( ofstream & os, void * file ) {
    31         os.file$ = file;
    32         os.sepDefault$ = true;
    33         os.sepOnOff$ = false;
    34         os.nlOnOff$ = true;
    35         os.prt$ = false;
    36         os.sawNL$ = false;
    37         os.acquired$ = false;
     30void ?{}( ofstream & os, void * file ) with(os) {
     31        file$ = file;
     32        sepDefault$ = true;
     33        sepOnOff$ = false;
     34        nlOnOff$ = true;
     35        prt$ = false;
     36        sawNL$ = false;
     37        acquired$ = false;
    3838        sepSetCur$( os, sepGet( os ) );
    3939        sepSet( os, " " );
     
    124124void open( ofstream & os, const char name[], const char mode[] ) {
    125125        FILE * file = fopen( name, mode );
    126         // #ifdef __CFA_DEBUG__
    127126        if ( file == 0p ) {
    128127                throw (Open_Failure){ os };
    129128                // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno );
    130129        } // if
    131         // #endif // __CFA_DEBUG__
    132         (os){ file };
     130        (os){ file };                                                                           // initialize
    133131} // open
    134132
     
    137135} // open
    138136
    139 void close( ofstream & os ) {
    140   if ( (FILE *)(os.file$) == 0p ) return;
    141   if ( (FILE *)(os.file$) == (FILE *)stdout || (FILE *)(os.file$) == (FILE *)stderr ) return;
    142 
    143         if ( fclose( (FILE *)(os.file$) ) == EOF ) {
     137void close( ofstream & os ) with(os) {
     138  if ( (FILE *)(file$) == 0p ) return;
     139  if ( (FILE *)(file$) == (FILE *)stdout || (FILE *)(file$) == (FILE *)stderr ) return;
     140
     141        if ( fclose( (FILE *)(file$) ) == EOF ) {
    144142                throw (Close_Failure){ os };
    145143                // abort | IO_MSG "close output" | nl | strerror( errno );
    146144        } // if
    147         os.file$ = 0p;
     145        file$ = 0p;
    148146} // close
    149147
     
    177175} // fmt
    178176
    179 inline void acquire( ofstream & os ) {
    180         lock( os.lock$ );
    181         if ( ! os.acquired$ ) os.acquired$ = true;
    182         else unlock( os.lock$ );
     177inline void acquire( ofstream & os ) with(os) {
     178        lock( lock$ );                                                                          // may increase recursive lock
     179        if ( ! acquired$ ) acquired$ = true;                            // not locked ?
     180        else unlock( lock$ );                                                           // unwind recursive lock at start
    183181} // acquire
    184182
     
    187185} // release
    188186
    189 void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.lock$ ); }
     187void ?{}( osacquire & acq, ofstream & os ) { lock( os.lock$ ); &acq.os = &os; }
    190188void ^?{}( osacquire & acq ) { release( acq.os ); }
    191189
     
    219217
    220218// private
    221 void ?{}( ifstream & is, void * file ) {
    222         is.file$ = file;
    223         is.nlOnOff$ = false;
    224         is.acquired$ = false;
     219void ?{}( ifstream & is, void * file ) with(is) {
     220        file$ = file;
     221        nlOnOff$ = false;
     222        acquired$ = false;
    225223} // ?{}
    226224
     
    262260void open( ifstream & is, const char name[], const char mode[] ) {
    263261        FILE * file = fopen( name, mode );
    264         // #ifdef __CFA_DEBUG__
    265262        if ( file == 0p ) {
    266263                throw (Open_Failure){ is };
    267264                // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
    268265        } // if
    269         // #endif // __CFA_DEBUG__
    270         is.file$ = file;
     266        (is){ file };                                                                           // initialize
    271267} // open
    272268
     
    275271} // open
    276272
    277 void close( ifstream & is ) {
    278   if ( (FILE *)(is.file$) == 0p ) return;
    279   if ( (FILE *)(is.file$) == (FILE *)stdin ) return;
    280 
    281         if ( fclose( (FILE *)(is.file$) ) == EOF ) {
     273void close( ifstream & is ) with(is) {
     274  if ( (FILE *)(file$) == 0p ) return;
     275  if ( (FILE *)(file$) == (FILE *)stdin ) return;
     276
     277        if ( fclose( (FILE *)(file$) ) == EOF ) {
    282278                throw (Close_Failure){ is };
    283279                // abort | IO_MSG "close input" | nl | strerror( errno );
    284280        } // if
    285         is.file$ = 0p;
     281        file$ = 0p;
    286282} // close
    287283
     
    324320} // fmt
    325321
    326 inline void acquire( ifstream & is ) {
    327         lock( is.lock$ );
    328         if ( ! is.acquired$ ) is.acquired$ = true;
    329         else unlock( is.lock$ );
     322inline void acquire( ifstream & is ) with(is) {
     323        lock( lock$ );                                                                          // may increase recursive lock
     324        if ( ! acquired$ ) acquired$ = true;                            // not locked ?
     325        else unlock( lock$ );                                                           // unwind recursive lock at start
    330326} // acquire
    331327
     
    334330} // release
    335331
    336 void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.lock$ ); }
     332void ?{}( isacquire & acq, ifstream & is ) { lock( is.lock$ ); &acq.is = &is; }
    337333void ^?{}( isacquire & acq ) { release( acq.is ); }
    338334
     
    347343
    348344// exception I/O constructors
    349 void ?{}( Open_Failure & this, ofstream & ostream ) {
    350         this.virtual_table = &Open_Failure_vt;
    351         this.ostream = &ostream;
    352         this.tag = 1;
    353 } // ?{}
    354 
    355 void ?{}( Open_Failure & this, ifstream & istream ) {
    356         this.virtual_table = &Open_Failure_vt;
    357         this.istream = &istream;
    358         this.tag = 0;
     345void ?{}( Open_Failure & ex, ofstream & ostream ) with(ex) {
     346        virtual_table = &Open_Failure_vt;
     347        ostream = &ostream;
     348        tag = 1;
     349} // ?{}
     350
     351void ?{}( Open_Failure & ex, ifstream & istream ) with(ex) {
     352        virtual_table = &Open_Failure_vt;
     353        istream = &istream;
     354        tag = 0;
    359355} // ?{}
    360356
     
    363359
    364360// exception I/O constructors
    365 void ?{}( Close_Failure & this, ofstream & ostream ) {
    366         this.virtual_table = &Close_Failure_vt;
    367         this.ostream = &ostream;
    368         this.tag = 1;
    369 } // ?{}
    370 
    371 void ?{}( Close_Failure & this, ifstream & istream ) {
    372         this.virtual_table = &Close_Failure_vt;
    373         this.istream = &istream;
    374         this.tag = 0;
     361void ?{}( Close_Failure & ex, ofstream & ostream ) with(ex) {
     362        virtual_table = &Close_Failure_vt;
     363        ostream = &ostream;
     364        tag = 1;
     365} // ?{}
     366
     367void ?{}( Close_Failure & ex, ifstream & istream ) with(ex) {
     368        virtual_table = &Close_Failure_vt;
     369        istream = &istream;
     370        tag = 0;
    375371} // ?{}
    376372
     
    379375
    380376// exception I/O constructors
    381 void ?{}( Write_Failure & this, ofstream & ostream ) {
    382         this.virtual_table = &Write_Failure_vt;
    383         this.ostream = &ostream;
    384         this.tag = 1;
    385 } // ?{}
    386 
    387 void ?{}( Write_Failure & this, ifstream & istream ) {
    388         this.virtual_table = &Write_Failure_vt;
    389         this.istream = &istream;
    390         this.tag = 0;
     377void ?{}( Write_Failure & ex, ofstream & ostream ) with(ex) {
     378        virtual_table = &Write_Failure_vt;
     379        ostream = &ostream;
     380        tag = 1;
     381} // ?{}
     382
     383void ?{}( Write_Failure & ex, ifstream & istream ) with(ex) {
     384        virtual_table = &Write_Failure_vt;
     385        istream = &istream;
     386        tag = 0;
    391387} // ?{}
    392388
     
    395391
    396392// exception I/O constructors
    397 void ?{}( Read_Failure & this, ofstream & ostream ) {
    398         this.virtual_table = &Read_Failure_vt;
    399         this.ostream = &ostream;
    400         this.tag = 1;
    401 } // ?{}
    402 
    403 void ?{}( Read_Failure & this, ifstream & istream ) {
    404         this.virtual_table = &Read_Failure_vt;
    405         this.istream = &istream;
    406         this.tag = 0;
     393void ?{}( Read_Failure & ex, ofstream & ostream ) with(ex) {
     394        virtual_table = &Read_Failure_vt;
     395        ostream = &ostream;
     396        tag = 1;
     397} // ?{}
     398
     399void ?{}( Read_Failure & ex, ifstream & istream ) with(ex) {
     400        virtual_table = &Read_Failure_vt;
     401        istream = &istream;
     402        tag = 0;
    407403} // ?{}
    408404
Note: See TracChangeset for help on using the changeset viewer.