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 Mar 1 21:12:15 2021 |
---|
13 | // Update Count : 424 |
---|
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 | // *********************************** ofstream *********************************** |
---|
28 | |
---|
29 | |
---|
30 | #define IO_MSG "I/O error: " |
---|
31 | |
---|
32 | void ?{}( ofstream & os, void * file ) { |
---|
33 | os.$file = file; |
---|
34 | os.$sepDefault = true; |
---|
35 | os.$sepOnOff = false; |
---|
36 | os.$nlOnOff = true; |
---|
37 | os.$prt = false; |
---|
38 | os.$sawNL = false; |
---|
39 | os.$acquired = false; |
---|
40 | $sepSetCur( os, sepGet( os ) ); |
---|
41 | sepSet( 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 = 0p; } |
---|
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 ^?{}( ofstream & os ) { |
---|
69 | close( os ); |
---|
70 | } // ^?{} |
---|
71 | |
---|
72 | void sepOn( ofstream & os ) { os.$sepOnOff = ! $getNL( os ); } |
---|
73 | void sepOff( ofstream & os ) { os.$sepOnOff = false; } |
---|
74 | |
---|
75 | bool sepDisable( ofstream & os ) { |
---|
76 | bool temp = os.$sepDefault; |
---|
77 | os.$sepDefault = false; |
---|
78 | $sepReset( os ); |
---|
79 | return temp; |
---|
80 | } // sepDisable |
---|
81 | |
---|
82 | bool sepEnable( ofstream & os ) { |
---|
83 | bool temp = os.$sepDefault; |
---|
84 | os.$sepDefault = true; |
---|
85 | if ( os.$sepOnOff ) $sepReset( os ); // start of line ? |
---|
86 | return temp; |
---|
87 | } // sepEnable |
---|
88 | |
---|
89 | void nlOn( ofstream & os ) { os.$nlOnOff = true; } |
---|
90 | void nlOff( ofstream & os ) { os.$nlOnOff = false; } |
---|
91 | |
---|
92 | const char * sepGet( ofstream & os ) { return os.$separator; } |
---|
93 | void sepSet( ofstream & os, const char s[] ) { |
---|
94 | assert( s ); |
---|
95 | strncpy( os.$separator, s, sepSize - 1 ); |
---|
96 | os.$separator[sepSize - 1] = '\0'; |
---|
97 | } // sepSet |
---|
98 | |
---|
99 | const char * sepGetTuple( ofstream & os ) { return os.$tupleSeparator; } |
---|
100 | void sepSetTuple( ofstream & os, const char s[] ) { |
---|
101 | assert( s ); |
---|
102 | strncpy( os.$tupleSeparator, s, sepSize - 1 ); |
---|
103 | os.$tupleSeparator[sepSize - 1] = '\0'; |
---|
104 | } // sepSet |
---|
105 | |
---|
106 | void ends( ofstream & os ) { |
---|
107 | if ( $getANL( os ) ) nl( os ); |
---|
108 | else $setPrt( os, false ); // turn off |
---|
109 | if ( &os == &exit ) exit( EXIT_FAILURE ); |
---|
110 | if ( &os == &abort ) abort(); |
---|
111 | if ( os.$acquired ) { os.$acquired = false; release( os ); } |
---|
112 | } // ends |
---|
113 | |
---|
114 | int fail( ofstream & os ) { |
---|
115 | return os.$file == 0 || ferror( (FILE *)(os.$file) ); |
---|
116 | } // fail |
---|
117 | |
---|
118 | int flush( ofstream & os ) { |
---|
119 | return fflush( (FILE *)(os.$file) ); |
---|
120 | } // flush |
---|
121 | |
---|
122 | void open( ofstream & os, const char name[], const char mode[] ) { |
---|
123 | FILE * file = fopen( name, mode ); |
---|
124 | #ifdef __CFA_DEBUG__ |
---|
125 | if ( file == 0p ) { |
---|
126 | throw (Open_Failure){ os }; |
---|
127 | // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno ); |
---|
128 | } // if |
---|
129 | #endif // __CFA_DEBUG__ |
---|
130 | (os){ file }; |
---|
131 | } // open |
---|
132 | |
---|
133 | void open( ofstream & os, const char name[] ) { |
---|
134 | open( os, name, "w" ); |
---|
135 | } // open |
---|
136 | |
---|
137 | void close( ofstream & os ) { |
---|
138 | if ( (FILE *)(os.$file) == 0p ) return; |
---|
139 | if ( (FILE *)(os.$file) == (FILE *)stdout || (FILE *)(os.$file) == (FILE *)stderr ) return; |
---|
140 | |
---|
141 | if ( fclose( (FILE *)(os.$file) ) == EOF ) { |
---|
142 | abort | IO_MSG "close output" | nl | strerror( errno ); |
---|
143 | } // if |
---|
144 | os.$file = 0p; |
---|
145 | } // close |
---|
146 | |
---|
147 | ofstream & write( ofstream & os, const char data[], size_t size ) { |
---|
148 | if ( fail( os ) ) { |
---|
149 | abort | IO_MSG "attempt write I/O on failed stream"; |
---|
150 | } // if |
---|
151 | |
---|
152 | if ( fwrite( data, 1, size, (FILE *)(os.$file) ) != size ) { |
---|
153 | abort | IO_MSG "write" | nl | strerror( errno ); |
---|
154 | } // if |
---|
155 | return os; |
---|
156 | } // write |
---|
157 | |
---|
158 | int fmt( ofstream & os, const char format[], ... ) { |
---|
159 | va_list args; |
---|
160 | va_start( args, format ); |
---|
161 | int len = vfprintf( (FILE *)(os.$file), format, args ); |
---|
162 | if ( len == EOF ) { |
---|
163 | if ( ferror( (FILE *)(os.$file) ) ) { |
---|
164 | abort | IO_MSG "invalid write"; |
---|
165 | } // if |
---|
166 | } // if |
---|
167 | va_end( args ); |
---|
168 | |
---|
169 | $setPrt( os, true ); // called in output cascade |
---|
170 | $sepReset( os ); // reset separator |
---|
171 | return len; |
---|
172 | } // fmt |
---|
173 | |
---|
174 | inline void acquire( ofstream & os ) { |
---|
175 | lock( os.$lock ); |
---|
176 | if ( ! os.$acquired ) os.$acquired = true; |
---|
177 | else unlock( os.$lock ); |
---|
178 | } // acquire |
---|
179 | |
---|
180 | inline void release( ofstream & os ) { |
---|
181 | unlock( os.$lock ); |
---|
182 | } // release |
---|
183 | |
---|
184 | void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.$lock ); } |
---|
185 | void ^?{}( osacquire & acq ) { release( acq.os ); } |
---|
186 | |
---|
187 | static ofstream soutFile = { (FILE *)stdout }; |
---|
188 | ofstream & sout = soutFile, & stdout = soutFile; |
---|
189 | static ofstream serrFile = { (FILE *)stderr }; |
---|
190 | ofstream & serr = serrFile, & stderr = serrFile; |
---|
191 | |
---|
192 | static ofstream lsoutFile = { (FILE *)stdout }; |
---|
193 | ofstream & lsout = lsoutFile; |
---|
194 | |
---|
195 | static ofstream exitFile = { (FILE *)stdout }; |
---|
196 | ofstream & exit = exitFile; |
---|
197 | static ofstream abortFile = { (FILE *)stderr }; |
---|
198 | ofstream & abort = abortFile; |
---|
199 | |
---|
200 | |
---|
201 | // *********************************** ifstream *********************************** |
---|
202 | |
---|
203 | |
---|
204 | // private |
---|
205 | void ?{}( ifstream & is, void * file ) { |
---|
206 | is.$file = file; |
---|
207 | is.$nlOnOff = false; |
---|
208 | is.$acquired = false; |
---|
209 | } // ?{} |
---|
210 | |
---|
211 | // public |
---|
212 | void ?{}( ifstream & is ) { is.$file = 0p; } |
---|
213 | |
---|
214 | void ?{}( ifstream & is, const char name[], const char mode[] ) { |
---|
215 | open( is, name, mode ); |
---|
216 | } // ?{} |
---|
217 | |
---|
218 | void ?{}( ifstream & is, const char name[] ) { |
---|
219 | open( is, name, "r" ); |
---|
220 | } // ?{} |
---|
221 | |
---|
222 | void ^?{}( ifstream & is ) { |
---|
223 | close( is ); |
---|
224 | } // ^?{} |
---|
225 | |
---|
226 | void nlOn( ifstream & os ) { os.$nlOnOff = true; } |
---|
227 | void nlOff( ifstream & os ) { os.$nlOnOff = false; } |
---|
228 | bool getANL( ifstream & os ) { return os.$nlOnOff; } |
---|
229 | |
---|
230 | int fail( ifstream & is ) { |
---|
231 | return is.$file == 0p || ferror( (FILE *)(is.$file) ); |
---|
232 | } // fail |
---|
233 | |
---|
234 | void ends( ifstream & is ) { |
---|
235 | if ( is.$acquired ) { is.$acquired = false; release( is ); } |
---|
236 | } // ends |
---|
237 | |
---|
238 | int eof( ifstream & is ) { |
---|
239 | return feof( (FILE *)(is.$file) ); |
---|
240 | } // eof |
---|
241 | |
---|
242 | void open( ifstream & is, const char name[], const char mode[] ) { |
---|
243 | FILE * file = fopen( name, mode ); |
---|
244 | #ifdef __CFA_DEBUG__ |
---|
245 | if ( file == 0p ) { |
---|
246 | throw (Open_Failure){ is }; |
---|
247 | // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno ); |
---|
248 | } // if |
---|
249 | #endif // __CFA_DEBUG__ |
---|
250 | is.$file = file; |
---|
251 | } // open |
---|
252 | |
---|
253 | void open( ifstream & is, const char name[] ) { |
---|
254 | open( is, name, "r" ); |
---|
255 | } // open |
---|
256 | |
---|
257 | void close( ifstream & is ) { |
---|
258 | if ( (FILE *)(is.$file) == 0p ) return; |
---|
259 | if ( (FILE *)(is.$file) == (FILE *)stdin ) return; |
---|
260 | |
---|
261 | if ( fclose( (FILE *)(is.$file) ) == EOF ) { |
---|
262 | abort | IO_MSG "close input" | nl | strerror( errno ); |
---|
263 | } // if |
---|
264 | is.$file = 0p; |
---|
265 | } // close |
---|
266 | |
---|
267 | ifstream & read( ifstream & is, char * data, size_t size ) { |
---|
268 | if ( fail( is ) ) { |
---|
269 | abort | IO_MSG "attempt read I/O on failed stream"; |
---|
270 | } // if |
---|
271 | |
---|
272 | if ( fread( data, size, 1, (FILE *)(is.$file) ) == 0 ) { |
---|
273 | abort | IO_MSG "read" | nl | strerror( errno ); |
---|
274 | } // if |
---|
275 | return is; |
---|
276 | } // read |
---|
277 | |
---|
278 | ifstream &ungetc( ifstream & is, char c ) { |
---|
279 | if ( fail( is ) ) { |
---|
280 | abort | IO_MSG "attempt ungetc I/O on failed stream"; |
---|
281 | } // if |
---|
282 | |
---|
283 | if ( ungetc( c, (FILE *)(is.$file) ) == EOF ) { |
---|
284 | abort | IO_MSG "ungetc" | nl | strerror( errno ); |
---|
285 | } // if |
---|
286 | return is; |
---|
287 | } // ungetc |
---|
288 | |
---|
289 | int fmt( ifstream & is, const char format[], ... ) { |
---|
290 | va_list args; |
---|
291 | |
---|
292 | va_start( args, format ); |
---|
293 | int len = vfscanf( (FILE *)(is.$file), format, args ); |
---|
294 | if ( len == EOF ) { |
---|
295 | if ( ferror( (FILE *)(is.$file) ) ) { |
---|
296 | abort | IO_MSG "invalid read"; |
---|
297 | } // if |
---|
298 | } // if |
---|
299 | va_end( args ); |
---|
300 | return len; |
---|
301 | } // fmt |
---|
302 | |
---|
303 | inline void acquire( ifstream & is ) { |
---|
304 | lock( is.$lock ); |
---|
305 | if ( ! is.$acquired ) is.$acquired = true; |
---|
306 | else unlock( is.$lock ); |
---|
307 | } // acquire |
---|
308 | |
---|
309 | inline void release( ifstream & is ) { |
---|
310 | unlock( is.$lock ); |
---|
311 | } // release |
---|
312 | |
---|
313 | void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.$lock ); } |
---|
314 | void ^?{}( isacquire & acq ) { release( acq.is ); } |
---|
315 | |
---|
316 | static ifstream sinFile = { (FILE *)stdin }; |
---|
317 | ifstream & sin = sinFile, & stdin = sinFile; |
---|
318 | |
---|
319 | |
---|
320 | // *********************************** exceptions *********************************** |
---|
321 | |
---|
322 | |
---|
323 | EHM_VIRTUAL_TABLE(Open_Failure, Open_Failure_main_table); |
---|
324 | void ?{}( Open_Failure & this, ofstream & ostream ) { |
---|
325 | this.virtual_table = &Open_Failure_main_table; |
---|
326 | this.ostream = &ostream; |
---|
327 | this.tag = 1; |
---|
328 | } |
---|
329 | void ?{}( Open_Failure & this, ifstream & istream ) { |
---|
330 | this.virtual_table = &Open_Failure_main_table; |
---|
331 | this.istream = &istream; |
---|
332 | this.tag = 0; |
---|
333 | } |
---|
334 | void throwOpen_Failure( ofstream & ostream ) { |
---|
335 | Open_Failure exc = { ostream }; |
---|
336 | } |
---|
337 | void throwOpen_Failure( ifstream & istream ) { |
---|
338 | Open_Failure exc = { istream }; |
---|
339 | } |
---|
340 | |
---|
341 | // Local Variables: // |
---|
342 | // tab-width: 4 // |
---|
343 | // End: // |
---|