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

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

formatting, remove release, and update lock/unlock for use with mutex statement

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