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 : Tue Oct 17 08:38:49 2023 |
---|
13 | // Update Count : 544 |
---|
14 | // |
---|
15 | |
---|
16 | #include "fstream.hfa" // also includes iostream.hfa |
---|
17 | |
---|
18 | #include <stdio.h> // vfprintf, vfscanf |
---|
19 | #include <stdlib.h> // exit |
---|
20 | #include <stdarg.h> // varargs |
---|
21 | #include <string.h> // strncpy, strerror |
---|
22 | #include <assert.h> |
---|
23 | #include <errno.h> // errno |
---|
24 | |
---|
25 | #pragma GCC visibility push(default) |
---|
26 | |
---|
27 | |
---|
28 | // *********************************** ofstream *********************************** |
---|
29 | |
---|
30 | |
---|
31 | #define IO_MSG "I/O error: " |
---|
32 | |
---|
33 | // private |
---|
34 | void ?{}( ofstream & os, void * file ) with( os ) { |
---|
35 | file$ = file; |
---|
36 | sepDefault$ = true; |
---|
37 | sepOnOff$ = false; |
---|
38 | nlOnOff$ = true; |
---|
39 | prt$ = false; |
---|
40 | sawNL$ = false; |
---|
41 | sepSetCur$( os, sepGet( os ) ); |
---|
42 | sepSet( os, " " ); |
---|
43 | sepSetTuple( os, ", " ); |
---|
44 | } // ?{} |
---|
45 | |
---|
46 | inline bool getNL$( ofstream & os ) { return os.sawNL$; } |
---|
47 | inline bool setNL$( ofstream & os, bool state ) { bool temp = os.sawNL$; os.sawNL$ = state; return temp; } |
---|
48 | inline bool getANL$( ofstream & os ) { return os.nlOnOff$; } |
---|
49 | inline bool setANL$( ofstream & os, bool state ) { bool temp = os.nlOnOff$; os.nlOnOff$ = state; return temp; } |
---|
50 | |
---|
51 | inline bool sepPrt$( ofstream & os ) { setNL$( os, false ); return os.sepOnOff$; } |
---|
52 | inline void sepReset$( ofstream & os ) { os.sepOnOff$ = os.sepDefault$; } |
---|
53 | inline void sepReset$( ofstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; } |
---|
54 | inline const char * sepGetCur$( ofstream & os ) { return os.sepCur$; } |
---|
55 | inline void sepSetCur$( ofstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; } |
---|
56 | |
---|
57 | inline bool getPrt$( ofstream & os ) { return os.prt$; } |
---|
58 | inline bool setPrt$( ofstream & os, bool state ) { bool temp = os.prt$; os.prt$ = state; return temp; } |
---|
59 | |
---|
60 | inline void lock( ofstream & os ) with( os ) { lock( os.lock$ ); } |
---|
61 | inline void unlock( ofstream & os ) { unlock( os.lock$ ); } |
---|
62 | |
---|
63 | // public |
---|
64 | void ?{}( ofstream & os ) { os.file$ = 0p; } |
---|
65 | void ?{}( ofstream & os, const char name[], const char mode[] ) { open( os, name, mode ); } |
---|
66 | void ?{}( ofstream & os, const char name[] ) { open( os, name, "w" ); } |
---|
67 | void ^?{}( ofstream & os ) { close( os ); } |
---|
68 | |
---|
69 | void nlOn( ofstream & os ) { os.nlOnOff$ = true; } |
---|
70 | void nlOff( ofstream & os ) { os.nlOnOff$ = false; } |
---|
71 | |
---|
72 | void sep( ofstream & os ) { os.sepOnOff$ = ! getNL$( os ); } |
---|
73 | void nosep( ofstream & os ) { os.sepOnOff$ = false; } |
---|
74 | |
---|
75 | bool sepOn( ofstream & os ) { |
---|
76 | bool temp = os.sepDefault$; |
---|
77 | os.sepDefault$ = true; |
---|
78 | if ( os.sepOnOff$ ) sepReset$( os ); // start of line ? |
---|
79 | return temp; |
---|
80 | } // sepOn |
---|
81 | |
---|
82 | bool sepOff( ofstream & os ) { |
---|
83 | bool temp = os.sepDefault$; |
---|
84 | os.sepDefault$ = false; |
---|
85 | sepReset$( os ); |
---|
86 | return temp; |
---|
87 | } // sepOff |
---|
88 | |
---|
89 | const char * sepGet( ofstream & os ) { return os.separator$; } |
---|
90 | void sepSet( ofstream & os, const char s[] ) { |
---|
91 | assert( s ); |
---|
92 | strncpy( os.separator$, s, ofstream_sepSize - 1 ); |
---|
93 | os.separator$[ofstream_sepSize - 1] = '\0'; |
---|
94 | } // sepSet |
---|
95 | |
---|
96 | const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator$; } |
---|
97 | void sepSetTuple( ofstream & os, const char s[] ) { |
---|
98 | assert( s ); |
---|
99 | strncpy( os.tupleSeparator$, s, ofstream_sepSize - 1 ); |
---|
100 | os.tupleSeparator$[ofstream_sepSize - 1] = '\0'; |
---|
101 | } // sepSet |
---|
102 | |
---|
103 | void ends( ofstream & os ) { |
---|
104 | if ( getANL$( os ) ) nl( os ); |
---|
105 | else setPrt$( os, false ); // turn off |
---|
106 | if ( &os == &exit ) exit( EXIT_FAILURE ); |
---|
107 | if ( &os == &abort ) abort(); |
---|
108 | } // ends |
---|
109 | |
---|
110 | bool fail( ofstream & os ) { return os.file$ == 0 || ferror( (FILE *)(os.file$) ); } |
---|
111 | void clear( ofstream & os ) { clearerr( (FILE *)(os.file$) ); } |
---|
112 | int flush( ofstream & os ) { return fflush( (FILE *)(os.file$) ); } |
---|
113 | |
---|
114 | void open( ofstream & os, const char name[], const char mode[] ) { |
---|
115 | FILE * file; |
---|
116 | for ( cnt; 10 ) { |
---|
117 | errno = 0; |
---|
118 | file = fopen( name, mode ); |
---|
119 | if ( file != 0p || errno != EINTR ) break; // timer interrupt ? |
---|
120 | if ( cnt == 9 ) abort( "ofstream open EINTR spinning exceeded" ); |
---|
121 | } // for |
---|
122 | if ( file == 0p ) { |
---|
123 | throw (open_failure){ os }; |
---|
124 | // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno ); |
---|
125 | } // if |
---|
126 | (os){ file }; // initialize |
---|
127 | } // open |
---|
128 | |
---|
129 | void open( ofstream & os, const char name[] ) { open( os, name, "w" ); } |
---|
130 | |
---|
131 | void close( ofstream & os ) with( os ) { |
---|
132 | if ( (FILE *)(file$) == 0p ) return; |
---|
133 | if ( (FILE *)(file$) == (FILE *)stdout || (FILE *)(file$) == (FILE *)stderr ) return; |
---|
134 | |
---|
135 | int ret; |
---|
136 | for ( cnt; 10 ) { |
---|
137 | errno = 0; |
---|
138 | ret = fclose( (FILE *)(file$) ); |
---|
139 | if ( ret != EOF || errno != EINTR ) break; // timer interrupt ? |
---|
140 | if ( cnt == 9 ) abort( "ofstream open EINTR spinning exceeded" ); |
---|
141 | } // for |
---|
142 | if ( ret == EOF ) { |
---|
143 | throw (close_failure){ os }; |
---|
144 | // abort | IO_MSG "close output" | nl | strerror( errno ); |
---|
145 | } // if |
---|
146 | file$ = 0p; // safety after close |
---|
147 | } // close |
---|
148 | |
---|
149 | ofstream & write( ofstream & os, const char data[], size_t size ) { |
---|
150 | if ( fail( os ) ) { |
---|
151 | throw (write_failure){ os }; |
---|
152 | // abort | IO_MSG "attempt write I/O on failed stream"; |
---|
153 | } // if |
---|
154 | |
---|
155 | if ( fwrite( data, 1, size, (FILE *)(os.file$) ) != size ) { |
---|
156 | throw (write_failure){ os }; |
---|
157 | // abort | IO_MSG "write" | nl | strerror( errno ); |
---|
158 | } // if |
---|
159 | return os; |
---|
160 | } // write |
---|
161 | |
---|
162 | int fmt( ofstream & os, const char format[], ... ) { |
---|
163 | va_list args; |
---|
164 | va_start( args, format ); |
---|
165 | |
---|
166 | int len; |
---|
167 | for ( cnt; 10 ) { |
---|
168 | errno = 0; |
---|
169 | disable_interrupts(); |
---|
170 | len = vfprintf( (FILE *)(os.file$), format, args ); |
---|
171 | enable_interrupts(); |
---|
172 | if ( len != EOF || errno != EINTR ) break; // timer interrupt ? |
---|
173 | if ( cnt == 9 ) abort( "ofstream fmt EINTR spinning exceeded" ); |
---|
174 | } // for |
---|
175 | if ( len == EOF ) { // error writing ? |
---|
176 | abort | IO_MSG "invalid write"; |
---|
177 | } // if |
---|
178 | va_end( args ); |
---|
179 | |
---|
180 | setPrt$( os, true ); // called in output cascade |
---|
181 | sepReset$( os ); // reset separator |
---|
182 | return len; |
---|
183 | } // fmt |
---|
184 | |
---|
185 | static ofstream soutFile = { (FILE *)stdout }; |
---|
186 | ofstream & sout = soutFile, & stdout = soutFile; |
---|
187 | static ofstream serrFile = { (FILE *)stderr }; |
---|
188 | ofstream & serr = serrFile, & stderr = serrFile; |
---|
189 | |
---|
190 | static ofstream lsoutFile = { (FILE *)stdout }; |
---|
191 | ofstream & lsout = lsoutFile; |
---|
192 | |
---|
193 | static ofstream exitFile = { (FILE *)stdout }; |
---|
194 | ofstream & exit = exitFile; |
---|
195 | static ofstream abortFile = { (FILE *)stderr }; |
---|
196 | ofstream & abort = abortFile; |
---|
197 | |
---|
198 | ofstream & nl( ofstream & os ) { |
---|
199 | nl$( os ); // call basic_ostream nl |
---|
200 | flush( os ); |
---|
201 | return os; |
---|
202 | } // nl |
---|
203 | |
---|
204 | |
---|
205 | // *********************************** ifstream *********************************** |
---|
206 | |
---|
207 | |
---|
208 | // private |
---|
209 | void ?{}( ifstream & is, void * file ) with( is ) { |
---|
210 | file$ = file; |
---|
211 | nlOnOff$ = false; |
---|
212 | } // ?{} |
---|
213 | |
---|
214 | bool getANL$( ifstream & os ) { return os.nlOnOff$; } |
---|
215 | bool setANL$( ifstream & os, bool state ) { bool temp = os.nlOnOff$; os.nlOnOff$ = state; return temp; } |
---|
216 | |
---|
217 | inline void lock( ifstream & os ) with( os ) { lock( os.lock$ ); } |
---|
218 | inline void unlock( ifstream & os ) { unlock( os.lock$ ); } |
---|
219 | |
---|
220 | // public |
---|
221 | void ?{}( ifstream & is ) { is.file$ = 0p; } |
---|
222 | void ?{}( ifstream & is, const char name[], const char mode[] ) { open( is, name, mode ); } |
---|
223 | void ?{}( ifstream & is, const char name[] ) { open( is, name, "r" ); } |
---|
224 | void ^?{}( ifstream & is ) { close( is ); } |
---|
225 | |
---|
226 | bool fail( ifstream & is ) { return is.file$ == 0p || ferror( (FILE *)(is.file$) ); } |
---|
227 | void clear( ifstream & is ) { clearerr( (FILE *)(is.file$) ); } |
---|
228 | |
---|
229 | void nlOn( ifstream & os ) { os.nlOnOff$ = true; } |
---|
230 | void nlOff( ifstream & os ) { os.nlOnOff$ = false; } |
---|
231 | |
---|
232 | void ends( ifstream & is ) {} |
---|
233 | |
---|
234 | bool eof( ifstream & is ) { return feof( (FILE *)(is.file$) ) != 0; } |
---|
235 | |
---|
236 | void open( ifstream & is, const char name[], const char mode[] ) { |
---|
237 | FILE * file; |
---|
238 | for ( cnt; 10 ) { |
---|
239 | errno = 0; |
---|
240 | file = fopen( name, mode ); |
---|
241 | if ( file != 0p || errno != EINTR ) break; // timer interrupt ? |
---|
242 | if ( cnt == 9 ) abort( "ifstream open EINTR spinning exceeded" ); |
---|
243 | } // for |
---|
244 | if ( file == 0p ) { |
---|
245 | throw (open_failure){ is }; |
---|
246 | // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno ); |
---|
247 | } // if |
---|
248 | (is){ file }; // initialize |
---|
249 | } // open |
---|
250 | |
---|
251 | void open( ifstream & is, const char name[] ) { open( is, name, "r" ); } |
---|
252 | |
---|
253 | void close( ifstream & is ) with( is ) { |
---|
254 | if ( (FILE *)(file$) == 0p ) return; |
---|
255 | if ( (FILE *)(file$) == (FILE *)stdin ) return; |
---|
256 | |
---|
257 | int ret; |
---|
258 | for ( cnt; 10 ) { |
---|
259 | errno = 0; |
---|
260 | ret = fclose( (FILE *)(file$) ); |
---|
261 | if ( ret != EOF || errno != EINTR ) break; // timer interrupt ? |
---|
262 | if ( cnt == 9 ) abort( "ifstream close EINTR spinning exceeded" ); |
---|
263 | } // for |
---|
264 | if ( ret == EOF ) { |
---|
265 | throw (close_failure){ is }; |
---|
266 | // abort | IO_MSG "close input" | nl | strerror( errno ); |
---|
267 | } // if |
---|
268 | file$ = 0p; // safety after close |
---|
269 | } // close |
---|
270 | |
---|
271 | ifstream & read( ifstream & is, char data[], size_t size ) { |
---|
272 | if ( fail( is ) ) { |
---|
273 | throw (read_failure){ is }; |
---|
274 | // abort | IO_MSG "attempt read I/O on failed stream"; |
---|
275 | } // if |
---|
276 | |
---|
277 | if ( fread( data, size, 1, (FILE *)(is.file$) ) == 0 ) { |
---|
278 | throw (read_failure){ is }; |
---|
279 | // abort | IO_MSG "read" | nl | strerror( errno ); |
---|
280 | } // if |
---|
281 | return is; |
---|
282 | } // read |
---|
283 | |
---|
284 | ifstream &ungetc( ifstream & is, char c ) { |
---|
285 | if ( fail( is ) ) { |
---|
286 | abort | IO_MSG "attempt ungetc I/O on failed stream"; |
---|
287 | } // if |
---|
288 | |
---|
289 | if ( ungetc( c, (FILE *)(is.file$) ) == EOF ) { |
---|
290 | abort | IO_MSG "ungetc" | nl | strerror( errno ); |
---|
291 | } // if |
---|
292 | return is; |
---|
293 | } // ungetc |
---|
294 | |
---|
295 | int fmt( ifstream & is, const char format[], ... ) { |
---|
296 | va_list args; |
---|
297 | va_start( args, format ); |
---|
298 | |
---|
299 | int len; |
---|
300 | for () { // no check for EINTR limit waiting for keyboard input |
---|
301 | errno = 0; |
---|
302 | disable_interrupts(); |
---|
303 | len = vfscanf( (FILE *)(is.file$), format, args ); |
---|
304 | enable_interrupts(); |
---|
305 | if ( len != EOF || errno != EINTR ) break; // timer interrupt ? |
---|
306 | } // for |
---|
307 | if ( len == EOF ) { // EOF or matching failure ? |
---|
308 | if ( ! feof( (FILE *)(is.file$) ) && ferror( (FILE *)(is.file$) ) ) { |
---|
309 | abort | IO_MSG "invalid read"; |
---|
310 | } // if |
---|
311 | } // if |
---|
312 | va_end( args ); |
---|
313 | // if ( len == 0 ) throw ExceptionInst( missing_data ); |
---|
314 | return len; |
---|
315 | } // fmt |
---|
316 | |
---|
317 | static ifstream sinFile = { (FILE *)stdin }; |
---|
318 | ifstream & sin = sinFile, & stdin = sinFile; |
---|
319 | |
---|
320 | |
---|
321 | // *********************************** exceptions *********************************** |
---|
322 | |
---|
323 | |
---|
324 | static vtable(open_failure) open_failure_vt; |
---|
325 | |
---|
326 | // exception I/O constructors |
---|
327 | void ?{}( open_failure & ex, ofstream & ostream ) with(ex) { |
---|
328 | virtual_table = &open_failure_vt; |
---|
329 | ostream = &ostream; |
---|
330 | tag = 1; |
---|
331 | } // ?{} |
---|
332 | |
---|
333 | void ?{}( open_failure & ex, ifstream & istream ) with(ex) { |
---|
334 | virtual_table = &open_failure_vt; |
---|
335 | istream = &istream; |
---|
336 | tag = 0; |
---|
337 | } // ?{} |
---|
338 | |
---|
339 | |
---|
340 | static vtable(close_failure) close_failure_vt; |
---|
341 | |
---|
342 | // exception I/O constructors |
---|
343 | void ?{}( close_failure & ex, ofstream & ostream ) with(ex) { |
---|
344 | virtual_table = &close_failure_vt; |
---|
345 | ostream = &ostream; |
---|
346 | tag = 1; |
---|
347 | } // ?{} |
---|
348 | |
---|
349 | void ?{}( close_failure & ex, ifstream & istream ) with(ex) { |
---|
350 | virtual_table = &close_failure_vt; |
---|
351 | istream = &istream; |
---|
352 | tag = 0; |
---|
353 | } // ?{} |
---|
354 | |
---|
355 | |
---|
356 | static vtable(write_failure) write_failure_vt; |
---|
357 | |
---|
358 | // exception I/O constructors |
---|
359 | void ?{}( write_failure & ex, ofstream & ostream ) with(ex) { |
---|
360 | virtual_table = &write_failure_vt; |
---|
361 | ostream = &ostream; |
---|
362 | tag = 1; |
---|
363 | } // ?{} |
---|
364 | |
---|
365 | void ?{}( write_failure & ex, ifstream & istream ) with(ex) { |
---|
366 | virtual_table = &write_failure_vt; |
---|
367 | istream = &istream; |
---|
368 | tag = 0; |
---|
369 | } // ?{} |
---|
370 | |
---|
371 | |
---|
372 | static vtable(read_failure) read_failure_vt; |
---|
373 | |
---|
374 | // exception I/O constructors |
---|
375 | void ?{}( read_failure & ex, ofstream & ostream ) with(ex) { |
---|
376 | virtual_table = &read_failure_vt; |
---|
377 | ostream = &ostream; |
---|
378 | tag = 1; |
---|
379 | } // ?{} |
---|
380 | |
---|
381 | void ?{}( read_failure & ex, ifstream & istream ) with(ex) { |
---|
382 | virtual_table = &read_failure_vt; |
---|
383 | istream = &istream; |
---|
384 | tag = 0; |
---|
385 | } // ?{} |
---|
386 | |
---|
387 | // void throwopen_failure( ofstream & ostream ) { |
---|
388 | // open_failure exc = { ostream }; |
---|
389 | // } |
---|
390 | |
---|
391 | // void throwopen_failure( ifstream & istream ) { |
---|
392 | // open_failure exc = { istream }; |
---|
393 | // } |
---|
394 | |
---|
395 | // Local Variables: // |
---|
396 | // tab-width: 4 // |
---|
397 | // End: // |
---|