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