Changes in libcfa/src/fstream.cfa [7ce2483:7e7a076]
- File:
-
- 1 edited
-
libcfa/src/fstream.cfa (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/fstream.cfa
r7ce2483 r7e7a076 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Oct 1 08:10:21202113 // Update Count : 4 7312 // Last Modified On : Thu Jul 29 22:34:10 2021 13 // Update Count : 454 14 14 // 15 15 … … 28 28 #define IO_MSG "I/O error: " 29 29 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; 38 acquired$ = false; 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; 39 38 sepSetCur$( os, sepGet( os ) ); 40 39 sepSet( os, " " ); … … 42 41 } // ?{} 43 42 44 inline bool sepPrt$( ofstream & os ) { setNL$( os, false ); return os.sepOnOff$; } 45 inline void sepReset$( ofstream & os ) { os.sepOnOff$ = os.sepDefault$; } 46 inline void sepReset$( ofstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; } 47 inline const char * sepGetCur$( ofstream & os ) { return os.sepCur$; } 48 inline void sepSetCur$( ofstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; } 49 inline bool getNL$( ofstream & os ) { return os.sawNL$; } 50 inline void setNL$( ofstream & os, bool state ) { os.sawNL$ = state; } 51 inline bool getANL$( ofstream & os ) { return os.nlOnOff$; } 52 inline bool getPrt$( ofstream & os ) { return os.prt$; } 53 inline void setPrt$( ofstream & os, bool state ) { os.prt$ = state; } 54 55 inline void lock( ofstream & os ) with( os ) { lock( os.lock$ ); } 56 inline void unlock( ofstream & os ) { unlock( os.lock$ ); } 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; } 57 54 58 55 // public 59 56 void ?{}( ofstream & os ) { os.file$ = 0p; } 60 void ?{}( ofstream & os, const char name[], const char mode[] ) { open( os, name, mode ); } 61 void ?{}( ofstream & os, const char name[] ) { open( os, name, "w" ); } 62 void ^?{}( ofstream & os ) { close( os ); } 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 } // ^?{} 63 69 64 70 void sepOn( ofstream & os ) { os.sepOnOff$ = ! getNL$( os ); } … … 101 107 if ( &os == &exit ) exit( EXIT_FAILURE ); 102 108 if ( &os == &abort ) abort(); 103 if ( os.acquired$ ) { os.acquired$ = false; unlock( os ); }109 if ( os.acquired$ ) { os.acquired$ = false; release( os ); } 104 110 } // ends 105 111 106 bool fail( ofstream & os ) { return os.file$ == 0 || ferror( (FILE *)(os.file$) ); } 107 void clear( ofstream & os ) { clearerr( (FILE *)(os.file$) ); } 108 int flush( ofstream & os ) { return fflush( (FILE *)(os.file$) ); } 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 109 123 110 124 void open( ofstream & os, const char name[], const char mode[] ) { 111 125 FILE * file = fopen( name, mode ); 126 // #ifdef __CFA_DEBUG__ 112 127 if ( file == 0p ) { 113 128 throw (Open_Failure){ os }; 114 129 // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno ); 115 130 } // if 116 (os){ file }; // initialize 131 // #endif // __CFA_DEBUG__ 132 (os){ file }; 117 133 } // open 118 134 119 void open( ofstream & os, const char name[] ) { open( os, name, "w" ); } 120 121 void close( ofstream & os ) with( os ) { 122 if ( (FILE *)(file$) == 0p ) return; 123 if ( (FILE *)(file$) == (FILE *)stdout || (FILE *)(file$) == (FILE *)stderr ) return; 124 125 if ( fclose( (FILE *)(file$) ) == EOF ) { 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 ) { 126 144 throw (Close_Failure){ os }; 127 145 // abort | IO_MSG "close output" | nl | strerror( errno ); 128 146 } // if 129 file$ = 0p; // safety after close147 os.file$ = 0p; 130 148 } // close 131 149 … … 159 177 } // fmt 160 178 161 void acquire( ofstream & os ) with(os ) {162 lock( os ); // may increase recursive lock163 if ( ! acquired$ ) acquired$ = true; // not locked ?164 else unlock( os ); // unwind recursive lock at start179 inline void acquire( ofstream & os ) { 180 lock( os.lock$ ); 181 if ( ! os.acquired$ ) os.acquired$ = true; 182 else unlock( os.lock$ ); 165 183 } // acquire 166 184 167 void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os ); } 168 void ^?{}( osacquire & acq ) { unlock( acq.os ); } 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 ); } 169 194 170 195 static ofstream soutFile = { (FILE *)stdout }; … … 185 210 flush( os ); 186 211 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 187 217 } // nl 188 218 … … 192 222 193 223 // private 194 void ?{}( ifstream & is, void * file ) with( is ) { 195 file$ = file; 196 nlOnOff$ = false; 197 acquired$ = false; 198 } // ?{} 199 200 inline void lock( ifstream & os ) with( os ) { lock( os.lock$ ); } 201 inline void unlock( ifstream & os ) { unlock( os.lock$ ); } 224 void ?{}( ifstream & is, void * file ) { 225 is.file$ = file; 226 is.nlOnOff$ = false; 227 is.acquired$ = false; 228 } // ?{} 202 229 203 230 // public 204 231 void ?{}( ifstream & is ) { is.file$ = 0p; } 205 void ?{}( ifstream & is, const char name[], const char mode[] ) { open( is, name, mode ); } 206 void ?{}( ifstream & is, const char name[] ) { open( is, name, "r" ); } 207 void ^?{}( ifstream & is ) { close( is ); } 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 } // ^?{} 208 244 209 245 void nlOn( ifstream & os ) { os.nlOnOff$ = true; } … … 211 247 bool getANL( ifstream & os ) { return os.nlOnOff$; } 212 248 213 bool fail( ifstream & is ) { return is.file$ == 0p || ferror( (FILE *)(is.file$) ); } 214 void clear( ifstream & is ) { clearerr( (FILE *)(is.file$) ); } 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 215 256 216 257 void ends( ifstream & is ) { 217 if ( is.acquired$ ) { is.acquired$ = false; unlock( is ); }258 if ( is.acquired$ ) { is.acquired$ = false; release( is ); } 218 259 } // ends 219 260 220 bool eof( ifstream & is ) { return feof( (FILE *)(is.file$) ); } 261 bool eof( ifstream & is ) { 262 return feof( (FILE *)(is.file$) ); 263 } // eof 221 264 222 265 void open( ifstream & is, const char name[], const char mode[] ) { 223 266 FILE * file = fopen( name, mode ); 267 // #ifdef __CFA_DEBUG__ 224 268 if ( file == 0p ) { 225 269 throw (Open_Failure){ is }; 226 270 // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno ); 227 271 } // if 228 (is){ file }; // initialize 272 // #endif // __CFA_DEBUG__ 273 is.file$ = file; 229 274 } // open 230 275 231 void open( ifstream & is, const char name[] ) { open( is, name, "r" ); } 232 233 void close( ifstream & is ) with( is ) { 234 if ( (FILE *)(file$) == 0p ) return; 235 if ( (FILE *)(file$) == (FILE *)stdin ) return; 236 237 if ( fclose( (FILE *)(file$) ) == EOF ) { 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 ) { 238 285 throw (Close_Failure){ is }; 239 286 // abort | IO_MSG "close input" | nl | strerror( errno ); 240 287 } // if 241 file$ = 0p;288 is.file$ = 0p; 242 289 } // close 243 290 … … 280 327 } // fmt 281 328 282 void acquire( ifstream & is ) with(is ) {283 lock( is ); // may increase recursive lock284 if ( ! acquired$ ) acquired$ = true; // not locked ?285 else unlock( is ); // unwind recursive lock at start329 inline void acquire( ifstream & is ) { 330 lock( is.lock$ ); 331 if ( ! is.acquired$ ) is.acquired$ = true; 332 else unlock( is.lock$ ); 286 333 } // acquire 287 334 288 void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is ); } 289 void ^?{}( isacquire & acq ) { unlock( acq.is ); } 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 ); } 290 341 291 342 static ifstream sinFile = { (FILE *)stdin }; … … 299 350 300 351 // exception I/O constructors 301 void ?{}( Open_Failure & ex, ofstream & ostream ) with(ex) {302 virtual_table = &Open_Failure_vt;303 ostream = &ostream;304 t ag = 1;305 } // ?{} 306 307 void ?{}( Open_Failure & ex, ifstream & istream ) with(ex) {308 virtual_table = &Open_Failure_vt;309 istream = &istream;310 t ag = 0;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; 311 362 } // ?{} 312 363 … … 315 366 316 367 // exception I/O constructors 317 void ?{}( Close_Failure & ex, ofstream & ostream ) with(ex) {318 virtual_table = &Close_Failure_vt;319 ostream = &ostream;320 t ag = 1;321 } // ?{} 322 323 void ?{}( Close_Failure & ex, ifstream & istream ) with(ex) {324 virtual_table = &Close_Failure_vt;325 istream = &istream;326 t ag = 0;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; 327 378 } // ?{} 328 379 … … 331 382 332 383 // exception I/O constructors 333 void ?{}( Write_Failure & ex, ofstream & ostream ) with(ex) {334 virtual_table = &Write_Failure_vt;335 ostream = &ostream;336 t ag = 1;337 } // ?{} 338 339 void ?{}( Write_Failure & ex, ifstream & istream ) with(ex) {340 virtual_table = &Write_Failure_vt;341 istream = &istream;342 t ag = 0;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; 343 394 } // ?{} 344 395 … … 347 398 348 399 // exception I/O constructors 349 void ?{}( Read_Failure & ex, ofstream & ostream ) with(ex) {350 virtual_table = &Read_Failure_vt;351 ostream = &ostream;352 t ag = 1;353 } // ?{} 354 355 void ?{}( Read_Failure & ex, ifstream & istream ) with(ex) {356 virtual_table = &Read_Failure_vt;357 istream = &istream;358 t ag = 0;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; 359 410 } // ?{} 360 411
Note:
See TracChangeset
for help on using the changeset viewer.