Changes in libcfa/src/fstream.cfa [8d321f9:4cae032]
- File:
-
- 1 edited
-
libcfa/src/fstream.cfa (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/fstream.cfa
r8d321f9 r4cae032 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 : 3 8412 // Last Modified On : Fri Nov 29 06:56:46 2019 13 // Update Count : 355 14 14 // 15 15 … … 26 26 27 27 28 // *********************************** ofstream ***********************************28 //*********************************** ofstream *********************************** 29 29 30 30 … … 32 32 33 33 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 ) ); 34 os.file = file; 35 os.sepDefault = true; 36 os.sepOnOff = false; 37 os.nlOnOff = true; 38 os.prt = false; 39 os.sawNL = false; 41 40 sepSet( os, " " ); 41 sepSetCur( os, sepGet( os ) ); 42 42 sepSetTuple( os, ", " ); 43 43 } // ?{} 44 44 45 45 // 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; }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; } 56 56 57 57 // public 58 void ?{}( ofstream & os ) { os. $file = 0p; }59 60 void ?{}( ofstream & os, const char name[], const char mode[]) {58 void ?{}( ofstream & os ) { os.file = 0; } 59 60 void ?{}( ofstream & os, const char * name, const char * mode ) { 61 61 open( os, name, mode ); 62 62 } // ?{} 63 63 64 void ?{}( ofstream & os, const char name[]) {64 void ?{}( ofstream & os, const char * name ) { 65 65 open( os, name, "w" ); 66 66 } // ?{} … … 70 70 } // ^?{} 71 71 72 void sepOn( ofstream & os ) { os. $sepOnOff = ! $getNL( os ); }73 void sepOff( ofstream & os ) { os. $sepOnOff = false; }72 void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); } 73 void sepOff( ofstream & os ) { os.sepOnOff = false; } 74 74 75 75 bool sepDisable( ofstream & os ) { 76 bool temp = os. $sepDefault;77 os. $sepDefault = false;78 $sepReset( os );76 bool temp = os.sepDefault; 77 os.sepDefault = false; 78 sepReset( os ); 79 79 return temp; 80 80 } // sepDisable 81 81 82 82 bool sepEnable( ofstream & os ) { 83 bool temp = os. $sepDefault;84 os. $sepDefault = true;85 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 ? 86 86 return temp; 87 87 } // sepEnable 88 88 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; }93 void sepSet( ofstream & os, const char s[]) {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; } 93 void sepSet( ofstream & os, const char * s ) { 94 94 assert( s ); 95 strncpy( os. $separator, s, sepSize - 1 );96 os. $separator[sepSize - 1] = '\0';95 strncpy( os.separator, s, sepSize - 1 ); 96 os.separator[sepSize - 1] = '\0'; 97 97 } // sepSet 98 98 99 const char * sepGetTuple( ofstream & os ) { return os. $tupleSeparator; }100 void sepSetTuple( ofstream & os, const char s[]) {99 const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator; } 100 void sepSetTuple( ofstream & os, const char * s ) { 101 101 assert( s ); 102 strncpy( os. $tupleSeparator, s, sepSize - 1 );103 os. $tupleSeparator[sepSize - 1] = '\0';102 strncpy( os.tupleSeparator, s, sepSize - 1 ); 103 os.tupleSeparator[sepSize - 1] = '\0'; 104 104 } // sepSet 105 105 106 106 void ends( ofstream & os ) { 107 if ( $getANL( os ) ) nl( os );108 else $setPrt( os, false ); // turn off107 if ( getANL( os ) ) nl( os ); 108 else setPrt( os, false ); // turn off 109 109 if ( &os == &exit ) exit( EXIT_FAILURE ); 110 110 if ( &os == &abort ) abort(); … … 112 112 113 113 int fail( ofstream & os ) { 114 return os. $file == 0 || ferror( (FILE *)(os.$file) );114 return os.file == 0 || ferror( (FILE *)(os.file) ); 115 115 } // fail 116 116 117 117 int flush( ofstream & os ) { 118 return fflush( (FILE *)(os. $file) );118 return fflush( (FILE *)(os.file) ); 119 119 } // flush 120 120 121 void open( ofstream & os, const char name[], const char mode[]) {121 void open( ofstream & os, const char * name, const char * mode ) { 122 122 FILE * file = fopen( name, mode ); 123 123 #ifdef __CFA_DEBUG__ 124 if ( file == 0p ) { 125 throw (Open_Failure){ os }; 126 // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno ); 124 if ( file == 0 ) { 125 abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno ); 127 126 } // if 128 127 #endif // __CFA_DEBUG__ … … 130 129 } // open 131 130 132 void open( ofstream & os, const char name[]) {131 void open( ofstream & os, const char * name ) { 133 132 open( os, name, "w" ); 134 133 } // open 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) == stdout || (FILE *)(os.file) == stderr ) return; 137 138 if ( fclose( (FILE *)(os.file) ) == EOF ) { 141 139 abort | IO_MSG "close output" | nl | strerror( errno ); 142 140 } // if 143 os.$file = 0p;144 141 } // close 145 142 146 ofstream & write( ofstream & os, const char data[], size_t size ) {143 ofstream & write( ofstream & os, const char * data, size_t size ) { 147 144 if ( fail( os ) ) { 148 145 abort | IO_MSG "attempt write I/O on failed stream"; 149 146 } // if 150 147 151 if ( fwrite( data, 1, size, (FILE *)(os. $file) ) != size ) {148 if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) { 152 149 abort | IO_MSG "write" | nl | strerror( errno ); 153 150 } // if … … 158 155 va_list args; 159 156 va_start( args, format ); 160 int len = vfprintf( (FILE *)(os. $file), format, args );157 int len = vfprintf( (FILE *)(os.file), format, args ); 161 158 if ( len == EOF ) { 162 if ( ferror( (FILE *)(os. $file) ) ) {159 if ( ferror( (FILE *)(os.file) ) ) { 163 160 abort | IO_MSG "invalid write"; 164 161 } // if … … 166 163 va_end( args ); 167 164 168 $setPrt( os, true );// called in output cascade169 $sepReset( os );// reset separator165 setPrt( os, true ); // called in output cascade 166 sepReset( os ); // reset separator 170 167 return len; 171 168 } // fmt … … 182 179 183 180 184 // *********************************** ifstream ***********************************181 //*********************************** ifstream *********************************** 185 182 186 183 187 184 // private 188 185 void ?{}( ifstream & is, void * file ) { 189 is. $file = file;190 is. $nlOnOff = false;186 is.file = file; 187 is.nlOnOff = false; 191 188 } // ?{} 192 189 193 190 // public 194 void ?{}( ifstream & is ) { is.$file = 0p; }195 196 void ?{}( ifstream & is, const char name[], const char mode[]) {191 void ?{}( ifstream & is ) { is.file = 0; } 192 193 void ?{}( ifstream & is, const char * name, const char * mode ) { 197 194 open( is, name, mode ); 198 195 } // ?{} 199 196 200 void ?{}( ifstream & is, const char name[]) {197 void ?{}( ifstream & is, const char * name ) { 201 198 open( is, name, "r" ); 202 199 } // ?{} … … 206 203 } // ^?{} 207 204 208 void nlOn( ifstream & os ) { os. $nlOnOff = true; }209 void nlOff( ifstream & os ) { os. $nlOnOff = false; }210 bool getANL( ifstream & os ) { return os. $nlOnOff; }205 void nlOn( ifstream & os ) { os.nlOnOff = true; } 206 void nlOff( ifstream & os ) { os.nlOnOff = false; } 207 bool getANL( ifstream & os ) { return os.nlOnOff; } 211 208 212 209 int fail( ifstream & is ) { 213 return is. $file == 0p || ferror( (FILE *)(is.$file) );210 return is.file == 0 || ferror( (FILE *)(is.file) ); 214 211 } // fail 215 212 216 213 int eof( ifstream & is ) { 217 return feof( (FILE *)(is. $file) );214 return feof( (FILE *)(is.file) ); 218 215 } // eof 219 216 220 void open( ifstream & is, const char name[], const char mode[]) {217 void open( ifstream & is, const char * name, const char * mode ) { 221 218 FILE * file = fopen( name, mode ); 222 219 #ifdef __CFA_DEBUG__ 223 if ( file == 0p ) { 224 throw (Open_Failure){ is }; 225 // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno ); 220 if ( file == 0 ) { 221 abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno ); 226 222 } // if 227 223 #endif // __CFA_DEBUG__ 228 is. $file = file;229 } // open 230 231 void open( ifstream & is, const char name[]) {224 is.file = file; 225 } // open 226 227 void open( ifstream & is, const char * name ) { 232 228 open( is, name, "r" ); 233 229 } // open 234 230 235 231 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 ) { 232 if ( (FILE *)(is.file) == stdin ) return; 233 234 if ( fclose( (FILE *)(is.file) ) == EOF ) { 240 235 abort | IO_MSG "close input" | nl | strerror( errno ); 241 236 } // if 242 is.$file = 0p;243 237 } // close 244 238 … … 248 242 } // if 249 243 250 if ( fread( data, size, 1, (FILE *)(is. $file) ) == 0 ) {244 if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) { 251 245 abort | IO_MSG "read" | nl | strerror( errno ); 252 246 } // if … … 259 253 } // if 260 254 261 if ( ungetc( c, (FILE *)(is. $file) ) == EOF ) {255 if ( ungetc( c, (FILE *)(is.file) ) == EOF ) { 262 256 abort | IO_MSG "ungetc" | nl | strerror( errno ); 263 257 } // if … … 269 263 270 264 va_start( args, format ); 271 int len = vfscanf( (FILE *)(is. $file), format, args );265 int len = vfscanf( (FILE *)(is.file), format, args ); 272 266 if ( len == EOF ) { 273 if ( ferror( (FILE *)(is. $file) ) ) {267 if ( ferror( (FILE *)(is.file) ) ) { 274 268 abort | IO_MSG "invalid read"; 275 269 } // if … … 282 276 ifstream & sin = sinFile, & stdin = sinFile; 283 277 284 285 // *********************************** exceptions ***********************************286 287 288 void ?{}( Open_Failure & this, ofstream & ostream ) {289 VTABLE_INIT(this, Open_Failure);290 this.ostream = &ostream;291 this.tag = 1;292 }293 void ?{}( Open_Failure & this, ifstream & istream ) {294 VTABLE_INIT(this, Open_Failure);295 this.istream = &istream;296 this.tag = 0;297 }298 const char * Open_Failure_msg(Open_Failure * this) {299 return "Open_Failure";300 }301 VTABLE_INSTANCE(Open_Failure)(Open_Failure_msg);302 void throwOpen_Failure( ofstream & ostream ) {303 Open_Failure exc = { ostream };304 }305 void throwOpen_Failure( ifstream & istream ) {306 Open_Failure exc = { istream };307 }308 309 278 // Local Variables: // 310 279 // tab-width: 4 //
Note:
See TracChangeset
for help on using the changeset viewer.