source: libcfa/src/fstream.cfa @ f48577a

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

add stream function clear, change eof to return bool

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