Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/fstream.cfa

    r52e311a rba0d2ea  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun Oct 10 11:23:05 2021
    13 // Update Count     : 512
     12// Last Modified On : Thu Jul 29 22:34:10 2021
     13// Update Count     : 454
    1414//
    1515
     
    2828#define IO_MSG "I/O error: "
    2929
    30 // private
    31 void ?{}( ofstream & os, void * file ) with( os ) {
    32         file$ = file;
    33         sepDefault$ = true;
    34         sepOnOff$ = false;
    35         nlOnOff$ = true;
    36         prt$ = false;
    37         sawNL$ = false;
     30void ?{}( 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;
    3838        sepSetCur$( os, sepGet( os ) );
    3939        sepSet( os, " " );
     
    4141} // ?{}
    4242
    43 inline bool sepPrt$( ofstream & os ) { setNL$( os, false ); return os.sepOnOff$; }
    44 inline void sepReset$( ofstream & os ) { os.sepOnOff$ = os.sepDefault$; }
    45 inline void sepReset$( ofstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; }
    46 inline const char * sepGetCur$( ofstream & os ) { return os.sepCur$; }
    47 inline void sepSetCur$( ofstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; }
    48 inline bool getNL$( ofstream & os ) { return os.sawNL$; }
    49 inline void setNL$( ofstream & os, bool state ) { os.sawNL$ = state; }
    50 inline bool getANL$( ofstream & os ) { return os.nlOnOff$; }
    51 inline bool getPrt$( ofstream & os ) { return os.prt$; }
    52 inline void setPrt$( ofstream & os, bool state ) { os.prt$ = state; }
    53 
    54 inline void lock( ofstream & os ) with( os ) {  lock( os.lock$ ); }
    55 inline void unlock( ofstream & os ) { unlock( os.lock$ ); }
     43// private
     44bool sepPrt$( ofstream & os ) { setNL$( os, false ); return os.sepOnOff$; }
     45void sepReset$( ofstream & os ) { os.sepOnOff$ = os.sepDefault$; }
     46void sepReset$( ofstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; }
     47const char * sepGetCur$( ofstream & os ) { return os.sepCur$; }
     48void sepSetCur$( ofstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; }
     49bool getNL$( ofstream & os ) { return os.sawNL$; }
     50void setNL$( ofstream & os, bool state ) { os.sawNL$ = state; }
     51bool getANL$( ofstream & os ) { return os.nlOnOff$; }
     52bool getPrt$( ofstream & os ) { return os.prt$; }
     53void setPrt$( ofstream & os, bool state ) { os.prt$ = state; }
    5654
    5755// public
    5856void ?{}( ofstream & os ) { os.file$ = 0p; }
    59 void ?{}( ofstream & os, const char name[], const char mode[] ) { open( os, name, mode ); }
    60 void ?{}( ofstream & os, const char name[] ) { open( os, name, "w" ); }
    61 void ^?{}( ofstream & os ) { close( os ); }
     57
     58void ?{}( ofstream & os, const char name[], const char mode[] ) {
     59        open( os, name, mode );
     60} // ?{}
     61
     62void ?{}( ofstream & os, const char name[] ) {
     63        open( os, name, "w" );
     64} // ?{}
     65
     66void ^?{}( ofstream & os ) {
     67        close( os );
     68} // ^?{}
    6269
    6370void sepOn( ofstream & os ) { os.sepOnOff$ = ! getNL$( os ); }
     
    100107        if ( &os == &exit ) exit( EXIT_FAILURE );
    101108        if ( &os == &abort ) abort();
     109        if ( os.acquired$ ) { os.acquired$ = false; release( os ); }
    102110} // ends
    103111
    104 bool fail( ofstream & os ) { return os.file$ == 0 || ferror( (FILE *)(os.file$) ); }
    105 void clear( ofstream & os ) { clearerr( (FILE *)(os.file$) ); }
    106 int flush( ofstream & os ) { return fflush( (FILE *)(os.file$) ); }
     112bool fail( ofstream & os ) {
     113        return os.file$ == 0 || ferror( (FILE *)(os.file$) );
     114} // fail
     115
     116void clear( ofstream & os ) {
     117        clearerr( (FILE *)(os.file$) );
     118} // clear
     119
     120int flush( ofstream & os ) {
     121        return fflush( (FILE *)(os.file$) );
     122} // flush
    107123
    108124void open( ofstream & os, const char name[], const char mode[] ) {
    109         FILE * file;
    110     for ( cnt; 10 ) {
    111                 errno = 0;
    112                 file = fopen( name, mode );
    113           if ( file != 0p || errno != EINTR ) break;            // timer interrupt ?
    114           if ( cnt == 9 ) abort( "ofstream open EINTR spinning exceeded" );
    115     } // for
     125        FILE * file = fopen( name, mode );
     126        #ifdef __CFA_DEBUG__
    116127        if ( file == 0p ) {
    117128                throw (Open_Failure){ os };
    118129                // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno );
    119130        } // if
    120         (os){ file };                                                                           // initialize
     131        #endif // __CFA_DEBUG__
     132        (os){ file };
    121133} // open
    122134
    123 void open( ofstream & os, const char name[] ) { open( os, name, "w" ); }
    124 
    125 void close( ofstream & os ) with( os ) {
    126   if ( (FILE *)(file$) == 0p ) return;
    127   if ( (FILE *)(file$) == (FILE *)stdout || (FILE *)(file$) == (FILE *)stderr ) return;
    128 
    129         int ret;
    130     for ( cnt; 10 ) {
    131                 errno = 0;
    132                 ret = fclose( (FILE *)(file$) );
    133           if ( ret != EOF || errno != EINTR ) break;            // timer interrupt ?
    134           if ( cnt == 9 ) abort( "ofstream open EINTR spinning exceeded" );
    135     } // for
    136         if ( ret == EOF ) {
     135void open( ofstream & os, const char name[] ) {
     136        open( os, name, "w" );
     137} // open
     138
     139void 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 ) {
    137144                throw (Close_Failure){ os };
    138145                // abort | IO_MSG "close output" | nl | strerror( errno );
    139146        } // if
    140         file$ = 0p;                                                                                     // safety after close
     147        os.file$ = 0p;
    141148} // close
    142149
     
    157164        va_list args;
    158165        va_start( args, format );
    159                
    160         int len;
    161     for ( cnt; 10 ) {
    162                 errno = 0;
    163                 len = vfprintf( (FILE *)(os.file$), format, args );
    164           if ( len != EOF || errno != EINTR ) break;            // timer interrupt ?
    165           if ( cnt == 9 ) abort( "ofstream fmt EINTR spinning exceeded" );
    166     } // for
     166        int len = vfprintf( (FILE *)(os.file$), format, args );
    167167        if ( len == EOF ) {
    168168                if ( ferror( (FILE *)(os.file$) ) ) {
     
    177177} // fmt
    178178
     179inline void acquire( ofstream & os ) {
     180        lock( os.lock$ );
     181        if ( ! os.acquired$ ) os.acquired$ = true;
     182        else unlock( os.lock$ );
     183} // acquire
     184
     185inline void release( ofstream & os ) {
     186        unlock( os.lock$ );
     187} // release
     188
     189void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.lock$ ); }
     190void ^?{}( osacquire & acq ) { release( acq.os ); }
     191
    179192static ofstream soutFile = { (FILE *)stdout };
    180193ofstream & sout = soutFile, & stdout = soutFile;
     
    194207        flush( os );
    195208        return os;
     209        // (ofstream &)(os | '\n');
     210        // setPrt$( os, false );                                                        // turn off
     211        // setNL$( os, true );
     212        // flush( os );
     213        // return sepOff( os );                                                 // prepare for next line
    196214} // nl
    197215
     
    201219
    202220// private
    203 void ?{}( ifstream & is, void * file ) with( is ) {
    204         file$ = file;
    205         nlOnOff$ = false;
    206 } // ?{}
    207 
    208 bool getANL$( ifstream & os ) { return os.nlOnOff$; }
    209 
    210 inline void lock( ifstream & os ) with( os ) { lock( os.lock$ ); }
    211 inline void unlock( ifstream & os ) { unlock( os.lock$ ); }
     221void ?{}( ifstream & is, void * file ) {
     222        is.file$ = file;
     223        is.nlOnOff$ = false;
     224        is.acquired$ = false;
     225} // ?{}
    212226
    213227// public
    214228void ?{}( ifstream & is ) { is.file$ = 0p; }
    215 void ?{}( ifstream & is, const char name[], const char mode[] ) { open( is, name, mode ); }
    216 void ?{}( ifstream & is, const char name[] ) { open( is, name, "r" ); }
    217 void ^?{}( ifstream & is ) { close( is ); }
    218 
    219 bool fail( ifstream & is ) { return is.file$ == 0p || ferror( (FILE *)(is.file$) ); }
    220 void clear( ifstream & is ) { clearerr( (FILE *)(is.file$) ); }
     229
     230void ?{}( ifstream & is, const char name[], const char mode[] ) {
     231        open( is, name, mode );
     232} // ?{}
     233
     234void ?{}( ifstream & is, const char name[] ) {
     235        open( is, name, "r" );
     236} // ?{}
     237
     238void ^?{}( ifstream & is ) {
     239        close( is );
     240} // ^?{}
    221241
    222242void nlOn( ifstream & os ) { os.nlOnOff$ = true; }
    223243void nlOff( ifstream & os ) { os.nlOnOff$ = false; }
    224 
    225 void ends( ifstream & is ) {}
    226 
    227 bool eof( ifstream & is ) { return feof( (FILE *)(is.file$) ) != 0; }
     244bool getANL( ifstream & os ) { return os.nlOnOff$; }
     245
     246bool fail( ifstream & is ) {
     247        return is.file$ == 0p || ferror( (FILE *)(is.file$) );
     248} // fail
     249
     250void clear( ifstream & is ) {
     251        clearerr( (FILE *)(is.file$) );
     252} // clear
     253
     254void ends( ifstream & is ) {
     255        if ( is.acquired$ ) { is.acquired$ = false; release( is ); }
     256} // ends
     257
     258bool eof( ifstream & is ) {
     259        return feof( (FILE *)(is.file$) );
     260} // eof
    228261
    229262void open( ifstream & is, const char name[], const char mode[] ) {
    230         FILE * file;
    231     for ( cnt; 10 ) {
    232                 errno = 0;
    233                 file = fopen( name, mode );
    234           if ( file != 0p || errno != EINTR ) break;            // timer interrupt ?
    235           if ( cnt == 9 ) abort( "ifstream open EINTR spinning exceeded" );
    236     } // for
     263        FILE * file = fopen( name, mode );
     264        #ifdef __CFA_DEBUG__
    237265        if ( file == 0p ) {
    238266                throw (Open_Failure){ is };
    239267                // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
    240268        } // if
    241         (is){ file };                                                                           // initialize
     269        #endif // __CFA_DEBUG__
     270        is.file$ = file;
    242271} // open
    243272
    244 void open( ifstream & is, const char name[] ) { open( is, name, "r" ); }
    245 
    246 void close( ifstream & is ) with( is ) {
    247   if ( (FILE *)(file$) == 0p ) return;
    248   if ( (FILE *)(file$) == (FILE *)stdin ) return;
    249 
    250         int ret;
    251     for ( cnt; 10 ) {
    252                 errno = 0;
    253                 ret = fclose( (FILE *)(file$) );
    254           if ( ret != EOF || errno != EINTR ) break;            // timer interrupt ?
    255           if ( cnt == 9 ) abort( "ifstream close EINTR spinning exceeded" );
    256     } // for
    257         if ( ret == EOF ) {
     273void open( ifstream & is, const char name[] ) {
     274        open( is, name, "r" );
     275} // open
     276
     277void 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 ) {
    258282                throw (Close_Failure){ is };
    259283                // abort | IO_MSG "close input" | nl | strerror( errno );
    260284        } // if
    261         file$ = 0p;                                                                                     // safety after close
     285        is.file$ = 0p;
    262286} // close
    263287
     
    288312int fmt( ifstream & is, const char format[], ... ) {
    289313        va_list args;
     314
    290315        va_start( args, format );
    291 
    292         int len;
    293     for () {                                                                                    // no check for EINTR limit waiting for keyboard input
    294                 errno = 0;
    295                 len = vfscanf( (FILE *)(is.file$), format, args );
    296           if ( len != EOF || errno != EINTR ) break;            // timer interrupt ?
    297     } // for
     316        int len = vfscanf( (FILE *)(is.file$), format, args );
    298317        if ( len == EOF ) {
    299318                if ( ferror( (FILE *)(is.file$) ) ) {
     
    305324} // fmt
    306325
     326inline void acquire( ifstream & is ) {
     327        lock( is.lock$ );
     328        if ( ! is.acquired$ ) is.acquired$ = true;
     329        else unlock( is.lock$ );
     330} // acquire
     331
     332inline void release( ifstream & is ) {
     333        unlock( is.lock$ );
     334} // release
     335
     336void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.lock$ ); }
     337void ^?{}( isacquire & acq ) { release( acq.is ); }
     338
    307339static ifstream sinFile = { (FILE *)stdin };
    308340ifstream & sin = sinFile, & stdin = sinFile;
     
    315347
    316348// exception I/O constructors
    317 void ?{}( Open_Failure & ex, ofstream & ostream ) with(ex) {
    318         virtual_table = &Open_Failure_vt;
    319         ostream = &ostream;
    320         tag = 1;
    321 } // ?{}
    322 
    323 void ?{}( Open_Failure & ex, ifstream & istream ) with(ex) {
    324         virtual_table = &Open_Failure_vt;
    325         istream = &istream;
    326         tag = 0;
     349void ?{}( Open_Failure & this, ofstream & ostream ) {
     350        this.virtual_table = &Open_Failure_vt;
     351        this.ostream = &ostream;
     352        this.tag = 1;
     353} // ?{}
     354
     355void ?{}( Open_Failure & this, ifstream & istream ) {
     356        this.virtual_table = &Open_Failure_vt;
     357        this.istream = &istream;
     358        this.tag = 0;
    327359} // ?{}
    328360
     
    331363
    332364// exception I/O constructors
    333 void ?{}( Close_Failure & ex, ofstream & ostream ) with(ex) {
    334         virtual_table = &Close_Failure_vt;
    335         ostream = &ostream;
    336         tag = 1;
    337 } // ?{}
    338 
    339 void ?{}( Close_Failure & ex, ifstream & istream ) with(ex) {
    340         virtual_table = &Close_Failure_vt;
    341         istream = &istream;
    342         tag = 0;
     365void ?{}( Close_Failure & this, ofstream & ostream ) {
     366        this.virtual_table = &Close_Failure_vt;
     367        this.ostream = &ostream;
     368        this.tag = 1;
     369} // ?{}
     370
     371void ?{}( Close_Failure & this, ifstream & istream ) {
     372        this.virtual_table = &Close_Failure_vt;
     373        this.istream = &istream;
     374        this.tag = 0;
    343375} // ?{}
    344376
     
    347379
    348380// exception I/O constructors
    349 void ?{}( Write_Failure & ex, ofstream & ostream ) with(ex) {
    350         virtual_table = &Write_Failure_vt;
    351         ostream = &ostream;
    352         tag = 1;
    353 } // ?{}
    354 
    355 void ?{}( Write_Failure & ex, ifstream & istream ) with(ex) {
    356         virtual_table = &Write_Failure_vt;
    357         istream = &istream;
    358         tag = 0;
     381void ?{}( Write_Failure & this, ofstream & ostream ) {
     382        this.virtual_table = &Write_Failure_vt;
     383        this.ostream = &ostream;
     384        this.tag = 1;
     385} // ?{}
     386
     387void ?{}( Write_Failure & this, ifstream & istream ) {
     388        this.virtual_table = &Write_Failure_vt;
     389        this.istream = &istream;
     390        this.tag = 0;
    359391} // ?{}
    360392
     
    363395
    364396// exception I/O constructors
    365 void ?{}( Read_Failure & ex, ofstream & ostream ) with(ex) {
    366         virtual_table = &Read_Failure_vt;
    367         ostream = &ostream;
    368         tag = 1;
    369 } // ?{}
    370 
    371 void ?{}( Read_Failure & ex, ifstream & istream ) with(ex) {
    372         virtual_table = &Read_Failure_vt;
    373         istream = &istream;
    374         tag = 0;
     397void ?{}( Read_Failure & this, ofstream & ostream ) {
     398        this.virtual_table = &Read_Failure_vt;
     399        this.ostream = &ostream;
     400        this.tag = 1;
     401} // ?{}
     402
     403void ?{}( Read_Failure & this, ifstream & istream ) {
     404        this.virtual_table = &Read_Failure_vt;
     405        this.istream = &istream;
     406        this.tag = 0;
    375407} // ?{}
    376408
Note: See TracChangeset for help on using the changeset viewer.