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