| 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 : Thu Jul  6 18:38:25 2017
 | 
|---|
| 13 | // Update Count     : 251
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "fstream"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | extern "C" {
 | 
|---|
| 19 | #include <stdio.h>                                                                              // vfprintf, vfscanf
 | 
|---|
| 20 | #include <stdlib.h>                                                                             // exit
 | 
|---|
| 21 | #include <stdarg.h>                                                                             // varargs
 | 
|---|
| 22 | #include <string.h>                                                                             // strlen
 | 
|---|
| 23 | #include <stdbool.h>                                                                    // true/false
 | 
|---|
| 24 | #include <float.h>                                                                              // DBL_DIG, LDBL_DIG
 | 
|---|
| 25 | #include <complex.h>                                                                    // creal, cimag
 | 
|---|
| 26 | }
 | 
|---|
| 27 | #include "assert"
 | 
|---|
| 28 | 
 | 
|---|
| 29 | #define IO_MSG "I/O error: "
 | 
|---|
| 30 | 
 | 
|---|
| 31 | void ?{}( ofstream & this, void * file, _Bool sepDefault, _Bool sepOnOff, const char * separator, const char * tupleSeparator ) {
 | 
|---|
| 32 |         this.file = file;
 | 
|---|
| 33 |         this.sepDefault = sepDefault;
 | 
|---|
| 34 |         this.sepOnOff = sepOnOff;
 | 
|---|
| 35 |         sepSet( &this, separator );
 | 
|---|
| 36 |         sepSetCur( &this, sepGet( &this ) );
 | 
|---|
| 37 |         sepSetTuple( &this, tupleSeparator );
 | 
|---|
| 38 | }
 | 
|---|
| 39 | 
 | 
|---|
| 40 | // private
 | 
|---|
| 41 | _Bool sepPrt( ofstream * os ) { setNL( os, false ); return os->sepOnOff; }
 | 
|---|
| 42 | void sepReset( ofstream * os ) { os->sepOnOff = os->sepDefault; }
 | 
|---|
| 43 | void sepReset( ofstream * os, _Bool reset ) { os->sepDefault = reset; os->sepOnOff = os->sepDefault; }
 | 
|---|
| 44 | const char * sepGetCur( ofstream * os ) { return os->sepCur; }
 | 
|---|
| 45 | void sepSetCur( ofstream * os, const char * sepCur ) { os->sepCur = sepCur; }
 | 
|---|
| 46 | _Bool getNL( ofstream * os ) { return os->sawNL; }
 | 
|---|
| 47 | void setNL( ofstream * os, _Bool state ) { os->sawNL = state; }
 | 
|---|
| 48 | 
 | 
|---|
| 49 | // public
 | 
|---|
| 50 | void sepOn( ofstream * os ) { os->sepOnOff = ! getNL( os ); }
 | 
|---|
| 51 | void sepOff( ofstream * os ) { os->sepOnOff = false; }
 | 
|---|
| 52 | 
 | 
|---|
| 53 | _Bool sepDisable( ofstream *os ) {
 | 
|---|
| 54 |         _Bool temp = os->sepDefault;
 | 
|---|
| 55 |         os->sepDefault = false;
 | 
|---|
| 56 |         sepReset( os );
 | 
|---|
| 57 |         return temp;
 | 
|---|
| 58 | } // sepDisable
 | 
|---|
| 59 | 
 | 
|---|
| 60 | _Bool sepEnable( ofstream *os ) {
 | 
|---|
| 61 |         _Bool temp = os->sepDefault;
 | 
|---|
| 62 |         os->sepDefault = true;
 | 
|---|
| 63 |         if ( os->sepOnOff ) sepReset( os );                                     // start of line ?
 | 
|---|
| 64 |         return temp;
 | 
|---|
| 65 | } // sepEnable
 | 
|---|
| 66 | 
 | 
|---|
| 67 | const char * sepGet( ofstream * os ) { return os->separator; }
 | 
|---|
| 68 | void sepSet( ofstream * os, const char * s ) {
 | 
|---|
| 69 |         assert( s );
 | 
|---|
| 70 |         strncpy( os->separator, s, sepSize - 1 );
 | 
|---|
| 71 |         os->separator[sepSize - 1] = '\0';
 | 
|---|
| 72 | } // sepSet
 | 
|---|
| 73 | 
 | 
|---|
| 74 | const char * sepGetTuple( ofstream * os ) { return os->tupleSeparator; }
 | 
|---|
| 75 | void sepSetTuple( ofstream * os, const char * s ) {
 | 
|---|
| 76 |         assert( s );
 | 
|---|
| 77 |         strncpy( os->tupleSeparator, s, sepSize - 1 );
 | 
|---|
| 78 |         os->tupleSeparator[sepSize - 1] = '\0';
 | 
|---|
| 79 | } // sepSet
 | 
|---|
| 80 | 
 | 
|---|
| 81 | int fail( ofstream * os ) {
 | 
|---|
| 82 |         return ferror( (FILE *)(os->file) );
 | 
|---|
| 83 | } // fail
 | 
|---|
| 84 | 
 | 
|---|
| 85 | int flush( ofstream * os ) {
 | 
|---|
| 86 |         return fflush( (FILE *)(os->file) );
 | 
|---|
| 87 | } // flush
 | 
|---|
| 88 | 
 | 
|---|
| 89 | void open( ofstream * os, const char * name, const char * mode ) {
 | 
|---|
| 90 |         FILE *file = fopen( name, mode );
 | 
|---|
| 91 |         if ( file == 0 ) {                                                                      // do not change unless successful
 | 
|---|
| 92 |                 fprintf( stderr, IO_MSG "open output file \"%s\", ", name );
 | 
|---|
| 93 |                 perror( 0 );
 | 
|---|
| 94 |                 exit( EXIT_FAILURE );
 | 
|---|
| 95 |         } // if
 | 
|---|
| 96 |         ?{}( *os, file, true, false, " ", ", " );
 | 
|---|
| 97 | } // open
 | 
|---|
| 98 | 
 | 
|---|
| 99 | void close( ofstream * os ) {
 | 
|---|
| 100 |         if ( (FILE *)(os->file) == stdout || (FILE *)(os->file) == stderr ) return;
 | 
|---|
| 101 | 
 | 
|---|
| 102 |         if ( fclose( (FILE *)(os->file) ) == EOF ) {
 | 
|---|
| 103 |                 perror( IO_MSG "close output" );
 | 
|---|
| 104 |         } // if
 | 
|---|
| 105 | } // close
 | 
|---|
| 106 | 
 | 
|---|
| 107 | ofstream * write( ofstream * os, const char * data, unsigned long int size ) {
 | 
|---|
| 108 |         if ( fail( os ) ) {
 | 
|---|
| 109 |                 fprintf( stderr, "attempt write I/O on failed stream\n" );
 | 
|---|
| 110 |                 exit( EXIT_FAILURE );
 | 
|---|
| 111 |         } // if
 | 
|---|
| 112 | 
 | 
|---|
| 113 |         if ( fwrite( data, 1, size, (FILE *)(os->file) ) != size ) {
 | 
|---|
| 114 |                 perror( IO_MSG "write" );
 | 
|---|
| 115 |                 exit( EXIT_FAILURE );
 | 
|---|
| 116 |         } // if
 | 
|---|
| 117 |         return os;
 | 
|---|
| 118 | } // write
 | 
|---|
| 119 | 
 | 
|---|
| 120 | int fmt( ofstream * os, const char format[], ... ) {
 | 
|---|
| 121 |         va_list args;
 | 
|---|
| 122 |         va_start( args, format );
 | 
|---|
| 123 |         int len = vfprintf( (FILE *)(os->file), format, args );
 | 
|---|
| 124 |         if ( len == EOF ) {
 | 
|---|
| 125 |                 if ( ferror( (FILE *)(os->file) ) ) {
 | 
|---|
| 126 |                         fprintf( stderr, "invalid write\n" );
 | 
|---|
| 127 |                         exit( EXIT_FAILURE );
 | 
|---|
| 128 |                 } // if
 | 
|---|
| 129 |         } // if
 | 
|---|
| 130 |         va_end( args );
 | 
|---|
| 131 | 
 | 
|---|
| 132 |         sepReset( os );                                                                         // reset separator
 | 
|---|
| 133 |         return len;
 | 
|---|
| 134 | } // fmt
 | 
|---|
| 135 | 
 | 
|---|
| 136 | static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), true, false, " ", ", " };
 | 
|---|
| 137 | ofstream *sout = &soutFile;
 | 
|---|
| 138 | static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), true, false, " ", ", " };
 | 
|---|
| 139 | ofstream *serr = &serrFile;
 | 
|---|
| 140 | 
 | 
|---|
| 141 | 
 | 
|---|
| 142 | //---------------------------------------
 | 
|---|
| 143 | 
 | 
|---|
| 144 | 
 | 
|---|
| 145 | int fail( ifstream * is ) {
 | 
|---|
| 146 |         return ferror( (FILE *)(is->file) );
 | 
|---|
| 147 | } // fail
 | 
|---|
| 148 | 
 | 
|---|
| 149 | int eof( ifstream * is ) {
 | 
|---|
| 150 |         return feof( (FILE *)(is->file) );
 | 
|---|
| 151 | } // eof
 | 
|---|
| 152 | 
 | 
|---|
| 153 | void open( ifstream * is, const char * name, const char * mode ) {
 | 
|---|
| 154 |         FILE *file = fopen( name, mode );
 | 
|---|
| 155 |         if ( file == 0 ) {                                                                      // do not change unless successful
 | 
|---|
| 156 |                 fprintf( stderr, IO_MSG "open input file \"%s\", ", name );
 | 
|---|
| 157 |                 perror( 0 );
 | 
|---|
| 158 |                 exit( EXIT_FAILURE );
 | 
|---|
| 159 |         } // if
 | 
|---|
| 160 |         is->file = file;
 | 
|---|
| 161 | } // open
 | 
|---|
| 162 | 
 | 
|---|
| 163 | void close( ifstream * is ) {
 | 
|---|
| 164 |         if ( (FILE *)(is->file) == stdin ) return;
 | 
|---|
| 165 | 
 | 
|---|
| 166 |         if ( fclose( (FILE *)(is->file) ) == EOF ) {
 | 
|---|
| 167 |                 perror( IO_MSG "close input" );
 | 
|---|
| 168 |         } // if
 | 
|---|
| 169 | } // close
 | 
|---|
| 170 | 
 | 
|---|
| 171 | ifstream * read( ifstream * is, char * data, unsigned long int size ) {
 | 
|---|
| 172 |         if ( fail( is ) ) {
 | 
|---|
| 173 |                 fprintf( stderr, "attempt read I/O on failed stream\n" );
 | 
|---|
| 174 |                 exit( EXIT_FAILURE );
 | 
|---|
| 175 |         } // if
 | 
|---|
| 176 | 
 | 
|---|
| 177 |         if ( fread( data, size, 1, (FILE *)(is->file) ) == 0 ) {
 | 
|---|
| 178 |                 perror( IO_MSG "read" );
 | 
|---|
| 179 |                 exit( EXIT_FAILURE );
 | 
|---|
| 180 |         } // if
 | 
|---|
| 181 |         return is;
 | 
|---|
| 182 | } // read
 | 
|---|
| 183 | 
 | 
|---|
| 184 | ifstream *ungetc( ifstream * is, char c ) {
 | 
|---|
| 185 |         if ( fail( is ) ) {
 | 
|---|
| 186 |                 fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
 | 
|---|
| 187 |                 exit( EXIT_FAILURE );
 | 
|---|
| 188 |         } // if
 | 
|---|
| 189 | 
 | 
|---|
| 190 |         if ( ungetc( c, (FILE *)(is->file) ) == EOF ) {
 | 
|---|
| 191 |                 perror( IO_MSG "ungetc" );
 | 
|---|
| 192 |                 exit( EXIT_FAILURE );
 | 
|---|
| 193 |         } // if
 | 
|---|
| 194 |         return is;
 | 
|---|
| 195 | } // ungetc
 | 
|---|
| 196 | 
 | 
|---|
| 197 | int fmt( ifstream * is, const char format[], ... ) {
 | 
|---|
| 198 |         va_list args;
 | 
|---|
| 199 | 
 | 
|---|
| 200 |         va_start( args, format );
 | 
|---|
| 201 |         int len = vfscanf( (FILE *)(is->file), format, args );
 | 
|---|
| 202 |         if ( len == EOF ) {
 | 
|---|
| 203 |                 if ( ferror( (FILE *)(is->file) ) ) {
 | 
|---|
| 204 |                         fprintf( stderr, "invalid read\n" );
 | 
|---|
| 205 |                         exit( EXIT_FAILURE );
 | 
|---|
| 206 |                 } // if
 | 
|---|
| 207 |         } // if
 | 
|---|
| 208 |         va_end( args );
 | 
|---|
| 209 |         return len;
 | 
|---|
| 210 | } // fmt
 | 
|---|
| 211 | 
 | 
|---|
| 212 | 
 | 
|---|
| 213 | static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
 | 
|---|
| 214 | ifstream *sin = &sinFile;
 | 
|---|
| 215 | 
 | 
|---|
| 216 | // Local Variables: //
 | 
|---|
| 217 | // tab-width: 4 //
 | 
|---|
| 218 | // End: //
 | 
|---|