| 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 : Fri Aug 10 18:19:40 2018 | 
|---|
| 13 | // Update Count     : 284 | 
|---|
| 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 <stdbool.h>                                                                    // true/false | 
|---|
| 23 | #include <float.h>                                                                              // DBL_DIG, LDBL_DIG | 
|---|
| 24 | #include <complex.h>                                                                    // creal, cimag | 
|---|
| 25 | #include <assert.h> | 
|---|
| 26 |  | 
|---|
| 27 | #define IO_MSG "I/O error: " | 
|---|
| 28 |  | 
|---|
| 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 ); | 
|---|
| 36 | } | 
|---|
| 37 |  | 
|---|
| 38 | // private | 
|---|
| 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; } | 
|---|
| 46 |  | 
|---|
| 47 | // public | 
|---|
| 48 | void ?{}( ofstream & os ) { os.file = 0; } | 
|---|
| 49 |  | 
|---|
| 50 | void ?{}( ofstream & os, const char * name, const char * mode ) { | 
|---|
| 51 | open( os, name, mode ); | 
|---|
| 52 | } | 
|---|
| 53 | void ?{}( ofstream & os, const char * name ) { | 
|---|
| 54 | open( os, name, "w" ); | 
|---|
| 55 | } | 
|---|
| 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; | 
|---|
| 63 | sepReset( os ); | 
|---|
| 64 | return temp; | 
|---|
| 65 | } // sepDisable | 
|---|
| 66 |  | 
|---|
| 67 | bool sepEnable( ofstream & os ) { | 
|---|
| 68 | bool temp = os.sepDefault; | 
|---|
| 69 | os.sepDefault = true; | 
|---|
| 70 | if ( os.sepOnOff ) sepReset( os );                                      // start of line ? | 
|---|
| 71 | return temp; | 
|---|
| 72 | } // sepEnable | 
|---|
| 73 |  | 
|---|
| 74 | const char * sepGet( ofstream & os ) { return os.separator; } | 
|---|
| 75 | void sepSet( ofstream & os, const char * s ) { | 
|---|
| 76 | assert( s ); | 
|---|
| 77 | strncpy( os.separator, s, sepSize - 1 ); | 
|---|
| 78 | os.separator[sepSize - 1] = '\0'; | 
|---|
| 79 | } // sepSet | 
|---|
| 80 |  | 
|---|
| 81 | const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator; } | 
|---|
| 82 | void sepSetTuple( ofstream & os, const char * s ) { | 
|---|
| 83 | assert( s ); | 
|---|
| 84 | strncpy( os.tupleSeparator, s, sepSize - 1 ); | 
|---|
| 85 | os.tupleSeparator[sepSize - 1] = '\0'; | 
|---|
| 86 | } // sepSet | 
|---|
| 87 |  | 
|---|
| 88 | int fail( ofstream & os ) { | 
|---|
| 89 | return os.file == 0 || ferror( (FILE *)(os.file) ); | 
|---|
| 90 | } // fail | 
|---|
| 91 |  | 
|---|
| 92 | int flush( ofstream & os ) { | 
|---|
| 93 | return fflush( (FILE *)(os.file) ); | 
|---|
| 94 | } // flush | 
|---|
| 95 |  | 
|---|
| 96 | void open( ofstream & os, const char * name, const char * mode ) { | 
|---|
| 97 | FILE *file = fopen( name, mode ); | 
|---|
| 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__ | 
|---|
| 105 | (os){ file, true, false, " ", ", " }; | 
|---|
| 106 | } // open | 
|---|
| 107 |  | 
|---|
| 108 | void open( ofstream & os, const char * name ) { | 
|---|
| 109 | open( os, name, "w" ); | 
|---|
| 110 | } // open | 
|---|
| 111 |  | 
|---|
| 112 | void close( ofstream & os ) { | 
|---|
| 113 | if ( (FILE *)(os.file) == stdout || (FILE *)(os.file) == stderr ) return; | 
|---|
| 114 |  | 
|---|
| 115 | if ( fclose( (FILE *)(os.file) ) == EOF ) { | 
|---|
| 116 | perror( IO_MSG "close output" ); | 
|---|
| 117 | } // if | 
|---|
| 118 | } // close | 
|---|
| 119 |  | 
|---|
| 120 | ofstream & write( ofstream & os, const char * data, size_t size ) { | 
|---|
| 121 | if ( fail( os ) ) { | 
|---|
| 122 | fprintf( stderr, "attempt write I/O on failed stream\n" ); | 
|---|
| 123 | exit( EXIT_FAILURE ); | 
|---|
| 124 | } // if | 
|---|
| 125 |  | 
|---|
| 126 | if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) { | 
|---|
| 127 | perror( IO_MSG "write" ); | 
|---|
| 128 | exit( EXIT_FAILURE ); | 
|---|
| 129 | } // if | 
|---|
| 130 | return os; | 
|---|
| 131 | } // write | 
|---|
| 132 |  | 
|---|
| 133 | int fmt( ofstream & os, const char format[], ... ) { | 
|---|
| 134 | va_list args; | 
|---|
| 135 | va_start( args, format ); | 
|---|
| 136 | int len = vfprintf( (FILE *)(os.file), format, args ); | 
|---|
| 137 | if ( len == EOF ) { | 
|---|
| 138 | if ( ferror( (FILE *)(os.file) ) ) { | 
|---|
| 139 | fprintf( stderr, "invalid write\n" ); | 
|---|
| 140 | exit( EXIT_FAILURE ); | 
|---|
| 141 | } // if | 
|---|
| 142 | } // if | 
|---|
| 143 | va_end( args ); | 
|---|
| 144 |  | 
|---|
| 145 | sepReset( os );                                                                         // reset separator | 
|---|
| 146 | return len; | 
|---|
| 147 | } // fmt | 
|---|
| 148 |  | 
|---|
| 149 | static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), true, false, " ", ", " }; | 
|---|
| 150 | ofstream & sout = soutFile; | 
|---|
| 151 | static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), true, false, " ", ", " }; | 
|---|
| 152 | ofstream & serr = serrFile; | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 | //--------------------------------------- | 
|---|
| 156 |  | 
|---|
| 157 | // private | 
|---|
| 158 | void ?{}( ifstream & is, void * file ) { | 
|---|
| 159 | is.file = file; | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | // public | 
|---|
| 163 | void ?{}( ifstream & is ) {     is.file = 0; } | 
|---|
| 164 |  | 
|---|
| 165 | void ?{}( ifstream & is, const char * name, const char * mode ) { | 
|---|
| 166 | open( is, name, mode ); | 
|---|
| 167 | } | 
|---|
| 168 | void ?{}( ifstream & is, const char * name ) { | 
|---|
| 169 | open( is, name, "r" ); | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | int fail( ifstream & is ) { | 
|---|
| 173 | return is.file == 0 || ferror( (FILE *)(is.file) ); | 
|---|
| 174 | } // fail | 
|---|
| 175 |  | 
|---|
| 176 | int eof( ifstream & is ) { | 
|---|
| 177 | return feof( (FILE *)(is.file) ); | 
|---|
| 178 | } // eof | 
|---|
| 179 |  | 
|---|
| 180 | void open( ifstream & is, const char * name, const char * mode ) { | 
|---|
| 181 | FILE *file = fopen( name, mode ); | 
|---|
| 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__ | 
|---|
| 189 | is.file = file; | 
|---|
| 190 | } // open | 
|---|
| 191 |  | 
|---|
| 192 | void open( ifstream & is, const char * name ) { | 
|---|
| 193 | open( is, name, "r" ); | 
|---|
| 194 | } // open | 
|---|
| 195 |  | 
|---|
| 196 | void close( ifstream & is ) { | 
|---|
| 197 | if ( (FILE *)(is.file) == stdin ) return; | 
|---|
| 198 |  | 
|---|
| 199 | if ( fclose( (FILE *)(is.file) ) == EOF ) { | 
|---|
| 200 | perror( IO_MSG "close input" ); | 
|---|
| 201 | } // if | 
|---|
| 202 | } // close | 
|---|
| 203 |  | 
|---|
| 204 | ifstream & read( ifstream & is, char * data, size_t size ) { | 
|---|
| 205 | if ( fail( is ) ) { | 
|---|
| 206 | fprintf( stderr, "attempt read I/O on failed stream\n" ); | 
|---|
| 207 | exit( EXIT_FAILURE ); | 
|---|
| 208 | } // if | 
|---|
| 209 |  | 
|---|
| 210 | if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) { | 
|---|
| 211 | perror( IO_MSG "read" ); | 
|---|
| 212 | exit( EXIT_FAILURE ); | 
|---|
| 213 | } // if | 
|---|
| 214 | return is; | 
|---|
| 215 | } // read | 
|---|
| 216 |  | 
|---|
| 217 | ifstream &ungetc( ifstream & is, char c ) { | 
|---|
| 218 | if ( fail( is ) ) { | 
|---|
| 219 | fprintf( stderr, "attempt ungetc I/O on failed stream\n" ); | 
|---|
| 220 | exit( EXIT_FAILURE ); | 
|---|
| 221 | } // if | 
|---|
| 222 |  | 
|---|
| 223 | if ( ungetc( c, (FILE *)(is.file) ) == EOF ) { | 
|---|
| 224 | perror( IO_MSG "ungetc" ); | 
|---|
| 225 | exit( EXIT_FAILURE ); | 
|---|
| 226 | } // if | 
|---|
| 227 | return is; | 
|---|
| 228 | } // ungetc | 
|---|
| 229 |  | 
|---|
| 230 | int fmt( ifstream & is, const char format[], ... ) { | 
|---|
| 231 | va_list args; | 
|---|
| 232 |  | 
|---|
| 233 | va_start( args, format ); | 
|---|
| 234 | int len = vfscanf( (FILE *)(is.file), format, args ); | 
|---|
| 235 | if ( len == EOF ) { | 
|---|
| 236 | if ( ferror( (FILE *)(is.file) ) ) { | 
|---|
| 237 | fprintf( stderr, "invalid read\n" ); | 
|---|
| 238 | exit( EXIT_FAILURE ); | 
|---|
| 239 | } // if | 
|---|
| 240 | } // if | 
|---|
| 241 | va_end( args ); | 
|---|
| 242 | return len; | 
|---|
| 243 | } // fmt | 
|---|
| 244 |  | 
|---|
| 245 |  | 
|---|
| 246 | static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) }; | 
|---|
| 247 | ifstream & sin = sinFile; | 
|---|
| 248 |  | 
|---|
| 249 | // Local Variables: // | 
|---|
| 250 | // tab-width: 4 // | 
|---|
| 251 | // End: // | 
|---|