[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 |
---|
[b431515] | 12 | // Last Modified On : Sat Apr 24 09:05:16 2021 |
---|
| 13 | // Update Count : 426 |
---|
[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 |
---|
[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 | |
---|
[09687aa] | 116 | int flush( ofstream & os ) { |
---|
[6c5d92f] | 117 | return fflush( (FILE *)(os.file$) ); |
---|
[6ba0659] | 118 | } // flush |
---|
| 119 | |
---|
[e3fea42] | 120 | void open( ofstream & os, const char name[], const char mode[] ) { |
---|
[5cb2b8c] | 121 | FILE * file = fopen( name, mode ); |
---|
[93c2e0a] | 122 | #ifdef __CFA_DEBUG__ |
---|
[d1a9ff5] | 123 | if ( file == 0p ) { |
---|
[8d321f9] | 124 | throw (Open_Failure){ os }; |
---|
| 125 | // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno ); |
---|
[93c2e0a] | 126 | } // if |
---|
| 127 | #endif // __CFA_DEBUG__ |
---|
[5cb2b8c] | 128 | (os){ file }; |
---|
[6ba0659] | 129 | } // open |
---|
| 130 | |
---|
[e3fea42] | 131 | void open( ofstream & os, const char name[] ) { |
---|
[8da74119] | 132 | open( os, name, "w" ); |
---|
| 133 | } // open |
---|
| 134 | |
---|
[09687aa] | 135 | void close( ofstream & os ) { |
---|
[6c5d92f] | 136 | if ( (FILE *)(os.file$) == 0p ) return; |
---|
| 137 | if ( (FILE *)(os.file$) == (FILE *)stdout || (FILE *)(os.file$) == (FILE *)stderr ) return; |
---|
[6ba0659] | 138 | |
---|
[6c5d92f] | 139 | if ( fclose( (FILE *)(os.file$) ) == EOF ) { |
---|
[ff2a33e] | 140 | abort | IO_MSG "close output" | nl | strerror( errno ); |
---|
[356189a] | 141 | } // if |
---|
[6c5d92f] | 142 | os.file$ = 0p; |
---|
[6ba0659] | 143 | } // close |
---|
| 144 | |
---|
[e3fea42] | 145 | ofstream & write( ofstream & os, const char data[], size_t size ) { |
---|
[6ba0659] | 146 | if ( fail( os ) ) { |
---|
[ff2a33e] | 147 | abort | IO_MSG "attempt write I/O on failed stream"; |
---|
[6ba0659] | 148 | } // if |
---|
| 149 | |
---|
[6c5d92f] | 150 | if ( fwrite( data, 1, size, (FILE *)(os.file$) ) != size ) { |
---|
[ff2a33e] | 151 | abort | IO_MSG "write" | nl | strerror( errno ); |
---|
[839ccbb] | 152 | } // if |
---|
[86bd7c1f] | 153 | return os; |
---|
[839ccbb] | 154 | } // write |
---|
[51b7345] | 155 | |
---|
[09687aa] | 156 | int fmt( ofstream & os, const char format[], ... ) { |
---|
[5d125e4] | 157 | va_list args; |
---|
[829c907] | 158 | va_start( args, format ); |
---|
[6c5d92f] | 159 | int len = vfprintf( (FILE *)(os.file$), format, args ); |
---|
[90c3b1c] | 160 | if ( len == EOF ) { |
---|
[6c5d92f] | 161 | if ( ferror( (FILE *)(os.file$) ) ) { |
---|
[ff2a33e] | 162 | abort | IO_MSG "invalid write"; |
---|
[90c3b1c] | 163 | } // if |
---|
| 164 | } // if |
---|
[5d125e4] | 165 | va_end( args ); |
---|
[b72bad4f] | 166 | |
---|
[6c5d92f] | 167 | setPrt$( os, true ); // called in output cascade |
---|
| 168 | sepReset$( os ); // reset separator |
---|
[90c3b1c] | 169 | return len; |
---|
[829c907] | 170 | } // fmt |
---|
| 171 | |
---|
[e474cf09] | 172 | inline void acquire( ofstream & os ) { |
---|
[6c5d92f] | 173 | lock( os.lock$ ); |
---|
| 174 | if ( ! os.acquired$ ) os.acquired$ = true; |
---|
| 175 | else unlock( os.lock$ ); |
---|
[e474cf09] | 176 | } // acquire |
---|
| 177 | |
---|
| 178 | inline void release( ofstream & os ) { |
---|
[6c5d92f] | 179 | unlock( os.lock$ ); |
---|
[e474cf09] | 180 | } // release |
---|
| 181 | |
---|
[6c5d92f] | 182 | void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.lock$ ); } |
---|
[e474cf09] | 183 | void ^?{}( osacquire & acq ) { release( acq.os ); } |
---|
| 184 | |
---|
[fd8f88f] | 185 | static ofstream soutFile = { (FILE *)stdout }; |
---|
[a87d40b] | 186 | ofstream & sout = soutFile, & stdout = soutFile; |
---|
[fd8f88f] | 187 | static ofstream serrFile = { (FILE *)stderr }; |
---|
[a87d40b] | 188 | ofstream & serr = serrFile, & stderr = serrFile; |
---|
[51b7345] | 189 | |
---|
[e474cf09] | 190 | static ofstream lsoutFile = { (FILE *)stdout }; |
---|
| 191 | ofstream & lsout = lsoutFile; |
---|
| 192 | |
---|
[fd8f88f] | 193 | static ofstream exitFile = { (FILE *)stdout }; |
---|
[65240bb] | 194 | ofstream & exit = exitFile; |
---|
[fd8f88f] | 195 | static ofstream abortFile = { (FILE *)stderr }; |
---|
[65240bb] | 196 | ofstream & abort = abortFile; |
---|
[5cb2b8c] | 197 | |
---|
[90c3b1c] | 198 | |
---|
[8d321f9] | 199 | // *********************************** ifstream *********************************** |
---|
[65240bb] | 200 | |
---|
[51b7345] | 201 | |
---|
[09687aa] | 202 | // private |
---|
| 203 | void ?{}( ifstream & is, void * file ) { |
---|
[6c5d92f] | 204 | is.file$ = file; |
---|
| 205 | is.nlOnOff$ = false; |
---|
| 206 | is.acquired$ = false; |
---|
[65240bb] | 207 | } // ?{} |
---|
[09687aa] | 208 | |
---|
| 209 | // public |
---|
[6c5d92f] | 210 | void ?{}( ifstream & is ) { is.file$ = 0p; } |
---|
[51b7345] | 211 | |
---|
[e3fea42] | 212 | void ?{}( ifstream & is, const char name[], const char mode[] ) { |
---|
[8da74119] | 213 | open( is, name, mode ); |
---|
[65240bb] | 214 | } // ?{} |
---|
| 215 | |
---|
[e3fea42] | 216 | void ?{}( ifstream & is, const char name[] ) { |
---|
[8da74119] | 217 | open( is, name, "r" ); |
---|
[65240bb] | 218 | } // ?{} |
---|
[09687aa] | 219 | |
---|
[4cae032] | 220 | void ^?{}( ifstream & is ) { |
---|
| 221 | close( is ); |
---|
| 222 | } // ^?{} |
---|
| 223 | |
---|
[6c5d92f] | 224 | void nlOn( ifstream & os ) { os.nlOnOff$ = true; } |
---|
| 225 | void nlOff( ifstream & os ) { os.nlOnOff$ = false; } |
---|
| 226 | bool getANL( ifstream & os ) { return os.nlOnOff$; } |
---|
[0efb269] | 227 | |
---|
[b431515] | 228 | bool fail( ifstream & is ) { |
---|
[6c5d92f] | 229 | return is.file$ == 0p || ferror( (FILE *)(is.file$) ); |
---|
[6ba0659] | 230 | } // fail |
---|
| 231 | |
---|
[e474cf09] | 232 | void ends( ifstream & is ) { |
---|
[6c5d92f] | 233 | if ( is.acquired$ ) { is.acquired$ = false; release( is ); } |
---|
[e474cf09] | 234 | } // ends |
---|
| 235 | |
---|
[09687aa] | 236 | int eof( ifstream & is ) { |
---|
[6c5d92f] | 237 | return feof( (FILE *)(is.file$) ); |
---|
[6ba0659] | 238 | } // eof |
---|
| 239 | |
---|
[e3fea42] | 240 | void open( ifstream & is, const char name[], const char mode[] ) { |
---|
[8a25be9] | 241 | FILE * file = fopen( name, mode ); |
---|
[93c2e0a] | 242 | #ifdef __CFA_DEBUG__ |
---|
[d1a9ff5] | 243 | if ( file == 0p ) { |
---|
[8d321f9] | 244 | throw (Open_Failure){ is }; |
---|
| 245 | // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno ); |
---|
[93c2e0a] | 246 | } // if |
---|
| 247 | #endif // __CFA_DEBUG__ |
---|
[6c5d92f] | 248 | is.file$ = file; |
---|
[90c3b1c] | 249 | } // open |
---|
[6ba0659] | 250 | |
---|
[e3fea42] | 251 | void open( ifstream & is, const char name[] ) { |
---|
[8da74119] | 252 | open( is, name, "r" ); |
---|
| 253 | } // open |
---|
| 254 | |
---|
[09687aa] | 255 | void close( ifstream & is ) { |
---|
[6c5d92f] | 256 | if ( (FILE *)(is.file$) == 0p ) return; |
---|
| 257 | if ( (FILE *)(is.file$) == (FILE *)stdin ) return; |
---|
[90c3b1c] | 258 | |
---|
[6c5d92f] | 259 | if ( fclose( (FILE *)(is.file$) ) == EOF ) { |
---|
[ff2a33e] | 260 | abort | IO_MSG "close input" | nl | strerror( errno ); |
---|
[356189a] | 261 | } // if |
---|
[6c5d92f] | 262 | is.file$ = 0p; |
---|
[90c3b1c] | 263 | } // close |
---|
| 264 | |
---|
[91d766d] | 265 | ifstream & read( ifstream & is, char * data, size_t size ) { |
---|
[6ba0659] | 266 | if ( fail( is ) ) { |
---|
[ff2a33e] | 267 | abort | IO_MSG "attempt read I/O on failed stream"; |
---|
[6ba0659] | 268 | } // if |
---|
| 269 | |
---|
[6c5d92f] | 270 | if ( fread( data, size, 1, (FILE *)(is.file$) ) == 0 ) { |
---|
[ff2a33e] | 271 | abort | IO_MSG "read" | nl | strerror( errno ); |
---|
[6ba0659] | 272 | } // if |
---|
[86bd7c1f] | 273 | return is; |
---|
[6ba0659] | 274 | } // read |
---|
[356189a] | 275 | |
---|
[09687aa] | 276 | ifstream &ungetc( ifstream & is, char c ) { |
---|
[6ba0659] | 277 | if ( fail( is ) ) { |
---|
[ff2a33e] | 278 | abort | IO_MSG "attempt ungetc I/O on failed stream"; |
---|
[6ba0659] | 279 | } // if |
---|
[51b7345] | 280 | |
---|
[6c5d92f] | 281 | if ( ungetc( c, (FILE *)(is.file$) ) == EOF ) { |
---|
[ff2a33e] | 282 | abort | IO_MSG "ungetc" | nl | strerror( errno ); |
---|
[6ba0659] | 283 | } // if |
---|
| 284 | return is; |
---|
| 285 | } // ungetc |
---|
[51b7345] | 286 | |
---|
[09687aa] | 287 | int fmt( ifstream & is, const char format[], ... ) { |
---|
[5d125e4] | 288 | va_list args; |
---|
[51b7345] | 289 | |
---|
[829c907] | 290 | va_start( args, format ); |
---|
[6c5d92f] | 291 | int len = vfscanf( (FILE *)(is.file$), format, args ); |
---|
[90c3b1c] | 292 | if ( len == EOF ) { |
---|
[6c5d92f] | 293 | if ( ferror( (FILE *)(is.file$) ) ) { |
---|
[ff2a33e] | 294 | abort | IO_MSG "invalid read"; |
---|
[90c3b1c] | 295 | } // if |
---|
| 296 | } // if |
---|
[5d125e4] | 297 | va_end( args ); |
---|
[90c3b1c] | 298 | return len; |
---|
[829c907] | 299 | } // fmt |
---|
[51b7345] | 300 | |
---|
[e474cf09] | 301 | inline void acquire( ifstream & is ) { |
---|
[6c5d92f] | 302 | lock( is.lock$ ); |
---|
| 303 | if ( ! is.acquired$ ) is.acquired$ = true; |
---|
| 304 | else unlock( is.lock$ ); |
---|
[e474cf09] | 305 | } // acquire |
---|
| 306 | |
---|
| 307 | inline void release( ifstream & is ) { |
---|
[6c5d92f] | 308 | unlock( is.lock$ ); |
---|
[e474cf09] | 309 | } // release |
---|
| 310 | |
---|
[6c5d92f] | 311 | void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.lock$ ); } |
---|
[e474cf09] | 312 | void ^?{}( isacquire & acq ) { release( acq.is ); } |
---|
| 313 | |
---|
[fd8f88f] | 314 | static ifstream sinFile = { (FILE *)stdin }; |
---|
[a87d40b] | 315 | ifstream & sin = sinFile, & stdin = sinFile; |
---|
[86bd7c1f] | 316 | |
---|
[91e52be] | 317 | |
---|
[8d321f9] | 318 | // *********************************** exceptions *********************************** |
---|
[91e52be] | 319 | |
---|
| 320 | |
---|
[ecfd758] | 321 | EHM_VIRTUAL_TABLE(Open_Failure, Open_Failure_main_table); |
---|
[8d321f9] | 322 | void ?{}( Open_Failure & this, ofstream & ostream ) { |
---|
[ecfd758] | 323 | this.virtual_table = &Open_Failure_main_table; |
---|
[91e52be] | 324 | this.ostream = &ostream; |
---|
[8d321f9] | 325 | this.tag = 1; |
---|
[91e52be] | 326 | } |
---|
[8d321f9] | 327 | void ?{}( Open_Failure & this, ifstream & istream ) { |
---|
[ecfd758] | 328 | this.virtual_table = &Open_Failure_main_table; |
---|
[91e52be] | 329 | this.istream = &istream; |
---|
[8d321f9] | 330 | this.tag = 0; |
---|
[91e52be] | 331 | } |
---|
[8d321f9] | 332 | void throwOpen_Failure( ofstream & ostream ) { |
---|
| 333 | Open_Failure exc = { ostream }; |
---|
[91e52be] | 334 | } |
---|
[8d321f9] | 335 | void throwOpen_Failure( ifstream & istream ) { |
---|
| 336 | Open_Failure exc = { istream }; |
---|
[91e52be] | 337 | } |
---|
| 338 | |
---|
[86bd7c1f] | 339 | // Local Variables: // |
---|
| 340 | // tab-width: 4 // |
---|
| 341 | // End: // |
---|