Changes in libcfa/src/fstream.cfa [5cb2b8c:9d362a0]
- File:
-
- 1 edited
-
libcfa/src/fstream.cfa (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/fstream.cfa
r5cb2b8c r9d362a0 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu May 16 08:33:28 201913 // Update Count : 3 2812 // Last Modified On : Mon Dec 24 18:33:38 2018 13 // Update Count : 304 14 14 // 15 15 … … 23 23 #include <complex.h> // creal, cimag 24 24 #include <assert.h> 25 #include <errno.h> // errno26 25 27 26 #define IO_MSG "I/O error: " 28 27 29 void ?{}( ofstream & os, void * file ) {28 void ?{}( ofstream & os, void * file, bool sepDefault, bool sepOnOff, bool nlOnOff, bool prt, const char * separator, const char * tupleSeparator ) { 30 29 os.file = file; 31 os.sepDefault = true; 32 os.sepOnOff = false; 33 os.nlOnOff = true; 34 os.prt = false; 35 os.sawNL = false; 36 sepSet( os, " " ); 30 os.sepDefault = sepDefault; 31 os.sepOnOff = sepOnOff; 32 os.nlOnOff = nlOnOff; 33 os.prt = prt; 34 sepSet( os, separator ); 37 35 sepSetCur( os, sepGet( os ) ); 38 sepSetTuple( os, ", ");36 sepSetTuple( os, tupleSeparator ); 39 37 } 40 38 … … 104 102 105 103 void open( ofstream & os, const char * name, const char * mode ) { 106 FILE * file = fopen( name, mode );104 FILE *file = fopen( name, mode ); 107 105 #ifdef __CFA_DEBUG__ 108 106 if ( file == 0 ) { 109 abort( IO_MSG "open output file \"%s\", %s", name, strerror( errno ) ); 107 fprintf( stderr, IO_MSG "open output file \"%s\", ", name ); 108 perror( 0 ); 109 exit( EXIT_FAILURE ); 110 110 } // if 111 111 #endif // __CFA_DEBUG__ 112 (os){ file };112 (os){ file, true, false, true, false, " ", ", " }; 113 113 } // open 114 114 … … 121 121 122 122 if ( fclose( (FILE *)(os.file) ) == EOF ) { 123 abort( IO_MSG "close output %s", strerror( errno ));123 perror( IO_MSG "close output" ); 124 124 } // if 125 125 } // close … … 127 127 ofstream & write( ofstream & os, const char * data, size_t size ) { 128 128 if ( fail( os ) ) { 129 abort( "attempt write I/O on failed stream\n" ); 129 fprintf( stderr, "attempt write I/O on failed stream\n" ); 130 exit( EXIT_FAILURE ); 130 131 } // if 131 132 132 133 if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) { 133 abort( IO_MSG "write %s", strerror( errno ) ); 134 perror( IO_MSG "write" ); 135 exit( EXIT_FAILURE ); 134 136 } // if 135 137 return os; … … 142 144 if ( len == EOF ) { 143 145 if ( ferror( (FILE *)(os.file) ) ) { 144 abort( "invalid write\n" ); 146 fprintf( stderr, "invalid write\n" ); 147 exit( EXIT_FAILURE ); 145 148 } // if 146 149 } // if … … 152 155 } // fmt 153 156 154 static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) };157 static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), true, false, true, false, " ", ", " }; 155 158 ofstream & sout = soutFile; 156 static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };159 static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), true, false, true, false, " ", ", " }; 157 160 ofstream & serr = serrFile; 158 161 159 // static ofstream sexitFile = { (FILE *)(&_IO_2_1_stdout_) };160 // ofstream & sexit = sexitFile;161 // static ofstream sabortFile = { (FILE *)(&_IO_2_1_stderr_) };162 // ofstream & sabort = sabortFile;163 164 void nl( ofstream & os ) {165 if ( getANL( os ) ) (ofstream &)(nl( os )); // implementation only166 else setPrt( os, false ); // turn off167 }168 162 169 163 //--------------------------------------- … … 172 166 void ?{}( ifstream & is, void * file ) { 173 167 is.file = file; 174 is.nlOnOff = false;175 168 } 176 169 … … 184 177 open( is, name, "r" ); 185 178 } 186 187 void nlOn( ifstream & os ) { os.nlOnOff = true; }188 void nlOff( ifstream & os ) { os.nlOnOff = false; }189 bool getANL( ifstream & os ) { return os.nlOnOff; }190 179 191 180 int fail( ifstream & is ) { … … 198 187 199 188 void open( ifstream & is, const char * name, const char * mode ) { 200 FILE * file = fopen( name, mode );189 FILE *file = fopen( name, mode ); 201 190 #ifdef __CFA_DEBUG__ 202 191 if ( file == 0 ) { 203 abort( IO_MSG "open input file \"%s\", %s\n", name, strerror( errno ) ); 192 fprintf( stderr, IO_MSG "open input file \"%s\", ", name ); 193 perror( 0 ); 194 exit( EXIT_FAILURE ); 204 195 } // if 205 196 #endif // __CFA_DEBUG__ … … 215 206 216 207 if ( fclose( (FILE *)(is.file) ) == EOF ) { 217 abort( IO_MSG "close input %s", strerror( errno ));208 perror( IO_MSG "close input" ); 218 209 } // if 219 210 } // close … … 221 212 ifstream & read( ifstream & is, char * data, size_t size ) { 222 213 if ( fail( is ) ) { 223 abort( "attempt read I/O on failed stream\n" ); 214 fprintf( stderr, "attempt read I/O on failed stream\n" ); 215 exit( EXIT_FAILURE ); 224 216 } // if 225 217 226 218 if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) { 227 abort( IO_MSG "read %s", strerror( errno ) ); 219 perror( IO_MSG "read" ); 220 exit( EXIT_FAILURE ); 228 221 } // if 229 222 return is; … … 232 225 ifstream &ungetc( ifstream & is, char c ) { 233 226 if ( fail( is ) ) { 234 abort( "attempt ungetc I/O on failed stream\n" ); 227 fprintf( stderr, "attempt ungetc I/O on failed stream\n" ); 228 exit( EXIT_FAILURE ); 235 229 } // if 236 230 237 231 if ( ungetc( c, (FILE *)(is.file) ) == EOF ) { 238 abort( IO_MSG "ungetc %s", strerror( errno ) ); 232 perror( IO_MSG "ungetc" ); 233 exit( EXIT_FAILURE ); 239 234 } // if 240 235 return is; … … 248 243 if ( len == EOF ) { 249 244 if ( ferror( (FILE *)(is.file) ) ) { 250 abort( "invalid read\n" ); 245 fprintf( stderr, "invalid read\n" ); 246 exit( EXIT_FAILURE ); 251 247 } // if 252 248 } // if
Note:
See TracChangeset
for help on using the changeset viewer.