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