source: libcfa/src/fstream.cfa@ 5614552a

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 5614552a was 00e9be9, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

add stream function clear, change eof to return bool

  • Property mode set to 100644
File size: 9.5 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 : Wed Apr 28 20:37:53 2021
13// Update Count : 445
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 abort | IO_MSG "close output" | nl | strerror( errno );
145 } // if
146 os.file$ = 0p;
147} // close
148
149ofstream & write( ofstream & os, const char data[], size_t size ) {
150 if ( fail( 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 abort | IO_MSG "write" | nl | strerror( errno );
156 } // if
157 return os;
158} // write
159
160int fmt( ofstream & os, const char format[], ... ) {
161 va_list args;
162 va_start( args, format );
163 int len = vfprintf( (FILE *)(os.file$), format, args );
164 if ( len == EOF ) {
165 if ( ferror( (FILE *)(os.file$) ) ) {
166 abort | IO_MSG "invalid write";
167 } // if
168 } // if
169 va_end( args );
170
171 setPrt$( os, true ); // called in output cascade
172 sepReset$( os ); // reset separator
173 return len;
174} // fmt
175
176inline void acquire( ofstream & os ) {
177 lock( os.lock$ );
178 if ( ! os.acquired$ ) os.acquired$ = true;
179 else unlock( os.lock$ );
180} // acquire
181
182inline void release( ofstream & os ) {
183 unlock( os.lock$ );
184} // release
185
186void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.lock$ ); }
187void ^?{}( osacquire & acq ) { release( acq.os ); }
188
189static ofstream soutFile = { (FILE *)stdout };
190ofstream & sout = soutFile, & stdout = soutFile;
191static ofstream serrFile = { (FILE *)stderr };
192ofstream & serr = serrFile, & stderr = serrFile;
193
194static ofstream lsoutFile = { (FILE *)stdout };
195ofstream & lsout = lsoutFile;
196
197static ofstream exitFile = { (FILE *)stdout };
198ofstream & exit = exitFile;
199static ofstream abortFile = { (FILE *)stderr };
200ofstream & abort = abortFile;
201
202ofstream & nl( ofstream & os ) {
203 nl$( os ); // call basic_ostream nl
204 flush( os );
205 return os;
206 // (ofstream &)(os | '\n');
207 // setPrt$( os, false ); // turn off
208 // setNL$( os, true );
209 // flush( os );
210 // return sepOff( os ); // prepare for next line
211} // nl
212
213
214// *********************************** ifstream ***********************************
215
216
217// private
218void ?{}( ifstream & is, void * file ) {
219 is.file$ = file;
220 is.nlOnOff$ = false;
221 is.acquired$ = false;
222} // ?{}
223
224// public
225void ?{}( ifstream & is ) { is.file$ = 0p; }
226
227void ?{}( ifstream & is, const char name[], const char mode[] ) {
228 open( is, name, mode );
229} // ?{}
230
231void ?{}( ifstream & is, const char name[] ) {
232 open( is, name, "r" );
233} // ?{}
234
235void ^?{}( ifstream & is ) {
236 close( is );
237} // ^?{}
238
239void nlOn( ifstream & os ) { os.nlOnOff$ = true; }
240void nlOff( ifstream & os ) { os.nlOnOff$ = false; }
241bool getANL( ifstream & os ) { return os.nlOnOff$; }
242
243bool fail( ifstream & is ) {
244 return is.file$ == 0p || ferror( (FILE *)(is.file$) );
245} // fail
246
247void clear( ifstream & is ) {
248 clearerr( (FILE *)(is.file$) );
249} // clear
250
251void ends( ifstream & is ) {
252 if ( is.acquired$ ) { is.acquired$ = false; release( is ); }
253} // ends
254
255bool eof( ifstream & is ) {
256 return feof( (FILE *)(is.file$) );
257} // eof
258
259void open( ifstream & is, const char name[], const char mode[] ) {
260 FILE * file = fopen( name, mode );
261 #ifdef __CFA_DEBUG__
262 if ( file == 0p ) {
263 throw (Open_Failure){ is };
264 // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
265 } // if
266 #endif // __CFA_DEBUG__
267 is.file$ = file;
268} // open
269
270void open( ifstream & is, const char name[] ) {
271 open( is, name, "r" );
272} // open
273
274void close( ifstream & is ) {
275 if ( (FILE *)(is.file$) == 0p ) return;
276 if ( (FILE *)(is.file$) == (FILE *)stdin ) return;
277
278 if ( fclose( (FILE *)(is.file$) ) == EOF ) {
279 abort | IO_MSG "close input" | nl | strerror( errno );
280 } // if
281 is.file$ = 0p;
282} // close
283
284ifstream & read( ifstream & is, char data[], size_t size ) {
285 if ( fail( is ) ) {
286 abort | IO_MSG "attempt read I/O on failed stream";
287 } // if
288
289 if ( fread( data, size, 1, (FILE *)(is.file$) ) == 0 ) {
290 abort | IO_MSG "read" | nl | strerror( errno );
291 } // if
292 return is;
293} // read
294
295ifstream &ungetc( ifstream & is, char c ) {
296 if ( fail( is ) ) {
297 abort | IO_MSG "attempt ungetc I/O on failed stream";
298 } // if
299
300 if ( ungetc( c, (FILE *)(is.file$) ) == EOF ) {
301 abort | IO_MSG "ungetc" | nl | strerror( errno );
302 } // if
303 return is;
304} // ungetc
305
306int fmt( ifstream & is, const char format[], ... ) {
307 va_list args;
308
309 va_start( args, format );
310 int len = vfscanf( (FILE *)(is.file$), format, args );
311 if ( len == EOF ) {
312 if ( ferror( (FILE *)(is.file$) ) ) {
313 abort | IO_MSG "invalid read";
314 } // if
315 } // if
316 va_end( args );
317 return len;
318} // fmt
319
320inline void acquire( ifstream & is ) {
321 lock( is.lock$ );
322 if ( ! is.acquired$ ) is.acquired$ = true;
323 else unlock( is.lock$ );
324} // acquire
325
326inline void release( ifstream & is ) {
327 unlock( is.lock$ );
328} // release
329
330void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.lock$ ); }
331void ^?{}( isacquire & acq ) { release( acq.is ); }
332
333static ifstream sinFile = { (FILE *)stdin };
334ifstream & sin = sinFile, & stdin = sinFile;
335
336
337// *********************************** exceptions ***********************************
338
339
340EHM_VIRTUAL_TABLE(Open_Failure, Open_Failure_main_table);
341void ?{}( Open_Failure & this, ofstream & ostream ) {
342 this.virtual_table = &Open_Failure_main_table;
343 this.ostream = &ostream;
344 this.tag = 1;
345}
346void ?{}( Open_Failure & this, ifstream & istream ) {
347 this.virtual_table = &Open_Failure_main_table;
348 this.istream = &istream;
349 this.tag = 0;
350}
351void throwOpen_Failure( ofstream & ostream ) {
352 Open_Failure exc = { ostream };
353}
354void throwOpen_Failure( ifstream & istream ) {
355 Open_Failure exc = { istream };
356}
357
358// Local Variables: //
359// tab-width: 4 //
360// End: //
Note: See TracBrowser for help on using the repository browser.