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