source: libcfa/src/fstream.cfa @ 666483d

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

remember basic_ostream nl and then override it using the basic_ostream version

  • Property mode set to 100644
File size: 9.3 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 Apr 27 22:08:57 2021
13// Update Count     : 442
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 ) {
31        os.file$ = file;
32        os.sepDefault$ = true;
33        os.sepOnOff$ = false;
34        os.nlOnOff$ = true;
35        os.prt$ = false;
36        os.sawNL$ = false;
37        os.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
116int flush( ofstream & os ) {
117        return fflush( (FILE *)(os.file$) );
118} // flush
119
120void open( ofstream & os, const char name[], const char mode[] ) {
121        FILE * file = fopen( name, mode );
122        #ifdef __CFA_DEBUG__
123        if ( file == 0p ) {
124                throw (Open_Failure){ os };
125                // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno );
126        } // if
127        #endif // __CFA_DEBUG__
128        (os){ file };
129} // open
130
131void open( ofstream & os, const char name[] ) {
132        open( os, name, "w" );
133} // open
134
135void close( ofstream & os ) {
136  if ( (FILE *)(os.file$) == 0p ) return;
137  if ( (FILE *)(os.file$) == (FILE *)stdout || (FILE *)(os.file$) == (FILE *)stderr ) return;
138
139        if ( fclose( (FILE *)(os.file$) ) == EOF ) {
140                abort | IO_MSG "close output" | nl | strerror( errno );
141        } // if
142        os.file$ = 0p;
143} // close
144
145ofstream & write( ofstream & os, const char data[], size_t size ) {
146        if ( fail( os ) ) {
147                abort | IO_MSG "attempt write I/O on failed stream";
148        } // if
149
150        if ( fwrite( data, 1, size, (FILE *)(os.file$) ) != size ) {
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        int len = vfprintf( (FILE *)(os.file$), format, args );
160        if ( len == EOF ) {
161                if ( ferror( (FILE *)(os.file$) ) ) {
162                        abort | IO_MSG "invalid write";
163                } // if
164        } // if
165        va_end( args );
166
167        setPrt$( os, true );                                                            // called in output cascade
168        sepReset$( os );                                                                        // reset separator
169        return len;
170} // fmt
171
172inline void acquire( ofstream & os ) {
173        lock( os.lock$ );
174        if ( ! os.acquired$ ) os.acquired$ = true;
175        else unlock( os.lock$ );
176} // acquire
177
178inline void release( ofstream & os ) {
179        unlock( os.lock$ );
180} // release
181
182void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.lock$ ); }
183void ^?{}( osacquire & acq ) { release( acq.os ); }
184
185static ofstream soutFile = { (FILE *)stdout };
186ofstream & sout = soutFile, & stdout = soutFile;
187static ofstream serrFile = { (FILE *)stderr };
188ofstream & serr = serrFile, & stderr = serrFile;
189
190static ofstream lsoutFile = { (FILE *)stdout };
191ofstream & lsout = lsoutFile;
192
193static ofstream exitFile = { (FILE *)stdout };
194ofstream & exit = exitFile;
195static ofstream abortFile = { (FILE *)stderr };
196ofstream & abort = abortFile;
197
198ofstream & nl( ofstream & os ) {
199        nl$( os );                                                                                      // call basic_ostream nl
200        flush( os );
201        return os;
202        // (ofstream &)(os | '\n');
203        // setPrt$( os, false );                                                        // turn off
204        // setNL$( os, true );
205        // flush( os );
206        // return sepOff( os );                                                 // prepare for next line
207} // nl
208
209// *********************************** ifstream ***********************************
210
211
212// private
213void ?{}( ifstream & is, void * file ) {
214        is.file$ = file;
215        is.nlOnOff$ = false;
216        is.acquired$ = false;
217} // ?{}
218
219// public
220void ?{}( ifstream & is ) { is.file$ = 0p; }
221
222void ?{}( ifstream & is, const char name[], const char mode[] ) {
223        open( is, name, mode );
224} // ?{}
225
226void ?{}( ifstream & is, const char name[] ) {
227        open( is, name, "r" );
228} // ?{}
229
230void ^?{}( ifstream & is ) {
231        close( is );
232} // ^?{}
233
234void nlOn( ifstream & os ) { os.nlOnOff$ = true; }
235void nlOff( ifstream & os ) { os.nlOnOff$ = false; }
236bool getANL( ifstream & os ) { return os.nlOnOff$; }
237
238bool fail( ifstream & is ) {
239        return is.file$ == 0p || ferror( (FILE *)(is.file$) );
240} // fail
241
242void ends( ifstream & is ) {
243        if ( is.acquired$ ) { is.acquired$ = false; release( is ); }
244} // ends
245
246int eof( ifstream & is ) {
247        return feof( (FILE *)(is.file$) );
248} // eof
249
250void open( ifstream & is, const char name[], const char mode[] ) {
251        FILE * file = fopen( name, mode );
252        #ifdef __CFA_DEBUG__
253        if ( file == 0p ) {
254                throw (Open_Failure){ is };
255                // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
256        } // if
257        #endif // __CFA_DEBUG__
258        is.file$ = file;
259} // open
260
261void open( ifstream & is, const char name[] ) {
262        open( is, name, "r" );
263} // open
264
265void close( ifstream & is ) {
266  if ( (FILE *)(is.file$) == 0p ) return;
267  if ( (FILE *)(is.file$) == (FILE *)stdin ) return;
268
269        if ( fclose( (FILE *)(is.file$) ) == EOF ) {
270                abort | IO_MSG "close input" | nl | strerror( errno );
271        } // if
272        is.file$ = 0p;
273} // close
274
275ifstream & read( ifstream & is, char * data, size_t size ) {
276        if ( fail( is ) ) {
277                abort | IO_MSG "attempt read I/O on failed stream";
278        } // if
279
280        if ( fread( data, size, 1, (FILE *)(is.file$) ) == 0 ) {
281                abort | IO_MSG "read" | nl | strerror( errno );
282        } // if
283        return is;
284} // read
285
286ifstream &ungetc( ifstream & is, char c ) {
287        if ( fail( is ) ) {
288                abort | IO_MSG "attempt ungetc I/O on failed stream";
289        } // if
290
291        if ( ungetc( c, (FILE *)(is.file$) ) == EOF ) {
292                abort | IO_MSG "ungetc" | nl | strerror( errno );
293        } // if
294        return is;
295} // ungetc
296
297int fmt( ifstream & is, const char format[], ... ) {
298        va_list args;
299
300        va_start( args, format );
301        int len = vfscanf( (FILE *)(is.file$), format, args );
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
311inline void acquire( ifstream & is ) {
312        lock( is.lock$ );
313        if ( ! is.acquired$ ) is.acquired$ = true;
314        else unlock( is.lock$ );
315} // acquire
316
317inline void release( ifstream & is ) {
318        unlock( is.lock$ );
319} // release
320
321void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.lock$ ); }
322void ^?{}( isacquire & acq ) { release( acq.is ); }
323
324static ifstream sinFile = { (FILE *)stdin };
325ifstream & sin = sinFile, & stdin = sinFile;
326
327
328// *********************************** exceptions ***********************************
329
330
331EHM_VIRTUAL_TABLE(Open_Failure, Open_Failure_main_table);
332void ?{}( Open_Failure & this, ofstream & ostream ) {
333        this.virtual_table = &Open_Failure_main_table;
334        this.ostream = &ostream;
335        this.tag = 1;
336}
337void ?{}( Open_Failure & this, ifstream & istream ) {
338        this.virtual_table = &Open_Failure_main_table;
339        this.istream = &istream;
340        this.tag = 0;
341}
342void throwOpen_Failure( ofstream & ostream ) {
343        Open_Failure exc = { ostream };
344}
345void throwOpen_Failure( ifstream & istream ) {
346        Open_Failure exc = { istream };
347}
348
349// Local Variables: //
350// tab-width: 4 //
351// End: //
Note: See TracBrowser for help on using the repository browser.