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