Changeset b067d9b for libcfa/src/fstream.cfa
- Timestamp:
- Oct 29, 2019, 4:01:24 PM (6 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 773db65, 9421f3d8
- Parents:
- 7951100 (diff), 8364209 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/fstream.cfa
r7951100 rb067d9b 10 10 // Created On : Wed May 27 17:56:53 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Jun 5 17:02:56 201813 // Update Count : 28114 // 15 16 #include "fstream "12 // Last Modified On : Tue Sep 10 22:19:56 2019 13 // Update Count : 354 14 // 15 16 #include "fstream.hfa" 17 17 18 18 #include <stdio.h> // vfprintf, vfscanf … … 20 20 #include <stdarg.h> // varargs 21 21 #include <string.h> // strlen 22 #include <stdbool.h> // true/false23 22 #include <float.h> // DBL_DIG, LDBL_DIG 24 23 #include <complex.h> // creal, cimag 25 24 #include <assert.h> 25 #include <errno.h> // errno 26 27 28 //*********************************** ofstream *********************************** 29 26 30 27 31 #define IO_MSG "I/O error: " 28 32 29 void ?{}( ofstream & os, void * file , _Bool sepDefault, _Bool sepOnOff, const char * separator, const char * tupleSeparator) {33 void ?{}( ofstream & os, void * file ) { 30 34 os.file = file; 31 os.sepDefault = sepDefault; 32 os.sepOnOff = sepOnOff; 33 sepSet( os, separator ); 35 os.sepDefault = true; 36 os.sepOnOff = false; 37 os.nlOnOff = true; 38 os.prt = false; 39 os.sawNL = false; 40 sepSet( os, " " ); 34 41 sepSetCur( os, sepGet( os ) ); 35 sepSetTuple( os, tupleSeparator);36 } 42 sepSetTuple( os, ", " ); 43 } // ?{} 37 44 38 45 // private 39 _Bool sepPrt( ofstream & os ) { setNL( os, false ); return os.sepOnOff; }46 bool sepPrt( ofstream & os ) { setNL( os, false ); return os.sepOnOff; } 40 47 void sepReset( ofstream & os ) { os.sepOnOff = os.sepDefault; } 41 void sepReset( ofstream & os, _Bool reset ) { os.sepDefault = reset; os.sepOnOff = os.sepDefault; }48 void sepReset( ofstream & os, bool reset ) { os.sepDefault = reset; os.sepOnOff = os.sepDefault; } 42 49 const char * sepGetCur( ofstream & os ) { return os.sepCur; } 43 50 void sepSetCur( ofstream & os, const char * sepCur ) { os.sepCur = sepCur; } 44 _Bool getNL( ofstream & os ) { return os.sawNL; } 45 void setNL( ofstream & os, _Bool state ) { os.sawNL = state; } 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 56 47 57 // public … … 50 60 void ?{}( ofstream & os, const char * name, const char * mode ) { 51 61 open( os, name, mode ); 52 } 62 } // ?{} 63 53 64 void ?{}( ofstream & os, const char * name ) { 54 65 open( os, name, "w" ); 55 } 66 } // ?{} 56 67 57 68 void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); } 58 69 void sepOff( ofstream & os ) { os.sepOnOff = false; } 59 70 60 _Bool sepDisable( ofstream & os ) {61 _Bool temp = os.sepDefault;71 bool sepDisable( ofstream & os ) { 72 bool temp = os.sepDefault; 62 73 os.sepDefault = false; 63 74 sepReset( os ); … … 65 76 } // sepDisable 66 77 67 _Bool sepEnable( ofstream & os ) {68 _Bool temp = os.sepDefault;78 bool sepEnable( ofstream & os ) { 79 bool temp = os.sepDefault; 69 80 os.sepDefault = true; 70 81 if ( os.sepOnOff ) sepReset( os ); // start of line ? 71 82 return temp; 72 83 } // sepEnable 84 85 void nlOn( ofstream & os ) { os.nlOnOff = true; } 86 void nlOff( ofstream & os ) { os.nlOnOff = false; } 73 87 74 88 const char * sepGet( ofstream & os ) { return os.separator; } … … 86 100 } // sepSet 87 101 102 void ends( ofstream & os ) { 103 if ( getANL( os ) ) nl( os ); 104 else setPrt( os, false ); // turn off 105 if ( &os == &exit ) exit( EXIT_FAILURE ); 106 if ( &os == &abort ) abort(); 107 } // ends 108 88 109 int fail( ofstream & os ) { 89 110 return os.file == 0 || ferror( (FILE *)(os.file) ); … … 95 116 96 117 void open( ofstream & os, const char * name, const char * mode ) { 97 FILE * file = fopen( name, mode );98 // if ( file == 0 ) { // do not change unless successful99 // fprintf( stderr, IO_MSG "open output file \"%s\", ", name );100 // perror( 0);101 // exit( EXIT_FAILURE );102 // } // if103 (os){ file , true, false, " ", ", "};118 FILE * file = fopen( name, mode ); 119 #ifdef __CFA_DEBUG__ 120 if ( file == 0 ) { 121 abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno ); 122 } // if 123 #endif // __CFA_DEBUG__ 124 (os){ file }; 104 125 } // open 105 126 … … 112 133 113 134 if ( fclose( (FILE *)(os.file) ) == EOF ) { 114 perror( IO_MSG "close output");135 abort | IO_MSG "close output" | nl | strerror( errno ); 115 136 } // if 116 137 } // close … … 118 139 ofstream & write( ofstream & os, const char * data, size_t size ) { 119 140 if ( fail( os ) ) { 120 fprintf( stderr, "attempt write I/O on failed stream\n" ); 121 exit( EXIT_FAILURE ); 141 abort | IO_MSG "attempt write I/O on failed stream"; 122 142 } // if 123 143 124 144 if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) { 125 perror( IO_MSG "write" ); 126 exit( EXIT_FAILURE ); 145 abort | IO_MSG "write" | nl | strerror( errno ); 127 146 } // if 128 147 return os; … … 135 154 if ( len == EOF ) { 136 155 if ( ferror( (FILE *)(os.file) ) ) { 137 fprintf( stderr, "invalid write\n" ); 138 exit( EXIT_FAILURE ); 156 abort | IO_MSG "invalid write"; 139 157 } // if 140 158 } // if 141 159 va_end( args ); 142 160 161 setPrt( os, true ); // called in output cascade 143 162 sepReset( os ); // reset separator 144 163 return len; 145 164 } // fmt 146 165 147 static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), true, false, " ", ", " }; 148 ofstream & sout = soutFile; 149 static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), true, false, " ", ", " }; 150 ofstream & serr = serrFile; 151 152 153 //--------------------------------------- 166 static ofstream soutFile = { (FILE *)stdout }; 167 ofstream & sout = soutFile, & stdout = soutFile; 168 static ofstream serrFile = { (FILE *)stderr }; 169 ofstream & serr = serrFile, & stderr = serrFile; 170 171 static ofstream exitFile = { (FILE *)stdout }; 172 ofstream & exit = exitFile; 173 static ofstream abortFile = { (FILE *)stderr }; 174 ofstream & abort = abortFile; 175 176 177 //*********************************** ifstream *********************************** 178 154 179 155 180 // private 156 181 void ?{}( ifstream & is, void * file ) { 157 182 is.file = file; 158 } 183 is.nlOnOff = false; 184 } // ?{} 159 185 160 186 // public … … 163 189 void ?{}( ifstream & is, const char * name, const char * mode ) { 164 190 open( is, name, mode ); 165 } 191 } // ?{} 192 166 193 void ?{}( ifstream & is, const char * name ) { 167 194 open( is, name, "r" ); 168 } 195 } // ?{} 196 197 void nlOn( ifstream & os ) { os.nlOnOff = true; } 198 void nlOff( ifstream & os ) { os.nlOnOff = false; } 199 bool getANL( ifstream & os ) { return os.nlOnOff; } 169 200 170 201 int fail( ifstream & is ) { … … 177 208 178 209 void open( ifstream & is, const char * name, const char * mode ) { 179 FILE * file = fopen( name, mode );180 // if ( file == 0 ) { // do not change unless successful181 // fprintf( stderr, IO_MSG "open input file \"%s\", ", name );182 // perror( 0);183 // exit( EXIT_FAILURE );184 // } // if210 FILE * file = fopen( name, mode ); 211 #ifdef __CFA_DEBUG__ 212 if ( file == 0 ) { 213 abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno ); 214 } // if 215 #endif // __CFA_DEBUG__ 185 216 is.file = file; 186 217 } // open … … 194 225 195 226 if ( fclose( (FILE *)(is.file) ) == EOF ) { 196 perror( IO_MSG "close input");227 abort | IO_MSG "close input" | nl | strerror( errno ); 197 228 } // if 198 229 } // close … … 200 231 ifstream & read( ifstream & is, char * data, size_t size ) { 201 232 if ( fail( is ) ) { 202 fprintf( stderr, "attempt read I/O on failed stream\n" ); 203 exit( EXIT_FAILURE ); 233 abort | IO_MSG "attempt read I/O on failed stream"; 204 234 } // if 205 235 206 236 if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) { 207 perror( IO_MSG "read" ); 208 exit( EXIT_FAILURE ); 237 abort | IO_MSG "read" | nl | strerror( errno ); 209 238 } // if 210 239 return is; … … 213 242 ifstream &ungetc( ifstream & is, char c ) { 214 243 if ( fail( is ) ) { 215 fprintf( stderr, "attempt ungetc I/O on failed stream\n" ); 216 exit( EXIT_FAILURE ); 244 abort | IO_MSG "attempt ungetc I/O on failed stream"; 217 245 } // if 218 246 219 247 if ( ungetc( c, (FILE *)(is.file) ) == EOF ) { 220 perror( IO_MSG "ungetc" ); 221 exit( EXIT_FAILURE ); 248 abort | IO_MSG "ungetc" | nl | strerror( errno ); 222 249 } // if 223 250 return is; … … 231 258 if ( len == EOF ) { 232 259 if ( ferror( (FILE *)(is.file) ) ) { 233 fprintf( stderr, "invalid read\n" ); 234 exit( EXIT_FAILURE ); 260 abort | IO_MSG "invalid read"; 235 261 } // if 236 262 } // if … … 239 265 } // fmt 240 266 241 242 static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) }; 243 ifstream & sin = sinFile; 267 static ifstream sinFile = { (FILE *)stdin }; 268 ifstream & sin = sinFile, & stdin = sinFile; 244 269 245 270 // Local Variables: //
Note:
See TracChangeset
for help on using the changeset viewer.