source: libcfa/src/fstream.cfa @ a51a02d

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since a51a02d was a51a02d, checked in by caparsons <caparson@…>, 2 years ago

fstream mutexstmt lock/unlock

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