| 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 : Sat Dec  9 09:31:23 2017
 | 
|---|
| 13 | // Update Count     : 275
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "fstream"
 | 
|---|
| 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 |         // if ( file == 0 ) {                                                                   // do not change unless successful
 | 
|---|
| 99 |         //      fprintf( stderr, IO_MSG "open output file \"%s\", ", name );
 | 
|---|
| 100 |         //      perror( 0 );
 | 
|---|
| 101 |         //      exit( EXIT_FAILURE );
 | 
|---|
| 102 |         // } // if
 | 
|---|
| 103 |         (os){ file, true, false, " ", ", " };
 | 
|---|
| 104 | } // open
 | 
|---|
| 105 | 
 | 
|---|
| 106 | void open( ofstream & os, const char * name ) {
 | 
|---|
| 107 |         open( os, name, "w" );
 | 
|---|
| 108 | } // open
 | 
|---|
| 109 | 
 | 
|---|
| 110 | void close( ofstream & os ) {
 | 
|---|
| 111 |         if ( (FILE *)(os.file) == stdout || (FILE *)(os.file) == stderr ) return;
 | 
|---|
| 112 | 
 | 
|---|
| 113 |         if ( fclose( (FILE *)(os.file) ) == EOF ) {
 | 
|---|
| 114 |                 perror( IO_MSG "close output" );
 | 
|---|
| 115 |         } // if
 | 
|---|
| 116 | } // close
 | 
|---|
| 117 | 
 | 
|---|
| 118 | ofstream & write( ofstream & os, const char * data, unsigned long int size ) {
 | 
|---|
| 119 |         if ( fail( os ) ) {
 | 
|---|
| 120 |                 fprintf( stderr, "attempt write I/O on failed stream\n" );
 | 
|---|
| 121 |                 exit( EXIT_FAILURE );
 | 
|---|
| 122 |         } // if
 | 
|---|
| 123 | 
 | 
|---|
| 124 |         if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) {
 | 
|---|
| 125 |                 perror( IO_MSG "write" );
 | 
|---|
| 126 |                 exit( EXIT_FAILURE );
 | 
|---|
| 127 |         } // if
 | 
|---|
| 128 |         return os;
 | 
|---|
| 129 | } // write
 | 
|---|
| 130 | 
 | 
|---|
| 131 | int fmt( ofstream & os, const char format[], ... ) {
 | 
|---|
| 132 |         va_list args;
 | 
|---|
| 133 |         va_start( args, format );
 | 
|---|
| 134 |         int len = vfprintf( (FILE *)(os.file), format, args );
 | 
|---|
| 135 |         if ( len == EOF ) {
 | 
|---|
| 136 |                 if ( ferror( (FILE *)(os.file) ) ) {
 | 
|---|
| 137 |                         fprintf( stderr, "invalid write\n" );
 | 
|---|
| 138 |                         exit( EXIT_FAILURE );
 | 
|---|
| 139 |                 } // if
 | 
|---|
| 140 |         } // if
 | 
|---|
| 141 |         va_end( args );
 | 
|---|
| 142 | 
 | 
|---|
| 143 |         sepReset( os );                                                                         // reset separator
 | 
|---|
| 144 |         return len;
 | 
|---|
| 145 | } // fmt
 | 
|---|
| 146 | 
 | 
|---|
| 147 | static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), true, false, " ", ", " };
 | 
|---|
| 148 | ofstream & sout = soutFile;
 | 
|---|
| 149 | static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), true, false, " ", ", " };
 | 
|---|
| 150 | ofstream & serr = serrFile;
 | 
|---|
| 151 | 
 | 
|---|
| 152 | 
 | 
|---|
| 153 | //---------------------------------------
 | 
|---|
| 154 | 
 | 
|---|
| 155 | // private
 | 
|---|
| 156 | void ?{}( ifstream & is, void * file ) {
 | 
|---|
| 157 |         is.file = file;
 | 
|---|
| 158 | }
 | 
|---|
| 159 | 
 | 
|---|
| 160 | // public
 | 
|---|
| 161 | void ?{}( ifstream & is ) {     is.file = 0; }
 | 
|---|
| 162 | 
 | 
|---|
| 163 | void ?{}( ifstream & is, const char * name, const char * mode ) {
 | 
|---|
| 164 |         open( is, name, mode );
 | 
|---|
| 165 | }
 | 
|---|
| 166 | void ?{}( ifstream & is, const char * name ) {
 | 
|---|
| 167 |         open( is, name, "r" );
 | 
|---|
| 168 | }
 | 
|---|
| 169 | 
 | 
|---|
| 170 | int fail( ifstream & is ) {
 | 
|---|
| 171 |         return is.file == 0 || ferror( (FILE *)(is.file) );
 | 
|---|
| 172 | } // fail
 | 
|---|
| 173 | 
 | 
|---|
| 174 | int eof( ifstream & is ) {
 | 
|---|
| 175 |         return feof( (FILE *)(is.file) );
 | 
|---|
| 176 | } // eof
 | 
|---|
| 177 | 
 | 
|---|
| 178 | void open( ifstream & is, const char * name, const char * mode ) {
 | 
|---|
| 179 |         FILE *file = fopen( name, mode );
 | 
|---|
| 180 |         // if ( file == 0 ) {                                                                   // do not change unless successful
 | 
|---|
| 181 |         //      fprintf( stderr, IO_MSG "open input file \"%s\", ", name );
 | 
|---|
| 182 |         //      perror( 0 );
 | 
|---|
| 183 |         //      exit( EXIT_FAILURE );
 | 
|---|
| 184 |         // } // if
 | 
|---|
| 185 |         is.file = file;
 | 
|---|
| 186 | } // open
 | 
|---|
| 187 | 
 | 
|---|
| 188 | void open( ifstream & is, const char * name ) {
 | 
|---|
| 189 |         open( is, name, "r" );
 | 
|---|
| 190 | } // open
 | 
|---|
| 191 | 
 | 
|---|
| 192 | void close( ifstream & is ) {
 | 
|---|
| 193 |         if ( (FILE *)(is.file) == stdin ) return;
 | 
|---|
| 194 | 
 | 
|---|
| 195 |         if ( fclose( (FILE *)(is.file) ) == EOF ) {
 | 
|---|
| 196 |                 perror( IO_MSG "close input" );
 | 
|---|
| 197 |         } // if
 | 
|---|
| 198 | } // close
 | 
|---|
| 199 | 
 | 
|---|
| 200 | ifstream & read( ifstream & is, char * data, unsigned long int size ) {
 | 
|---|
| 201 |         if ( fail( is ) ) {
 | 
|---|
| 202 |                 fprintf( stderr, "attempt read I/O on failed stream\n" );
 | 
|---|
| 203 |                 exit( EXIT_FAILURE );
 | 
|---|
| 204 |         } // if
 | 
|---|
| 205 | 
 | 
|---|
| 206 |         if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
 | 
|---|
| 207 |                 perror( IO_MSG "read" );
 | 
|---|
| 208 |                 exit( EXIT_FAILURE );
 | 
|---|
| 209 |         } // if
 | 
|---|
| 210 |         return is;
 | 
|---|
| 211 | } // read
 | 
|---|
| 212 | 
 | 
|---|
| 213 | ifstream &ungetc( ifstream & is, char c ) {
 | 
|---|
| 214 |         if ( fail( is ) ) {
 | 
|---|
| 215 |                 fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
 | 
|---|
| 216 |                 exit( EXIT_FAILURE );
 | 
|---|
| 217 |         } // if
 | 
|---|
| 218 | 
 | 
|---|
| 219 |         if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
 | 
|---|
| 220 |                 perror( IO_MSG "ungetc" );
 | 
|---|
| 221 |                 exit( EXIT_FAILURE );
 | 
|---|
| 222 |         } // if
 | 
|---|
| 223 |         return is;
 | 
|---|
| 224 | } // ungetc
 | 
|---|
| 225 | 
 | 
|---|
| 226 | int fmt( ifstream & is, const char format[], ... ) {
 | 
|---|
| 227 |         va_list args;
 | 
|---|
| 228 | 
 | 
|---|
| 229 |         va_start( args, format );
 | 
|---|
| 230 |         int len = vfscanf( (FILE *)(is.file), format, args );
 | 
|---|
| 231 |         if ( len == EOF ) {
 | 
|---|
| 232 |                 if ( ferror( (FILE *)(is.file) ) ) {
 | 
|---|
| 233 |                         fprintf( stderr, "invalid read\n" );
 | 
|---|
| 234 |                         exit( EXIT_FAILURE );
 | 
|---|
| 235 |                 } // if
 | 
|---|
| 236 |         } // if
 | 
|---|
| 237 |         va_end( args );
 | 
|---|
| 238 |         return len;
 | 
|---|
| 239 | } // fmt
 | 
|---|
| 240 | 
 | 
|---|
| 241 | 
 | 
|---|
| 242 | static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
 | 
|---|
| 243 | ifstream & sin = sinFile;
 | 
|---|
| 244 | 
 | 
|---|
| 245 | // Local Variables: //
 | 
|---|
| 246 | // tab-width: 4 //
 | 
|---|
| 247 | // End: //
 | 
|---|