[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 | //
|
---|
| 7 | // fstream.c --
|
---|
| 8 | //
|
---|
[90c3b1c] | 9 | // Author : Peter A. Buhr
|
---|
[86bd7c1f] | 10 | // Created On : Wed May 27 17:56:53 2015
|
---|
| 11 | // Last Modified By : Peter A. Buhr
|
---|
[90c3b1c] | 12 | // Last Modified On : Mon Feb 29 18:41:10 2016
|
---|
| 13 | // Update Count : 162
|
---|
[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
|
---|
| 23 | #include <float.h> // DBL_DIG, LDBL_DIG
|
---|
| 24 | #include <complex.h> // creal, cimag
|
---|
[51b73452] | 25 | }
|
---|
| 26 |
|
---|
[6ba0659] | 27 | #define IO_MSG "I/O error "
|
---|
| 28 |
|
---|
[90c3b1c] | 29 | _Bool sepPrt( ofstream * os ) { return os->separate == 1; }
|
---|
| 30 | void sepOn( ofstream * os ) { if ( os->separate != 2 ) os->separate = 1; }
|
---|
| 31 | void sepOff( ofstream * os ) { if ( os->separate != 2 ) os->separate = 0; }
|
---|
| 32 | void sepSet( ofstream * os, const char * s ) {
|
---|
| 33 | strncpy( &(os->separator[0]), s, separateSize - 1 );
|
---|
| 34 | os->separator[separateSize - 1] = '\0';
|
---|
| 35 | } // sepSet
|
---|
| 36 | const char * sepGet( ofstream * os ) { return &(os->separator[0]); }
|
---|
| 37 | void sepDisable( ofstream *os ) { os->separate = 2; }
|
---|
| 38 | void sepEnable( ofstream *os ) { os->separate = 0; }
|
---|
| 39 |
|
---|
[6ba0659] | 40 | int fail( ofstream * os ) {
|
---|
[90c3b1c] | 41 | return ferror( (FILE *)(os->file) );
|
---|
[6ba0659] | 42 | } // fail
|
---|
| 43 |
|
---|
| 44 | int flush( ofstream * os ) {
|
---|
[90c3b1c] | 45 | return fflush( (FILE *)(os->file) );
|
---|
[6ba0659] | 46 | } // flush
|
---|
| 47 |
|
---|
[90c3b1c] | 48 | void open( ofstream * os, const char * name, const char * mode ) {
|
---|
| 49 | FILE *file = fopen( name, mode );
|
---|
| 50 | if ( file == 0 ) { // do not change unless successful
|
---|
[6ba0659] | 51 | perror( IO_MSG "open output" );
|
---|
| 52 | exit( EXIT_FAILURE );
|
---|
| 53 | } // if
|
---|
[90c3b1c] | 54 | os->file = file;
|
---|
| 55 | sepOff( os );
|
---|
| 56 | sepSet( os, " " );
|
---|
[6ba0659] | 57 | } // open
|
---|
| 58 |
|
---|
| 59 | void close( ofstream * os ) {
|
---|
[90c3b1c] | 60 | if ( (FILE *)(os->file) == stdout || (FILE *)(os->file) == stderr ) return;
|
---|
[6ba0659] | 61 |
|
---|
[90c3b1c] | 62 | if ( fclose( (FILE *)(os->file) ) == EOF ) {
|
---|
[6ba0659] | 63 | perror( IO_MSG "close output" );
|
---|
| 64 | } // if
|
---|
| 65 | } // close
|
---|
| 66 |
|
---|
[90c3b1c] | 67 | ofstream * write( ofstream * os, const char * data, unsigned long int size ) {
|
---|
[6ba0659] | 68 | if ( fail( os ) ) {
|
---|
| 69 | fprintf( stderr, "attempt write I/O on failed stream\n" );
|
---|
| 70 | exit( EXIT_FAILURE );
|
---|
| 71 | } // if
|
---|
| 72 |
|
---|
[90c3b1c] | 73 | if ( fwrite( data, 1, size, (FILE *)(os->file) ) != size ) {
|
---|
[6ba0659] | 74 | perror( IO_MSG "write" );
|
---|
| 75 | exit( EXIT_FAILURE );
|
---|
[839ccbb] | 76 | } // if
|
---|
[86bd7c1f] | 77 | return os;
|
---|
[839ccbb] | 78 | } // write
|
---|
[51b73452] | 79 |
|
---|
[90c3b1c] | 80 | int prtfmt( ofstream * os, const char fmt[], ... ) {
|
---|
| 81 | va_list args;
|
---|
| 82 |
|
---|
| 83 | va_start( args, fmt );
|
---|
| 84 | int len = vfprintf( (FILE *)(os->file), fmt, args );
|
---|
| 85 | if ( len == EOF ) {
|
---|
| 86 | if ( ferror( (FILE *)(os->file) ) ) {
|
---|
| 87 | fprintf( stderr, "invalid write\n" );
|
---|
| 88 | exit( EXIT_FAILURE );
|
---|
| 89 | } // if
|
---|
| 90 | } // if
|
---|
| 91 | va_end( args );
|
---|
| 92 | return len;
|
---|
| 93 | } // prtfmt
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), 0, { ' ', '\0' } };
|
---|
[6ba0659] | 97 | ofstream *sout = &soutFile;
|
---|
[90c3b1c] | 98 | static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), 0, { ' ', '\0' } };
|
---|
[6ba0659] | 99 | ofstream *serr = &serrFile;
|
---|
[51b73452] | 100 |
|
---|
[90c3b1c] | 101 |
|
---|
[6ba0659] | 102 | //---------------------------------------
|
---|
[51b73452] | 103 |
|
---|
| 104 |
|
---|
[6ba0659] | 105 | int fail( ifstream * is ) {
|
---|
[90c3b1c] | 106 | return ferror( (FILE *)(is->file) );
|
---|
[6ba0659] | 107 | } // fail
|
---|
| 108 |
|
---|
| 109 | int eof( ifstream * is ) {
|
---|
[90c3b1c] | 110 | return feof( (FILE *)(is->file) );
|
---|
[6ba0659] | 111 | } // eof
|
---|
| 112 |
|
---|
[90c3b1c] | 113 | void open( ifstream * is, const char * name, const char * mode ) {
|
---|
| 114 | FILE *t = fopen( name, mode );
|
---|
| 115 | if ( t == 0 ) { // do not change unless successful
|
---|
| 116 | perror( IO_MSG "open input" );
|
---|
| 117 | exit( EXIT_FAILURE );
|
---|
[6ba0659] | 118 | } // if
|
---|
[90c3b1c] | 119 | is->file = t;
|
---|
| 120 | } // open
|
---|
[6ba0659] | 121 |
|
---|
[90c3b1c] | 122 | void close( ifstream * is ) {
|
---|
| 123 | if ( (FILE *)(is->file) == stdin ) return;
|
---|
| 124 |
|
---|
| 125 | if ( fclose( (FILE *)(is->file) ) == EOF ) {
|
---|
| 126 | perror( IO_MSG "close input" );
|
---|
| 127 | } // if
|
---|
| 128 | } // close
|
---|
| 129 |
|
---|
| 130 | ifstream * read( ifstream * is, char * data, unsigned long int size ) {
|
---|
[6ba0659] | 131 | if ( fail( is ) ) {
|
---|
| 132 | fprintf( stderr, "attempt read I/O on failed stream\n" );
|
---|
| 133 | exit( EXIT_FAILURE );
|
---|
| 134 | } // if
|
---|
| 135 |
|
---|
[90c3b1c] | 136 | if ( fread( data, size, 1, (FILE *)(is->file) ) == 0 ) {
|
---|
[6ba0659] | 137 | perror( IO_MSG "read" );
|
---|
| 138 | exit( EXIT_FAILURE );
|
---|
| 139 | } // if
|
---|
[86bd7c1f] | 140 | return is;
|
---|
[6ba0659] | 141 | } // read
|
---|
| 142 |
|
---|
| 143 | ifstream *ungetc( ifstream * is, char c ) {
|
---|
| 144 | if ( fail( is ) ) {
|
---|
| 145 | fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
|
---|
| 146 | exit( EXIT_FAILURE );
|
---|
| 147 | } // if
|
---|
[51b73452] | 148 |
|
---|
[90c3b1c] | 149 | if ( ungetc( c, (FILE *)(is->file) ) == EOF ) {
|
---|
[6ba0659] | 150 | perror( IO_MSG "ungetc" );
|
---|
| 151 | exit( EXIT_FAILURE );
|
---|
| 152 | } // if
|
---|
| 153 | return is;
|
---|
| 154 | } // ungetc
|
---|
[51b73452] | 155 |
|
---|
[90c3b1c] | 156 | int scanfmt( ifstream * is, const char fmt[], ... ) {
|
---|
| 157 | va_list args;
|
---|
[51b73452] | 158 |
|
---|
[90c3b1c] | 159 | va_start( args, fmt );
|
---|
| 160 | int len = vfscanf( (FILE *)(is->file), fmt, args );
|
---|
| 161 | if ( len == EOF ) {
|
---|
| 162 | if ( ferror( (FILE *)(is->file) ) ) {
|
---|
| 163 | fprintf( stderr, "invalid read\n" );
|
---|
| 164 | exit( EXIT_FAILURE );
|
---|
| 165 | } // if
|
---|
| 166 | } // if
|
---|
| 167 | va_end( args );
|
---|
| 168 | return len;
|
---|
| 169 | } // prtfmt
|
---|
[51b73452] | 170 |
|
---|
| 171 |
|
---|
[6ba0659] | 172 | static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
|
---|
| 173 | ifstream *sin = &sinFile;
|
---|
[86bd7c1f] | 174 |
|
---|
| 175 | // Local Variables: //
|
---|
| 176 | // tab-width: 4 //
|
---|
| 177 | // compile-command: "cfa fstream.c" //
|
---|
| 178 | // End: //
|
---|