[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
|
---|
[4cae032] | 12 | // Last Modified On : Fri Nov 29 06:56:46 2019
|
---|
| 13 | // Update Count : 355
|
---|
[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 |
|
---|
[65240bb] | 27 |
|
---|
| 28 | //*********************************** ofstream ***********************************
|
---|
| 29 |
|
---|
| 30 |
|
---|
[53ba273] | 31 | #define IO_MSG "I/O error: "
|
---|
[6ba0659] | 32 |
|
---|
[5cb2b8c] | 33 | void ?{}( ofstream & os, void * file ) {
|
---|
[09687aa] | 34 | os.file = file;
|
---|
[5cb2b8c] | 35 | os.sepDefault = true;
|
---|
| 36 | os.sepOnOff = false;
|
---|
| 37 | os.nlOnOff = true;
|
---|
| 38 | os.prt = false;
|
---|
[0efb269] | 39 | os.sawNL = false;
|
---|
[5cb2b8c] | 40 | sepSet( os, " " );
|
---|
[09687aa] | 41 | sepSetCur( os, sepGet( os ) );
|
---|
[5cb2b8c] | 42 | sepSetTuple( os, ", " );
|
---|
[65240bb] | 43 | } // ?{}
|
---|
[0583064b] | 44 |
|
---|
[9ebd778] | 45 | // private
|
---|
[93c2e0a] | 46 | bool sepPrt( ofstream & os ) { setNL( os, false ); return os.sepOnOff; }
|
---|
[09687aa] | 47 | void sepReset( ofstream & os ) { os.sepOnOff = os.sepDefault; }
|
---|
[93c2e0a] | 48 | void sepReset( ofstream & os, bool reset ) { os.sepDefault = reset; os.sepOnOff = os.sepDefault; }
|
---|
[09687aa] | 49 | const char * sepGetCur( ofstream & os ) { return os.sepCur; }
|
---|
| 50 | void sepSetCur( ofstream & os, const char * sepCur ) { os.sepCur = sepCur; }
|
---|
[93c2e0a] | 51 | bool getNL( ofstream & os ) { return os.sawNL; }
|
---|
| 52 | void setNL( ofstream & os, bool state ) { os.sawNL = state; }
|
---|
[200fcb3] | 53 | bool getANL( ofstream & os ) { return os.nlOnOff; }
|
---|
[5ea5b28] | 54 | bool getPrt( ofstream & os ) { return os.prt; }
|
---|
| 55 | void setPrt( ofstream & os, bool state ) { os.prt = state; }
|
---|
[829c907] | 56 |
|
---|
[9ebd778] | 57 | // public
|
---|
[8da74119] | 58 | void ?{}( ofstream & os ) { os.file = 0; }
|
---|
[829c907] | 59 |
|
---|
[09687aa] | 60 | void ?{}( ofstream & os, const char * name, const char * mode ) {
|
---|
| 61 | open( os, name, mode );
|
---|
[65240bb] | 62 | } // ?{}
|
---|
| 63 |
|
---|
[8da74119] | 64 | void ?{}( ofstream & os, const char * name ) {
|
---|
| 65 | open( os, name, "w" );
|
---|
[65240bb] | 66 | } // ?{}
|
---|
[09687aa] | 67 |
|
---|
[4cae032] | 68 | void ^?{}( ofstream & os ) {
|
---|
| 69 | close( os );
|
---|
| 70 | } // ^?{}
|
---|
| 71 |
|
---|
[09687aa] | 72 | void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); }
|
---|
| 73 | void sepOff( ofstream & os ) { os.sepOnOff = false; }
|
---|
| 74 |
|
---|
[93c2e0a] | 75 | bool sepDisable( ofstream & os ) {
|
---|
| 76 | bool temp = os.sepDefault;
|
---|
[09687aa] | 77 | os.sepDefault = false;
|
---|
[53ba273] | 78 | sepReset( os );
|
---|
| 79 | return temp;
|
---|
| 80 | } // sepDisable
|
---|
[6152c81] | 81 |
|
---|
[93c2e0a] | 82 | bool sepEnable( ofstream & os ) {
|
---|
| 83 | bool temp = os.sepDefault;
|
---|
[09687aa] | 84 | os.sepDefault = true;
|
---|
| 85 | if ( os.sepOnOff ) sepReset( os ); // start of line ?
|
---|
[53ba273] | 86 | return temp;
|
---|
| 87 | } // sepEnable
|
---|
[90c3b1c] | 88 |
|
---|
[200fcb3] | 89 | void nlOn( ofstream & os ) { os.nlOnOff = true; }
|
---|
| 90 | void nlOff( ofstream & os ) { os.nlOnOff = false; }
|
---|
| 91 |
|
---|
[09687aa] | 92 | const char * sepGet( ofstream & os ) { return os.separator; }
|
---|
| 93 | void sepSet( ofstream & os, const char * s ) {
|
---|
[9ebd778] | 94 | assert( s );
|
---|
[09687aa] | 95 | strncpy( os.separator, s, sepSize - 1 );
|
---|
| 96 | os.separator[sepSize - 1] = '\0';
|
---|
[9ebd778] | 97 | } // sepSet
|
---|
| 98 |
|
---|
[09687aa] | 99 | const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator; }
|
---|
| 100 | void sepSetTuple( ofstream & os, const char * s ) {
|
---|
[9ebd778] | 101 | assert( s );
|
---|
[09687aa] | 102 | strncpy( os.tupleSeparator, s, sepSize - 1 );
|
---|
| 103 | os.tupleSeparator[sepSize - 1] = '\0';
|
---|
[9ebd778] | 104 | } // sepSet
|
---|
| 105 |
|
---|
[65240bb] | 106 | void ends( ofstream & os ) {
|
---|
| 107 | if ( getANL( os ) ) nl( os );
|
---|
| 108 | else setPrt( os, false ); // turn off
|
---|
| 109 | if ( &os == &exit ) exit( EXIT_FAILURE );
|
---|
| 110 | if ( &os == &abort ) abort();
|
---|
| 111 | } // ends
|
---|
| 112 |
|
---|
[09687aa] | 113 | int fail( ofstream & os ) {
|
---|
[8da74119] | 114 | return os.file == 0 || ferror( (FILE *)(os.file) );
|
---|
[6ba0659] | 115 | } // fail
|
---|
| 116 |
|
---|
[09687aa] | 117 | int flush( ofstream & os ) {
|
---|
| 118 | return fflush( (FILE *)(os.file) );
|
---|
[6ba0659] | 119 | } // flush
|
---|
| 120 |
|
---|
[09687aa] | 121 | void open( ofstream & os, const char * name, const char * mode ) {
|
---|
[5cb2b8c] | 122 | FILE * file = fopen( name, mode );
|
---|
[93c2e0a] | 123 | #ifdef __CFA_DEBUG__
|
---|
| 124 | if ( file == 0 ) {
|
---|
[ff2a33e] | 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 |
|
---|
[8da74119] | 131 | void open( ofstream & os, const char * name ) {
|
---|
| 132 | open( os, name, "w" );
|
---|
| 133 | } // open
|
---|
| 134 |
|
---|
[09687aa] | 135 | void close( ofstream & os ) {
|
---|
| 136 | if ( (FILE *)(os.file) == stdout || (FILE *)(os.file) == stderr ) return;
|
---|
[6ba0659] | 137 |
|
---|
[09687aa] | 138 | if ( fclose( (FILE *)(os.file) ) == EOF ) {
|
---|
[ff2a33e] | 139 | abort | IO_MSG "close output" | nl | strerror( errno );
|
---|
[356189a] | 140 | } // if
|
---|
[6ba0659] | 141 | } // close
|
---|
| 142 |
|
---|
[91d766d] | 143 | ofstream & write( ofstream & os, const char * data, size_t size ) {
|
---|
[6ba0659] | 144 | if ( fail( os ) ) {
|
---|
[ff2a33e] | 145 | abort | IO_MSG "attempt write I/O on failed stream";
|
---|
[6ba0659] | 146 | } // if
|
---|
| 147 |
|
---|
[09687aa] | 148 | if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) {
|
---|
[ff2a33e] | 149 | abort | IO_MSG "write" | nl | strerror( errno );
|
---|
[839ccbb] | 150 | } // if
|
---|
[86bd7c1f] | 151 | return os;
|
---|
[839ccbb] | 152 | } // write
|
---|
[51b73452] | 153 |
|
---|
[09687aa] | 154 | int fmt( ofstream & os, const char format[], ... ) {
|
---|
[5d125e4] | 155 | va_list args;
|
---|
[829c907] | 156 | va_start( args, format );
|
---|
[09687aa] | 157 | int len = vfprintf( (FILE *)(os.file), format, args );
|
---|
[90c3b1c] | 158 | if ( len == EOF ) {
|
---|
[09687aa] | 159 | if ( ferror( (FILE *)(os.file) ) ) {
|
---|
[ff2a33e] | 160 | abort | IO_MSG "invalid write";
|
---|
[90c3b1c] | 161 | } // if
|
---|
| 162 | } // if
|
---|
[5d125e4] | 163 | va_end( args );
|
---|
[b72bad4f] | 164 |
|
---|
[9d362a0] | 165 | setPrt( os, true ); // called in output cascade
|
---|
[b72bad4f] | 166 | sepReset( os ); // reset separator
|
---|
[90c3b1c] | 167 | return len;
|
---|
[829c907] | 168 | } // fmt
|
---|
| 169 |
|
---|
[fd8f88f] | 170 | static ofstream soutFile = { (FILE *)stdout };
|
---|
[a87d40b] | 171 | ofstream & sout = soutFile, & stdout = soutFile;
|
---|
[fd8f88f] | 172 | static ofstream serrFile = { (FILE *)stderr };
|
---|
[a87d40b] | 173 | ofstream & serr = serrFile, & stderr = serrFile;
|
---|
[51b73452] | 174 |
|
---|
[fd8f88f] | 175 | static ofstream exitFile = { (FILE *)stdout };
|
---|
[65240bb] | 176 | ofstream & exit = exitFile;
|
---|
[fd8f88f] | 177 | static ofstream abortFile = { (FILE *)stderr };
|
---|
[65240bb] | 178 | ofstream & abort = abortFile;
|
---|
[5cb2b8c] | 179 |
|
---|
[90c3b1c] | 180 |
|
---|
[65240bb] | 181 | //*********************************** ifstream ***********************************
|
---|
| 182 |
|
---|
[51b73452] | 183 |
|
---|
[09687aa] | 184 | // private
|
---|
| 185 | void ?{}( ifstream & is, void * file ) {
|
---|
| 186 | is.file = file;
|
---|
[0efb269] | 187 | is.nlOnOff = false;
|
---|
[65240bb] | 188 | } // ?{}
|
---|
[09687aa] | 189 |
|
---|
| 190 | // public
|
---|
[8da74119] | 191 | void ?{}( ifstream & is ) { is.file = 0; }
|
---|
[51b73452] | 192 |
|
---|
[8da74119] | 193 | void ?{}( ifstream & is, const char * name, const char * mode ) {
|
---|
| 194 | open( is, name, mode );
|
---|
[65240bb] | 195 | } // ?{}
|
---|
| 196 |
|
---|
[09687aa] | 197 | void ?{}( ifstream & is, const char * name ) {
|
---|
[8da74119] | 198 | open( is, name, "r" );
|
---|
[65240bb] | 199 | } // ?{}
|
---|
[09687aa] | 200 |
|
---|
[4cae032] | 201 | void ^?{}( ifstream & is ) {
|
---|
| 202 | close( is );
|
---|
| 203 | } // ^?{}
|
---|
| 204 |
|
---|
[0efb269] | 205 | void nlOn( ifstream & os ) { os.nlOnOff = true; }
|
---|
| 206 | void nlOff( ifstream & os ) { os.nlOnOff = false; }
|
---|
| 207 | bool getANL( ifstream & os ) { return os.nlOnOff; }
|
---|
| 208 |
|
---|
[09687aa] | 209 | int fail( ifstream & is ) {
|
---|
[8da74119] | 210 | return is.file == 0 || ferror( (FILE *)(is.file) );
|
---|
[6ba0659] | 211 | } // fail
|
---|
| 212 |
|
---|
[09687aa] | 213 | int eof( ifstream & is ) {
|
---|
| 214 | return feof( (FILE *)(is.file) );
|
---|
[6ba0659] | 215 | } // eof
|
---|
| 216 |
|
---|
[8da74119] | 217 | void open( ifstream & is, const char * name, const char * mode ) {
|
---|
[8a25be9] | 218 | FILE * file = fopen( name, mode );
|
---|
[93c2e0a] | 219 | #ifdef __CFA_DEBUG__
|
---|
| 220 | if ( file == 0 ) {
|
---|
[ff2a33e] | 221 | abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
|
---|
[93c2e0a] | 222 | } // if
|
---|
| 223 | #endif // __CFA_DEBUG__
|
---|
[09687aa] | 224 | is.file = file;
|
---|
[90c3b1c] | 225 | } // open
|
---|
[6ba0659] | 226 |
|
---|
[8da74119] | 227 | void open( ifstream & is, const char * name ) {
|
---|
| 228 | open( is, name, "r" );
|
---|
| 229 | } // open
|
---|
| 230 |
|
---|
[09687aa] | 231 | void close( ifstream & is ) {
|
---|
| 232 | if ( (FILE *)(is.file) == stdin ) return;
|
---|
[90c3b1c] | 233 |
|
---|
[09687aa] | 234 | if ( fclose( (FILE *)(is.file) ) == EOF ) {
|
---|
[ff2a33e] | 235 | abort | IO_MSG "close input" | nl | strerror( errno );
|
---|
[356189a] | 236 | } // if
|
---|
[90c3b1c] | 237 | } // close
|
---|
| 238 |
|
---|
[91d766d] | 239 | ifstream & read( ifstream & is, char * data, size_t size ) {
|
---|
[6ba0659] | 240 | if ( fail( is ) ) {
|
---|
[ff2a33e] | 241 | abort | IO_MSG "attempt read I/O on failed stream";
|
---|
[6ba0659] | 242 | } // if
|
---|
| 243 |
|
---|
[09687aa] | 244 | if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
|
---|
[ff2a33e] | 245 | abort | IO_MSG "read" | nl | strerror( errno );
|
---|
[6ba0659] | 246 | } // if
|
---|
[86bd7c1f] | 247 | return is;
|
---|
[6ba0659] | 248 | } // read
|
---|
[356189a] | 249 |
|
---|
[09687aa] | 250 | ifstream &ungetc( ifstream & is, char c ) {
|
---|
[6ba0659] | 251 | if ( fail( is ) ) {
|
---|
[ff2a33e] | 252 | abort | IO_MSG "attempt ungetc I/O on failed stream";
|
---|
[6ba0659] | 253 | } // if
|
---|
[51b73452] | 254 |
|
---|
[09687aa] | 255 | if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
|
---|
[ff2a33e] | 256 | abort | IO_MSG "ungetc" | nl | strerror( errno );
|
---|
[6ba0659] | 257 | } // if
|
---|
| 258 | return is;
|
---|
| 259 | } // ungetc
|
---|
[51b73452] | 260 |
|
---|
[09687aa] | 261 | int fmt( ifstream & is, const char format[], ... ) {
|
---|
[5d125e4] | 262 | va_list args;
|
---|
[51b73452] | 263 |
|
---|
[829c907] | 264 | va_start( args, format );
|
---|
[09687aa] | 265 | int len = vfscanf( (FILE *)(is.file), format, args );
|
---|
[90c3b1c] | 266 | if ( len == EOF ) {
|
---|
[09687aa] | 267 | if ( ferror( (FILE *)(is.file) ) ) {
|
---|
[ff2a33e] | 268 | abort | IO_MSG "invalid read";
|
---|
[90c3b1c] | 269 | } // if
|
---|
| 270 | } // if
|
---|
[5d125e4] | 271 | va_end( args );
|
---|
[90c3b1c] | 272 | return len;
|
---|
[829c907] | 273 | } // fmt
|
---|
[51b73452] | 274 |
|
---|
[fd8f88f] | 275 | static ifstream sinFile = { (FILE *)stdin };
|
---|
[a87d40b] | 276 | ifstream & sin = sinFile, & stdin = sinFile;
|
---|
[86bd7c1f] | 277 |
|
---|
| 278 | // Local Variables: //
|
---|
| 279 | // tab-width: 4 //
|
---|
| 280 | // End: //
|
---|