source: libcfa/src/fstream.cfa @ a1538cd

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since a1538cd was e474cf09, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

add concurrency lock to IO stream and provide user interface to lock stream

  • Property mode set to 100644
File size: 9.1 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 : Mon Mar  1 21:12:15 2021
13// Update Count     : 424
14//
15
16#include "fstream.hfa"
17
18#include <stdio.h>                                                                              // vfprintf, vfscanf
19#include <stdlib.h>                                                                             // exit
20#include <stdarg.h>                                                                             // varargs
21#include <string.h>                                                                             // strlen
22#include <float.h>                                                                              // DBL_DIG, LDBL_DIG
23#include <complex.h>                                                                    // creal, cimag
24#include <assert.h>
25#include <errno.h>                                                                              // errno
26
27// *********************************** ofstream ***********************************
28
29
30#define IO_MSG "I/O error: "
31
32void ?{}( ofstream & os, void * file ) {
33        os.$file = file;
34        os.$sepDefault = true;
35        os.$sepOnOff = false;
36        os.$nlOnOff = true;
37        os.$prt = false;
38        os.$sawNL = false;
39        os.$acquired = false;
40        $sepSetCur( os, sepGet( os ) );
41        sepSet( os, " " );
42        sepSetTuple( os, ", " );
43} // ?{}
44
45// private
46bool $sepPrt( ofstream & os ) { $setNL( os, false ); return os.$sepOnOff; }
47void $sepReset( ofstream & os ) { os.$sepOnOff = os.$sepDefault; }
48void $sepReset( ofstream & os, bool reset ) { os.$sepDefault = reset; os.$sepOnOff = os.$sepDefault; }
49const char * $sepGetCur( ofstream & os ) { return os.$sepCur; }
50void $sepSetCur( ofstream & os, const char sepCur[] ) { os.$sepCur = sepCur; }
51bool $getNL( ofstream & os ) { return os.$sawNL; }
52void $setNL( ofstream & os, bool state ) { os.$sawNL = state; }
53bool $getANL( ofstream & os ) { return os.$nlOnOff; }
54bool $getPrt( ofstream & os ) { return os.$prt; }
55void $setPrt( ofstream & os, bool state ) { os.$prt = state; }
56
57// public
58void ?{}( ofstream & os ) { os.$file = 0p; }
59
60void ?{}( ofstream & os, const char name[], const char mode[] ) {
61        open( os, name, mode );
62} // ?{}
63
64void ?{}( ofstream & os, const char name[] ) {
65        open( os, name, "w" );
66} // ?{}
67
68void ^?{}( ofstream & os ) {
69        close( os );
70} // ^?{}
71
72void sepOn( ofstream & os ) { os.$sepOnOff = ! $getNL( os ); }
73void sepOff( ofstream & os ) { os.$sepOnOff = false; }
74
75bool sepDisable( ofstream & os ) {
76        bool temp = os.$sepDefault;
77        os.$sepDefault = false;
78        $sepReset( os );
79        return temp;
80} // sepDisable
81
82bool sepEnable( ofstream & os ) {
83        bool temp = os.$sepDefault;
84        os.$sepDefault = true;
85        if ( os.$sepOnOff ) $sepReset( os );                            // start of line ?
86        return temp;
87} // sepEnable
88
89void nlOn( ofstream & os ) { os.$nlOnOff = true; }
90void nlOff( ofstream & os ) { os.$nlOnOff = false; }
91
92const char * sepGet( ofstream & os ) { return os.$separator; }
93void sepSet( ofstream & os, const char s[] ) {
94        assert( s );
95        strncpy( os.$separator, s, sepSize - 1 );
96        os.$separator[sepSize - 1] = '\0';
97} // sepSet
98
99const char * sepGetTuple( ofstream & os ) { return os.$tupleSeparator; }
100void sepSetTuple( ofstream & os, const char s[] ) {
101        assert( s );
102        strncpy( os.$tupleSeparator, s, sepSize - 1 );
103        os.$tupleSeparator[sepSize - 1] = '\0';
104} // sepSet
105
106void ends( ofstream & os ) {
107        if ( $getANL( os ) ) nl( os );
108        else $setPrt( os, false );                                                      // turn off
109        if ( &os == &exit ) exit( EXIT_FAILURE );
110        if ( &os == &abort ) abort();
111        if ( os.$acquired ) { os.$acquired = false; release( os ); }
112} // ends
113
114int fail( ofstream & os ) {
115        return os.$file == 0 || ferror( (FILE *)(os.$file) );
116} // fail
117
118int flush( ofstream & os ) {
119        return fflush( (FILE *)(os.$file) );
120} // flush
121
122void open( ofstream & os, const char name[], const char mode[] ) {
123        FILE * file = fopen( name, mode );
124        #ifdef __CFA_DEBUG__
125        if ( file == 0p ) {
126                throw (Open_Failure){ os };
127                // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno );
128        } // if
129        #endif // __CFA_DEBUG__
130        (os){ file };
131} // open
132
133void open( ofstream & os, const char name[] ) {
134        open( os, name, "w" );
135} // open
136
137void close( ofstream & os ) {
138  if ( (FILE *)(os.$file) == 0p ) return;
139  if ( (FILE *)(os.$file) == (FILE *)stdout || (FILE *)(os.$file) == (FILE *)stderr ) return;
140
141        if ( fclose( (FILE *)(os.$file) ) == EOF ) {
142                abort | IO_MSG "close output" | nl | strerror( errno );
143        } // if
144        os.$file = 0p;
145} // close
146
147ofstream & write( ofstream & os, const char data[], size_t size ) {
148        if ( fail( os ) ) {
149                abort | IO_MSG "attempt write I/O on failed stream";
150        } // if
151
152        if ( fwrite( data, 1, size, (FILE *)(os.$file) ) != size ) {
153                abort | IO_MSG "write" | nl | strerror( errno );
154        } // if
155        return os;
156} // write
157
158int fmt( ofstream & os, const char format[], ... ) {
159        va_list args;
160        va_start( args, format );
161        int len = vfprintf( (FILE *)(os.$file), format, args );
162        if ( len == EOF ) {
163                if ( ferror( (FILE *)(os.$file) ) ) {
164                        abort | IO_MSG "invalid write";
165                } // if
166        } // if
167        va_end( args );
168
169        $setPrt( os, true );                                                            // called in output cascade
170        $sepReset( os );                                                                        // reset separator
171        return len;
172} // fmt
173
174inline void acquire( ofstream & os ) {
175        lock( os.$lock );
176        if ( ! os.$acquired ) os.$acquired = true;
177        else unlock( os.$lock );
178} // acquire
179
180inline void release( ofstream & os ) {
181        unlock( os.$lock );
182} // release
183
184void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.$lock ); }
185void ^?{}( osacquire & acq ) { release( acq.os ); }
186
187static ofstream soutFile = { (FILE *)stdout };
188ofstream & sout = soutFile, & stdout = soutFile;
189static ofstream serrFile = { (FILE *)stderr };
190ofstream & serr = serrFile, & stderr = serrFile;
191
192static ofstream lsoutFile = { (FILE *)stdout };
193ofstream & lsout = lsoutFile;
194
195static ofstream exitFile = { (FILE *)stdout };
196ofstream & exit = exitFile;
197static ofstream abortFile = { (FILE *)stderr };
198ofstream & abort = abortFile;
199
200
201// *********************************** ifstream ***********************************
202
203
204// private
205void ?{}( ifstream & is, void * file ) {
206        is.$file = file;
207        is.$nlOnOff = false;
208        is.$acquired = false;
209} // ?{}
210
211// public
212void ?{}( ifstream & is ) { is.$file = 0p; }
213
214void ?{}( ifstream & is, const char name[], const char mode[] ) {
215        open( is, name, mode );
216} // ?{}
217
218void ?{}( ifstream & is, const char name[] ) {
219        open( is, name, "r" );
220} // ?{}
221
222void ^?{}( ifstream & is ) {
223        close( is );
224} // ^?{}
225
226void nlOn( ifstream & os ) { os.$nlOnOff = true; }
227void nlOff( ifstream & os ) { os.$nlOnOff = false; }
228bool getANL( ifstream & os ) { return os.$nlOnOff; }
229
230int fail( ifstream & is ) {
231        return is.$file == 0p || ferror( (FILE *)(is.$file) );
232} // fail
233
234void ends( ifstream & is ) {
235        if ( is.$acquired ) { is.$acquired = false; release( is ); }
236} // ends
237
238int eof( ifstream & is ) {
239        return feof( (FILE *)(is.$file) );
240} // eof
241
242void open( ifstream & is, const char name[], const char mode[] ) {
243        FILE * file = fopen( name, mode );
244        #ifdef __CFA_DEBUG__
245        if ( file == 0p ) {
246                throw (Open_Failure){ is };
247                // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
248        } // if
249        #endif // __CFA_DEBUG__
250        is.$file = file;
251} // open
252
253void open( ifstream & is, const char name[] ) {
254        open( is, name, "r" );
255} // open
256
257void close( ifstream & is ) {
258  if ( (FILE *)(is.$file) == 0p ) return;
259  if ( (FILE *)(is.$file) == (FILE *)stdin ) return;
260
261        if ( fclose( (FILE *)(is.$file) ) == EOF ) {
262                abort | IO_MSG "close input" | nl | strerror( errno );
263        } // if
264        is.$file = 0p;
265} // close
266
267ifstream & read( ifstream & is, char * data, size_t size ) {
268        if ( fail( 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                abort | IO_MSG "read" | nl | strerror( errno );
274        } // if
275        return is;
276} // read
277
278ifstream &ungetc( ifstream & is, char c ) {
279        if ( fail( is ) ) {
280                abort | IO_MSG "attempt ungetc I/O on failed stream";
281        } // if
282
283        if ( ungetc( c, (FILE *)(is.$file) ) == EOF ) {
284                abort | IO_MSG "ungetc" | nl | strerror( errno );
285        } // if
286        return is;
287} // ungetc
288
289int fmt( ifstream & is, const char format[], ... ) {
290        va_list args;
291
292        va_start( args, format );
293        int len = vfscanf( (FILE *)(is.$file), format, args );
294        if ( len == EOF ) {
295                if ( ferror( (FILE *)(is.$file) ) ) {
296                        abort | IO_MSG "invalid read";
297                } // if
298        } // if
299        va_end( args );
300        return len;
301} // fmt
302
303inline void acquire( ifstream & is ) {
304        lock( is.$lock );
305        if ( ! is.$acquired ) is.$acquired = true;
306        else unlock( is.$lock );
307} // acquire
308
309inline void release( ifstream & is ) {
310        unlock( is.$lock );
311} // release
312
313void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.$lock ); }
314void ^?{}( isacquire & acq ) { release( acq.is ); }
315
316static ifstream sinFile = { (FILE *)stdin };
317ifstream & sin = sinFile, & stdin = sinFile;
318
319
320// *********************************** exceptions ***********************************
321
322
323void ?{}( Open_Failure & this, ofstream & ostream ) {
324        VTABLE_INIT(this, Open_Failure);
325        this.ostream = &ostream;
326        this.tag = 1;
327}
328void ?{}( Open_Failure & this, ifstream & istream ) {
329        VTABLE_INIT(this, Open_Failure);
330        this.istream = &istream;
331        this.tag = 0;
332}
333const char * Open_Failure_msg(Open_Failure * this) {
334        return "Open_Failure";
335}
336VTABLE_INSTANCE(Open_Failure)(Open_Failure_msg);
337void throwOpen_Failure( ofstream & ostream ) {
338        Open_Failure exc = { ostream };
339}
340void throwOpen_Failure( ifstream & istream ) {
341        Open_Failure exc = { istream };
342}
343
344// Local Variables: //
345// tab-width: 4 //
346// End: //
Note: See TracBrowser for help on using the repository browser.