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