[86bd7c1f] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
[356189a] | 7 | // fstream.c -- |
---|
[86bd7c1f] | 8 | // |
---|
[90c3b1c] | 9 | // Author : Peter A. Buhr |
---|
[86bd7c1f] | 10 | // Created On : Wed May 27 17:56:53 2015 |
---|
[5d125e4] | 11 | // Last Modified By : Peter A. Buhr |
---|
[6c5d92f] | 12 | // Last Modified On : Tue Apr 20 19:04:46 2021 |
---|
| 13 | // Update Count : 425 |
---|
[86bd7c1f] | 14 | // |
---|
| 15 | |
---|
[58b6d1b] | 16 | #include "fstream.hfa" |
---|
[51b7345] | 17 | |
---|
[90c3b1c] | 18 | #include <stdio.h> // vfprintf, vfscanf |
---|
| 19 | #include <stdlib.h> // exit |
---|
| 20 | #include <stdarg.h> // varargs |
---|
| 21 | #include <string.h> // strlen |
---|
| 22 | #include <float.h> // DBL_DIG, LDBL_DIG |
---|
| 23 | #include <complex.h> // creal, cimag |
---|
[91c389a] | 24 | #include <assert.h> |
---|
[8a25be9] | 25 | #include <errno.h> // errno |
---|
[51b7345] | 26 | |
---|
[8d321f9] | 27 | // *********************************** ofstream *********************************** |
---|
[65240bb] | 28 | |
---|
| 29 | |
---|
[53ba273] | 30 | #define IO_MSG "I/O error: " |
---|
[6ba0659] | 31 | |
---|
[5cb2b8c] | 32 | void ?{}( ofstream & os, void * file ) { |
---|
[6c5d92f] | 33 | os.file$ = file; |
---|
| 34 | os.sepDefault$ = true; |
---|
| 35 | os.sepOnOff$ = false; |
---|
| 36 | os.nlOnOff$ = true; |
---|
| 37 | os.prt$ = false; |
---|
| 38 | os.sawNL$ = false; |
---|
| 39 | os.acquired$ = false; |
---|
| 40 | sepSetCur$( os, sepGet( os ) ); |
---|
[5cb2b8c] | 41 | sepSet( os, " " ); |
---|
| 42 | sepSetTuple( os, ", " ); |
---|
[65240bb] | 43 | } // ?{} |
---|
[0583064b] | 44 | |
---|
[9ebd778] | 45 | // private |
---|
[6c5d92f] | 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; } |
---|
[829c907] | 56 | |
---|
[9ebd778] | 57 | // public |
---|
[6c5d92f] | 58 | void ?{}( ofstream & os ) { os.file$ = 0p; } |
---|
[829c907] | 59 | |
---|
[e3fea42] | 60 | void ?{}( ofstream & os, const char name[], const char mode[] ) { |
---|
[09687aa] | 61 | open( os, name, mode ); |
---|
[65240bb] | 62 | } // ?{} |
---|
| 63 | |
---|
[e3fea42] | 64 | void ?{}( ofstream & os, const char name[] ) { |
---|
[8da74119] | 65 | open( os, name, "w" ); |
---|
[65240bb] | 66 | } // ?{} |
---|
[09687aa] | 67 | |
---|
[4cae032] | 68 | void ^?{}( ofstream & os ) { |
---|
| 69 | close( os ); |
---|
| 70 | } // ^?{} |
---|
| 71 | |
---|
[6c5d92f] | 72 | void sepOn( ofstream & os ) { os.sepOnOff$ = ! getNL$( os ); } |
---|
| 73 | void sepOff( ofstream & os ) { os.sepOnOff$ = false; } |
---|
[09687aa] | 74 | |
---|
[93c2e0a] | 75 | bool sepDisable( ofstream & os ) { |
---|
[6c5d92f] | 76 | bool temp = os.sepDefault$; |
---|
| 77 | os.sepDefault$ = false; |
---|
| 78 | sepReset$( os ); |
---|
[53ba273] | 79 | return temp; |
---|
| 80 | } // sepDisable |
---|
[6152c81] | 81 | |
---|
[93c2e0a] | 82 | bool sepEnable( ofstream & os ) { |
---|
[6c5d92f] | 83 | bool temp = os.sepDefault$; |
---|
| 84 | os.sepDefault$ = true; |
---|
| 85 | if ( os.sepOnOff$ ) sepReset$( os ); // start of line ? |
---|
[53ba273] | 86 | return temp; |
---|
| 87 | } // sepEnable |
---|
[90c3b1c] | 88 | |
---|
[6c5d92f] | 89 | void nlOn( ofstream & os ) { os.nlOnOff$ = true; } |
---|
| 90 | void nlOff( ofstream & os ) { os.nlOnOff$ = false; } |
---|
[200fcb3] | 91 | |
---|
[6c5d92f] | 92 | const char * sepGet( ofstream & os ) { return os.separator$; } |
---|
[e3fea42] | 93 | void sepSet( ofstream & os, const char s[] ) { |
---|
[9ebd778] | 94 | assert( s ); |
---|
[6c5d92f] | 95 | strncpy( os.separator$, s, sepSize - 1 ); |
---|
| 96 | os.separator$[sepSize - 1] = '\0'; |
---|
[9ebd778] | 97 | } // sepSet |
---|
| 98 | |
---|
[6c5d92f] | 99 | const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator$; } |
---|
[e3fea42] | 100 | void sepSetTuple( ofstream & os, const char s[] ) { |
---|
[9ebd778] | 101 | assert( s ); |
---|
[6c5d92f] | 102 | strncpy( os.tupleSeparator$, s, sepSize - 1 ); |
---|
| 103 | os.tupleSeparator$[sepSize - 1] = '\0'; |
---|
[9ebd778] | 104 | } // sepSet |
---|
| 105 | |
---|
[65240bb] | 106 | void ends( ofstream & os ) { |
---|
[6c5d92f] | 107 | if ( getANL$( os ) ) nl( os ); |
---|
| 108 | else setPrt$( os, false ); // turn off |
---|
[65240bb] | 109 | if ( &os == &exit ) exit( EXIT_FAILURE ); |
---|
| 110 | if ( &os == &abort ) abort(); |
---|
[6c5d92f] | 111 | if ( os.acquired$ ) { os.acquired$ = false; release( os ); } |
---|
[65240bb] | 112 | } // ends |
---|
| 113 | |
---|
[09687aa] | 114 | int fail( ofstream & os ) { |
---|
[6c5d92f] | 115 | return os.file$ == 0 || ferror( (FILE *)(os.file$) ); |
---|
[6ba0659] | 116 | } // fail |
---|
| 117 | |
---|
[09687aa] | 118 | int flush( ofstream & os ) { |
---|
[6c5d92f] | 119 | return fflush( (FILE *)(os.file$) ); |
---|
[6ba0659] | 120 | } // flush |
---|
| 121 | |
---|
[e3fea42] | 122 | void open( ofstream & os, const char name[], const char mode[] ) { |
---|
[5cb2b8c] | 123 | FILE * file = fopen( name, mode ); |
---|
[93c2e0a] | 124 | #ifdef __CFA_DEBUG__ |
---|
[d1a9ff5] | 125 | if ( file == 0p ) { |
---|
[8d321f9] | 126 | throw (Open_Failure){ os }; |
---|
| 127 | // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno ); |
---|
[93c2e0a] | 128 | } // if |
---|
| 129 | #endif // __CFA_DEBUG__ |
---|
[5cb2b8c] | 130 | (os){ file }; |
---|
[6ba0659] | 131 | } // open |
---|
| 132 | |
---|
[e3fea42] | 133 | void open( ofstream & os, const char name[] ) { |
---|
[8da74119] | 134 | open( os, name, "w" ); |
---|
| 135 | } // open |
---|
| 136 | |
---|
[09687aa] | 137 | void close( ofstream & os ) { |
---|
[6c5d92f] | 138 | if ( (FILE *)(os.file$) == 0p ) return; |
---|
| 139 | if ( (FILE *)(os.file$) == (FILE *)stdout || (FILE *)(os.file$) == (FILE *)stderr ) return; |
---|
[6ba0659] | 140 | |
---|
[6c5d92f] | 141 | if ( fclose( (FILE *)(os.file$) ) == EOF ) { |
---|
[ff2a33e] | 142 | abort | IO_MSG "close output" | nl | strerror( errno ); |
---|
[356189a] | 143 | } // if |
---|
[6c5d92f] | 144 | os.file$ = 0p; |
---|
[6ba0659] | 145 | } // close |
---|
| 146 | |
---|
[e3fea42] | 147 | ofstream & write( ofstream & os, const char data[], size_t size ) { |
---|
[6ba0659] | 148 | if ( fail( os ) ) { |
---|
[ff2a33e] | 149 | abort | IO_MSG "attempt write I/O on failed stream"; |
---|
[6ba0659] | 150 | } // if |
---|
| 151 | |
---|
[6c5d92f] | 152 | if ( fwrite( data, 1, size, (FILE *)(os.file$) ) != size ) { |
---|
[ff2a33e] | 153 | abort | IO_MSG "write" | nl | strerror( errno ); |
---|
[839ccbb] | 154 | } // if |
---|
[86bd7c1f] | 155 | return os; |
---|
[839ccbb] | 156 | } // write |
---|
[51b7345] | 157 | |
---|
[09687aa] | 158 | int fmt( ofstream & os, const char format[], ... ) { |
---|
[5d125e4] | 159 | va_list args; |
---|
[829c907] | 160 | va_start( args, format ); |
---|
[6c5d92f] | 161 | int len = vfprintf( (FILE *)(os.file$), format, args ); |
---|
[90c3b1c] | 162 | if ( len == EOF ) { |
---|
[6c5d92f] | 163 | if ( ferror( (FILE *)(os.file$) ) ) { |
---|
[ff2a33e] | 164 | abort | IO_MSG "invalid write"; |
---|
[90c3b1c] | 165 | } // if |
---|
| 166 | } // if |
---|
[5d125e4] | 167 | va_end( args ); |
---|
[b72bad4f] | 168 | |
---|
[6c5d92f] | 169 | setPrt$( os, true ); // called in output cascade |
---|
| 170 | sepReset$( os ); // reset separator |
---|
[90c3b1c] | 171 | return len; |
---|
[829c907] | 172 | } // fmt |
---|
| 173 | |
---|
[e474cf09] | 174 | inline void acquire( ofstream & os ) { |
---|
[6c5d92f] | 175 | lock( os.lock$ ); |
---|
| 176 | if ( ! os.acquired$ ) os.acquired$ = true; |
---|
| 177 | else unlock( os.lock$ ); |
---|
[e474cf09] | 178 | } // acquire |
---|
| 179 | |
---|
| 180 | inline void release( ofstream & os ) { |
---|
[6c5d92f] | 181 | unlock( os.lock$ ); |
---|
[e474cf09] | 182 | } // release |
---|
| 183 | |
---|
[6c5d92f] | 184 | void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.lock$ ); } |
---|
[e474cf09] | 185 | void ^?{}( osacquire & acq ) { release( acq.os ); } |
---|
| 186 | |
---|
[fd8f88f] | 187 | static ofstream soutFile = { (FILE *)stdout }; |
---|
[a87d40b] | 188 | ofstream & sout = soutFile, & stdout = soutFile; |
---|
[fd8f88f] | 189 | static ofstream serrFile = { (FILE *)stderr }; |
---|
[a87d40b] | 190 | ofstream & serr = serrFile, & stderr = serrFile; |
---|
[51b7345] | 191 | |
---|
[e474cf09] | 192 | static ofstream lsoutFile = { (FILE *)stdout }; |
---|
| 193 | ofstream & lsout = lsoutFile; |
---|
| 194 | |
---|
[fd8f88f] | 195 | static ofstream exitFile = { (FILE *)stdout }; |
---|
[65240bb] | 196 | ofstream & exit = exitFile; |
---|
[fd8f88f] | 197 | static ofstream abortFile = { (FILE *)stderr }; |
---|
[65240bb] | 198 | ofstream & abort = abortFile; |
---|
[5cb2b8c] | 199 | |
---|
[90c3b1c] | 200 | |
---|
[8d321f9] | 201 | // *********************************** ifstream *********************************** |
---|
[65240bb] | 202 | |
---|
[51b7345] | 203 | |
---|
[09687aa] | 204 | // private |
---|
| 205 | void ?{}( ifstream & is, void * file ) { |
---|
[6c5d92f] | 206 | is.file$ = file; |
---|
| 207 | is.nlOnOff$ = false; |
---|
| 208 | is.acquired$ = false; |
---|
[65240bb] | 209 | } // ?{} |
---|
[09687aa] | 210 | |
---|
| 211 | // public |
---|
[6c5d92f] | 212 | void ?{}( ifstream & is ) { is.file$ = 0p; } |
---|
[51b7345] | 213 | |
---|
[e3fea42] | 214 | void ?{}( ifstream & is, const char name[], const char mode[] ) { |
---|
[8da74119] | 215 | open( is, name, mode ); |
---|
[65240bb] | 216 | } // ?{} |
---|
| 217 | |
---|
[e3fea42] | 218 | void ?{}( ifstream & is, const char name[] ) { |
---|
[8da74119] | 219 | open( is, name, "r" ); |
---|
[65240bb] | 220 | } // ?{} |
---|
[09687aa] | 221 | |
---|
[4cae032] | 222 | void ^?{}( ifstream & is ) { |
---|
| 223 | close( is ); |
---|
| 224 | } // ^?{} |
---|
| 225 | |
---|
[6c5d92f] | 226 | void nlOn( ifstream & os ) { os.nlOnOff$ = true; } |
---|
| 227 | void nlOff( ifstream & os ) { os.nlOnOff$ = false; } |
---|
| 228 | bool getANL( ifstream & os ) { return os.nlOnOff$; } |
---|
[0efb269] | 229 | |
---|
[09687aa] | 230 | int fail( ifstream & is ) { |
---|
[6c5d92f] | 231 | return is.file$ == 0p || ferror( (FILE *)(is.file$) ); |
---|
[6ba0659] | 232 | } // fail |
---|
| 233 | |
---|
[e474cf09] | 234 | void ends( ifstream & is ) { |
---|
[6c5d92f] | 235 | if ( is.acquired$ ) { is.acquired$ = false; release( is ); } |
---|
[e474cf09] | 236 | } // ends |
---|
| 237 | |
---|
[09687aa] | 238 | int eof( ifstream & is ) { |
---|
[6c5d92f] | 239 | return feof( (FILE *)(is.file$) ); |
---|
[6ba0659] | 240 | } // eof |
---|
| 241 | |
---|
[e3fea42] | 242 | void open( ifstream & is, const char name[], const char mode[] ) { |
---|
[8a25be9] | 243 | FILE * file = fopen( name, mode ); |
---|
[93c2e0a] | 244 | #ifdef __CFA_DEBUG__ |
---|
[d1a9ff5] | 245 | if ( file == 0p ) { |
---|
[8d321f9] | 246 | throw (Open_Failure){ is }; |
---|
| 247 | // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno ); |
---|
[93c2e0a] | 248 | } // if |
---|
| 249 | #endif // __CFA_DEBUG__ |
---|
[6c5d92f] | 250 | is.file$ = file; |
---|
[90c3b1c] | 251 | } // open |
---|
[6ba0659] | 252 | |
---|
[e3fea42] | 253 | void open( ifstream & is, const char name[] ) { |
---|
[8da74119] | 254 | open( is, name, "r" ); |
---|
| 255 | } // open |
---|
| 256 | |
---|
[09687aa] | 257 | void close( ifstream & is ) { |
---|
[6c5d92f] | 258 | if ( (FILE *)(is.file$) == 0p ) return; |
---|
| 259 | if ( (FILE *)(is.file$) == (FILE *)stdin ) return; |
---|
[90c3b1c] | 260 | |
---|
[6c5d92f] | 261 | if ( fclose( (FILE *)(is.file$) ) == EOF ) { |
---|
[ff2a33e] | 262 | abort | IO_MSG "close input" | nl | strerror( errno ); |
---|
[356189a] | 263 | } // if |
---|
[6c5d92f] | 264 | is.file$ = 0p; |
---|
[90c3b1c] | 265 | } // close |
---|
| 266 | |
---|
[91d766d] | 267 | ifstream & read( ifstream & is, char * data, size_t size ) { |
---|
[6ba0659] | 268 | if ( fail( is ) ) { |
---|
[ff2a33e] | 269 | abort | IO_MSG "attempt read I/O on failed stream"; |
---|
[6ba0659] | 270 | } // if |
---|
| 271 | |
---|
[6c5d92f] | 272 | if ( fread( data, size, 1, (FILE *)(is.file$) ) == 0 ) { |
---|
[ff2a33e] | 273 | abort | IO_MSG "read" | nl | strerror( errno ); |
---|
[6ba0659] | 274 | } // if |
---|
[86bd7c1f] | 275 | return is; |
---|
[6ba0659] | 276 | } // read |
---|
[356189a] | 277 | |
---|
[09687aa] | 278 | ifstream &ungetc( ifstream & is, char c ) { |
---|
[6ba0659] | 279 | if ( fail( is ) ) { |
---|
[ff2a33e] | 280 | abort | IO_MSG "attempt ungetc I/O on failed stream"; |
---|
[6ba0659] | 281 | } // if |
---|
[51b7345] | 282 | |
---|
[6c5d92f] | 283 | if ( ungetc( c, (FILE *)(is.file$) ) == EOF ) { |
---|
[ff2a33e] | 284 | abort | IO_MSG "ungetc" | nl | strerror( errno ); |
---|
[6ba0659] | 285 | } // if |
---|
| 286 | return is; |
---|
| 287 | } // ungetc |
---|
[51b7345] | 288 | |
---|
[09687aa] | 289 | int fmt( ifstream & is, const char format[], ... ) { |
---|
[5d125e4] | 290 | va_list args; |
---|
[51b7345] | 291 | |
---|
[829c907] | 292 | va_start( args, format ); |
---|
[6c5d92f] | 293 | int len = vfscanf( (FILE *)(is.file$), format, args ); |
---|
[90c3b1c] | 294 | if ( len == EOF ) { |
---|
[6c5d92f] | 295 | if ( ferror( (FILE *)(is.file$) ) ) { |
---|
[ff2a33e] | 296 | abort | IO_MSG "invalid read"; |
---|
[90c3b1c] | 297 | } // if |
---|
| 298 | } // if |
---|
[5d125e4] | 299 | va_end( args ); |
---|
[90c3b1c] | 300 | return len; |
---|
[829c907] | 301 | } // fmt |
---|
[51b7345] | 302 | |
---|
[e474cf09] | 303 | inline void acquire( ifstream & is ) { |
---|
[6c5d92f] | 304 | lock( is.lock$ ); |
---|
| 305 | if ( ! is.acquired$ ) is.acquired$ = true; |
---|
| 306 | else unlock( is.lock$ ); |
---|
[e474cf09] | 307 | } // acquire |
---|
| 308 | |
---|
| 309 | inline void release( ifstream & is ) { |
---|
[6c5d92f] | 310 | unlock( is.lock$ ); |
---|
[e474cf09] | 311 | } // release |
---|
| 312 | |
---|
[6c5d92f] | 313 | void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.lock$ ); } |
---|
[e474cf09] | 314 | void ^?{}( isacquire & acq ) { release( acq.is ); } |
---|
| 315 | |
---|
[fd8f88f] | 316 | static ifstream sinFile = { (FILE *)stdin }; |
---|
[a87d40b] | 317 | ifstream & sin = sinFile, & stdin = sinFile; |
---|
[86bd7c1f] | 318 | |
---|
[91e52be] | 319 | |
---|
[8d321f9] | 320 | // *********************************** exceptions *********************************** |
---|
[91e52be] | 321 | |
---|
| 322 | |
---|
[ecfd758] | 323 | EHM_VIRTUAL_TABLE(Open_Failure, Open_Failure_main_table); |
---|
[8d321f9] | 324 | void ?{}( Open_Failure & this, ofstream & ostream ) { |
---|
[ecfd758] | 325 | this.virtual_table = &Open_Failure_main_table; |
---|
[91e52be] | 326 | this.ostream = &ostream; |
---|
[8d321f9] | 327 | this.tag = 1; |
---|
[91e52be] | 328 | } |
---|
[8d321f9] | 329 | void ?{}( Open_Failure & this, ifstream & istream ) { |
---|
[ecfd758] | 330 | this.virtual_table = &Open_Failure_main_table; |
---|
[91e52be] | 331 | this.istream = &istream; |
---|
[8d321f9] | 332 | this.tag = 0; |
---|
[91e52be] | 333 | } |
---|
[8d321f9] | 334 | void throwOpen_Failure( ofstream & ostream ) { |
---|
| 335 | Open_Failure exc = { ostream }; |
---|
[91e52be] | 336 | } |
---|
[8d321f9] | 337 | void throwOpen_Failure( ifstream & istream ) { |
---|
| 338 | Open_Failure exc = { istream }; |
---|
[91e52be] | 339 | } |
---|
| 340 | |
---|
[86bd7c1f] | 341 | // Local Variables: // |
---|
| 342 | // tab-width: 4 // |
---|
| 343 | // End: // |
---|