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