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