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 |
|
---|
30 | void ?{}( 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
|
---|
44 | bool sepPrt$( ofstream & os ) { setNL$( os, false ); return os.sepOnOff$; }
|
---|
45 | void sepReset$( ofstream & os ) { os.sepOnOff$ = os.sepDefault$; }
|
---|
46 | void sepReset$( ofstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; }
|
---|
47 | const char * sepGetCur$( ofstream & os ) { return os.sepCur$; }
|
---|
48 | void sepSetCur$( ofstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; }
|
---|
49 | bool getNL$( ofstream & os ) { return os.sawNL$; }
|
---|
50 | void setNL$( ofstream & os, bool state ) { os.sawNL$ = state; }
|
---|
51 | bool getANL$( ofstream & os ) { return os.nlOnOff$; }
|
---|
52 | bool getPrt$( ofstream & os ) { return os.prt$; }
|
---|
53 | void setPrt$( ofstream & os, bool state ) { os.prt$ = state; }
|
---|
54 |
|
---|
55 | // public
|
---|
56 | void ?{}( ofstream & os ) { os.file$ = 0p; }
|
---|
57 |
|
---|
58 | void ?{}( ofstream & os, const char name[], const char mode[] ) {
|
---|
59 | open( os, name, mode );
|
---|
60 | } // ?{}
|
---|
61 |
|
---|
62 | void ?{}( ofstream & os, const char name[] ) {
|
---|
63 | open( os, name, "w" );
|
---|
64 | } // ?{}
|
---|
65 |
|
---|
66 | void ^?{}( ofstream & os ) {
|
---|
67 | close( os );
|
---|
68 | } // ^?{}
|
---|
69 |
|
---|
70 | void sepOn( ofstream & os ) { os.sepOnOff$ = ! getNL$( os ); }
|
---|
71 | void sepOff( ofstream & os ) { os.sepOnOff$ = false; }
|
---|
72 |
|
---|
73 | bool sepDisable( ofstream & os ) {
|
---|
74 | bool temp = os.sepDefault$;
|
---|
75 | os.sepDefault$ = false;
|
---|
76 | sepReset$( os );
|
---|
77 | return temp;
|
---|
78 | } // sepDisable
|
---|
79 |
|
---|
80 | bool 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 |
|
---|
87 | void nlOn( ofstream & os ) { os.nlOnOff$ = true; }
|
---|
88 | void nlOff( ofstream & os ) { os.nlOnOff$ = false; }
|
---|
89 |
|
---|
90 | const char * sepGet( ofstream & os ) { return os.separator$; }
|
---|
91 | void 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 |
|
---|
97 | const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator$; }
|
---|
98 | void 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 |
|
---|
104 | void 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 |
|
---|
112 | bool fail( ofstream & os ) {
|
---|
113 | return os.file$ == 0 || ferror( (FILE *)(os.file$) );
|
---|
114 | } // fail
|
---|
115 |
|
---|
116 | void clear( ofstream & os ) {
|
---|
117 | clearerr( (FILE *)(os.file$) );
|
---|
118 | } // clear
|
---|
119 |
|
---|
120 | int flush( ofstream & os ) {
|
---|
121 | return fflush( (FILE *)(os.file$) );
|
---|
122 | } // flush
|
---|
123 |
|
---|
124 | void 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 |
|
---|
135 | void open( ofstream & os, const char name[] ) {
|
---|
136 | open( os, name, "w" );
|
---|
137 | } // open
|
---|
138 |
|
---|
139 | void 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 |
|
---|
149 | ofstream & 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 |
|
---|
160 | int 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 |
|
---|
176 | inline void acquire( ofstream & os ) {
|
---|
177 | lock( os.lock$ );
|
---|
178 | if ( ! os.acquired$ ) os.acquired$ = true;
|
---|
179 | else unlock( os.lock$ );
|
---|
180 | } // acquire
|
---|
181 |
|
---|
182 | inline void release( ofstream & os ) {
|
---|
183 | unlock( os.lock$ );
|
---|
184 | } // release
|
---|
185 |
|
---|
186 | void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.lock$ ); }
|
---|
187 | void ^?{}( osacquire & acq ) { release( acq.os ); }
|
---|
188 |
|
---|
189 | static ofstream soutFile = { (FILE *)stdout };
|
---|
190 | ofstream & sout = soutFile, & stdout = soutFile;
|
---|
191 | static ofstream serrFile = { (FILE *)stderr };
|
---|
192 | ofstream & serr = serrFile, & stderr = serrFile;
|
---|
193 |
|
---|
194 | static ofstream lsoutFile = { (FILE *)stdout };
|
---|
195 | ofstream & lsout = lsoutFile;
|
---|
196 |
|
---|
197 | static ofstream exitFile = { (FILE *)stdout };
|
---|
198 | ofstream & exit = exitFile;
|
---|
199 | static ofstream abortFile = { (FILE *)stderr };
|
---|
200 | ofstream & abort = abortFile;
|
---|
201 |
|
---|
202 | ofstream & 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
|
---|
218 | void ?{}( ifstream & is, void * file ) {
|
---|
219 | is.file$ = file;
|
---|
220 | is.nlOnOff$ = false;
|
---|
221 | is.acquired$ = false;
|
---|
222 | } // ?{}
|
---|
223 |
|
---|
224 | // public
|
---|
225 | void ?{}( ifstream & is ) { is.file$ = 0p; }
|
---|
226 |
|
---|
227 | void ?{}( ifstream & is, const char name[], const char mode[] ) {
|
---|
228 | open( is, name, mode );
|
---|
229 | } // ?{}
|
---|
230 |
|
---|
231 | void ?{}( ifstream & is, const char name[] ) {
|
---|
232 | open( is, name, "r" );
|
---|
233 | } // ?{}
|
---|
234 |
|
---|
235 | void ^?{}( ifstream & is ) {
|
---|
236 | close( is );
|
---|
237 | } // ^?{}
|
---|
238 |
|
---|
239 | void nlOn( ifstream & os ) { os.nlOnOff$ = true; }
|
---|
240 | void nlOff( ifstream & os ) { os.nlOnOff$ = false; }
|
---|
241 | bool getANL( ifstream & os ) { return os.nlOnOff$; }
|
---|
242 |
|
---|
243 | bool fail( ifstream & is ) {
|
---|
244 | return is.file$ == 0p || ferror( (FILE *)(is.file$) );
|
---|
245 | } // fail
|
---|
246 |
|
---|
247 | void clear( ifstream & is ) {
|
---|
248 | clearerr( (FILE *)(is.file$) );
|
---|
249 | } // clear
|
---|
250 |
|
---|
251 | void ends( ifstream & is ) {
|
---|
252 | if ( is.acquired$ ) { is.acquired$ = false; release( is ); }
|
---|
253 | } // ends
|
---|
254 |
|
---|
255 | bool eof( ifstream & is ) {
|
---|
256 | return feof( (FILE *)(is.file$) );
|
---|
257 | } // eof
|
---|
258 |
|
---|
259 | void 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 |
|
---|
270 | void open( ifstream & is, const char name[] ) {
|
---|
271 | open( is, name, "r" );
|
---|
272 | } // open
|
---|
273 |
|
---|
274 | void 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 |
|
---|
284 | ifstream & 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 |
|
---|
295 | ifstream &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 |
|
---|
306 | int 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 |
|
---|
320 | inline void acquire( ifstream & is ) {
|
---|
321 | lock( is.lock$ );
|
---|
322 | if ( ! is.acquired$ ) is.acquired$ = true;
|
---|
323 | else unlock( is.lock$ );
|
---|
324 | } // acquire
|
---|
325 |
|
---|
326 | inline void release( ifstream & is ) {
|
---|
327 | unlock( is.lock$ );
|
---|
328 | } // release
|
---|
329 |
|
---|
330 | void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.lock$ ); }
|
---|
331 | void ^?{}( isacquire & acq ) { release( acq.is ); }
|
---|
332 |
|
---|
333 | static ifstream sinFile = { (FILE *)stdin };
|
---|
334 | ifstream & sin = sinFile, & stdin = sinFile;
|
---|
335 |
|
---|
336 |
|
---|
337 | // *********************************** exceptions ***********************************
|
---|
338 |
|
---|
339 |
|
---|
340 | EHM_VIRTUAL_TABLE(Open_Failure, Open_Failure_main_table);
|
---|
341 | void ?{}( Open_Failure & this, ofstream & ostream ) {
|
---|
342 | this.virtual_table = &Open_Failure_main_table;
|
---|
343 | this.ostream = &ostream;
|
---|
344 | this.tag = 1;
|
---|
345 | }
|
---|
346 | void ?{}( Open_Failure & this, ifstream & istream ) {
|
---|
347 | this.virtual_table = &Open_Failure_main_table;
|
---|
348 | this.istream = &istream;
|
---|
349 | this.tag = 0;
|
---|
350 | }
|
---|
351 | void throwOpen_Failure( ofstream & ostream ) {
|
---|
352 | Open_Failure exc = { ostream };
|
---|
353 | }
|
---|
354 | void throwOpen_Failure( ifstream & istream ) {
|
---|
355 | Open_Failure exc = { istream };
|
---|
356 | }
|
---|
357 |
|
---|
358 | // Local Variables: //
|
---|
359 | // tab-width: 4 //
|
---|
360 | // End: //
|
---|