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