source: libcfa/src/fstream.cfa@ 57dff2f

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation pthread-emulation qualifiedEnum
Last change on this file since 57dff2f was ba0d2ea, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

add exceptions Close_Failure, Write_Failure, Read_Failure to fstream

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