Changes in libcfa/src/fstream.cfa [8d321f9:f451177]
- File:
-
- 1 edited
-
libcfa/src/fstream.cfa (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/fstream.cfa
r8d321f9 rf451177 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Jun 19 16:24:54 202013 // Update Count : 38414 // 15 16 #include "fstream.hfa" 12 // Last Modified On : Tue Apr 27 22:08:57 2021 13 // Update Count : 442 14 // 15 16 #include "fstream.hfa" // also includes iostream.hfa 17 17 18 18 #include <stdio.h> // vfprintf, vfscanf 19 19 #include <stdlib.h> // exit 20 20 #include <stdarg.h> // varargs 21 #include <string.h> // strlen 22 #include <float.h> // DBL_DIG, LDBL_DIG 23 #include <complex.h> // creal, cimag 21 #include <string.h> // strncpy, strerror 24 22 #include <assert.h> 25 23 #include <errno.h> // errno 26 24 27 28 25 // *********************************** ofstream *********************************** 29 26 … … 32 29 33 30 void ?{}( ofstream & os, void * file ) { 34 os.$file = file; 35 os.$sepDefault = true; 36 os.$sepOnOff = false; 37 os.$nlOnOff = true; 38 os.$prt = false; 39 os.$sawNL = false; 40 $sepSetCur( os, sepGet( os ) ); 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; 38 sepSetCur$( os, sepGet( os ) ); 41 39 sepSet( os, " " ); 42 40 sepSetTuple( os, ", " ); … … 44 42 45 43 // private 46 bool $sepPrt( ofstream & os ) { $setNL( os, false ); return os.$sepOnOff; }47 void $sepReset( ofstream & os ) { os.$sepOnOff = os.$sepDefault; }48 void $sepReset( ofstream & os, bool reset ) { os.$sepDefault = reset; os.$sepOnOff = os.$sepDefault; }49 const char * $sepGetCur( ofstream & os ) { return os.$sepCur; }50 void $sepSetCur( ofstream & os, const char sepCur[] ) { os.$sepCur= sepCur; }51 bool $getNL( ofstream & os ) { return os.$sawNL; }52 void $setNL( ofstream & os, bool state ) { os.$sawNL= state; }53 bool $getANL( ofstream & os ) { return os.$nlOnOff; }54 bool $getPrt( ofstream & os ) { return os.$prt; }55 void $setPrt( ofstream & os, bool state ) { os.$prt= state; }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; } 56 54 57 55 // public 58 void ?{}( ofstream & os ) { os. $file= 0p; }56 void ?{}( ofstream & os ) { os.file$ = 0p; } 59 57 60 58 void ?{}( ofstream & os, const char name[], const char mode[] ) { … … 70 68 } // ^?{} 71 69 72 void sepOn( ofstream & os ) { os. $sepOnOff = ! $getNL( os ); }73 void sepOff( ofstream & os ) { os. $sepOnOff= false; }70 void sepOn( ofstream & os ) { os.sepOnOff$ = ! getNL$( os ); } 71 void sepOff( ofstream & os ) { os.sepOnOff$ = false; } 74 72 75 73 bool sepDisable( ofstream & os ) { 76 bool temp = os. $sepDefault;77 os. $sepDefault= false;78 $sepReset( os );74 bool temp = os.sepDefault$; 75 os.sepDefault$ = false; 76 sepReset$( os ); 79 77 return temp; 80 78 } // sepDisable 81 79 82 80 bool sepEnable( ofstream & os ) { 83 bool temp = os. $sepDefault;84 os. $sepDefault= true;85 if ( os. $sepOnOff ) $sepReset( os ); // start of line ?81 bool temp = os.sepDefault$; 82 os.sepDefault$ = true; 83 if ( os.sepOnOff$ ) sepReset$( os ); // start of line ? 86 84 return temp; 87 85 } // sepEnable 88 86 89 void nlOn( ofstream & os ) { os. $nlOnOff= true; }90 void nlOff( ofstream & os ) { os. $nlOnOff= false; }91 92 const char * sepGet( ofstream & os ) { return os. $separator; }87 void nlOn( ofstream & os ) { os.nlOnOff$ = true; } 88 void nlOff( ofstream & os ) { os.nlOnOff$ = false; } 89 90 const char * sepGet( ofstream & os ) { return os.separator$; } 93 91 void sepSet( ofstream & os, const char s[] ) { 94 92 assert( s ); 95 strncpy( os. $separator, s,sepSize - 1 );96 os. $separator[sepSize - 1] = '\0';93 strncpy( os.separator$, s, ofstream_sepSize - 1 ); 94 os.separator$[ofstream_sepSize - 1] = '\0'; 97 95 } // sepSet 98 96 99 const char * sepGetTuple( ofstream & os ) { return os. $tupleSeparator; }97 const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator$; } 100 98 void sepSetTuple( ofstream & os, const char s[] ) { 101 99 assert( s ); 102 strncpy( os. $tupleSeparator, s,sepSize - 1 );103 os. $tupleSeparator[sepSize - 1] = '\0';100 strncpy( os.tupleSeparator$, s, ofstream_sepSize - 1 ); 101 os.tupleSeparator$[ofstream_sepSize - 1] = '\0'; 104 102 } // sepSet 105 103 106 104 void ends( ofstream & os ) { 107 if ( $getANL( os ) ) nl( os );108 else $setPrt( os, false ); // turn off105 if ( getANL$( os ) ) nl( os ); 106 else setPrt$( os, false ); // turn off 109 107 if ( &os == &exit ) exit( EXIT_FAILURE ); 110 108 if ( &os == &abort ) abort(); 109 if ( os.acquired$ ) { os.acquired$ = false; release( os ); } 111 110 } // ends 112 111 113 intfail( ofstream & os ) {114 return os. $file == 0 || ferror( (FILE *)(os.$file) );112 bool fail( ofstream & os ) { 113 return os.file$ == 0 || ferror( (FILE *)(os.file$) ); 115 114 } // fail 116 115 117 116 int flush( ofstream & os ) { 118 return fflush( (FILE *)(os. $file) );117 return fflush( (FILE *)(os.file$) ); 119 118 } // flush 120 119 … … 135 134 136 135 void close( ofstream & os ) { 137 if ( (FILE *)(os. $file) == 0p ) return;138 if ( (FILE *)(os. $file) == (FILE *)stdout || (FILE *)(os.$file) == (FILE *)stderr ) return;139 140 if ( fclose( (FILE *)(os. $file) ) == EOF ) {136 if ( (FILE *)(os.file$) == 0p ) return; 137 if ( (FILE *)(os.file$) == (FILE *)stdout || (FILE *)(os.file$) == (FILE *)stderr ) return; 138 139 if ( fclose( (FILE *)(os.file$) ) == EOF ) { 141 140 abort | IO_MSG "close output" | nl | strerror( errno ); 142 141 } // if 143 os. $file= 0p;142 os.file$ = 0p; 144 143 } // close 145 144 … … 149 148 } // if 150 149 151 if ( fwrite( data, 1, size, (FILE *)(os. $file) ) != size ) {150 if ( fwrite( data, 1, size, (FILE *)(os.file$) ) != size ) { 152 151 abort | IO_MSG "write" | nl | strerror( errno ); 153 152 } // if … … 158 157 va_list args; 159 158 va_start( args, format ); 160 int len = vfprintf( (FILE *)(os. $file), format, args );159 int len = vfprintf( (FILE *)(os.file$), format, args ); 161 160 if ( len == EOF ) { 162 if ( ferror( (FILE *)(os. $file) ) ) {161 if ( ferror( (FILE *)(os.file$) ) ) { 163 162 abort | IO_MSG "invalid write"; 164 163 } // if … … 166 165 va_end( args ); 167 166 168 $setPrt( os, true ); // called in output cascade169 $sepReset( os ); // reset separator167 setPrt$( os, true ); // called in output cascade 168 sepReset$( os ); // reset separator 170 169 return len; 171 170 } // fmt 171 172 inline void acquire( ofstream & os ) { 173 lock( os.lock$ ); 174 if ( ! os.acquired$ ) os.acquired$ = true; 175 else unlock( os.lock$ ); 176 } // acquire 177 178 inline void release( ofstream & os ) { 179 unlock( os.lock$ ); 180 } // release 181 182 void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.lock$ ); } 183 void ^?{}( osacquire & acq ) { release( acq.os ); } 172 184 173 185 static ofstream soutFile = { (FILE *)stdout }; … … 176 188 ofstream & serr = serrFile, & stderr = serrFile; 177 189 190 static ofstream lsoutFile = { (FILE *)stdout }; 191 ofstream & lsout = lsoutFile; 192 178 193 static ofstream exitFile = { (FILE *)stdout }; 179 194 ofstream & exit = exitFile; … … 181 196 ofstream & abort = abortFile; 182 197 198 ofstream & nl( ofstream & os ) { 199 nl$( os ); // call basic_ostream nl 200 flush( os ); 201 return os; 202 // (ofstream &)(os | '\n'); 203 // setPrt$( os, false ); // turn off 204 // setNL$( os, true ); 205 // flush( os ); 206 // return sepOff( os ); // prepare for next line 207 } // nl 183 208 184 209 // *********************************** ifstream *********************************** … … 187 212 // private 188 213 void ?{}( ifstream & is, void * file ) { 189 is.$file = file; 190 is.$nlOnOff = false; 214 is.file$ = file; 215 is.nlOnOff$ = false; 216 is.acquired$ = false; 191 217 } // ?{} 192 218 193 219 // public 194 void ?{}( ifstream & is ) { is. $file= 0p; }220 void ?{}( ifstream & is ) { is.file$ = 0p; } 195 221 196 222 void ?{}( ifstream & is, const char name[], const char mode[] ) { … … 206 232 } // ^?{} 207 233 208 void nlOn( ifstream & os ) { os. $nlOnOff= true; }209 void nlOff( ifstream & os ) { os. $nlOnOff= false; }210 bool getANL( ifstream & os ) { return os. $nlOnOff; }211 212 intfail( ifstream & is ) {213 return is. $file == 0p || ferror( (FILE *)(is.$file) );234 void nlOn( ifstream & os ) { os.nlOnOff$ = true; } 235 void nlOff( ifstream & os ) { os.nlOnOff$ = false; } 236 bool getANL( ifstream & os ) { return os.nlOnOff$; } 237 238 bool fail( ifstream & is ) { 239 return is.file$ == 0p || ferror( (FILE *)(is.file$) ); 214 240 } // fail 215 241 242 void ends( ifstream & is ) { 243 if ( is.acquired$ ) { is.acquired$ = false; release( is ); } 244 } // ends 245 216 246 int eof( ifstream & is ) { 217 return feof( (FILE *)(is. $file) );247 return feof( (FILE *)(is.file$) ); 218 248 } // eof 219 249 … … 226 256 } // if 227 257 #endif // __CFA_DEBUG__ 228 is. $file= file;258 is.file$ = file; 229 259 } // open 230 260 … … 234 264 235 265 void close( ifstream & is ) { 236 if ( (FILE *)(is. $file) == 0p ) return;237 if ( (FILE *)(is. $file) == (FILE *)stdin ) return;238 239 if ( fclose( (FILE *)(is. $file) ) == EOF ) {266 if ( (FILE *)(is.file$) == 0p ) return; 267 if ( (FILE *)(is.file$) == (FILE *)stdin ) return; 268 269 if ( fclose( (FILE *)(is.file$) ) == EOF ) { 240 270 abort | IO_MSG "close input" | nl | strerror( errno ); 241 271 } // if 242 is. $file= 0p;272 is.file$ = 0p; 243 273 } // close 244 274 … … 248 278 } // if 249 279 250 if ( fread( data, size, 1, (FILE *)(is. $file) ) == 0 ) {280 if ( fread( data, size, 1, (FILE *)(is.file$) ) == 0 ) { 251 281 abort | IO_MSG "read" | nl | strerror( errno ); 252 282 } // if … … 259 289 } // if 260 290 261 if ( ungetc( c, (FILE *)(is. $file) ) == EOF ) {291 if ( ungetc( c, (FILE *)(is.file$) ) == EOF ) { 262 292 abort | IO_MSG "ungetc" | nl | strerror( errno ); 263 293 } // if … … 269 299 270 300 va_start( args, format ); 271 int len = vfscanf( (FILE *)(is. $file), format, args );301 int len = vfscanf( (FILE *)(is.file$), format, args ); 272 302 if ( len == EOF ) { 273 if ( ferror( (FILE *)(is. $file) ) ) {303 if ( ferror( (FILE *)(is.file$) ) ) { 274 304 abort | IO_MSG "invalid read"; 275 305 } // if … … 279 309 } // fmt 280 310 311 inline void acquire( ifstream & is ) { 312 lock( is.lock$ ); 313 if ( ! is.acquired$ ) is.acquired$ = true; 314 else unlock( is.lock$ ); 315 } // acquire 316 317 inline void release( ifstream & is ) { 318 unlock( is.lock$ ); 319 } // release 320 321 void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.lock$ ); } 322 void ^?{}( isacquire & acq ) { release( acq.is ); } 323 281 324 static ifstream sinFile = { (FILE *)stdin }; 282 325 ifstream & sin = sinFile, & stdin = sinFile; … … 286 329 287 330 331 EHM_VIRTUAL_TABLE(Open_Failure, Open_Failure_main_table); 288 332 void ?{}( Open_Failure & this, ofstream & ostream ) { 289 VTABLE_INIT(this, Open_Failure);333 this.virtual_table = &Open_Failure_main_table; 290 334 this.ostream = &ostream; 291 335 this.tag = 1; 292 336 } 293 337 void ?{}( Open_Failure & this, ifstream & istream ) { 294 VTABLE_INIT(this, Open_Failure);338 this.virtual_table = &Open_Failure_main_table; 295 339 this.istream = &istream; 296 340 this.tag = 0; 297 341 } 298 const char * Open_Failure_msg(Open_Failure * this) {299 return "Open_Failure";300 }301 VTABLE_INSTANCE(Open_Failure)(Open_Failure_msg);302 342 void throwOpen_Failure( ofstream & ostream ) { 303 343 Open_Failure exc = { ostream };
Note:
See TracChangeset
for help on using the changeset viewer.