| [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 | 
|---|
| [8d321f9] | 12 | // Last Modified On : Fri Jun 19 16:24:54 2020 | 
|---|
|  | 13 | // Update Count     : 384 | 
|---|
| [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 |  | 
|---|
| [65240bb] | 27 |  | 
|---|
| [8d321f9] | 28 | // *********************************** ofstream *********************************** | 
|---|
| [65240bb] | 29 |  | 
|---|
|  | 30 |  | 
|---|
| [53ba273] | 31 | #define IO_MSG "I/O error: " | 
|---|
| [6ba0659] | 32 |  | 
|---|
| [5cb2b8c] | 33 | void ?{}( ofstream & os, void * file ) { | 
|---|
| [d1a9ff5] | 34 | os.$file = file; | 
|---|
|  | 35 | os.$sepDefault = true; | 
|---|
|  | 36 | os.$sepOnOff = false; | 
|---|
|  | 37 | os.$nlOnOff = true; | 
|---|
|  | 38 | os.$prt = false; | 
|---|
|  | 39 | os.$sawNL = false; | 
|---|
|  | 40 | $sepSetCur( os, sepGet( os ) ); | 
|---|
| [5cb2b8c] | 41 | sepSet( os, " " ); | 
|---|
|  | 42 | sepSetTuple( os, ", " ); | 
|---|
| [65240bb] | 43 | } // ?{} | 
|---|
| [0583064b] | 44 |  | 
|---|
| [9ebd778] | 45 | // private | 
|---|
| [d1a9ff5] | 46 | bool $sepPrt( ofstream & os ) { $setNL( os, false ); return os.$sepOnOff; } | 
|---|
|  | 47 | void $sepReset( ofstream & os ) { os.$sepOnOff = os.$sepDefault; } | 
|---|
|  | 48 | void $sepReset( ofstream & os, bool reset ) { os.$sepDefault = reset; os.$sepOnOff = os.$sepDefault; } | 
|---|
|  | 49 | const char * $sepGetCur( ofstream & os ) { return os.$sepCur; } | 
|---|
|  | 50 | void $sepSetCur( ofstream & os, const char sepCur[] ) { os.$sepCur = sepCur; } | 
|---|
|  | 51 | bool $getNL( ofstream & os ) { return os.$sawNL; } | 
|---|
|  | 52 | void $setNL( ofstream & os, bool state ) { os.$sawNL = state; } | 
|---|
|  | 53 | bool $getANL( ofstream & os ) { return os.$nlOnOff; } | 
|---|
|  | 54 | bool $getPrt( ofstream & os ) { return os.$prt; } | 
|---|
|  | 55 | void $setPrt( ofstream & os, bool state ) { os.$prt = state; } | 
|---|
| [829c907] | 56 |  | 
|---|
| [9ebd778] | 57 | // public | 
|---|
| [d1a9ff5] | 58 | void ?{}( ofstream & os ) { os.$file = 0p; } | 
|---|
| [829c907] | 59 |  | 
|---|
| [e3fea42] | 60 | void ?{}( ofstream & os, const char name[], const char mode[] ) { | 
|---|
| [09687aa] | 61 | open( os, name, mode ); | 
|---|
| [65240bb] | 62 | } // ?{} | 
|---|
|  | 63 |  | 
|---|
| [e3fea42] | 64 | void ?{}( ofstream & os, const char name[] ) { | 
|---|
| [8da74119] | 65 | open( os, name, "w" ); | 
|---|
| [65240bb] | 66 | } // ?{} | 
|---|
| [09687aa] | 67 |  | 
|---|
| [4cae032] | 68 | void ^?{}( ofstream & os ) { | 
|---|
|  | 69 | close( os ); | 
|---|
|  | 70 | } // ^?{} | 
|---|
|  | 71 |  | 
|---|
| [d1a9ff5] | 72 | void sepOn( ofstream & os ) { os.$sepOnOff = ! $getNL( os ); } | 
|---|
|  | 73 | void sepOff( ofstream & os ) { os.$sepOnOff = false; } | 
|---|
| [09687aa] | 74 |  | 
|---|
| [93c2e0a] | 75 | bool sepDisable( ofstream & os ) { | 
|---|
| [d1a9ff5] | 76 | bool temp = os.$sepDefault; | 
|---|
|  | 77 | os.$sepDefault = false; | 
|---|
|  | 78 | $sepReset( os ); | 
|---|
| [53ba273] | 79 | return temp; | 
|---|
|  | 80 | } // sepDisable | 
|---|
| [6152c81] | 81 |  | 
|---|
| [93c2e0a] | 82 | bool sepEnable( ofstream & os ) { | 
|---|
| [d1a9ff5] | 83 | bool temp = os.$sepDefault; | 
|---|
|  | 84 | os.$sepDefault = true; | 
|---|
|  | 85 | if ( os.$sepOnOff ) $sepReset( os );                            // start of line ? | 
|---|
| [53ba273] | 86 | return temp; | 
|---|
|  | 87 | } // sepEnable | 
|---|
| [90c3b1c] | 88 |  | 
|---|
| [d1a9ff5] | 89 | void nlOn( ofstream & os ) { os.$nlOnOff = true; } | 
|---|
|  | 90 | void nlOff( ofstream & os ) { os.$nlOnOff = false; } | 
|---|
| [200fcb3] | 91 |  | 
|---|
| [d1a9ff5] | 92 | const char * sepGet( ofstream & os ) { return os.$separator; } | 
|---|
| [e3fea42] | 93 | void sepSet( ofstream & os, const char s[] ) { | 
|---|
| [9ebd778] | 94 | assert( s ); | 
|---|
| [d1a9ff5] | 95 | strncpy( os.$separator, s, sepSize - 1 ); | 
|---|
|  | 96 | os.$separator[sepSize - 1] = '\0'; | 
|---|
| [9ebd778] | 97 | } // sepSet | 
|---|
|  | 98 |  | 
|---|
| [d1a9ff5] | 99 | const char * sepGetTuple( ofstream & os ) { return os.$tupleSeparator; } | 
|---|
| [e3fea42] | 100 | void sepSetTuple( ofstream & os, const char s[] ) { | 
|---|
| [9ebd778] | 101 | assert( s ); | 
|---|
| [d1a9ff5] | 102 | strncpy( os.$tupleSeparator, s, sepSize - 1 ); | 
|---|
|  | 103 | os.$tupleSeparator[sepSize - 1] = '\0'; | 
|---|
| [9ebd778] | 104 | } // sepSet | 
|---|
|  | 105 |  | 
|---|
| [65240bb] | 106 | void ends( ofstream & os ) { | 
|---|
| [d1a9ff5] | 107 | if ( $getANL( os ) ) nl( os ); | 
|---|
|  | 108 | else $setPrt( os, false );                                                      // turn off | 
|---|
| [65240bb] | 109 | if ( &os == &exit ) exit( EXIT_FAILURE ); | 
|---|
|  | 110 | if ( &os == &abort ) abort(); | 
|---|
|  | 111 | } // ends | 
|---|
|  | 112 |  | 
|---|
| [09687aa] | 113 | int fail( ofstream & os ) { | 
|---|
| [d1a9ff5] | 114 | return os.$file == 0 || ferror( (FILE *)(os.$file) ); | 
|---|
| [6ba0659] | 115 | } // fail | 
|---|
|  | 116 |  | 
|---|
| [09687aa] | 117 | int flush( ofstream & os ) { | 
|---|
| [d1a9ff5] | 118 | return fflush( (FILE *)(os.$file) ); | 
|---|
| [6ba0659] | 119 | } // flush | 
|---|
|  | 120 |  | 
|---|
| [e3fea42] | 121 | void open( ofstream & os, const char name[], const char mode[] ) { | 
|---|
| [5cb2b8c] | 122 | FILE * file = fopen( name, mode ); | 
|---|
| [93c2e0a] | 123 | #ifdef __CFA_DEBUG__ | 
|---|
| [d1a9ff5] | 124 | if ( file == 0p ) { | 
|---|
| [8d321f9] | 125 | throw (Open_Failure){ os }; | 
|---|
|  | 126 | // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno ); | 
|---|
| [93c2e0a] | 127 | } // if | 
|---|
|  | 128 | #endif // __CFA_DEBUG__ | 
|---|
| [5cb2b8c] | 129 | (os){ file }; | 
|---|
| [6ba0659] | 130 | } // open | 
|---|
|  | 131 |  | 
|---|
| [e3fea42] | 132 | void open( ofstream & os, const char name[] ) { | 
|---|
| [8da74119] | 133 | open( os, name, "w" ); | 
|---|
|  | 134 | } // open | 
|---|
|  | 135 |  | 
|---|
| [09687aa] | 136 | void close( ofstream & os ) { | 
|---|
| [91e52be] | 137 | if ( (FILE *)(os.$file) == 0p ) return; | 
|---|
|  | 138 | if ( (FILE *)(os.$file) == (FILE *)stdout || (FILE *)(os.$file) == (FILE *)stderr ) return; | 
|---|
| [6ba0659] | 139 |  | 
|---|
| [d1a9ff5] | 140 | if ( fclose( (FILE *)(os.$file) ) == EOF ) { | 
|---|
| [ff2a33e] | 141 | abort | IO_MSG "close output" | nl | strerror( errno ); | 
|---|
| [356189a] | 142 | } // if | 
|---|
| [91e52be] | 143 | os.$file = 0p; | 
|---|
| [6ba0659] | 144 | } // close | 
|---|
|  | 145 |  | 
|---|
| [e3fea42] | 146 | ofstream & write( ofstream & os, const char data[], size_t size ) { | 
|---|
| [6ba0659] | 147 | if ( fail( os ) ) { | 
|---|
| [ff2a33e] | 148 | abort | IO_MSG "attempt write I/O on failed stream"; | 
|---|
| [6ba0659] | 149 | } // if | 
|---|
|  | 150 |  | 
|---|
| [d1a9ff5] | 151 | if ( fwrite( data, 1, size, (FILE *)(os.$file) ) != size ) { | 
|---|
| [ff2a33e] | 152 | abort | IO_MSG "write" | nl | strerror( errno ); | 
|---|
| [839ccbb] | 153 | } // if | 
|---|
| [86bd7c1f] | 154 | return os; | 
|---|
| [839ccbb] | 155 | } // write | 
|---|
| [51b73452] | 156 |  | 
|---|
| [09687aa] | 157 | int fmt( ofstream & os, const char format[], ... ) { | 
|---|
| [5d125e4] | 158 | va_list args; | 
|---|
| [829c907] | 159 | va_start( args, format ); | 
|---|
| [d1a9ff5] | 160 | int len = vfprintf( (FILE *)(os.$file), format, args ); | 
|---|
| [90c3b1c] | 161 | if ( len == EOF ) { | 
|---|
| [d1a9ff5] | 162 | if ( ferror( (FILE *)(os.$file) ) ) { | 
|---|
| [ff2a33e] | 163 | abort | IO_MSG "invalid write"; | 
|---|
| [90c3b1c] | 164 | } // if | 
|---|
|  | 165 | } // if | 
|---|
| [5d125e4] | 166 | va_end( args ); | 
|---|
| [b72bad4f] | 167 |  | 
|---|
| [d1a9ff5] | 168 | $setPrt( os, true );                                                            // called in output cascade | 
|---|
|  | 169 | $sepReset( os );                                                                        // reset separator | 
|---|
| [90c3b1c] | 170 | return len; | 
|---|
| [829c907] | 171 | } // fmt | 
|---|
|  | 172 |  | 
|---|
| [fd8f88f] | 173 | static ofstream soutFile = { (FILE *)stdout }; | 
|---|
| [a87d40b] | 174 | ofstream & sout = soutFile, & stdout = soutFile; | 
|---|
| [fd8f88f] | 175 | static ofstream serrFile = { (FILE *)stderr }; | 
|---|
| [a87d40b] | 176 | ofstream & serr = serrFile, & stderr = serrFile; | 
|---|
| [51b73452] | 177 |  | 
|---|
| [fd8f88f] | 178 | static ofstream exitFile = { (FILE *)stdout }; | 
|---|
| [65240bb] | 179 | ofstream & exit = exitFile; | 
|---|
| [fd8f88f] | 180 | static ofstream abortFile = { (FILE *)stderr }; | 
|---|
| [65240bb] | 181 | ofstream & abort = abortFile; | 
|---|
| [5cb2b8c] | 182 |  | 
|---|
| [90c3b1c] | 183 |  | 
|---|
| [8d321f9] | 184 | // *********************************** ifstream *********************************** | 
|---|
| [65240bb] | 185 |  | 
|---|
| [51b73452] | 186 |  | 
|---|
| [09687aa] | 187 | // private | 
|---|
|  | 188 | void ?{}( ifstream & is, void * file ) { | 
|---|
| [d1a9ff5] | 189 | is.$file = file; | 
|---|
|  | 190 | is.$nlOnOff = false; | 
|---|
| [65240bb] | 191 | } // ?{} | 
|---|
| [09687aa] | 192 |  | 
|---|
|  | 193 | // public | 
|---|
| [d1a9ff5] | 194 | void ?{}( ifstream & is ) { is.$file = 0p; } | 
|---|
| [51b73452] | 195 |  | 
|---|
| [e3fea42] | 196 | void ?{}( ifstream & is, const char name[], const char mode[] ) { | 
|---|
| [8da74119] | 197 | open( is, name, mode ); | 
|---|
| [65240bb] | 198 | } // ?{} | 
|---|
|  | 199 |  | 
|---|
| [e3fea42] | 200 | void ?{}( ifstream & is, const char name[] ) { | 
|---|
| [8da74119] | 201 | open( is, name, "r" ); | 
|---|
| [65240bb] | 202 | } // ?{} | 
|---|
| [09687aa] | 203 |  | 
|---|
| [4cae032] | 204 | void ^?{}( ifstream & is ) { | 
|---|
|  | 205 | close( is ); | 
|---|
|  | 206 | } // ^?{} | 
|---|
|  | 207 |  | 
|---|
| [d1a9ff5] | 208 | void nlOn( ifstream & os ) { os.$nlOnOff = true; } | 
|---|
|  | 209 | void nlOff( ifstream & os ) { os.$nlOnOff = false; } | 
|---|
|  | 210 | bool getANL( ifstream & os ) { return os.$nlOnOff; } | 
|---|
| [0efb269] | 211 |  | 
|---|
| [09687aa] | 212 | int fail( ifstream & is ) { | 
|---|
| [d1a9ff5] | 213 | return is.$file == 0p || ferror( (FILE *)(is.$file) ); | 
|---|
| [6ba0659] | 214 | } // fail | 
|---|
|  | 215 |  | 
|---|
| [09687aa] | 216 | int eof( ifstream & is ) { | 
|---|
| [d1a9ff5] | 217 | return feof( (FILE *)(is.$file) ); | 
|---|
| [6ba0659] | 218 | } // eof | 
|---|
|  | 219 |  | 
|---|
| [e3fea42] | 220 | void open( ifstream & is, const char name[], const char mode[] ) { | 
|---|
| [8a25be9] | 221 | FILE * file = fopen( name, mode ); | 
|---|
| [93c2e0a] | 222 | #ifdef __CFA_DEBUG__ | 
|---|
| [d1a9ff5] | 223 | if ( file == 0p ) { | 
|---|
| [8d321f9] | 224 | throw (Open_Failure){ is }; | 
|---|
|  | 225 | // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno ); | 
|---|
| [93c2e0a] | 226 | } // if | 
|---|
|  | 227 | #endif // __CFA_DEBUG__ | 
|---|
| [d1a9ff5] | 228 | is.$file = file; | 
|---|
| [90c3b1c] | 229 | } // open | 
|---|
| [6ba0659] | 230 |  | 
|---|
| [e3fea42] | 231 | void open( ifstream & is, const char name[] ) { | 
|---|
| [8da74119] | 232 | open( is, name, "r" ); | 
|---|
|  | 233 | } // open | 
|---|
|  | 234 |  | 
|---|
| [09687aa] | 235 | void close( ifstream & is ) { | 
|---|
| [91e52be] | 236 | if ( (FILE *)(is.$file) == 0p ) return; | 
|---|
|  | 237 | if ( (FILE *)(is.$file) == (FILE *)stdin ) return; | 
|---|
| [90c3b1c] | 238 |  | 
|---|
| [d1a9ff5] | 239 | if ( fclose( (FILE *)(is.$file) ) == EOF ) { | 
|---|
| [ff2a33e] | 240 | abort | IO_MSG "close input" | nl | strerror( errno ); | 
|---|
| [356189a] | 241 | } // if | 
|---|
| [91e52be] | 242 | is.$file = 0p; | 
|---|
| [90c3b1c] | 243 | } // close | 
|---|
|  | 244 |  | 
|---|
| [91d766d] | 245 | ifstream & read( ifstream & is, char * data, size_t size ) { | 
|---|
| [6ba0659] | 246 | if ( fail( is ) ) { | 
|---|
| [ff2a33e] | 247 | abort | IO_MSG "attempt read I/O on failed stream"; | 
|---|
| [6ba0659] | 248 | } // if | 
|---|
|  | 249 |  | 
|---|
| [d1a9ff5] | 250 | if ( fread( data, size, 1, (FILE *)(is.$file) ) == 0 ) { | 
|---|
| [ff2a33e] | 251 | abort | IO_MSG "read" | nl | strerror( errno ); | 
|---|
| [6ba0659] | 252 | } // if | 
|---|
| [86bd7c1f] | 253 | return is; | 
|---|
| [6ba0659] | 254 | } // read | 
|---|
| [356189a] | 255 |  | 
|---|
| [09687aa] | 256 | ifstream &ungetc( ifstream & is, char c ) { | 
|---|
| [6ba0659] | 257 | if ( fail( is ) ) { | 
|---|
| [ff2a33e] | 258 | abort | IO_MSG "attempt ungetc I/O on failed stream"; | 
|---|
| [6ba0659] | 259 | } // if | 
|---|
| [51b73452] | 260 |  | 
|---|
| [d1a9ff5] | 261 | if ( ungetc( c, (FILE *)(is.$file) ) == EOF ) { | 
|---|
| [ff2a33e] | 262 | abort | IO_MSG "ungetc" | nl | strerror( errno ); | 
|---|
| [6ba0659] | 263 | } // if | 
|---|
|  | 264 | return is; | 
|---|
|  | 265 | } // ungetc | 
|---|
| [51b73452] | 266 |  | 
|---|
| [09687aa] | 267 | int fmt( ifstream & is, const char format[], ... ) { | 
|---|
| [5d125e4] | 268 | va_list args; | 
|---|
| [51b73452] | 269 |  | 
|---|
| [829c907] | 270 | va_start( args, format ); | 
|---|
| [d1a9ff5] | 271 | int len = vfscanf( (FILE *)(is.$file), format, args ); | 
|---|
| [90c3b1c] | 272 | if ( len == EOF ) { | 
|---|
| [d1a9ff5] | 273 | if ( ferror( (FILE *)(is.$file) ) ) { | 
|---|
| [ff2a33e] | 274 | abort | IO_MSG "invalid read"; | 
|---|
| [90c3b1c] | 275 | } // if | 
|---|
|  | 276 | } // if | 
|---|
| [5d125e4] | 277 | va_end( args ); | 
|---|
| [90c3b1c] | 278 | return len; | 
|---|
| [829c907] | 279 | } // fmt | 
|---|
| [51b73452] | 280 |  | 
|---|
| [fd8f88f] | 281 | static ifstream sinFile = { (FILE *)stdin }; | 
|---|
| [a87d40b] | 282 | ifstream & sin = sinFile, & stdin = sinFile; | 
|---|
| [86bd7c1f] | 283 |  | 
|---|
| [91e52be] | 284 |  | 
|---|
| [8d321f9] | 285 | // *********************************** exceptions *********************************** | 
|---|
| [91e52be] | 286 |  | 
|---|
|  | 287 |  | 
|---|
| [8d321f9] | 288 | void ?{}( Open_Failure & this, ofstream & ostream ) { | 
|---|
|  | 289 | VTABLE_INIT(this, Open_Failure); | 
|---|
| [91e52be] | 290 | this.ostream = &ostream; | 
|---|
| [8d321f9] | 291 | this.tag = 1; | 
|---|
| [91e52be] | 292 | } | 
|---|
| [8d321f9] | 293 | void ?{}( Open_Failure & this, ifstream & istream ) { | 
|---|
|  | 294 | VTABLE_INIT(this, Open_Failure); | 
|---|
| [91e52be] | 295 | this.istream = &istream; | 
|---|
| [8d321f9] | 296 | this.tag = 0; | 
|---|
| [91e52be] | 297 | } | 
|---|
| [8d321f9] | 298 | const char * Open_Failure_msg(Open_Failure * this) { | 
|---|
|  | 299 | return "Open_Failure"; | 
|---|
| [91e52be] | 300 | } | 
|---|
| [8d321f9] | 301 | VTABLE_INSTANCE(Open_Failure)(Open_Failure_msg); | 
|---|
|  | 302 | void throwOpen_Failure( ofstream & ostream ) { | 
|---|
|  | 303 | Open_Failure exc = { ostream }; | 
|---|
| [91e52be] | 304 | } | 
|---|
| [8d321f9] | 305 | void throwOpen_Failure( ifstream & istream ) { | 
|---|
|  | 306 | Open_Failure exc = { istream }; | 
|---|
| [91e52be] | 307 | } | 
|---|
|  | 308 |  | 
|---|
| [86bd7c1f] | 309 | // Local Variables: // | 
|---|
|  | 310 | // tab-width: 4 // | 
|---|
|  | 311 | // End: // | 
|---|