source: libcfa/src/fstream.cfa@ 082510e

Last change on this file since 082510e was 7baff35, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

formatting

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