source: libcfa/src/fstream.cfa @ 8a039be

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 8a039be was 3bf3b6b, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

clean code, add fix that might deal with the I/O acquire timeout

  • Property mode set to 100644
File size: 10.9 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 : Tue Sep 21 21:51:38 2021
13// Update Count     : 460
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 ) with(os) {
31        file$ = file;
32        sepDefault$ = true;
33        sepOnOff$ = false;
34        nlOnOff$ = true;
35        prt$ = false;
36        sawNL$ = false;
37        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        if ( file == 0p ) {
127                throw (Open_Failure){ os };
128                // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno );
129        } // if
130        (os){ file };                                                                           // initialize
131} // open
132
133void open( ofstream & os, const char name[] ) {
134        open( os, name, "w" );
135} // open
136
137void close( ofstream & os ) with(os) {
138  if ( (FILE *)(file$) == 0p ) return;
139  if ( (FILE *)(file$) == (FILE *)stdout || (FILE *)(file$) == (FILE *)stderr ) return;
140
141        if ( fclose( (FILE *)(file$) ) == EOF ) {
142                throw (Close_Failure){ os };
143                // abort | IO_MSG "close output" | nl | strerror( errno );
144        } // if
145        file$ = 0p;
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        int len = vfprintf( (FILE *)(os.file$), format, args );
165        if ( len == EOF ) {
166                if ( ferror( (FILE *)(os.file$) ) ) {
167                        abort | IO_MSG "invalid write";
168                } // if
169        } // if
170        va_end( args );
171
172        setPrt$( os, true );                                                            // called in output cascade
173        sepReset$( os );                                                                        // reset separator
174        return len;
175} // fmt
176
177inline void acquire( ofstream & os ) with(os) {
178        lock( lock$ );                                                                          // may increase recursive lock
179        if ( ! acquired$ ) acquired$ = true;                            // not locked ?
180        else unlock( lock$ );                                                           // unwind recursive lock at start
181} // acquire
182
183inline void release( ofstream & os ) {
184        unlock( os.lock$ );
185} // release
186
187void ?{}( osacquire & acq, ofstream & os ) { lock( os.lock$ ); &acq.os = &os; }
188void ^?{}( osacquire & acq ) { release( acq.os ); }
189
190static ofstream soutFile = { (FILE *)stdout };
191ofstream & sout = soutFile, & stdout = soutFile;
192static ofstream serrFile = { (FILE *)stderr };
193ofstream & serr = serrFile, & stderr = serrFile;
194
195static ofstream lsoutFile = { (FILE *)stdout };
196ofstream & lsout = lsoutFile;
197
198static ofstream exitFile = { (FILE *)stdout };
199ofstream & exit = exitFile;
200static ofstream abortFile = { (FILE *)stderr };
201ofstream & abort = abortFile;
202
203ofstream & nl( ofstream & os ) {
204        nl$( os );                                                                                      // call basic_ostream nl
205        flush( os );
206        return os;
207        // (ofstream &)(os | '\n');
208        // setPrt$( os, false );                                                        // turn off
209        // setNL$( os, true );
210        // flush( os );
211        // return sepOff( os );                                                 // prepare for next line
212} // nl
213
214
215// *********************************** ifstream ***********************************
216
217
218// private
219void ?{}( ifstream & is, void * file ) with(is) {
220        file$ = file;
221        nlOnOff$ = false;
222        acquired$ = false;
223} // ?{}
224
225// public
226void ?{}( ifstream & is ) { is.file$ = 0p; }
227
228void ?{}( ifstream & is, const char name[], const char mode[] ) {
229        open( is, name, mode );
230} // ?{}
231
232void ?{}( ifstream & is, const char name[] ) {
233        open( is, name, "r" );
234} // ?{}
235
236void ^?{}( ifstream & is ) {
237        close( is );
238} // ^?{}
239
240void nlOn( ifstream & os ) { os.nlOnOff$ = true; }
241void nlOff( ifstream & os ) { os.nlOnOff$ = false; }
242bool getANL( ifstream & os ) { return os.nlOnOff$; }
243
244bool fail( ifstream & is ) {
245        return is.file$ == 0p || ferror( (FILE *)(is.file$) );
246} // fail
247
248void clear( ifstream & is ) {
249        clearerr( (FILE *)(is.file$) );
250} // clear
251
252void ends( ifstream & is ) {
253        if ( is.acquired$ ) { is.acquired$ = false; release( is ); }
254} // ends
255
256bool eof( ifstream & is ) {
257        return feof( (FILE *)(is.file$) );
258} // eof
259
260void open( ifstream & is, const char name[], const char mode[] ) {
261        FILE * file = fopen( name, mode );
262        if ( file == 0p ) {
263                throw (Open_Failure){ is };
264                // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
265        } // if
266        (is){ file };                                                                           // initialize
267} // open
268
269void open( ifstream & is, const char name[] ) {
270        open( is, name, "r" );
271} // open
272
273void close( ifstream & is ) with(is) {
274  if ( (FILE *)(file$) == 0p ) return;
275  if ( (FILE *)(file$) == (FILE *)stdin ) return;
276
277        if ( fclose( (FILE *)(file$) ) == EOF ) {
278                throw (Close_Failure){ is };
279                // abort | IO_MSG "close input" | nl | strerror( errno );
280        } // if
281        file$ = 0p;
282} // close
283
284ifstream & read( ifstream & is, char data[], size_t size ) {
285        if ( fail( is ) ) {
286                throw (Read_Failure){ is };
287                // abort | IO_MSG "attempt read I/O on failed stream";
288        } // if
289
290        if ( fread( data, size, 1, (FILE *)(is.file$) ) == 0 ) {
291                throw (Read_Failure){ is };
292                // abort | IO_MSG "read" | nl | strerror( errno );
293        } // if
294        return is;
295} // read
296
297ifstream &ungetc( ifstream & is, char c ) {
298        if ( fail( is ) ) {
299                abort | IO_MSG "attempt ungetc I/O on failed stream";
300        } // if
301
302        if ( ungetc( c, (FILE *)(is.file$) ) == EOF ) {
303                abort | IO_MSG "ungetc" | nl | strerror( errno );
304        } // if
305        return is;
306} // ungetc
307
308int fmt( ifstream & is, const char format[], ... ) {
309        va_list args;
310
311        va_start( args, format );
312        int len = vfscanf( (FILE *)(is.file$), format, args );
313        if ( len == EOF ) {
314                if ( ferror( (FILE *)(is.file$) ) ) {
315                        abort | IO_MSG "invalid read";
316                } // if
317        } // if
318        va_end( args );
319        return len;
320} // fmt
321
322inline void acquire( ifstream & is ) with(is) {
323        lock( lock$ );                                                                          // may increase recursive lock
324        if ( ! acquired$ ) acquired$ = true;                            // not locked ?
325        else unlock( lock$ );                                                           // unwind recursive lock at start
326} // acquire
327
328inline void release( ifstream & is ) {
329        unlock( is.lock$ );
330} // release
331
332void ?{}( isacquire & acq, ifstream & is ) { lock( is.lock$ ); &acq.is = &is; }
333void ^?{}( isacquire & acq ) { release( acq.is ); }
334
335static ifstream sinFile = { (FILE *)stdin };
336ifstream & sin = sinFile, & stdin = sinFile;
337
338
339// *********************************** exceptions ***********************************
340
341
342static vtable(Open_Failure) Open_Failure_vt;
343
344// exception I/O constructors
345void ?{}( Open_Failure & ex, ofstream & ostream ) with(ex) {
346        virtual_table = &Open_Failure_vt;
347        ostream = &ostream;
348        tag = 1;
349} // ?{}
350
351void ?{}( Open_Failure & ex, ifstream & istream ) with(ex) {
352        virtual_table = &Open_Failure_vt;
353        istream = &istream;
354        tag = 0;
355} // ?{}
356
357
358static vtable(Close_Failure) Close_Failure_vt;
359
360// exception I/O constructors
361void ?{}( Close_Failure & ex, ofstream & ostream ) with(ex) {
362        virtual_table = &Close_Failure_vt;
363        ostream = &ostream;
364        tag = 1;
365} // ?{}
366
367void ?{}( Close_Failure & ex, ifstream & istream ) with(ex) {
368        virtual_table = &Close_Failure_vt;
369        istream = &istream;
370        tag = 0;
371} // ?{}
372
373
374static vtable(Write_Failure) Write_Failure_vt;
375
376// exception I/O constructors
377void ?{}( Write_Failure & ex, ofstream & ostream ) with(ex) {
378        virtual_table = &Write_Failure_vt;
379        ostream = &ostream;
380        tag = 1;
381} // ?{}
382
383void ?{}( Write_Failure & ex, ifstream & istream ) with(ex) {
384        virtual_table = &Write_Failure_vt;
385        istream = &istream;
386        tag = 0;
387} // ?{}
388
389
390static vtable(Read_Failure) Read_Failure_vt;
391
392// exception I/O constructors
393void ?{}( Read_Failure & ex, ofstream & ostream ) with(ex) {
394        virtual_table = &Read_Failure_vt;
395        ostream = &ostream;
396        tag = 1;
397} // ?{}
398
399void ?{}( Read_Failure & ex, ifstream & istream ) with(ex) {
400        virtual_table = &Read_Failure_vt;
401        istream = &istream;
402        tag = 0;
403} // ?{}
404
405// void throwOpen_Failure( ofstream & ostream ) {
406//      Open_Failure exc = { ostream };
407// }
408
409// void throwOpen_Failure( ifstream & istream ) {
410//      Open_Failure exc = { istream };
411// }
412
413// Local Variables: //
414// tab-width: 4 //
415// End: //
Note: See TracBrowser for help on using the repository browser.