source: libcfa/src/fstream.cfa @ c993b15

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since c993b15 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
RevLine 
[86bd7c1f]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//
[356189a]7// fstream.c --
[86bd7c1f]8//
[90c3b1c]9// Author           : Peter A. Buhr
[86bd7c1f]10// Created On       : Wed May 27 17:56:53 2015
[5d125e4]11// Last Modified By : Peter A. Buhr
[f451177]12// Last Modified On : Tue Apr 27 22:08:57 2021
13// Update Count     : 442
[86bd7c1f]14//
15
[f451177]16#include "fstream.hfa"                                                                  // also includes iostream.hfa
[51b7345]17
[90c3b1c]18#include <stdio.h>                                                                              // vfprintf, vfscanf
19#include <stdlib.h>                                                                             // exit
20#include <stdarg.h>                                                                             // varargs
[b431515]21#include <string.h>                                                                             // strncpy, strerror
[91c389a]22#include <assert.h>
[8a25be9]23#include <errno.h>                                                                              // errno
[51b7345]24
[8d321f9]25// *********************************** ofstream ***********************************
[65240bb]26
27
[53ba273]28#define IO_MSG "I/O error: "
[6ba0659]29
[5cb2b8c]30void ?{}( ofstream & os, void * file ) {
[6c5d92f]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 ) );
[5cb2b8c]39        sepSet( os, " " );
40        sepSetTuple( os, ", " );
[65240bb]41} // ?{}
[0583064b]42
[9ebd778]43// private
[6c5d92f]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; }
[829c907]54
[9ebd778]55// public
[6c5d92f]56void ?{}( ofstream & os ) { os.file$ = 0p; }
[829c907]57
[e3fea42]58void ?{}( ofstream & os, const char name[], const char mode[] ) {
[09687aa]59        open( os, name, mode );
[65240bb]60} // ?{}
61
[e3fea42]62void ?{}( ofstream & os, const char name[] ) {
[8da74119]63        open( os, name, "w" );
[65240bb]64} // ?{}
[09687aa]65
[4cae032]66void ^?{}( ofstream & os ) {
67        close( os );
68} // ^?{}
69
[6c5d92f]70void sepOn( ofstream & os ) { os.sepOnOff$ = ! getNL$( os ); }
71void sepOff( ofstream & os ) { os.sepOnOff$ = false; }
[09687aa]72
[93c2e0a]73bool sepDisable( ofstream & os ) {
[6c5d92f]74        bool temp = os.sepDefault$;
75        os.sepDefault$ = false;
76        sepReset$( os );
[53ba273]77        return temp;
78} // sepDisable
[6152c81]79
[93c2e0a]80bool sepEnable( ofstream & os ) {
[6c5d92f]81        bool temp = os.sepDefault$;
82        os.sepDefault$ = true;
83        if ( os.sepOnOff$ ) sepReset$( os );                            // start of line ?
[53ba273]84        return temp;
85} // sepEnable
[90c3b1c]86
[6c5d92f]87void nlOn( ofstream & os ) { os.nlOnOff$ = true; }
88void nlOff( ofstream & os ) { os.nlOnOff$ = false; }
[200fcb3]89
[6c5d92f]90const char * sepGet( ofstream & os ) { return os.separator$; }
[e3fea42]91void sepSet( ofstream & os, const char s[] ) {
[9ebd778]92        assert( s );
[b431515]93        strncpy( os.separator$, s, ofstream_sepSize - 1 );
94        os.separator$[ofstream_sepSize - 1] = '\0';
[9ebd778]95} // sepSet
96
[6c5d92f]97const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator$; }
[e3fea42]98void sepSetTuple( ofstream & os, const char s[] ) {
[9ebd778]99        assert( s );
[b431515]100        strncpy( os.tupleSeparator$, s, ofstream_sepSize - 1 );
101        os.tupleSeparator$[ofstream_sepSize - 1] = '\0';
[9ebd778]102} // sepSet
103
[65240bb]104void ends( ofstream & os ) {
[6c5d92f]105        if ( getANL$( os ) ) nl( os );
106        else setPrt$( os, false );                                                      // turn off
[65240bb]107        if ( &os == &exit ) exit( EXIT_FAILURE );
108        if ( &os == &abort ) abort();
[6c5d92f]109        if ( os.acquired$ ) { os.acquired$ = false; release( os ); }
[65240bb]110} // ends
111
[b431515]112bool fail( ofstream & os ) {
[6c5d92f]113        return os.file$ == 0 || ferror( (FILE *)(os.file$) );
[6ba0659]114} // fail
115
[09687aa]116int flush( ofstream & os ) {
[6c5d92f]117        return fflush( (FILE *)(os.file$) );
[6ba0659]118} // flush
119
[e3fea42]120void open( ofstream & os, const char name[], const char mode[] ) {
[5cb2b8c]121        FILE * file = fopen( name, mode );
[93c2e0a]122        #ifdef __CFA_DEBUG__
[d1a9ff5]123        if ( file == 0p ) {
[8d321f9]124                throw (Open_Failure){ os };
125                // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno );
[93c2e0a]126        } // if
127        #endif // __CFA_DEBUG__
[5cb2b8c]128        (os){ file };
[6ba0659]129} // open
130
[e3fea42]131void open( ofstream & os, const char name[] ) {
[8da74119]132        open( os, name, "w" );
133} // open
134
[09687aa]135void close( ofstream & os ) {
[6c5d92f]136  if ( (FILE *)(os.file$) == 0p ) return;
137  if ( (FILE *)(os.file$) == (FILE *)stdout || (FILE *)(os.file$) == (FILE *)stderr ) return;
[6ba0659]138
[6c5d92f]139        if ( fclose( (FILE *)(os.file$) ) == EOF ) {
[ff2a33e]140                abort | IO_MSG "close output" | nl | strerror( errno );
[356189a]141        } // if
[6c5d92f]142        os.file$ = 0p;
[6ba0659]143} // close
144
[e3fea42]145ofstream & write( ofstream & os, const char data[], size_t size ) {
[6ba0659]146        if ( fail( os ) ) {
[ff2a33e]147                abort | IO_MSG "attempt write I/O on failed stream";
[6ba0659]148        } // if
149
[6c5d92f]150        if ( fwrite( data, 1, size, (FILE *)(os.file$) ) != size ) {
[ff2a33e]151                abort | IO_MSG "write" | nl | strerror( errno );
[839ccbb]152        } // if
[86bd7c1f]153        return os;
[839ccbb]154} // write
[51b7345]155
[09687aa]156int fmt( ofstream & os, const char format[], ... ) {
[5d125e4]157        va_list args;
[829c907]158        va_start( args, format );
[6c5d92f]159        int len = vfprintf( (FILE *)(os.file$), format, args );
[90c3b1c]160        if ( len == EOF ) {
[6c5d92f]161                if ( ferror( (FILE *)(os.file$) ) ) {
[ff2a33e]162                        abort | IO_MSG "invalid write";
[90c3b1c]163                } // if
164        } // if
[5d125e4]165        va_end( args );
[b72bad4f]166
[6c5d92f]167        setPrt$( os, true );                                                            // called in output cascade
168        sepReset$( os );                                                                        // reset separator
[90c3b1c]169        return len;
[829c907]170} // fmt
171
[e474cf09]172inline void acquire( ofstream & os ) {
[6c5d92f]173        lock( os.lock$ );
174        if ( ! os.acquired$ ) os.acquired$ = true;
175        else unlock( os.lock$ );
[e474cf09]176} // acquire
177
178inline void release( ofstream & os ) {
[6c5d92f]179        unlock( os.lock$ );
[e474cf09]180} // release
181
[6c5d92f]182void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.lock$ ); }
[e474cf09]183void ^?{}( osacquire & acq ) { release( acq.os ); }
184
[fd8f88f]185static ofstream soutFile = { (FILE *)stdout };
[a87d40b]186ofstream & sout = soutFile, & stdout = soutFile;
[fd8f88f]187static ofstream serrFile = { (FILE *)stderr };
[a87d40b]188ofstream & serr = serrFile, & stderr = serrFile;
[51b7345]189
[e474cf09]190static ofstream lsoutFile = { (FILE *)stdout };
191ofstream & lsout = lsoutFile;
192
[fd8f88f]193static ofstream exitFile = { (FILE *)stdout };
[65240bb]194ofstream & exit = exitFile;
[fd8f88f]195static ofstream abortFile = { (FILE *)stderr };
[65240bb]196ofstream & abort = abortFile;
[5cb2b8c]197
[f451177]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
[90c3b1c]208
[8d321f9]209// *********************************** ifstream ***********************************
[65240bb]210
[51b7345]211
[09687aa]212// private
213void ?{}( ifstream & is, void * file ) {
[6c5d92f]214        is.file$ = file;
215        is.nlOnOff$ = false;
216        is.acquired$ = false;
[65240bb]217} // ?{}
[09687aa]218
219// public
[6c5d92f]220void ?{}( ifstream & is ) { is.file$ = 0p; }
[51b7345]221
[e3fea42]222void ?{}( ifstream & is, const char name[], const char mode[] ) {
[8da74119]223        open( is, name, mode );
[65240bb]224} // ?{}
225
[e3fea42]226void ?{}( ifstream & is, const char name[] ) {
[8da74119]227        open( is, name, "r" );
[65240bb]228} // ?{}
[09687aa]229
[4cae032]230void ^?{}( ifstream & is ) {
231        close( is );
232} // ^?{}
233
[6c5d92f]234void nlOn( ifstream & os ) { os.nlOnOff$ = true; }
235void nlOff( ifstream & os ) { os.nlOnOff$ = false; }
236bool getANL( ifstream & os ) { return os.nlOnOff$; }
[0efb269]237
[b431515]238bool fail( ifstream & is ) {
[6c5d92f]239        return is.file$ == 0p || ferror( (FILE *)(is.file$) );
[6ba0659]240} // fail
241
[e474cf09]242void ends( ifstream & is ) {
[6c5d92f]243        if ( is.acquired$ ) { is.acquired$ = false; release( is ); }
[e474cf09]244} // ends
245
[09687aa]246int eof( ifstream & is ) {
[6c5d92f]247        return feof( (FILE *)(is.file$) );
[6ba0659]248} // eof
249
[e3fea42]250void open( ifstream & is, const char name[], const char mode[] ) {
[8a25be9]251        FILE * file = fopen( name, mode );
[93c2e0a]252        #ifdef __CFA_DEBUG__
[d1a9ff5]253        if ( file == 0p ) {
[8d321f9]254                throw (Open_Failure){ is };
255                // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
[93c2e0a]256        } // if
257        #endif // __CFA_DEBUG__
[6c5d92f]258        is.file$ = file;
[90c3b1c]259} // open
[6ba0659]260
[e3fea42]261void open( ifstream & is, const char name[] ) {
[8da74119]262        open( is, name, "r" );
263} // open
264
[09687aa]265void close( ifstream & is ) {
[6c5d92f]266  if ( (FILE *)(is.file$) == 0p ) return;
267  if ( (FILE *)(is.file$) == (FILE *)stdin ) return;
[90c3b1c]268
[6c5d92f]269        if ( fclose( (FILE *)(is.file$) ) == EOF ) {
[ff2a33e]270                abort | IO_MSG "close input" | nl | strerror( errno );
[356189a]271        } // if
[6c5d92f]272        is.file$ = 0p;
[90c3b1c]273} // close
274
[91d766d]275ifstream & read( ifstream & is, char * data, size_t size ) {
[6ba0659]276        if ( fail( is ) ) {
[ff2a33e]277                abort | IO_MSG "attempt read I/O on failed stream";
[6ba0659]278        } // if
279
[6c5d92f]280        if ( fread( data, size, 1, (FILE *)(is.file$) ) == 0 ) {
[ff2a33e]281                abort | IO_MSG "read" | nl | strerror( errno );
[6ba0659]282        } // if
[86bd7c1f]283        return is;
[6ba0659]284} // read
[356189a]285
[09687aa]286ifstream &ungetc( ifstream & is, char c ) {
[6ba0659]287        if ( fail( is ) ) {
[ff2a33e]288                abort | IO_MSG "attempt ungetc I/O on failed stream";
[6ba0659]289        } // if
[51b7345]290
[6c5d92f]291        if ( ungetc( c, (FILE *)(is.file$) ) == EOF ) {
[ff2a33e]292                abort | IO_MSG "ungetc" | nl | strerror( errno );
[6ba0659]293        } // if
294        return is;
295} // ungetc
[51b7345]296
[09687aa]297int fmt( ifstream & is, const char format[], ... ) {
[5d125e4]298        va_list args;
[51b7345]299
[829c907]300        va_start( args, format );
[6c5d92f]301        int len = vfscanf( (FILE *)(is.file$), format, args );
[90c3b1c]302        if ( len == EOF ) {
[6c5d92f]303                if ( ferror( (FILE *)(is.file$) ) ) {
[ff2a33e]304                        abort | IO_MSG "invalid read";
[90c3b1c]305                } // if
306        } // if
[5d125e4]307        va_end( args );
[90c3b1c]308        return len;
[829c907]309} // fmt
[51b7345]310
[e474cf09]311inline void acquire( ifstream & is ) {
[6c5d92f]312        lock( is.lock$ );
313        if ( ! is.acquired$ ) is.acquired$ = true;
314        else unlock( is.lock$ );
[e474cf09]315} // acquire
316
317inline void release( ifstream & is ) {
[6c5d92f]318        unlock( is.lock$ );
[e474cf09]319} // release
320
[6c5d92f]321void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.lock$ ); }
[e474cf09]322void ^?{}( isacquire & acq ) { release( acq.is ); }
323
[fd8f88f]324static ifstream sinFile = { (FILE *)stdin };
[a87d40b]325ifstream & sin = sinFile, & stdin = sinFile;
[86bd7c1f]326
[91e52be]327
[8d321f9]328// *********************************** exceptions ***********************************
[91e52be]329
330
[ecfd758]331EHM_VIRTUAL_TABLE(Open_Failure, Open_Failure_main_table);
[8d321f9]332void ?{}( Open_Failure & this, ofstream & ostream ) {
[ecfd758]333        this.virtual_table = &Open_Failure_main_table;
[91e52be]334        this.ostream = &ostream;
[8d321f9]335        this.tag = 1;
[91e52be]336}
[8d321f9]337void ?{}( Open_Failure & this, ifstream & istream ) {
[ecfd758]338        this.virtual_table = &Open_Failure_main_table;
[91e52be]339        this.istream = &istream;
[8d321f9]340        this.tag = 0;
[91e52be]341}
[8d321f9]342void throwOpen_Failure( ofstream & ostream ) {
343        Open_Failure exc = { ostream };
[91e52be]344}
[8d321f9]345void throwOpen_Failure( ifstream & istream ) {
346        Open_Failure exc = { istream };
[91e52be]347}
348
[86bd7c1f]349// Local Variables: //
350// tab-width: 4 //
351// End: //
Note: See TracBrowser for help on using the repository browser.