Changes in libcfa/src/fstream.cfa [d1a9ff5:a87d40b]
- File:
-
- 1 edited
-
libcfa/src/fstream.cfa (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/fstream.cfa
rd1a9ff5 ra87d40b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Feb 7 19:01:01 202013 // Update Count : 3 6312 // Last Modified On : Mon Jul 15 18:11:26 2019 13 // Update Count : 349 14 14 // 15 15 … … 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 } // ?{} 67 67 68 void ^?{}( ofstream & os ) { 69 close( os ); 70 } // ^?{} 71 72 void sepOn( ofstream & os ) { os.$sepOnOff = ! $getNL( os ); } 73 void sepOff( ofstream & os ) { os.$sepOnOff = false; } 68 void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); } 69 void sepOff( ofstream & os ) { os.sepOnOff = false; } 74 70 75 71 bool sepDisable( ofstream & os ) { 76 bool temp = os. $sepDefault;77 os. $sepDefault = false;78 $sepReset( os );72 bool temp = os.sepDefault; 73 os.sepDefault = false; 74 sepReset( os ); 79 75 return temp; 80 76 } // sepDisable 81 77 82 78 bool sepEnable( ofstream & os ) { 83 bool temp = os. $sepDefault;84 os. $sepDefault = true;85 if ( os. $sepOnOff ) $sepReset( os );// start of line ?79 bool temp = os.sepDefault; 80 os.sepDefault = true; 81 if ( os.sepOnOff ) sepReset( os ); // start of line ? 86 82 return temp; 87 83 } // sepEnable 88 84 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[]) {85 void nlOn( ofstream & os ) { os.nlOnOff = true; } 86 void nlOff( ofstream & os ) { os.nlOnOff = false; } 87 88 const char * sepGet( ofstream & os ) { return os.separator; } 89 void sepSet( ofstream & os, const char * s ) { 94 90 assert( s ); 95 strncpy( os. $separator, s, sepSize - 1 );96 os. $separator[sepSize - 1] = '\0';91 strncpy( os.separator, s, sepSize - 1 ); 92 os.separator[sepSize - 1] = '\0'; 97 93 } // sepSet 98 94 99 const char * sepGetTuple( ofstream & os ) { return os. $tupleSeparator; }100 void sepSetTuple( ofstream & os, const char s[]) {95 const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator; } 96 void sepSetTuple( ofstream & os, const char * s ) { 101 97 assert( s ); 102 strncpy( os. $tupleSeparator, s, sepSize - 1 );103 os. $tupleSeparator[sepSize - 1] = '\0';98 strncpy( os.tupleSeparator, s, sepSize - 1 ); 99 os.tupleSeparator[sepSize - 1] = '\0'; 104 100 } // sepSet 105 101 106 102 void ends( ofstream & os ) { 107 if ( $getANL( os ) ) nl( os );108 else $setPrt( os, false ); // turn off103 if ( getANL( os ) ) nl( os ); 104 else setPrt( os, false ); // turn off 109 105 if ( &os == &exit ) exit( EXIT_FAILURE ); 110 106 if ( &os == &abort ) abort(); … … 112 108 113 109 int fail( ofstream & os ) { 114 return os. $file == 0 || ferror( (FILE *)(os.$file) );110 return os.file == 0 || ferror( (FILE *)(os.file) ); 115 111 } // fail 116 112 117 113 int flush( ofstream & os ) { 118 return fflush( (FILE *)(os. $file) );114 return fflush( (FILE *)(os.file) ); 119 115 } // flush 120 116 121 void open( ofstream & os, const char name[], const char mode[]) {117 void open( ofstream & os, const char * name, const char * mode ) { 122 118 FILE * file = fopen( name, mode ); 123 119 #ifdef __CFA_DEBUG__ 124 if ( file == 0 p) {120 if ( file == 0 ) { 125 121 abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno ); 126 122 } // if … … 129 125 } // open 130 126 131 void open( ofstream & os, const char name[]) {127 void open( ofstream & os, const char * name ) { 132 128 open( os, name, "w" ); 133 129 } // open 134 130 135 131 void close( ofstream & os ) { 136 if ( (FILE *)(os. $file) == stdout || (FILE *)(os.$file) == stderr ) return;137 138 if ( fclose( (FILE *)(os. $file) ) == EOF ) {132 if ( (FILE *)(os.file) == stdout || (FILE *)(os.file) == stderr ) return; 133 134 if ( fclose( (FILE *)(os.file) ) == EOF ) { 139 135 abort | IO_MSG "close output" | nl | strerror( errno ); 140 136 } // if 141 137 } // close 142 138 143 ofstream & write( ofstream & os, const char data[], size_t size ) {139 ofstream & write( ofstream & os, const char * data, size_t size ) { 144 140 if ( fail( os ) ) { 145 141 abort | IO_MSG "attempt write I/O on failed stream"; 146 142 } // if 147 143 148 if ( fwrite( data, 1, size, (FILE *)(os. $file) ) != size ) {144 if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) { 149 145 abort | IO_MSG "write" | nl | strerror( errno ); 150 146 } // if … … 155 151 va_list args; 156 152 va_start( args, format ); 157 int len = vfprintf( (FILE *)(os. $file), format, args );153 int len = vfprintf( (FILE *)(os.file), format, args ); 158 154 if ( len == EOF ) { 159 if ( ferror( (FILE *)(os. $file) ) ) {155 if ( ferror( (FILE *)(os.file) ) ) { 160 156 abort | IO_MSG "invalid write"; 161 157 } // if … … 163 159 va_end( args ); 164 160 165 $setPrt( os, true );// called in output cascade166 $sepReset( os );// reset separator161 setPrt( os, true ); // called in output cascade 162 sepReset( os ); // reset separator 167 163 return len; 168 164 } // fmt 169 165 170 static ofstream soutFile = { (FILE *) stdout};166 static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) }; 171 167 ofstream & sout = soutFile, & stdout = soutFile; 172 static ofstream serrFile = { (FILE *) stderr};168 static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) }; 173 169 ofstream & serr = serrFile, & stderr = serrFile; 174 170 175 static ofstream exitFile = { (FILE *) stdout};171 static ofstream exitFile = { (FILE *)(&_IO_2_1_stdout_) }; 176 172 ofstream & exit = exitFile; 177 static ofstream abortFile = { (FILE *) stderr};173 static ofstream abortFile = { (FILE *)(&_IO_2_1_stderr_) }; 178 174 ofstream & abort = abortFile; 179 175 … … 184 180 // private 185 181 void ?{}( ifstream & is, void * file ) { 186 is. $file = file;187 is. $nlOnOff = false;182 is.file = file; 183 is.nlOnOff = false; 188 184 } // ?{} 189 185 190 186 // public 191 void ?{}( ifstream & is ) { is.$file = 0p; }192 193 void ?{}( ifstream & is, const char name[], const char mode[]) {187 void ?{}( ifstream & is ) { is.file = 0; } 188 189 void ?{}( ifstream & is, const char * name, const char * mode ) { 194 190 open( is, name, mode ); 195 191 } // ?{} 196 192 197 void ?{}( ifstream & is, const char name[]) {193 void ?{}( ifstream & is, const char * name ) { 198 194 open( is, name, "r" ); 199 195 } // ?{} 200 196 201 void ^?{}( ifstream & is ) { 202 close( is ); 203 } // ^?{} 204 205 void nlOn( ifstream & os ) { os.$nlOnOff = true; } 206 void nlOff( ifstream & os ) { os.$nlOnOff = false; } 207 bool getANL( ifstream & os ) { return os.$nlOnOff; } 197 void nlOn( ifstream & os ) { os.nlOnOff = true; } 198 void nlOff( ifstream & os ) { os.nlOnOff = false; } 199 bool getANL( ifstream & os ) { return os.nlOnOff; } 208 200 209 201 int fail( ifstream & is ) { 210 return is. $file == 0p || ferror( (FILE *)(is.$file) );202 return is.file == 0 || ferror( (FILE *)(is.file) ); 211 203 } // fail 212 204 213 205 int eof( ifstream & is ) { 214 return feof( (FILE *)(is. $file) );206 return feof( (FILE *)(is.file) ); 215 207 } // eof 216 208 217 void open( ifstream & is, const char name[], const char mode[]) {209 void open( ifstream & is, const char * name, const char * mode ) { 218 210 FILE * file = fopen( name, mode ); 219 211 #ifdef __CFA_DEBUG__ 220 if ( file == 0 p) {212 if ( file == 0 ) { 221 213 abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno ); 222 214 } // if 223 215 #endif // __CFA_DEBUG__ 224 is. $file = file;225 } // open 226 227 void open( ifstream & is, const char name[]) {216 is.file = file; 217 } // open 218 219 void open( ifstream & is, const char * name ) { 228 220 open( is, name, "r" ); 229 221 } // open 230 222 231 223 void close( ifstream & is ) { 232 if ( (FILE *)(is. $file) == stdin ) return;233 234 if ( fclose( (FILE *)(is. $file) ) == EOF ) {224 if ( (FILE *)(is.file) == stdin ) return; 225 226 if ( fclose( (FILE *)(is.file) ) == EOF ) { 235 227 abort | IO_MSG "close input" | nl | strerror( errno ); 236 228 } // if … … 242 234 } // if 243 235 244 if ( fread( data, size, 1, (FILE *)(is. $file) ) == 0 ) {236 if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) { 245 237 abort | IO_MSG "read" | nl | strerror( errno ); 246 238 } // if … … 253 245 } // if 254 246 255 if ( ungetc( c, (FILE *)(is. $file) ) == EOF ) {247 if ( ungetc( c, (FILE *)(is.file) ) == EOF ) { 256 248 abort | IO_MSG "ungetc" | nl | strerror( errno ); 257 249 } // if … … 263 255 264 256 va_start( args, format ); 265 int len = vfscanf( (FILE *)(is. $file), format, args );257 int len = vfscanf( (FILE *)(is.file), format, args ); 266 258 if ( len == EOF ) { 267 if ( ferror( (FILE *)(is. $file) ) ) {259 if ( ferror( (FILE *)(is.file) ) ) { 268 260 abort | IO_MSG "invalid read"; 269 261 } // if … … 273 265 } // fmt 274 266 275 static ifstream sinFile = { (FILE *)stdin }; 267 268 static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) }; 276 269 ifstream & sin = sinFile, & stdin = sinFile; 277 270
Note:
See TracChangeset
for help on using the changeset viewer.