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