[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
|
---|
[e474cf09] | 12 | // Last Modified On : Mon Mar 1 21:12:15 2021
|
---|
| 13 | // Update Count : 424
|
---|
[86bd7c1f] | 14 | //
|
---|
| 15 |
|
---|
[58b6d1b] | 16 | #include "fstream.hfa"
|
---|
[51b73452] | 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
|
---|
[51b73452] | 26 |
|
---|
[8d321f9] | 27 | // *********************************** ofstream ***********************************
|
---|
[65240bb] | 28 |
|
---|
| 29 |
|
---|
[53ba273] | 30 | #define IO_MSG "I/O error: "
|
---|
[6ba0659] | 31 |
|
---|
[5cb2b8c] | 32 | void ?{}( ofstream & os, void * file ) {
|
---|
[d1a9ff5] | 33 | os.$file = file;
|
---|
| 34 | os.$sepDefault = true;
|
---|
| 35 | os.$sepOnOff = false;
|
---|
| 36 | os.$nlOnOff = true;
|
---|
| 37 | os.$prt = false;
|
---|
| 38 | os.$sawNL = false;
|
---|
[e474cf09] | 39 | os.$acquired = false;
|
---|
[d1a9ff5] | 40 | $sepSetCur( os, sepGet( os ) );
|
---|
[5cb2b8c] | 41 | sepSet( os, " " );
|
---|
| 42 | sepSetTuple( os, ", " );
|
---|
[65240bb] | 43 | } // ?{}
|
---|
[0583064b] | 44 |
|
---|
[9ebd778] | 45 | // private
|
---|
[d1a9ff5] | 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
|
---|
[d1a9ff5] | 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 |
|
---|
[d1a9ff5] | 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 ) {
|
---|
[d1a9ff5] | 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 ) {
|
---|
[d1a9ff5] | 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 |
|
---|
[d1a9ff5] | 89 | void nlOn( ofstream & os ) { os.$nlOnOff = true; }
|
---|
| 90 | void nlOff( ofstream & os ) { os.$nlOnOff = false; }
|
---|
[200fcb3] | 91 |
|
---|
[d1a9ff5] | 92 | const char * sepGet( ofstream & os ) { return os.$separator; }
|
---|
[e3fea42] | 93 | void sepSet( ofstream & os, const char s[] ) {
|
---|
[9ebd778] | 94 | assert( s );
|
---|
[d1a9ff5] | 95 | strncpy( os.$separator, s, sepSize - 1 );
|
---|
| 96 | os.$separator[sepSize - 1] = '\0';
|
---|
[9ebd778] | 97 | } // sepSet
|
---|
| 98 |
|
---|
[d1a9ff5] | 99 | const char * sepGetTuple( ofstream & os ) { return os.$tupleSeparator; }
|
---|
[e3fea42] | 100 | void sepSetTuple( ofstream & os, const char s[] ) {
|
---|
[9ebd778] | 101 | assert( s );
|
---|
[d1a9ff5] | 102 | strncpy( os.$tupleSeparator, s, sepSize - 1 );
|
---|
| 103 | os.$tupleSeparator[sepSize - 1] = '\0';
|
---|
[9ebd778] | 104 | } // sepSet
|
---|
| 105 |
|
---|
[65240bb] | 106 | void ends( ofstream & os ) {
|
---|
[d1a9ff5] | 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();
|
---|
[e474cf09] | 111 | if ( os.$acquired ) { os.$acquired = false; release( os ); }
|
---|
[65240bb] | 112 | } // ends
|
---|
| 113 |
|
---|
[09687aa] | 114 | int fail( ofstream & os ) {
|
---|
[d1a9ff5] | 115 | return os.$file == 0 || ferror( (FILE *)(os.$file) );
|
---|
[6ba0659] | 116 | } // fail
|
---|
| 117 |
|
---|
[09687aa] | 118 | int flush( ofstream & os ) {
|
---|
[d1a9ff5] | 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 ) {
|
---|
[91e52be] | 138 | if ( (FILE *)(os.$file) == 0p ) return;
|
---|
| 139 | if ( (FILE *)(os.$file) == (FILE *)stdout || (FILE *)(os.$file) == (FILE *)stderr ) return;
|
---|
[6ba0659] | 140 |
|
---|
[d1a9ff5] | 141 | if ( fclose( (FILE *)(os.$file) ) == EOF ) {
|
---|
[ff2a33e] | 142 | abort | IO_MSG "close output" | nl | strerror( errno );
|
---|
[356189a] | 143 | } // if
|
---|
[91e52be] | 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 |
|
---|
[d1a9ff5] | 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
|
---|
[51b73452] | 157 |
|
---|
[09687aa] | 158 | int fmt( ofstream & os, const char format[], ... ) {
|
---|
[5d125e4] | 159 | va_list args;
|
---|
[829c907] | 160 | va_start( args, format );
|
---|
[d1a9ff5] | 161 | int len = vfprintf( (FILE *)(os.$file), format, args );
|
---|
[90c3b1c] | 162 | if ( len == EOF ) {
|
---|
[d1a9ff5] | 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 |
|
---|
[d1a9ff5] | 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 ) {
|
---|
| 175 | lock( os.$lock );
|
---|
| 176 | if ( ! os.$acquired ) os.$acquired = true;
|
---|
| 177 | else unlock( os.$lock );
|
---|
| 178 | } // acquire
|
---|
| 179 |
|
---|
| 180 | inline void release( ofstream & os ) {
|
---|
| 181 | unlock( os.$lock );
|
---|
| 182 | } // release
|
---|
| 183 |
|
---|
| 184 | void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.$lock ); }
|
---|
| 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;
|
---|
[51b73452] | 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 |
|
---|
[51b73452] | 203 |
|
---|
[09687aa] | 204 | // private
|
---|
| 205 | void ?{}( ifstream & is, void * file ) {
|
---|
[d1a9ff5] | 206 | is.$file = file;
|
---|
| 207 | is.$nlOnOff = false;
|
---|
[e474cf09] | 208 | is.$acquired = false;
|
---|
[65240bb] | 209 | } // ?{}
|
---|
[09687aa] | 210 |
|
---|
| 211 | // public
|
---|
[d1a9ff5] | 212 | void ?{}( ifstream & is ) { is.$file = 0p; }
|
---|
[51b73452] | 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 |
|
---|
[d1a9ff5] | 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 ) {
|
---|
[d1a9ff5] | 231 | return is.$file == 0p || ferror( (FILE *)(is.$file) );
|
---|
[6ba0659] | 232 | } // fail
|
---|
| 233 |
|
---|
[e474cf09] | 234 | void ends( ifstream & is ) {
|
---|
| 235 | if ( is.$acquired ) { is.$acquired = false; release( is ); }
|
---|
| 236 | } // ends
|
---|
| 237 |
|
---|
[09687aa] | 238 | int eof( ifstream & is ) {
|
---|
[d1a9ff5] | 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__
|
---|
[d1a9ff5] | 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 ) {
|
---|
[91e52be] | 258 | if ( (FILE *)(is.$file) == 0p ) return;
|
---|
| 259 | if ( (FILE *)(is.$file) == (FILE *)stdin ) return;
|
---|
[90c3b1c] | 260 |
|
---|
[d1a9ff5] | 261 | if ( fclose( (FILE *)(is.$file) ) == EOF ) {
|
---|
[ff2a33e] | 262 | abort | IO_MSG "close input" | nl | strerror( errno );
|
---|
[356189a] | 263 | } // if
|
---|
[91e52be] | 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 |
|
---|
[d1a9ff5] | 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
|
---|
[51b73452] | 282 |
|
---|
[d1a9ff5] | 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
|
---|
[51b73452] | 288 |
|
---|
[09687aa] | 289 | int fmt( ifstream & is, const char format[], ... ) {
|
---|
[5d125e4] | 290 | va_list args;
|
---|
[51b73452] | 291 |
|
---|
[829c907] | 292 | va_start( args, format );
|
---|
[d1a9ff5] | 293 | int len = vfscanf( (FILE *)(is.$file), format, args );
|
---|
[90c3b1c] | 294 | if ( len == EOF ) {
|
---|
[d1a9ff5] | 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
|
---|
[51b73452] | 302 |
|
---|
[e474cf09] | 303 | inline void acquire( ifstream & is ) {
|
---|
| 304 | lock( is.$lock );
|
---|
| 305 | if ( ! is.$acquired ) is.$acquired = true;
|
---|
| 306 | else unlock( is.$lock );
|
---|
| 307 | } // acquire
|
---|
| 308 |
|
---|
| 309 | inline void release( ifstream & is ) {
|
---|
| 310 | unlock( is.$lock );
|
---|
| 311 | } // release
|
---|
| 312 |
|
---|
| 313 | void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.$lock ); }
|
---|
| 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 |
|
---|
[8d321f9] | 323 | void ?{}( Open_Failure & this, ofstream & ostream ) {
|
---|
| 324 | VTABLE_INIT(this, Open_Failure);
|
---|
[91e52be] | 325 | this.ostream = &ostream;
|
---|
[8d321f9] | 326 | this.tag = 1;
|
---|
[91e52be] | 327 | }
|
---|
[8d321f9] | 328 | void ?{}( Open_Failure & this, ifstream & istream ) {
|
---|
| 329 | VTABLE_INIT(this, Open_Failure);
|
---|
[91e52be] | 330 | this.istream = &istream;
|
---|
[8d321f9] | 331 | this.tag = 0;
|
---|
[91e52be] | 332 | }
|
---|
[8d321f9] | 333 | const char * Open_Failure_msg(Open_Failure * this) {
|
---|
| 334 | return "Open_Failure";
|
---|
[91e52be] | 335 | }
|
---|
[8d321f9] | 336 | VTABLE_INSTANCE(Open_Failure)(Open_Failure_msg);
|
---|
| 337 | void throwOpen_Failure( ofstream & ostream ) {
|
---|
| 338 | Open_Failure exc = { ostream };
|
---|
[91e52be] | 339 | }
|
---|
[8d321f9] | 340 | void throwOpen_Failure( ifstream & istream ) {
|
---|
| 341 | Open_Failure exc = { istream };
|
---|
[91e52be] | 342 | }
|
---|
| 343 |
|
---|
[86bd7c1f] | 344 | // Local Variables: //
|
---|
| 345 | // tab-width: 4 //
|
---|
| 346 | // End: //
|
---|