Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/fstream.cfa

    ra51a02d rc8371b5  
    1010// Created On       : Wed May 27 17:56:53 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Sep 21 21:51:38 2021
    13 // Update Count     : 460
     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 ) with(os) {
     30// private
     31void ?{}( ofstream & os, void * file ) with( os ) {
    3132        file$ = file;
    3233        sepDefault$ = true;
     
    3536        prt$ = false;
    3637        sawNL$ = false;
    37         acquired$ = 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 );
     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
    126116        if ( file == 0p ) {
    127117                throw (Open_Failure){ os };
     
    131121} // open
    132122
    133 void open( ofstream & os, const char name[] ) {
    134         open( os, name, "w" );
    135 } // open
    136 
    137 void close( ofstream & os ) with(os) {
     123void open( ofstream & os, const char name[] ) { open( os, name, "w" ); }
     124
     125void close( ofstream & os ) with( os ) {
    138126  if ( (FILE *)(file$) == 0p ) return;
    139127  if ( (FILE *)(file$) == (FILE *)stdout || (FILE *)(file$) == (FILE *)stderr ) return;
    140128
    141         if ( fclose( (FILE *)(file$) ) == EOF ) {
     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 ) {
    142137                throw (Close_Failure){ os };
    143138                // abort | IO_MSG "close output" | nl | strerror( errno );
    144139        } // if
    145         file$ = 0p;
     140        file$ = 0p;                                                                                     // safety after close
    146141} // close
    147142
     
    162157        va_list args;
    163158        va_start( args, format );
    164         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
    165167        if ( len == EOF ) {
    166168                if ( ferror( (FILE *)(os.file$) ) ) {
     
    175177} // fmt
    176178
    177 inline 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
    181 } // acquire
    182 
    183 inline void release( ofstream & os ) {
    184         unlock( os.lock$ );
    185 } // release
    186 
    187 void lock( ofstream & os ) { acquire( os ); }
    188 void unlock( ofstream & os ) { release( os ); }
    189 
    190 void ?{}( osacquire & acq, ofstream & os ) { lock( os.lock$ ); &acq.os = &os; }
    191 void ^?{}( osacquire & acq ) { release( acq.os ); }
    192 
    193179static ofstream soutFile = { (FILE *)stdout };
    194180ofstream & sout = soutFile, & stdout = soutFile;
     
    208194        flush( os );
    209195        return os;
    210         // (ofstream &)(os | '\n');
    211         // setPrt$( os, false );                                                        // turn off
    212         // setNL$( os, true );
    213         // flush( os );
    214         // return sepOff( os );                                                 // prepare for next line
    215196} // nl
    216197
     
    220201
    221202// private
    222 void ?{}( ifstream & is, void * file ) with(is) {
     203void ?{}( ifstream & is, void * file ) with( is ) {
    223204        file$ = file;
    224205        nlOnOff$ = false;
    225         acquired$ = false;
    226 } // ?{}
     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$ ); }
    227212
    228213// public
    229214void ?{}( ifstream & is ) { is.file$ = 0p; }
    230 
    231 void ?{}( ifstream & is, const char name[], const char mode[] ) {
    232         open( is, name, mode );
    233 } // ?{}
    234 
    235 void ?{}( ifstream & is, const char name[] ) {
    236         open( is, name, "r" );
    237 } // ?{}
    238 
    239 void ^?{}( ifstream & is ) {
    240         close( is );
    241 } // ^?{}
     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$) ); }
    242221
    243222void nlOn( ifstream & os ) { os.nlOnOff$ = true; }
    244223void nlOff( ifstream & os ) { os.nlOnOff$ = false; }
    245 bool getANL( ifstream & os ) { return os.nlOnOff$; }
    246 
    247 bool fail( ifstream & is ) {
    248         return is.file$ == 0p || ferror( (FILE *)(is.file$) );
    249 } // fail
    250 
    251 void clear( ifstream & is ) {
    252         clearerr( (FILE *)(is.file$) );
    253 } // clear
    254 
    255 void ends( ifstream & is ) {
    256         if ( is.acquired$ ) { is.acquired$ = false; release( is ); }
    257 } // ends
    258 
    259 bool eof( ifstream & is ) {
    260         return feof( (FILE *)(is.file$) );
    261 } // eof
     224
     225void ends( ifstream & is ) {}
     226
     227bool eof( ifstream & is ) { return feof( (FILE *)(is.file$) ) != 0; }
    262228
    263229void open( ifstream & is, const char name[], const char mode[] ) {
    264         FILE * file = fopen( name, 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
    265237        if ( file == 0p ) {
    266238                throw (Open_Failure){ is };
     
    270242} // open
    271243
    272 void open( ifstream & is, const char name[] ) {
    273         open( is, name, "r" );
    274 } // open
    275 
    276 void close( ifstream & is ) with(is) {
     244void open( ifstream & is, const char name[] ) { open( is, name, "r" ); }
     245
     246void close( ifstream & is ) with( is ) {
    277247  if ( (FILE *)(file$) == 0p ) return;
    278248  if ( (FILE *)(file$) == (FILE *)stdin ) return;
    279249
    280         if ( fclose( (FILE *)(file$) ) == EOF ) {
     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 ) {
    281258                throw (Close_Failure){ is };
    282259                // abort | IO_MSG "close input" | nl | strerror( errno );
    283260        } // if
    284         file$ = 0p;
     261        file$ = 0p;                                                                                     // safety after close
    285262} // close
    286263
     
    311288int fmt( ifstream & is, const char format[], ... ) {
    312289        va_list args;
    313 
    314290        va_start( args, format );
    315         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
    316298        if ( len == EOF ) {
    317299                if ( ferror( (FILE *)(is.file$) ) ) {
     
    322304        return len;
    323305} // fmt
    324 
    325 inline void acquire( ifstream & is ) with(is) {
    326         lock( lock$ );                                                                          // may increase recursive lock
    327         if ( ! acquired$ ) acquired$ = true;                            // not locked ?
    328         else unlock( lock$ );                                                           // unwind recursive lock at start
    329 } // acquire
    330 
    331 inline void release( ifstream & is ) {
    332         unlock( is.lock$ );
    333 } // release
    334 
    335 void ?{}( isacquire & acq, ifstream & is ) { lock( is.lock$ ); &acq.is = &is; }
    336 void ^?{}( isacquire & acq ) { release( acq.is ); }
    337306
    338307static ifstream sinFile = { (FILE *)stdin };
Note: See TracChangeset for help on using the changeset viewer.