| [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
 | 
|---|
| [53ba273] | 12 | // Last Modified On : Wed Apr  6 17:55:27 2016
 | 
|---|
 | 13 | // Update Count     : 176
 | 
|---|
| [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 | 
 | 
|---|
| [53ba273] | 27 | #define IO_MSG "I/O error: "
 | 
|---|
| [6ba0659] | 28 | 
 | 
|---|
| [53ba273] | 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; }
 | 
|---|
| [90c3b1c] | 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]); }
 | 
|---|
| [53ba273] | 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
 | 
|---|
| [90c3b1c] | 51 | 
 | 
|---|
| [6ba0659] | 52 | int fail( ofstream * os ) {
 | 
|---|
| [90c3b1c] | 53 |         return ferror( (FILE *)(os->file) );
 | 
|---|
| [6ba0659] | 54 | } // fail
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 | int flush( ofstream * os ) {
 | 
|---|
| [90c3b1c] | 57 |         return fflush( (FILE *)(os->file) );
 | 
|---|
| [6ba0659] | 58 | } // flush
 | 
|---|
 | 59 | 
 | 
|---|
| [90c3b1c] | 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
 | 
|---|
| [53ba273] | 63 |                 fprintf( stderr, IO_MSG "open output file \"%s\", ", name );
 | 
|---|
 | 64 |                 perror( 0 );
 | 
|---|
| [6ba0659] | 65 |                 exit( EXIT_FAILURE );
 | 
|---|
 | 66 |         } // if
 | 
|---|
| [90c3b1c] | 67 |         os->file = file;
 | 
|---|
 | 68 |         sepOff( os );
 | 
|---|
 | 69 |         sepSet( os, " " );
 | 
|---|
| [6ba0659] | 70 | } // open
 | 
|---|
 | 71 | 
 | 
|---|
 | 72 | void close( ofstream * os ) {
 | 
|---|
| [90c3b1c] | 73 |         if ( (FILE *)(os->file) == stdout || (FILE *)(os->file) == stderr ) return;
 | 
|---|
| [6ba0659] | 74 | 
 | 
|---|
| [90c3b1c] | 75 |         if ( fclose( (FILE *)(os->file) ) == EOF ) {
 | 
|---|
| [6ba0659] | 76 |                 perror( IO_MSG "close output" );
 | 
|---|
 | 77 |         } // if 
 | 
|---|
 | 78 | } // close
 | 
|---|
 | 79 | 
 | 
|---|
| [90c3b1c] | 80 | ofstream * write( ofstream * os, const char * data, unsigned long int size ) {
 | 
|---|
| [6ba0659] | 81 |         if ( fail( os ) ) {
 | 
|---|
 | 82 |                 fprintf( stderr, "attempt write I/O on failed stream\n" );
 | 
|---|
 | 83 |                 exit( EXIT_FAILURE );
 | 
|---|
 | 84 |         } // if
 | 
|---|
 | 85 | 
 | 
|---|
| [90c3b1c] | 86 |         if ( fwrite( data, 1, size, (FILE *)(os->file) ) != size ) {
 | 
|---|
| [6ba0659] | 87 |                 perror( IO_MSG "write" );
 | 
|---|
 | 88 |                 exit( EXIT_FAILURE );
 | 
|---|
| [839ccbb] | 89 |         } // if
 | 
|---|
| [86bd7c1f] | 90 |         return os;
 | 
|---|
| [839ccbb] | 91 | } // write
 | 
|---|
| [51b73452] | 92 | 
 | 
|---|
| [90c3b1c] | 93 | int prtfmt( ofstream * os, const char fmt[], ... ) {
 | 
|---|
 | 94 |     va_list args;
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 |     va_start( args, fmt );
 | 
|---|
 | 97 |     int len = vfprintf( (FILE *)(os->file), fmt, args );
 | 
|---|
 | 98 |         if ( len == EOF ) {
 | 
|---|
 | 99 |                 if ( ferror( (FILE *)(os->file) ) ) {
 | 
|---|
 | 100 |                         fprintf( stderr, "invalid write\n" );
 | 
|---|
 | 101 |                         exit( EXIT_FAILURE );
 | 
|---|
 | 102 |                 } // if
 | 
|---|
 | 103 |         } // if
 | 
|---|
 | 104 |     va_end( args );
 | 
|---|
 | 105 |         return len;
 | 
|---|
 | 106 | } // prtfmt
 | 
|---|
 | 107 | 
 | 
|---|
 | 108 | 
 | 
|---|
| [53ba273] | 109 | static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), 1, 0, { ' ', '\0' } };
 | 
|---|
| [6ba0659] | 110 | ofstream *sout = &soutFile;
 | 
|---|
| [53ba273] | 111 | static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), 1, 0, { ' ', '\0' } };
 | 
|---|
| [6ba0659] | 112 | ofstream *serr = &serrFile;
 | 
|---|
| [51b73452] | 113 | 
 | 
|---|
| [90c3b1c] | 114 | 
 | 
|---|
| [6ba0659] | 115 | //---------------------------------------
 | 
|---|
| [51b73452] | 116 | 
 | 
|---|
 | 117 | 
 | 
|---|
| [6ba0659] | 118 | int fail( ifstream * is ) {
 | 
|---|
| [90c3b1c] | 119 |         return ferror( (FILE *)(is->file) );
 | 
|---|
| [6ba0659] | 120 | } // fail
 | 
|---|
 | 121 | 
 | 
|---|
 | 122 | int eof( ifstream * is ) {
 | 
|---|
| [90c3b1c] | 123 |         return feof( (FILE *)(is->file) );
 | 
|---|
| [6ba0659] | 124 | } // eof
 | 
|---|
 | 125 | 
 | 
|---|
| [90c3b1c] | 126 | void open( ifstream * is, const char * name, const char * mode ) {
 | 
|---|
 | 127 |         FILE *t = fopen( name, mode );
 | 
|---|
 | 128 |         if ( t == 0 ) {                                                                         // do not change unless successful
 | 
|---|
| [53ba273] | 129 |                 fprintf( stderr, IO_MSG "open input file \"%s\", ", name );
 | 
|---|
 | 130 |                 perror( 0 );
 | 
|---|
| [90c3b1c] | 131 |                 exit( EXIT_FAILURE );
 | 
|---|
| [6ba0659] | 132 |         } // if
 | 
|---|
| [90c3b1c] | 133 |         is->file = t;
 | 
|---|
 | 134 | } // open
 | 
|---|
| [6ba0659] | 135 | 
 | 
|---|
| [90c3b1c] | 136 | void close( ifstream * is ) {
 | 
|---|
 | 137 |         if ( (FILE *)(is->file) == stdin ) return;
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 |         if ( fclose( (FILE *)(is->file) ) == EOF ) {
 | 
|---|
 | 140 |                 perror( IO_MSG "close input" );
 | 
|---|
 | 141 |         } // if 
 | 
|---|
 | 142 | } // close
 | 
|---|
 | 143 | 
 | 
|---|
 | 144 | ifstream * read( ifstream * is, char * data, unsigned long int size ) {
 | 
|---|
| [6ba0659] | 145 |         if ( fail( is ) ) {
 | 
|---|
 | 146 |                 fprintf( stderr, "attempt read I/O on failed stream\n" );
 | 
|---|
 | 147 |                 exit( EXIT_FAILURE );
 | 
|---|
 | 148 |         } // if
 | 
|---|
 | 149 | 
 | 
|---|
| [90c3b1c] | 150 |         if ( fread( data, size, 1, (FILE *)(is->file) ) == 0 ) {
 | 
|---|
| [6ba0659] | 151 |                 perror( IO_MSG "read" );
 | 
|---|
 | 152 |                 exit( EXIT_FAILURE );
 | 
|---|
 | 153 |         } // if
 | 
|---|
| [86bd7c1f] | 154 |         return is;
 | 
|---|
| [6ba0659] | 155 | } // read
 | 
|---|
 | 156 |   
 | 
|---|
 | 157 | ifstream *ungetc( ifstream * is, char c ) {
 | 
|---|
 | 158 |         if ( fail( is ) ) {
 | 
|---|
 | 159 |                 fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
 | 
|---|
 | 160 |                 exit( EXIT_FAILURE );
 | 
|---|
 | 161 |         } // if
 | 
|---|
| [51b73452] | 162 | 
 | 
|---|
| [90c3b1c] | 163 |         if ( ungetc( c, (FILE *)(is->file) ) == EOF ) {
 | 
|---|
| [6ba0659] | 164 |                 perror( IO_MSG "ungetc" );
 | 
|---|
 | 165 |                 exit( EXIT_FAILURE );
 | 
|---|
 | 166 |         } // if
 | 
|---|
 | 167 |         return is;
 | 
|---|
 | 168 | } // ungetc
 | 
|---|
| [51b73452] | 169 | 
 | 
|---|
| [90c3b1c] | 170 | int scanfmt( ifstream * is, const char fmt[], ... ) {
 | 
|---|
 | 171 |     va_list args;
 | 
|---|
| [51b73452] | 172 | 
 | 
|---|
| [90c3b1c] | 173 |     va_start( args, fmt );
 | 
|---|
 | 174 |     int len = vfscanf( (FILE *)(is->file), fmt, args );
 | 
|---|
 | 175 |         if ( len == EOF ) {
 | 
|---|
 | 176 |                 if ( ferror( (FILE *)(is->file) ) ) {
 | 
|---|
 | 177 |                         fprintf( stderr, "invalid read\n" );
 | 
|---|
 | 178 |                         exit( EXIT_FAILURE );
 | 
|---|
 | 179 |                 } // if
 | 
|---|
 | 180 |         } // if
 | 
|---|
 | 181 |     va_end( args );
 | 
|---|
 | 182 |         return len;
 | 
|---|
 | 183 | } // prtfmt
 | 
|---|
| [51b73452] | 184 | 
 | 
|---|
 | 185 | 
 | 
|---|
| [6ba0659] | 186 | static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
 | 
|---|
 | 187 | ifstream *sin = &sinFile;
 | 
|---|
| [86bd7c1f] | 188 | 
 | 
|---|
 | 189 | // Local Variables: //
 | 
|---|
 | 190 | // tab-width: 4 //
 | 
|---|
 | 191 | // End: //
 | 
|---|