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 : Fri Jul 12 12:03:53 2019
|
---|
13 | // Update Count : 344
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "fstream.hfa"
|
---|
17 |
|
---|
18 | #include <stdio.h> // vfprintf, vfscanf
|
---|
19 | #include <stdlib.h> // exit
|
---|
20 | #include <stdarg.h> // varargs
|
---|
21 | #include <string.h> // strlen
|
---|
22 | #include <float.h> // DBL_DIG, LDBL_DIG
|
---|
23 | #include <complex.h> // creal, cimag
|
---|
24 | #include <assert.h>
|
---|
25 | #include <errno.h> // errno
|
---|
26 |
|
---|
27 |
|
---|
28 | //*********************************** ofstream ***********************************
|
---|
29 |
|
---|
30 |
|
---|
31 | #define IO_MSG "I/O error: "
|
---|
32 |
|
---|
33 | void ?{}( ofstream & os, void * file ) {
|
---|
34 | os.file = file;
|
---|
35 | os.sepDefault = true;
|
---|
36 | os.sepOnOff = false;
|
---|
37 | os.nlOnOff = true;
|
---|
38 | os.prt = false;
|
---|
39 | os.sawNL = false;
|
---|
40 | sepSet( os, " " );
|
---|
41 | sepSetCur( os, sepGet( os ) );
|
---|
42 | sepSetTuple( os, ", " );
|
---|
43 | } // ?{}
|
---|
44 |
|
---|
45 | // private
|
---|
46 | bool sepPrt( ofstream & os ) { setNL( os, false ); return os.sepOnOff; }
|
---|
47 | void sepReset( ofstream & os ) { os.sepOnOff = os.sepDefault; }
|
---|
48 | void sepReset( ofstream & os, bool reset ) { os.sepDefault = reset; os.sepOnOff = os.sepDefault; }
|
---|
49 | const char * sepGetCur( ofstream & os ) { return os.sepCur; }
|
---|
50 | void sepSetCur( ofstream & os, const char * sepCur ) { os.sepCur = sepCur; }
|
---|
51 | bool getNL( ofstream & os ) { return os.sawNL; }
|
---|
52 | void setNL( ofstream & os, bool state ) { os.sawNL = state; }
|
---|
53 | bool getANL( ofstream & os ) { return os.nlOnOff; }
|
---|
54 | bool getPrt( ofstream & os ) { return os.prt; }
|
---|
55 | void setPrt( ofstream & os, bool state ) { os.prt = state; }
|
---|
56 |
|
---|
57 | // public
|
---|
58 | void ?{}( ofstream & os ) { os.file = 0; }
|
---|
59 |
|
---|
60 | void ?{}( ofstream & os, const char * name, const char * mode ) {
|
---|
61 | open( os, name, mode );
|
---|
62 | } // ?{}
|
---|
63 |
|
---|
64 | void ?{}( ofstream & os, const char * name ) {
|
---|
65 | open( os, name, "w" );
|
---|
66 | } // ?{}
|
---|
67 |
|
---|
68 | void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); }
|
---|
69 | void sepOff( ofstream & os ) { os.sepOnOff = false; }
|
---|
70 |
|
---|
71 | bool sepDisable( ofstream & os ) {
|
---|
72 | bool temp = os.sepDefault;
|
---|
73 | os.sepDefault = false;
|
---|
74 | sepReset( os );
|
---|
75 | return temp;
|
---|
76 | } // sepDisable
|
---|
77 |
|
---|
78 | bool sepEnable( ofstream & os ) {
|
---|
79 | bool temp = os.sepDefault;
|
---|
80 | os.sepDefault = true;
|
---|
81 | if ( os.sepOnOff ) sepReset( os ); // start of line ?
|
---|
82 | return temp;
|
---|
83 | } // sepEnable
|
---|
84 |
|
---|
85 | void nlOn( ofstream & os ) { os.nlOnOff = true; }
|
---|
86 | void nlOff( ofstream & os ) { os.nlOnOff = false; }
|
---|
87 |
|
---|
88 | const char * sepGet( ofstream & os ) { return os.separator; }
|
---|
89 | void sepSet( ofstream & os, const char * s ) {
|
---|
90 | assert( s );
|
---|
91 | strncpy( os.separator, s, sepSize - 1 );
|
---|
92 | os.separator[sepSize - 1] = '\0';
|
---|
93 | } // sepSet
|
---|
94 |
|
---|
95 | const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator; }
|
---|
96 | void sepSetTuple( ofstream & os, const char * s ) {
|
---|
97 | assert( s );
|
---|
98 | strncpy( os.tupleSeparator, s, sepSize - 1 );
|
---|
99 | os.tupleSeparator[sepSize - 1] = '\0';
|
---|
100 | } // sepSet
|
---|
101 |
|
---|
102 | void ends( ofstream & os ) {
|
---|
103 | if ( getANL( os ) ) nl( os );
|
---|
104 | else setPrt( os, false ); // turn off
|
---|
105 | if ( &os == &exit ) exit( EXIT_FAILURE );
|
---|
106 | if ( &os == &abort ) abort();
|
---|
107 | } // ends
|
---|
108 |
|
---|
109 | int fail( ofstream & os ) {
|
---|
110 | return os.file == 0 || ferror( (FILE *)(os.file) );
|
---|
111 | } // fail
|
---|
112 |
|
---|
113 | int flush( ofstream & os ) {
|
---|
114 | return fflush( (FILE *)(os.file) );
|
---|
115 | } // flush
|
---|
116 |
|
---|
117 | void open( ofstream & os, const char * name, const char * mode ) {
|
---|
118 | FILE * file = fopen( name, mode );
|
---|
119 | #ifdef __CFA_DEBUG__
|
---|
120 | if ( file == 0 ) {
|
---|
121 | abort( IO_MSG "open output file \"%s\", %s", name, strerror( errno ) );
|
---|
122 | } // if
|
---|
123 | #endif // __CFA_DEBUG__
|
---|
124 | (os){ file };
|
---|
125 | } // open
|
---|
126 |
|
---|
127 | void open( ofstream & os, const char * name ) {
|
---|
128 | open( os, name, "w" );
|
---|
129 | } // open
|
---|
130 |
|
---|
131 | void close( ofstream & os ) {
|
---|
132 | if ( (FILE *)(os.file) == stdout || (FILE *)(os.file) == stderr ) return;
|
---|
133 |
|
---|
134 | if ( fclose( (FILE *)(os.file) ) == EOF ) {
|
---|
135 | abort( IO_MSG "close output %s", strerror( errno ) );
|
---|
136 | } // if
|
---|
137 | } // close
|
---|
138 |
|
---|
139 | ofstream & write( ofstream & os, const char * data, size_t size ) {
|
---|
140 | if ( fail( os ) ) {
|
---|
141 | abort( "attempt write I/O on failed stream\n" );
|
---|
142 | } // if
|
---|
143 |
|
---|
144 | if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) {
|
---|
145 | abort( IO_MSG "write %s", strerror( errno ) );
|
---|
146 | } // if
|
---|
147 | return os;
|
---|
148 | } // write
|
---|
149 |
|
---|
150 | int fmt( ofstream & os, const char format[], ... ) {
|
---|
151 | va_list args;
|
---|
152 | va_start( args, format );
|
---|
153 | int len = vfprintf( (FILE *)(os.file), format, args );
|
---|
154 | if ( len == EOF ) {
|
---|
155 | if ( ferror( (FILE *)(os.file) ) ) {
|
---|
156 | abort( "invalid write\n" );
|
---|
157 | } // if
|
---|
158 | } // if
|
---|
159 | va_end( args );
|
---|
160 |
|
---|
161 | setPrt( os, true ); // called in output cascade
|
---|
162 | sepReset( os ); // reset separator
|
---|
163 | return len;
|
---|
164 | } // fmt
|
---|
165 |
|
---|
166 | static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_) };
|
---|
167 | ofstream & sout = soutFile;
|
---|
168 | static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };
|
---|
169 | ofstream & serr = serrFile;
|
---|
170 |
|
---|
171 | static ofstream exitFile = { (FILE *)(&_IO_2_1_stdout_) };
|
---|
172 | ofstream & exit = exitFile;
|
---|
173 | static ofstream abortFile = { (FILE *)(&_IO_2_1_stderr_) };
|
---|
174 | ofstream & abort = abortFile;
|
---|
175 |
|
---|
176 |
|
---|
177 | //*********************************** ifstream ***********************************
|
---|
178 |
|
---|
179 |
|
---|
180 | // private
|
---|
181 | void ?{}( ifstream & is, void * file ) {
|
---|
182 | is.file = file;
|
---|
183 | is.nlOnOff = false;
|
---|
184 | } // ?{}
|
---|
185 |
|
---|
186 | // public
|
---|
187 | void ?{}( ifstream & is ) { is.file = 0; }
|
---|
188 |
|
---|
189 | void ?{}( ifstream & is, const char * name, const char * mode ) {
|
---|
190 | open( is, name, mode );
|
---|
191 | } // ?{}
|
---|
192 |
|
---|
193 | void ?{}( ifstream & is, const char * name ) {
|
---|
194 | open( is, name, "r" );
|
---|
195 | } // ?{}
|
---|
196 |
|
---|
197 | void nlOn( ifstream & os ) { os.nlOnOff = true; }
|
---|
198 | void nlOff( ifstream & os ) { os.nlOnOff = false; }
|
---|
199 | bool getANL( ifstream & os ) { return os.nlOnOff; }
|
---|
200 |
|
---|
201 | int fail( ifstream & is ) {
|
---|
202 | return is.file == 0 || ferror( (FILE *)(is.file) );
|
---|
203 | } // fail
|
---|
204 |
|
---|
205 | int eof( ifstream & is ) {
|
---|
206 | return feof( (FILE *)(is.file) );
|
---|
207 | } // eof
|
---|
208 |
|
---|
209 | void open( ifstream & is, const char * name, const char * mode ) {
|
---|
210 | FILE * file = fopen( name, mode );
|
---|
211 | #ifdef __CFA_DEBUG__
|
---|
212 | if ( file == 0 ) {
|
---|
213 | abort( IO_MSG "open input file \"%s\", %s\n", name, strerror( errno ) );
|
---|
214 | } // if
|
---|
215 | #endif // __CFA_DEBUG__
|
---|
216 | is.file = file;
|
---|
217 | } // open
|
---|
218 |
|
---|
219 | void open( ifstream & is, const char * name ) {
|
---|
220 | open( is, name, "r" );
|
---|
221 | } // open
|
---|
222 |
|
---|
223 | void close( ifstream & is ) {
|
---|
224 | if ( (FILE *)(is.file) == stdin ) return;
|
---|
225 |
|
---|
226 | if ( fclose( (FILE *)(is.file) ) == EOF ) {
|
---|
227 | abort( IO_MSG "close input %s", strerror( errno ) );
|
---|
228 | } // if
|
---|
229 | } // close
|
---|
230 |
|
---|
231 | ifstream & read( ifstream & is, char * data, size_t size ) {
|
---|
232 | if ( fail( is ) ) {
|
---|
233 | abort( "attempt read I/O on failed stream\n" );
|
---|
234 | } // if
|
---|
235 |
|
---|
236 | if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
|
---|
237 | abort( IO_MSG "read %s", strerror( errno ) );
|
---|
238 | } // if
|
---|
239 | return is;
|
---|
240 | } // read
|
---|
241 |
|
---|
242 | ifstream &ungetc( ifstream & is, char c ) {
|
---|
243 | if ( fail( is ) ) {
|
---|
244 | abort( "attempt ungetc I/O on failed stream\n" );
|
---|
245 | } // if
|
---|
246 |
|
---|
247 | if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
|
---|
248 | abort( IO_MSG "ungetc %s", strerror( errno ) );
|
---|
249 | } // if
|
---|
250 | return is;
|
---|
251 | } // ungetc
|
---|
252 |
|
---|
253 | int fmt( ifstream & is, const char format[], ... ) {
|
---|
254 | va_list args;
|
---|
255 |
|
---|
256 | va_start( args, format );
|
---|
257 | int len = vfscanf( (FILE *)(is.file), format, args );
|
---|
258 | if ( len == EOF ) {
|
---|
259 | if ( ferror( (FILE *)(is.file) ) ) {
|
---|
260 | abort( "invalid read\n" );
|
---|
261 | } // if
|
---|
262 | } // if
|
---|
263 | va_end( args );
|
---|
264 | return len;
|
---|
265 | } // fmt
|
---|
266 |
|
---|
267 |
|
---|
268 | static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
|
---|
269 | ifstream & sin = sinFile;
|
---|
270 |
|
---|
271 | // Local Variables: //
|
---|
272 | // tab-width: 4 //
|
---|
273 | // End: //
|
---|