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