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