source: libcfa/src/fstream.cfa @ 89eff25

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

formatting, rename public enum sepSize to ofstream_sepSize, change return type to bool for function fail

  • Property mode set to 100644
File size: 9.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 : Sat Apr 24 09:05:16 2021
13// Update Count     : 426
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>                                                                             // 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
198
199// *********************************** ifstream ***********************************
200
201
202// private
203void ?{}( ifstream & is, void * file ) {
204        is.file$ = file;
205        is.nlOnOff$ = false;
206        is.acquired$ = false;
207} // ?{}
208
209// public
210void ?{}( ifstream & is ) { is.file$ = 0p; }
211
212void ?{}( ifstream & is, const char name[], const char mode[] ) {
213        open( is, name, mode );
214} // ?{}
215
216void ?{}( ifstream & is, const char name[] ) {
217        open( is, name, "r" );
218} // ?{}
219
220void ^?{}( ifstream & is ) {
221        close( is );
222} // ^?{}
223
224void nlOn( ifstream & os ) { os.nlOnOff$ = true; }
225void nlOff( ifstream & os ) { os.nlOnOff$ = false; }
226bool getANL( ifstream & os ) { return os.nlOnOff$; }
227
228bool fail( ifstream & is ) {
229        return is.file$ == 0p || ferror( (FILE *)(is.file$) );
230} // fail
231
232void ends( ifstream & is ) {
233        if ( is.acquired$ ) { is.acquired$ = false; release( is ); }
234} // ends
235
236int eof( ifstream & is ) {
237        return feof( (FILE *)(is.file$) );
238} // eof
239
240void open( ifstream & is, const char name[], const char mode[] ) {
241        FILE * file = fopen( name, mode );
242        #ifdef __CFA_DEBUG__
243        if ( file == 0p ) {
244                throw (Open_Failure){ is };
245                // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
246        } // if
247        #endif // __CFA_DEBUG__
248        is.file$ = file;
249} // open
250
251void open( ifstream & is, const char name[] ) {
252        open( is, name, "r" );
253} // open
254
255void close( ifstream & is ) {
256  if ( (FILE *)(is.file$) == 0p ) return;
257  if ( (FILE *)(is.file$) == (FILE *)stdin ) return;
258
259        if ( fclose( (FILE *)(is.file$) ) == EOF ) {
260                abort | IO_MSG "close input" | nl | strerror( errno );
261        } // if
262        is.file$ = 0p;
263} // close
264
265ifstream & read( ifstream & is, char * data, size_t size ) {
266        if ( fail( is ) ) {
267                abort | IO_MSG "attempt read I/O on failed stream";
268        } // if
269
270        if ( fread( data, size, 1, (FILE *)(is.file$) ) == 0 ) {
271                abort | IO_MSG "read" | nl | strerror( errno );
272        } // if
273        return is;
274} // read
275
276ifstream &ungetc( ifstream & is, char c ) {
277        if ( fail( is ) ) {
278                abort | IO_MSG "attempt ungetc I/O on failed stream";
279        } // if
280
281        if ( ungetc( c, (FILE *)(is.file$) ) == EOF ) {
282                abort | IO_MSG "ungetc" | nl | strerror( errno );
283        } // if
284        return is;
285} // ungetc
286
287int fmt( ifstream & is, const char format[], ... ) {
288        va_list args;
289
290        va_start( args, format );
291        int len = vfscanf( (FILE *)(is.file$), format, args );
292        if ( len == EOF ) {
293                if ( ferror( (FILE *)(is.file$) ) ) {
294                        abort | IO_MSG "invalid read";
295                } // if
296        } // if
297        va_end( args );
298        return len;
299} // fmt
300
301inline void acquire( ifstream & is ) {
302        lock( is.lock$ );
303        if ( ! is.acquired$ ) is.acquired$ = true;
304        else unlock( is.lock$ );
305} // acquire
306
307inline void release( ifstream & is ) {
308        unlock( is.lock$ );
309} // release
310
311void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.lock$ ); }
312void ^?{}( isacquire & acq ) { release( acq.is ); }
313
314static ifstream sinFile = { (FILE *)stdin };
315ifstream & sin = sinFile, & stdin = sinFile;
316
317
318// *********************************** exceptions ***********************************
319
320
321EHM_VIRTUAL_TABLE(Open_Failure, Open_Failure_main_table);
322void ?{}( Open_Failure & this, ofstream & ostream ) {
323        this.virtual_table = &Open_Failure_main_table;
324        this.ostream = &ostream;
325        this.tag = 1;
326}
327void ?{}( Open_Failure & this, ifstream & istream ) {
328        this.virtual_table = &Open_Failure_main_table;
329        this.istream = &istream;
330        this.tag = 0;
331}
332void throwOpen_Failure( ofstream & ostream ) {
333        Open_Failure exc = { ostream };
334}
335void throwOpen_Failure( ifstream & istream ) {
336        Open_Failure exc = { istream };
337}
338
339// Local Variables: //
340// tab-width: 4 //
341// End: //
Note: See TracBrowser for help on using the repository browser.