source: libcfa/src/fstream.cfa @ 9c4330d5

Last change on this file since 9c4330d5 was bbdf954, checked in by Peter A. Buhr <pabuhr@…>, 17 months ago

fix end-of-file bug when checking for I/O errors

  • Property mode set to 100644
File size: 11.2 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
[bbdf954]12// Last Modified On : Sat Jun 17 08:51:12 2023
13// Update Count     : 528
[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
[cce4648]25#pragma GCC visibility push(default)
26
[8d321f9]27// *********************************** ofstream ***********************************
[65240bb]28
29
[53ba273]30#define IO_MSG "I/O error: "
[6ba0659]31
[7ce2483]32// private
33void ?{}( ofstream & os, void * file ) with( os ) {
[3bf3b6b]34        file$ = file;
35        sepDefault$ = true;
36        sepOnOff$ = false;
37        nlOnOff$ = true;
38        prt$ = false;
39        sawNL$ = false;
[6c5d92f]40        sepSetCur$( os, sepGet( os ) );
[5cb2b8c]41        sepSet( os, " " );
42        sepSetTuple( os, ", " );
[65240bb]43} // ?{}
[0583064b]44
[7ce2483]45inline bool sepPrt$( ofstream & os ) { setNL$( os, false ); return os.sepOnOff$; }
46inline void sepReset$( ofstream & os ) { os.sepOnOff$ = os.sepDefault$; }
47inline void sepReset$( ofstream & os, bool reset ) { os.sepDefault$ = reset; os.sepOnOff$ = os.sepDefault$; }
48inline const char * sepGetCur$( ofstream & os ) { return os.sepCur$; }
49inline void sepSetCur$( ofstream & os, const char sepCur[] ) { os.sepCur$ = sepCur; }
50inline bool getNL$( ofstream & os ) { return os.sawNL$; }
51inline void setNL$( ofstream & os, bool state ) { os.sawNL$ = state; }
52inline bool getANL$( ofstream & os ) { return os.nlOnOff$; }
53inline bool getPrt$( ofstream & os ) { return os.prt$; }
54inline void setPrt$( ofstream & os, bool state ) { os.prt$ = state; }
55
[c52f033]56inline void lock( ofstream & os ) with( os ) { lock( os.lock$ ); }
[7ce2483]57inline void unlock( ofstream & os ) { unlock( os.lock$ ); }
[829c907]58
[9ebd778]59// public
[6c5d92f]60void ?{}( ofstream & os ) { os.file$ = 0p; }
[7ce2483]61void ?{}( ofstream & os, const char name[], const char mode[] ) { open( os, name, mode ); }
62void ?{}( ofstream & os, const char name[] ) { open( os, name, "w" ); }
63void ^?{}( ofstream & os ) { close( os ); }
[4cae032]64
[6c5d92f]65void sepOn( ofstream & os ) { os.sepOnOff$ = ! getNL$( os ); }
66void sepOff( ofstream & os ) { os.sepOnOff$ = false; }
[09687aa]67
[93c2e0a]68bool sepDisable( ofstream & os ) {
[6c5d92f]69        bool temp = os.sepDefault$;
70        os.sepDefault$ = false;
71        sepReset$( os );
[53ba273]72        return temp;
73} // sepDisable
[6152c81]74
[93c2e0a]75bool sepEnable( ofstream & os ) {
[6c5d92f]76        bool temp = os.sepDefault$;
77        os.sepDefault$ = true;
78        if ( os.sepOnOff$ ) sepReset$( os );                            // start of line ?
[53ba273]79        return temp;
80} // sepEnable
[90c3b1c]81
[6c5d92f]82void nlOn( ofstream & os ) { os.nlOnOff$ = true; }
83void nlOff( ofstream & os ) { os.nlOnOff$ = false; }
[200fcb3]84
[6c5d92f]85const char * sepGet( ofstream & os ) { return os.separator$; }
[e3fea42]86void sepSet( ofstream & os, const char s[] ) {
[9ebd778]87        assert( s );
[b431515]88        strncpy( os.separator$, s, ofstream_sepSize - 1 );
89        os.separator$[ofstream_sepSize - 1] = '\0';
[9ebd778]90} // sepSet
91
[6c5d92f]92const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator$; }
[e3fea42]93void sepSetTuple( ofstream & os, const char s[] ) {
[9ebd778]94        assert( s );
[b431515]95        strncpy( os.tupleSeparator$, s, ofstream_sepSize - 1 );
96        os.tupleSeparator$[ofstream_sepSize - 1] = '\0';
[9ebd778]97} // sepSet
98
[65240bb]99void ends( ofstream & os ) {
[6c5d92f]100        if ( getANL$( os ) ) nl( os );
101        else setPrt$( os, false );                                                      // turn off
[65240bb]102        if ( &os == &exit ) exit( EXIT_FAILURE );
103        if ( &os == &abort ) abort();
104} // ends
105
[7ce2483]106bool fail( ofstream & os ) { return os.file$ == 0 || ferror( (FILE *)(os.file$) ); }
107void clear( ofstream & os ) { clearerr( (FILE *)(os.file$) ); }
108int flush( ofstream & os ) { return fflush( (FILE *)(os.file$) ); }
[6ba0659]109
[e3fea42]110void open( ofstream & os, const char name[], const char mode[] ) {
[8dcb832]111        FILE * file;
112    for ( cnt; 10 ) {
113                errno = 0;
114                file = fopen( name, mode );
115          if ( file != 0p || errno != EINTR ) break;            // timer interrupt ?
116          if ( cnt == 9 ) abort( "ofstream open EINTR spinning exceeded" );
117    } // for
[d1a9ff5]118        if ( file == 0p ) {
[874b16e]119                throw (open_failure){ os };
[8d321f9]120                // abort | IO_MSG "open output file \"" | name | "\"" | nl | strerror( errno );
[93c2e0a]121        } // if
[cce4648]122        (os){ file };                                                                           // initialize
[6ba0659]123} // open
124
[7ce2483]125void open( ofstream & os, const char name[] ) { open( os, name, "w" ); }
[8da74119]126
[7ce2483]127void close( ofstream & os ) with( os ) {
[3bf3b6b]128  if ( (FILE *)(file$) == 0p ) return;
129  if ( (FILE *)(file$) == (FILE *)stdout || (FILE *)(file$) == (FILE *)stderr ) return;
[6ba0659]130
[8dcb832]131        int ret;
132    for ( cnt; 10 ) {
133                errno = 0;
134                ret = fclose( (FILE *)(file$) );
135          if ( ret != EOF || errno != EINTR ) break;            // timer interrupt ?
136          if ( cnt == 9 ) abort( "ofstream open EINTR spinning exceeded" );
137    } // for
138        if ( ret == EOF ) {
[874b16e]139                throw (close_failure){ os };
[ba0d2ea]140                // abort | IO_MSG "close output" | nl | strerror( errno );
[356189a]141        } // if
[7ce2483]142        file$ = 0p;                                                                                     // safety after close
[6ba0659]143} // close
144
[e3fea42]145ofstream & write( ofstream & os, const char data[], size_t size ) {
[6ba0659]146        if ( fail( os ) ) {
[874b16e]147                throw (write_failure){ os };
[ba0d2ea]148                // abort | IO_MSG "attempt write I/O on failed stream";
[6ba0659]149        } // if
150
[6c5d92f]151        if ( fwrite( data, 1, size, (FILE *)(os.file$) ) != size ) {
[874b16e]152                throw (write_failure){ os };
[ba0d2ea]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 );
[cce4648]161
[8dcb832]162        int len;
163    for ( cnt; 10 ) {
164                errno = 0;
[7a1b7e6]165                disable_interrupts();
[8dcb832]166                len = vfprintf( (FILE *)(os.file$), format, args );
[7a1b7e6]167                enable_interrupts();
[8dcb832]168          if ( len != EOF || errno != EINTR ) break;            // timer interrupt ?
169          if ( cnt == 9 ) abort( "ofstream fmt EINTR spinning exceeded" );
170    } // for
[bbdf954]171        if ( len == EOF ) {                                                                     // error writing ?
172                abort | IO_MSG "invalid write";
[90c3b1c]173        } // if
[5d125e4]174        va_end( args );
[b72bad4f]175
[6c5d92f]176        setPrt$( os, true );                                                            // called in output cascade
177        sepReset$( os );                                                                        // reset separator
[90c3b1c]178        return len;
[829c907]179} // fmt
180
[fd8f88f]181static ofstream soutFile = { (FILE *)stdout };
[a87d40b]182ofstream & sout = soutFile, & stdout = soutFile;
[fd8f88f]183static ofstream serrFile = { (FILE *)stderr };
[a87d40b]184ofstream & serr = serrFile, & stderr = serrFile;
[51b7345]185
[e474cf09]186static ofstream lsoutFile = { (FILE *)stdout };
187ofstream & lsout = lsoutFile;
188
[fd8f88f]189static ofstream exitFile = { (FILE *)stdout };
[65240bb]190ofstream & exit = exitFile;
[fd8f88f]191static ofstream abortFile = { (FILE *)stderr };
[65240bb]192ofstream & abort = abortFile;
[5cb2b8c]193
[f451177]194ofstream & nl( ofstream & os ) {
195        nl$( os );                                                                                      // call basic_ostream nl
196        flush( os );
197        return os;
198} // nl
[90c3b1c]199
[00e9be9]200
[8d321f9]201// *********************************** ifstream ***********************************
[65240bb]202
[51b7345]203
[09687aa]204// private
[7ce2483]205void ?{}( ifstream & is, void * file ) with( is ) {
[3bf3b6b]206        file$ = file;
207        nlOnOff$ = false;
[65240bb]208} // ?{}
[09687aa]209
[c8371b5]210bool getANL$( ifstream & os ) { return os.nlOnOff$; }
211
[7ce2483]212inline void lock( ifstream & os ) with( os ) { lock( os.lock$ ); }
213inline void unlock( ifstream & os ) { unlock( os.lock$ ); }
214
[09687aa]215// public
[6c5d92f]216void ?{}( ifstream & is ) { is.file$ = 0p; }
[7ce2483]217void ?{}( ifstream & is, const char name[], const char mode[] ) { open( is, name, mode ); }
218void ?{}( ifstream & is, const char name[] ) { open( is, name, "r" ); }
219void ^?{}( ifstream & is ) { close( is ); }
[4cae032]220
[7ce2483]221bool fail( ifstream & is ) { return is.file$ == 0p || ferror( (FILE *)(is.file$) ); }
222void clear( ifstream & is ) { clearerr( (FILE *)(is.file$) ); }
[00e9be9]223
[c8371b5]224void nlOn( ifstream & os ) { os.nlOnOff$ = true; }
225void nlOff( ifstream & os ) { os.nlOnOff$ = false; }
226
227void ends( ifstream & is ) {}
[e474cf09]228
[c8371b5]229bool eof( ifstream & is ) { return feof( (FILE *)(is.file$) ) != 0; }
[6ba0659]230
[e3fea42]231void open( ifstream & is, const char name[], const char mode[] ) {
[8dcb832]232        FILE * file;
233    for ( cnt; 10 ) {
234                errno = 0;
235                file = fopen( name, mode );
236          if ( file != 0p || errno != EINTR ) break;            // timer interrupt ?
237          if ( cnt == 9 ) abort( "ifstream open EINTR spinning exceeded" );
238    } // for
[d1a9ff5]239        if ( file == 0p ) {
[874b16e]240                throw (open_failure){ is };
[8d321f9]241                // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
[93c2e0a]242        } // if
[cce4648]243        (is){ file };                                                                           // initialize
[90c3b1c]244} // open
[6ba0659]245
[7ce2483]246void open( ifstream & is, const char name[] ) { open( is, name, "r" ); }
[8da74119]247
[7ce2483]248void close( ifstream & is ) with( is ) {
[3bf3b6b]249  if ( (FILE *)(file$) == 0p ) return;
250  if ( (FILE *)(file$) == (FILE *)stdin ) return;
[90c3b1c]251
[8dcb832]252        int ret;
253    for ( cnt; 10 ) {
254                errno = 0;
255                ret = fclose( (FILE *)(file$) );
256          if ( ret != EOF || errno != EINTR ) break;            // timer interrupt ?
257          if ( cnt == 9 ) abort( "ifstream close EINTR spinning exceeded" );
258    } // for
259        if ( ret == EOF ) {
[874b16e]260                throw (close_failure){ is };
[ba0d2ea]261                // abort | IO_MSG "close input" | nl | strerror( errno );
[356189a]262        } // if
[8dcb832]263        file$ = 0p;                                                                                     // safety after close
[90c3b1c]264} // close
265
[00e9be9]266ifstream & read( ifstream & is, char data[], size_t size ) {
[6ba0659]267        if ( fail( is ) ) {
[874b16e]268                throw (read_failure){ is };
[ba0d2ea]269                // abort | IO_MSG "attempt read I/O on failed stream";
[6ba0659]270        } // if
271
[6c5d92f]272        if ( fread( data, size, 1, (FILE *)(is.file$) ) == 0 ) {
[874b16e]273                throw (read_failure){ is };
[ba0d2ea]274                // abort | IO_MSG "read" | nl | strerror( errno );
[6ba0659]275        } // if
[86bd7c1f]276        return is;
[6ba0659]277} // read
[356189a]278
[09687aa]279ifstream &ungetc( ifstream & is, char c ) {
[6ba0659]280        if ( fail( is ) ) {
[ff2a33e]281                abort | IO_MSG "attempt ungetc I/O on failed stream";
[6ba0659]282        } // if
[51b7345]283
[6c5d92f]284        if ( ungetc( c, (FILE *)(is.file$) ) == EOF ) {
[ff2a33e]285                abort | IO_MSG "ungetc" | nl | strerror( errno );
[6ba0659]286        } // if
287        return is;
288} // ungetc
[51b7345]289
[09687aa]290int fmt( ifstream & is, const char format[], ... ) {
[5d125e4]291        va_list args;
[829c907]292        va_start( args, format );
[8dcb832]293
294        int len;
295    for () {                                                                                    // no check for EINTR limit waiting for keyboard input
296                errno = 0;
[7a1b7e6]297                disable_interrupts();
[8dcb832]298                len = vfscanf( (FILE *)(is.file$), format, args );
[7a1b7e6]299                enable_interrupts();
[8dcb832]300          if ( len != EOF || errno != EINTR ) break;            // timer interrupt ?
301    } // for
[bbdf954]302        if ( len == EOF ) {                                                                     // EOF or matching failure ?
303                if ( ! feof( (FILE *)(is.file$) ) && 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
[fd8f88f]311static ifstream sinFile = { (FILE *)stdin };
[a87d40b]312ifstream & sin = sinFile, & stdin = sinFile;
[86bd7c1f]313
[91e52be]314
[8d321f9]315// *********************************** exceptions ***********************************
[91e52be]316
317
[874b16e]318static vtable(open_failure) open_failure_vt;
[a5a6a1a8]319
320// exception I/O constructors
[874b16e]321void ?{}( open_failure & ex, ofstream & ostream ) with(ex) {
322        virtual_table = &open_failure_vt;
[3bf3b6b]323        ostream = &ostream;
324        tag = 1;
[a5a6a1a8]325} // ?{}
326
[874b16e]327void ?{}( open_failure & ex, ifstream & istream ) with(ex) {
328        virtual_table = &open_failure_vt;
[3bf3b6b]329        istream = &istream;
330        tag = 0;
[a5a6a1a8]331} // ?{}
332
333
[874b16e]334static vtable(close_failure) close_failure_vt;
[ba0d2ea]335
336// exception I/O constructors
[874b16e]337void ?{}( close_failure & ex, ofstream & ostream ) with(ex) {
338        virtual_table = &close_failure_vt;
[3bf3b6b]339        ostream = &ostream;
340        tag = 1;
[ba0d2ea]341} // ?{}
342
[874b16e]343void ?{}( close_failure & ex, ifstream & istream ) with(ex) {
344        virtual_table = &close_failure_vt;
[3bf3b6b]345        istream = &istream;
346        tag = 0;
[ba0d2ea]347} // ?{}
348
349
[874b16e]350static vtable(write_failure) write_failure_vt;
[ba0d2ea]351
352// exception I/O constructors
[874b16e]353void ?{}( write_failure & ex, ofstream & ostream ) with(ex) {
354        virtual_table = &write_failure_vt;
[3bf3b6b]355        ostream = &ostream;
356        tag = 1;
[ba0d2ea]357} // ?{}
358
[874b16e]359void ?{}( write_failure & ex, ifstream & istream ) with(ex) {
360        virtual_table = &write_failure_vt;
[3bf3b6b]361        istream = &istream;
362        tag = 0;
[ba0d2ea]363} // ?{}
364
365
[874b16e]366static vtable(read_failure) read_failure_vt;
[ba0d2ea]367
368// exception I/O constructors
[874b16e]369void ?{}( read_failure & ex, ofstream & ostream ) with(ex) {
370        virtual_table = &read_failure_vt;
[3bf3b6b]371        ostream = &ostream;
372        tag = 1;
[ba0d2ea]373} // ?{}
374
[874b16e]375void ?{}( read_failure & ex, ifstream & istream ) with(ex) {
376        virtual_table = &read_failure_vt;
[3bf3b6b]377        istream = &istream;
378        tag = 0;
[ba0d2ea]379} // ?{}
380
[874b16e]381// void throwopen_failure( ofstream & ostream ) {
382//      open_failure exc = { ostream };
[ba0d2ea]383// }
384
[874b16e]385// void throwopen_failure( ifstream & istream ) {
386//      open_failure exc = { istream };
[ba0d2ea]387// }
[91e52be]388
[86bd7c1f]389// Local Variables: //
390// tab-width: 4 //
391// End: //
Note: See TracBrowser for help on using the repository browser.