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