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