Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/fstream.cfa

    r7e7a076 rc8371b5  
    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 : Sun Oct 10 11:23:05 2021
     13// Update Count     : 512
    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;
     30// private
     31void ?{}( ofstream & os, void * file ) with( os ) {
     32        file$ = file;
     33        sepDefault$ = true;
     34        sepOnOff$ = false;
     35        nlOnOff$ = true;
     36        prt$ = false;
     37        sawNL$ = false;
    3838        sepSetCur$( os, sepGet( os ) );
    3939        sepSet( os, " " );
     
    4141} // ?{}
    4242
    43 // private
    44 bool sepPrt$( ofstream & os ) { setNL$( os, false ); return os.sepOnOff$; }
    45 void sepReset$( ofstream & os ) { os.sepOnOff$ = os.sepDefault$; }
    46 void sepReset$( ofstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; }
    47 const char * sepGetCur$( ofstream & os ) { return os.sepCur$; }
    48 void sepSetCur$( ofstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; }
    49 bool getNL$( ofstream & os ) { return os.sawNL$; }
    50 void setNL$( ofstream & os, bool state ) { os.sawNL$ = state; }
    51 bool getANL$( ofstream & os ) { return os.nlOnOff$; }
    52 bool getPrt$( ofstream & os ) { return os.prt$; }
    53 void setPrt$( ofstream & os, bool state ) { os.prt$ = state; }
     43inline bool sepPrt$( ofstream & os ) { setNL$( os, false ); return os.sepOnOff$; }
     44inline void sepReset$( ofstream & os ) { os.sepOnOff$ = os.sepDefault$; }
     45inline void sepReset$( ofstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; }
     46inline const char * sepGetCur$( ofstream & os ) { return os.sepCur$; }
     47inline void sepSetCur$( ofstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; }
     48inline bool getNL$( ofstream & os ) { return os.sawNL$; }
     49inline void setNL$( ofstream & os, bool state ) { os.sawNL$ = state; }
     50inline bool getANL$( ofstream & os ) { return os.nlOnOff$; }
     51inline bool getPrt$( ofstream & os ) { return os.prt$; }
     52inline void setPrt$( ofstream & os, bool state ) { os.prt$ = state; }
     53
     54inline void lock( ofstream & os ) with( os ) {  lock( os.lock$ ); }
     55inline void unlock( ofstream & os ) { unlock( os.lock$ ); }
    5456
    5557// public
    5658void ?{}( ofstream & os ) { os.file$ = 0p; }
    57 
    58 void ?{}( ofstream & os, const char name[], const char mode[] ) {
    59         open( os, name, mode );
    60 } // ?{}
    61 
    62 void ?{}( ofstream & os, const char name[] ) {
    63         open( os, name, "w" );
    64 } // ?{}
    65 
    66 void ^?{}( ofstream & os ) {
    67         close( os );
    68 } // ^?{}
     59void ?{}( ofstream & os, const char name[], const char mode[] ) { open( os, name, mode ); }
     60void ?{}( ofstream & os, const char name[] ) { open( os, name, "w" ); }
     61void ^?{}( ofstream & os ) { close( os ); }
    6962
    7063void sepOn( ofstream & os ) { os.sepOnOff$ = ! getNL$( os ); }
     
    107100        if ( &os == &exit ) exit( EXIT_FAILURE );
    108101        if ( &os == &abort ) abort();
    109         if ( os.acquired$ ) { os.acquired$ = false; release( os ); }
    110102} // ends
    111103
    112 bool fail( ofstream & os ) {
    113         return os.file$ == 0 || ferror( (FILE *)(os.file$) );
    114 } // fail
    115 
    116 void clear( ofstream & os ) {
    117         clearerr( (FILE *)(os.file$) );
    118 } // clear
    119 
    120 int flush( ofstream & os ) {
    121         return fflush( (FILE *)(os.file$) );
    122 } // flush
     104bool fail( ofstream & os ) { return os.file$ == 0 || ferror( (FILE *)(os.file$) ); }
     105void clear( ofstream & os ) { clearerr( (FILE *)(os.file$) ); }
     106int flush( ofstream & os ) { return fflush( (FILE *)(os.file$) ); }
    123107
    124108void open( ofstream & os, const char name[], const char mode[] ) {
    125         FILE * file = fopen( name, mode );
    126         // #ifdef __CFA_DEBUG__
     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
    127116        if ( file == 0p ) {
    128117                throw (Open_Failure){ os };
    129118                // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno );
    130119        } // if
    131         // #endif // __CFA_DEBUG__
    132         (os){ file };
     120        (os){ file };                                                                           // initialize
    133121} // open
    134122
    135 void open( ofstream & os, const char name[] ) {
    136         open( os, name, "w" );
    137 } // open
    138 
    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 ) {
     123void open( ofstream & os, const char name[] ) { open( os, name, "w" ); }
     124
     125void 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 ) {
    144137                throw (Close_Failure){ os };
    145138                // abort | IO_MSG "close output" | nl | strerror( errno );
    146139        } // if
    147         os.file$ = 0p;
     140        file$ = 0p;                                                                                     // safety after close
    148141} // close
    149142
     
    164157        va_list args;
    165158        va_start( args, format );
    166         int len = vfprintf( (FILE *)(os.file$), format, args );
     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
    167167        if ( len == EOF ) {
    168168                if ( ferror( (FILE *)(os.file$) ) ) {
     
    177177} // fmt
    178178
    179 inline void acquire( ofstream & os ) {
    180         lock( os.lock$ );
    181         if ( ! os.acquired$ ) os.acquired$ = true;
    182         else unlock( os.lock$ );
    183 } // acquire
    184 
    185 inline void release( ofstream & os ) {
    186         unlock( os.lock$ );
    187 } // release
    188 
    189 inline void lock( ofstream & os ) { acquire( os ); }
    190 inline void unlock( ofstream & os ) { release( os ); }
    191 
    192 void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.lock$ ); }
    193 void ^?{}( osacquire & acq ) { release( acq.os ); }
    194 
    195179static ofstream soutFile = { (FILE *)stdout };
    196180ofstream & sout = soutFile, & stdout = soutFile;
     
    210194        flush( os );
    211195        return os;
    212         // (ofstream &)(os | '\n');
    213         // setPrt$( os, false );                                                        // turn off
    214         // setNL$( os, true );
    215         // flush( os );
    216         // return sepOff( os );                                                 // prepare for next line
    217196} // nl
    218197
     
    222201
    223202// private
    224 void ?{}( ifstream & is, void * file ) {
    225         is.file$ = file;
    226         is.nlOnOff$ = false;
    227         is.acquired$ = false;
    228 } // ?{}
     203void ?{}( ifstream & is, void * file ) with( is ) {
     204        file$ = file;
     205        nlOnOff$ = false;
     206} // ?{}
     207
     208bool getANL$( ifstream & os ) { return os.nlOnOff$; }
     209
     210inline void lock( ifstream & os ) with( os ) { lock( os.lock$ ); }
     211inline void unlock( ifstream & os ) { unlock( os.lock$ ); }
    229212
    230213// public
    231214void ?{}( ifstream & is ) { is.file$ = 0p; }
    232 
    233 void ?{}( ifstream & is, const char name[], const char mode[] ) {
    234         open( is, name, mode );
    235 } // ?{}
    236 
    237 void ?{}( ifstream & is, const char name[] ) {
    238         open( is, name, "r" );
    239 } // ?{}
    240 
    241 void ^?{}( ifstream & is ) {
    242         close( is );
    243 } // ^?{}
     215void ?{}( ifstream & is, const char name[], const char mode[] ) { open( is, name, mode ); }
     216void ?{}( ifstream & is, const char name[] ) { open( is, name, "r" ); }
     217void ^?{}( ifstream & is ) { close( is ); }
     218
     219bool fail( ifstream & is ) { return is.file$ == 0p || ferror( (FILE *)(is.file$) ); }
     220void clear( ifstream & is ) { clearerr( (FILE *)(is.file$) ); }
    244221
    245222void nlOn( ifstream & os ) { os.nlOnOff$ = true; }
    246223void nlOff( ifstream & os ) { os.nlOnOff$ = false; }
    247 bool getANL( ifstream & os ) { return os.nlOnOff$; }
    248 
    249 bool fail( ifstream & is ) {
    250         return is.file$ == 0p || ferror( (FILE *)(is.file$) );
    251 } // fail
    252 
    253 void clear( ifstream & is ) {
    254         clearerr( (FILE *)(is.file$) );
    255 } // clear
    256 
    257 void ends( ifstream & is ) {
    258         if ( is.acquired$ ) { is.acquired$ = false; release( is ); }
    259 } // ends
    260 
    261 bool eof( ifstream & is ) {
    262         return feof( (FILE *)(is.file$) );
    263 } // eof
     224
     225void ends( ifstream & is ) {}
     226
     227bool eof( ifstream & is ) { return feof( (FILE *)(is.file$) ) != 0; }
    264228
    265229void open( ifstream & is, const char name[], const char mode[] ) {
    266         FILE * file = fopen( name, mode );
    267         // #ifdef __CFA_DEBUG__
     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
    268237        if ( file == 0p ) {
    269238                throw (Open_Failure){ is };
    270239                // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
    271240        } // if
    272         // #endif // __CFA_DEBUG__
    273         is.file$ = file;
     241        (is){ file };                                                                           // initialize
    274242} // open
    275243
    276 void open( ifstream & is, const char name[] ) {
    277         open( is, name, "r" );
    278 } // open
    279 
    280 void close( ifstream & is ) {
    281   if ( (FILE *)(is.file$) == 0p ) return;
    282   if ( (FILE *)(is.file$) == (FILE *)stdin ) return;
    283 
    284         if ( fclose( (FILE *)(is.file$) ) == EOF ) {
     244void open( ifstream & is, const char name[] ) { open( is, name, "r" ); }
     245
     246void 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 ) {
    285258                throw (Close_Failure){ is };
    286259                // abort | IO_MSG "close input" | nl | strerror( errno );
    287260        } // if
    288         is.file$ = 0p;
     261        file$ = 0p;                                                                                     // safety after close
    289262} // close
    290263
     
    315288int fmt( ifstream & is, const char format[], ... ) {
    316289        va_list args;
    317 
    318290        va_start( args, format );
    319         int len = vfscanf( (FILE *)(is.file$), format, args );
     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
    320298        if ( len == EOF ) {
    321299                if ( ferror( (FILE *)(is.file$) ) ) {
     
    327305} // fmt
    328306
    329 inline void acquire( ifstream & is ) {
    330         lock( is.lock$ );
    331         if ( ! is.acquired$ ) is.acquired$ = true;
    332         else unlock( is.lock$ );
    333 } // acquire
    334 
    335 inline void release( ifstream & is ) {
    336         unlock( is.lock$ );
    337 } // release
    338 
    339 void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.lock$ ); }
    340 void ^?{}( isacquire & acq ) { release( acq.is ); }
    341 
    342307static ifstream sinFile = { (FILE *)stdin };
    343308ifstream & sin = sinFile, & stdin = sinFile;
     
    350315
    351316// exception I/O constructors
    352 void ?{}( Open_Failure & this, ofstream & ostream ) {
    353         this.virtual_table = &Open_Failure_vt;
    354         this.ostream = &ostream;
    355         this.tag = 1;
    356 } // ?{}
    357 
    358 void ?{}( Open_Failure & this, ifstream & istream ) {
    359         this.virtual_table = &Open_Failure_vt;
    360         this.istream = &istream;
    361         this.tag = 0;
     317void ?{}( Open_Failure & ex, ofstream & ostream ) with(ex) {
     318        virtual_table = &Open_Failure_vt;
     319        ostream = &ostream;
     320        tag = 1;
     321} // ?{}
     322
     323void ?{}( Open_Failure & ex, ifstream & istream ) with(ex) {
     324        virtual_table = &Open_Failure_vt;
     325        istream = &istream;
     326        tag = 0;
    362327} // ?{}
    363328
     
    366331
    367332// exception I/O constructors
    368 void ?{}( Close_Failure & this, ofstream & ostream ) {
    369         this.virtual_table = &Close_Failure_vt;
    370         this.ostream = &ostream;
    371         this.tag = 1;
    372 } // ?{}
    373 
    374 void ?{}( Close_Failure & this, ifstream & istream ) {
    375         this.virtual_table = &Close_Failure_vt;
    376         this.istream = &istream;
    377         this.tag = 0;
     333void ?{}( Close_Failure & ex, ofstream & ostream ) with(ex) {
     334        virtual_table = &Close_Failure_vt;
     335        ostream = &ostream;
     336        tag = 1;
     337} // ?{}
     338
     339void ?{}( Close_Failure & ex, ifstream & istream ) with(ex) {
     340        virtual_table = &Close_Failure_vt;
     341        istream = &istream;
     342        tag = 0;
    378343} // ?{}
    379344
     
    382347
    383348// exception I/O constructors
    384 void ?{}( Write_Failure & this, ofstream & ostream ) {
    385         this.virtual_table = &Write_Failure_vt;
    386         this.ostream = &ostream;
    387         this.tag = 1;
    388 } // ?{}
    389 
    390 void ?{}( Write_Failure & this, ifstream & istream ) {
    391         this.virtual_table = &Write_Failure_vt;
    392         this.istream = &istream;
    393         this.tag = 0;
     349void ?{}( Write_Failure & ex, ofstream & ostream ) with(ex) {
     350        virtual_table = &Write_Failure_vt;
     351        ostream = &ostream;
     352        tag = 1;
     353} // ?{}
     354
     355void ?{}( Write_Failure & ex, ifstream & istream ) with(ex) {
     356        virtual_table = &Write_Failure_vt;
     357        istream = &istream;
     358        tag = 0;
    394359} // ?{}
    395360
     
    398363
    399364// exception I/O constructors
    400 void ?{}( Read_Failure & this, ofstream & ostream ) {
    401         this.virtual_table = &Read_Failure_vt;
    402         this.ostream = &ostream;
    403         this.tag = 1;
    404 } // ?{}
    405 
    406 void ?{}( Read_Failure & this, ifstream & istream ) {
    407         this.virtual_table = &Read_Failure_vt;
    408         this.istream = &istream;
    409         this.tag = 0;
     365void ?{}( Read_Failure & ex, ofstream & ostream ) with(ex) {
     366        virtual_table = &Read_Failure_vt;
     367        ostream = &ostream;
     368        tag = 1;
     369} // ?{}
     370
     371void ?{}( Read_Failure & ex, ifstream & istream ) with(ex) {
     372        virtual_table = &Read_Failure_vt;
     373        istream = &istream;
     374        tag = 0;
    410375} // ?{}
    411376
Note: See TracChangeset for help on using the changeset viewer.