source: libcfa/src/fstream.cfa @ 7f958c4

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 7f958c4 was 7a1b7e6, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

bracket vfprintf/vfscanf with enable/disable interrupts in fstream

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