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