source: libcfa/src/fstream.cfa @ ca33b15

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

add concurrency lock to IO stream and provide user interface to lock stream

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