source: libcfa/src/fstream.cfa@ d9b7b66

Last change on this file since d9b7b66 was f5d9c37, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

harmonize separator manipulators names with newline names: change from sep, sepTuple, sepEnable, sepDisable, sepOn, sepOff to sepVal, sepTupleVal, sepOn, sepOff, sep, nosep; fix bug printing null string with WD so no separator

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