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

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

added routines so fstreams could be acquired with the mutex stmt

  • Property mode set to 100644
File size: 10.9 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 : Thu Jul 29 22:34:10 2021
13// Update Count     : 454
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
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        // #ifdef __CFA_DEBUG__
127        if ( file == 0p ) {
128                throw (Open_Failure){ os };
129                // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno );
130        } // if
131        // #endif // __CFA_DEBUG__
132        (os){ file };
133} // open
134
135void open( ofstream & os, const char name[] ) {
136        open( os, name, "w" );
137} // open
138
139void close( ofstream & os ) {
140  if ( (FILE *)(os.file$) == 0p ) return;
141  if ( (FILE *)(os.file$) == (FILE *)stdout || (FILE *)(os.file$) == (FILE *)stderr ) return;
142
143        if ( fclose( (FILE *)(os.file$) ) == EOF ) {
144                throw (Close_Failure){ os };
145                // abort | IO_MSG "close output" | nl | strerror( errno );
146        } // if
147        os.file$ = 0p;
148} // close
149
150ofstream & write( ofstream & os, const char data[], size_t size ) {
151        if ( fail( os ) ) {
152                throw (Write_Failure){ os };
153                // abort | IO_MSG "attempt write I/O on failed stream";
154        } // if
155
156        if ( fwrite( data, 1, size, (FILE *)(os.file$) ) != size ) {
157                throw (Write_Failure){ os };
158                // abort | IO_MSG "write" | nl | strerror( errno );
159        } // if
160        return os;
161} // write
162
163int fmt( ofstream & os, const char format[], ... ) {
164        va_list args;
165        va_start( args, format );
166        int len = vfprintf( (FILE *)(os.file$), format, args );
167        if ( len == EOF ) {
168                if ( ferror( (FILE *)(os.file$) ) ) {
169                        abort | IO_MSG "invalid write";
170                } // if
171        } // if
172        va_end( args );
173
174        setPrt$( os, true );                                                            // called in output cascade
175        sepReset$( os );                                                                        // reset separator
176        return len;
177} // fmt
178
179inline void acquire( ofstream & os ) {
180        lock( os.lock$ );
181        if ( ! os.acquired$ ) os.acquired$ = true;
182        else unlock( os.lock$ );
183} // acquire
184
185inline void release( ofstream & os ) {
186        unlock( os.lock$ );
187} // release
188
189inline void lock( ofstream & os ) { acquire( os ); }
190inline void unlock( ofstream & os ) { release( os ); }
191
192void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.lock$ ); }
193void ^?{}( osacquire & acq ) { release( acq.os ); }
194
195static ofstream soutFile = { (FILE *)stdout };
196ofstream & sout = soutFile, & stdout = soutFile;
197static ofstream serrFile = { (FILE *)stderr };
198ofstream & serr = serrFile, & stderr = serrFile;
199
200static ofstream lsoutFile = { (FILE *)stdout };
201ofstream & lsout = lsoutFile;
202
203static ofstream exitFile = { (FILE *)stdout };
204ofstream & exit = exitFile;
205static ofstream abortFile = { (FILE *)stderr };
206ofstream & abort = abortFile;
207
208ofstream & nl( ofstream & os ) {
209        nl$( os );                                                                                      // call basic_ostream nl
210        flush( os );
211        return os;
212        // (ofstream &)(os | '\n');
213        // setPrt$( os, false );                                                        // turn off
214        // setNL$( os, true );
215        // flush( os );
216        // return sepOff( os );                                                 // prepare for next line
217} // nl
218
219
220// *********************************** ifstream ***********************************
221
222
223// private
224void ?{}( ifstream & is, void * file ) {
225        is.file$ = file;
226        is.nlOnOff$ = false;
227        is.acquired$ = false;
228} // ?{}
229
230// public
231void ?{}( ifstream & is ) { is.file$ = 0p; }
232
233void ?{}( ifstream & is, const char name[], const char mode[] ) {
234        open( is, name, mode );
235} // ?{}
236
237void ?{}( ifstream & is, const char name[] ) {
238        open( is, name, "r" );
239} // ?{}
240
241void ^?{}( ifstream & is ) {
242        close( is );
243} // ^?{}
244
245void nlOn( ifstream & os ) { os.nlOnOff$ = true; }
246void nlOff( ifstream & os ) { os.nlOnOff$ = false; }
247bool getANL( ifstream & os ) { return os.nlOnOff$; }
248
249bool fail( ifstream & is ) {
250        return is.file$ == 0p || ferror( (FILE *)(is.file$) );
251} // fail
252
253void clear( ifstream & is ) {
254        clearerr( (FILE *)(is.file$) );
255} // clear
256
257void ends( ifstream & is ) {
258        if ( is.acquired$ ) { is.acquired$ = false; release( is ); }
259} // ends
260
261bool eof( ifstream & is ) {
262        return feof( (FILE *)(is.file$) );
263} // eof
264
265void open( ifstream & is, const char name[], const char mode[] ) {
266        FILE * file = fopen( name, mode );
267        // #ifdef __CFA_DEBUG__
268        if ( file == 0p ) {
269                throw (Open_Failure){ is };
270                // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
271        } // if
272        // #endif // __CFA_DEBUG__
273        is.file$ = file;
274} // open
275
276void open( ifstream & is, const char name[] ) {
277        open( is, name, "r" );
278} // open
279
280void close( ifstream & is ) {
281  if ( (FILE *)(is.file$) == 0p ) return;
282  if ( (FILE *)(is.file$) == (FILE *)stdin ) return;
283
284        if ( fclose( (FILE *)(is.file$) ) == EOF ) {
285                throw (Close_Failure){ is };
286                // abort | IO_MSG "close input" | nl | strerror( errno );
287        } // if
288        is.file$ = 0p;
289} // close
290
291ifstream & read( ifstream & is, char data[], size_t size ) {
292        if ( fail( is ) ) {
293                throw (Read_Failure){ is };
294                // abort | IO_MSG "attempt read I/O on failed stream";
295        } // if
296
297        if ( fread( data, size, 1, (FILE *)(is.file$) ) == 0 ) {
298                throw (Read_Failure){ is };
299                // abort | IO_MSG "read" | nl | strerror( errno );
300        } // if
301        return is;
302} // read
303
304ifstream &ungetc( ifstream & is, char c ) {
305        if ( fail( is ) ) {
306                abort | IO_MSG "attempt ungetc I/O on failed stream";
307        } // if
308
309        if ( ungetc( c, (FILE *)(is.file$) ) == EOF ) {
310                abort | IO_MSG "ungetc" | nl | strerror( errno );
311        } // if
312        return is;
313} // ungetc
314
315int fmt( ifstream & is, const char format[], ... ) {
316        va_list args;
317
318        va_start( args, format );
319        int len = vfscanf( (FILE *)(is.file$), format, args );
320        if ( len == EOF ) {
321                if ( ferror( (FILE *)(is.file$) ) ) {
322                        abort | IO_MSG "invalid read";
323                } // if
324        } // if
325        va_end( args );
326        return len;
327} // fmt
328
329inline void acquire( ifstream & is ) {
330        lock( is.lock$ );
331        if ( ! is.acquired$ ) is.acquired$ = true;
332        else unlock( is.lock$ );
333} // acquire
334
335inline void release( ifstream & is ) {
336        unlock( is.lock$ );
337} // release
338
339void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.lock$ ); }
340void ^?{}( isacquire & acq ) { release( acq.is ); }
341
342static ifstream sinFile = { (FILE *)stdin };
343ifstream & sin = sinFile, & stdin = sinFile;
344
345
346// *********************************** exceptions ***********************************
347
348
349static vtable(Open_Failure) Open_Failure_vt;
350
351// exception I/O constructors
352void ?{}( Open_Failure & this, ofstream & ostream ) {
353        this.virtual_table = &Open_Failure_vt;
354        this.ostream = &ostream;
355        this.tag = 1;
356} // ?{}
357
358void ?{}( Open_Failure & this, ifstream & istream ) {
359        this.virtual_table = &Open_Failure_vt;
360        this.istream = &istream;
361        this.tag = 0;
362} // ?{}
363
364
365static vtable(Close_Failure) Close_Failure_vt;
366
367// exception I/O constructors
368void ?{}( Close_Failure & this, ofstream & ostream ) {
369        this.virtual_table = &Close_Failure_vt;
370        this.ostream = &ostream;
371        this.tag = 1;
372} // ?{}
373
374void ?{}( Close_Failure & this, ifstream & istream ) {
375        this.virtual_table = &Close_Failure_vt;
376        this.istream = &istream;
377        this.tag = 0;
378} // ?{}
379
380
381static vtable(Write_Failure) Write_Failure_vt;
382
383// exception I/O constructors
384void ?{}( Write_Failure & this, ofstream & ostream ) {
385        this.virtual_table = &Write_Failure_vt;
386        this.ostream = &ostream;
387        this.tag = 1;
388} // ?{}
389
390void ?{}( Write_Failure & this, ifstream & istream ) {
391        this.virtual_table = &Write_Failure_vt;
392        this.istream = &istream;
393        this.tag = 0;
394} // ?{}
395
396
397static vtable(Read_Failure) Read_Failure_vt;
398
399// exception I/O constructors
400void ?{}( Read_Failure & this, ofstream & ostream ) {
401        this.virtual_table = &Read_Failure_vt;
402        this.ostream = &ostream;
403        this.tag = 1;
404} // ?{}
405
406void ?{}( Read_Failure & this, ifstream & istream ) {
407        this.virtual_table = &Read_Failure_vt;
408        this.istream = &istream;
409        this.tag = 0;
410} // ?{}
411
412// void throwOpen_Failure( ofstream & ostream ) {
413//      Open_Failure exc = { ostream };
414// }
415
416// void throwOpen_Failure( ifstream & istream ) {
417//      Open_Failure exc = { istream };
418// }
419
420// Local Variables: //
421// tab-width: 4 //
422// End: //
Note: See TracBrowser for help on using the repository browser.