source: libcfa/src/fstream.cfa @ 637240f

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 637240f was c8371b5, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

change getANL to getANL$ (private)

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