[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 |
---|
[93c2e0a] | 12 | // Last Modified On : Fri Aug 10 18:19:40 2018 |
---|
| 13 | // Update Count : 284 |
---|
[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 |
---|
[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> |
---|
[51b7345] | 26 | |
---|
[53ba273] | 27 | #define IO_MSG "I/O error: " |
---|
[6ba0659] | 28 | |
---|
[93c2e0a] | 29 | void ?{}( ofstream & os, void * file, bool sepDefault, bool sepOnOff, const char * separator, const char * tupleSeparator ) { |
---|
[09687aa] | 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 |
---|
[93c2e0a] | 39 | bool sepPrt( ofstream & os ) { setNL( os, false ); return os.sepOnOff; } |
---|
[09687aa] | 40 | void sepReset( ofstream & os ) { os.sepOnOff = os.sepDefault; } |
---|
[93c2e0a] | 41 | void sepReset( ofstream & os, bool reset ) { os.sepDefault = reset; os.sepOnOff = os.sepDefault; } |
---|
[09687aa] | 42 | const char * sepGetCur( ofstream & os ) { return os.sepCur; } |
---|
| 43 | void sepSetCur( ofstream & os, const char * sepCur ) { os.sepCur = sepCur; } |
---|
[93c2e0a] | 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 | |
---|
[93c2e0a] | 60 | bool sepDisable( ofstream & os ) { |
---|
| 61 | bool temp = os.sepDefault; |
---|
[09687aa] | 62 | os.sepDefault = false; |
---|
[53ba273] | 63 | sepReset( os ); |
---|
| 64 | return temp; |
---|
| 65 | } // sepDisable |
---|
[6152c81] | 66 | |
---|
[93c2e0a] | 67 | bool sepEnable( ofstream & os ) { |
---|
| 68 | bool temp = os.sepDefault; |
---|
[09687aa] | 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 ); |
---|
[93c2e0a] | 98 | #ifdef __CFA_DEBUG__ |
---|
| 99 | if ( file == 0 ) { |
---|
| 100 | fprintf( stderr, IO_MSG "open output file \"%s\", ", name ); |
---|
| 101 | perror( 0 ); |
---|
| 102 | exit( EXIT_FAILURE ); |
---|
| 103 | } // if |
---|
| 104 | #endif // __CFA_DEBUG__ |
---|
[09687aa] | 105 | (os){ file, true, false, " ", ", " }; |
---|
[6ba0659] | 106 | } // open |
---|
| 107 | |
---|
[8da74119] | 108 | void open( ofstream & os, const char * name ) { |
---|
| 109 | open( os, name, "w" ); |
---|
| 110 | } // open |
---|
| 111 | |
---|
[09687aa] | 112 | void close( ofstream & os ) { |
---|
| 113 | if ( (FILE *)(os.file) == stdout || (FILE *)(os.file) == stderr ) return; |
---|
[6ba0659] | 114 | |
---|
[09687aa] | 115 | if ( fclose( (FILE *)(os.file) ) == EOF ) { |
---|
[6ba0659] | 116 | perror( IO_MSG "close output" ); |
---|
[356189a] | 117 | } // if |
---|
[6ba0659] | 118 | } // close |
---|
| 119 | |
---|
[91d766d] | 120 | ofstream & write( ofstream & os, const char * data, size_t size ) { |
---|
[6ba0659] | 121 | if ( fail( os ) ) { |
---|
| 122 | fprintf( stderr, "attempt write I/O on failed stream\n" ); |
---|
| 123 | exit( EXIT_FAILURE ); |
---|
| 124 | } // if |
---|
| 125 | |
---|
[09687aa] | 126 | if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) { |
---|
[6ba0659] | 127 | perror( IO_MSG "write" ); |
---|
| 128 | exit( EXIT_FAILURE ); |
---|
[839ccbb] | 129 | } // if |
---|
[86bd7c1f] | 130 | return os; |
---|
[839ccbb] | 131 | } // write |
---|
[51b7345] | 132 | |
---|
[09687aa] | 133 | int fmt( ofstream & os, const char format[], ... ) { |
---|
[5d125e4] | 134 | va_list args; |
---|
[829c907] | 135 | va_start( args, format ); |
---|
[09687aa] | 136 | int len = vfprintf( (FILE *)(os.file), format, args ); |
---|
[90c3b1c] | 137 | if ( len == EOF ) { |
---|
[09687aa] | 138 | if ( ferror( (FILE *)(os.file) ) ) { |
---|
[90c3b1c] | 139 | fprintf( stderr, "invalid write\n" ); |
---|
| 140 | exit( EXIT_FAILURE ); |
---|
| 141 | } // if |
---|
| 142 | } // if |
---|
[5d125e4] | 143 | va_end( args ); |
---|
[b72bad4f] | 144 | |
---|
| 145 | sepReset( os ); // reset separator |
---|
[90c3b1c] | 146 | return len; |
---|
[829c907] | 147 | } // fmt |
---|
| 148 | |
---|
[d395012] | 149 | static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), true, false, " ", ", " }; |
---|
[09687aa] | 150 | ofstream & sout = soutFile; |
---|
[d395012] | 151 | static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), true, false, " ", ", " }; |
---|
[09687aa] | 152 | ofstream & serr = serrFile; |
---|
[51b7345] | 153 | |
---|
[90c3b1c] | 154 | |
---|
[6ba0659] | 155 | //--------------------------------------- |
---|
[51b7345] | 156 | |
---|
[09687aa] | 157 | // private |
---|
| 158 | void ?{}( ifstream & is, void * file ) { |
---|
| 159 | is.file = file; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | // public |
---|
[8da74119] | 163 | void ?{}( ifstream & is ) { is.file = 0; } |
---|
[51b7345] | 164 | |
---|
[8da74119] | 165 | void ?{}( ifstream & is, const char * name, const char * mode ) { |
---|
| 166 | open( is, name, mode ); |
---|
| 167 | } |
---|
[09687aa] | 168 | void ?{}( ifstream & is, const char * name ) { |
---|
[8da74119] | 169 | open( is, name, "r" ); |
---|
[09687aa] | 170 | } |
---|
| 171 | |
---|
| 172 | int fail( ifstream & is ) { |
---|
[8da74119] | 173 | return is.file == 0 || ferror( (FILE *)(is.file) ); |
---|
[6ba0659] | 174 | } // fail |
---|
| 175 | |
---|
[09687aa] | 176 | int eof( ifstream & is ) { |
---|
| 177 | return feof( (FILE *)(is.file) ); |
---|
[6ba0659] | 178 | } // eof |
---|
| 179 | |
---|
[8da74119] | 180 | void open( ifstream & is, const char * name, const char * mode ) { |
---|
| 181 | FILE *file = fopen( name, mode ); |
---|
[93c2e0a] | 182 | #ifdef __CFA_DEBUG__ |
---|
| 183 | if ( file == 0 ) { |
---|
| 184 | fprintf( stderr, IO_MSG "open input file \"%s\", ", name ); |
---|
| 185 | perror( 0 ); |
---|
| 186 | exit( EXIT_FAILURE ); |
---|
| 187 | } // if |
---|
| 188 | #endif // __CFA_DEBUG__ |
---|
[09687aa] | 189 | is.file = file; |
---|
[90c3b1c] | 190 | } // open |
---|
[6ba0659] | 191 | |
---|
[8da74119] | 192 | void open( ifstream & is, const char * name ) { |
---|
| 193 | open( is, name, "r" ); |
---|
| 194 | } // open |
---|
| 195 | |
---|
[09687aa] | 196 | void close( ifstream & is ) { |
---|
| 197 | if ( (FILE *)(is.file) == stdin ) return; |
---|
[90c3b1c] | 198 | |
---|
[09687aa] | 199 | if ( fclose( (FILE *)(is.file) ) == EOF ) { |
---|
[90c3b1c] | 200 | perror( IO_MSG "close input" ); |
---|
[356189a] | 201 | } // if |
---|
[90c3b1c] | 202 | } // close |
---|
| 203 | |
---|
[91d766d] | 204 | ifstream & read( ifstream & is, char * data, size_t size ) { |
---|
[6ba0659] | 205 | if ( fail( is ) ) { |
---|
| 206 | fprintf( stderr, "attempt read I/O on failed stream\n" ); |
---|
| 207 | exit( EXIT_FAILURE ); |
---|
| 208 | } // if |
---|
| 209 | |
---|
[09687aa] | 210 | if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) { |
---|
[6ba0659] | 211 | perror( IO_MSG "read" ); |
---|
| 212 | exit( EXIT_FAILURE ); |
---|
| 213 | } // if |
---|
[86bd7c1f] | 214 | return is; |
---|
[6ba0659] | 215 | } // read |
---|
[356189a] | 216 | |
---|
[09687aa] | 217 | ifstream &ungetc( ifstream & is, char c ) { |
---|
[6ba0659] | 218 | if ( fail( is ) ) { |
---|
| 219 | fprintf( stderr, "attempt ungetc I/O on failed stream\n" ); |
---|
| 220 | exit( EXIT_FAILURE ); |
---|
| 221 | } // if |
---|
[51b7345] | 222 | |
---|
[09687aa] | 223 | if ( ungetc( c, (FILE *)(is.file) ) == EOF ) { |
---|
[6ba0659] | 224 | perror( IO_MSG "ungetc" ); |
---|
| 225 | exit( EXIT_FAILURE ); |
---|
| 226 | } // if |
---|
| 227 | return is; |
---|
| 228 | } // ungetc |
---|
[51b7345] | 229 | |
---|
[09687aa] | 230 | int fmt( ifstream & is, const char format[], ... ) { |
---|
[5d125e4] | 231 | va_list args; |
---|
[51b7345] | 232 | |
---|
[829c907] | 233 | va_start( args, format ); |
---|
[09687aa] | 234 | int len = vfscanf( (FILE *)(is.file), format, args ); |
---|
[90c3b1c] | 235 | if ( len == EOF ) { |
---|
[09687aa] | 236 | if ( ferror( (FILE *)(is.file) ) ) { |
---|
[90c3b1c] | 237 | fprintf( stderr, "invalid read\n" ); |
---|
| 238 | exit( EXIT_FAILURE ); |
---|
| 239 | } // if |
---|
| 240 | } // if |
---|
[5d125e4] | 241 | va_end( args ); |
---|
[90c3b1c] | 242 | return len; |
---|
[829c907] | 243 | } // fmt |
---|
[51b7345] | 244 | |
---|
| 245 | |
---|
[6ba0659] | 246 | static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) }; |
---|
[09687aa] | 247 | ifstream & sin = sinFile; |
---|
[86bd7c1f] | 248 | |
---|
| 249 | // Local Variables: // |
---|
| 250 | // tab-width: 4 // |
---|
| 251 | // End: // |
---|