source: libcfa/src/fstream.cfa@ 42a02ce

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 42a02ce was ba0d2ea, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

add exceptions Close_Failure, Write_Failure, Read_Failure to fstream

  • Property mode set to 100644
File size: 10.8 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
[ba0d2ea]12// Last Modified On : Thu Jul 29 22:34:10 2021
13// Update Count : 454
[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
[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 ) {
[ba0d2ea]144 throw (Close_Failure){ os };
145 // abort | IO_MSG "close output" | nl | strerror( errno );
[356189a]146 } // if
[6c5d92f]147 os.file$ = 0p;
[6ba0659]148} // close
149
[e3fea42]150ofstream & write( ofstream & os, const char data[], size_t size ) {
[6ba0659]151 if ( fail( os ) ) {
[ba0d2ea]152 throw (Write_Failure){ os };
153 // abort | IO_MSG "attempt write I/O on failed stream";
[6ba0659]154 } // if
155
[6c5d92f]156 if ( fwrite( data, 1, size, (FILE *)(os.file$) ) != size ) {
[ba0d2ea]157 throw (Write_Failure){ os };
158 // abort | IO_MSG "write" | nl | strerror( errno );
[839ccbb]159 } // if
[86bd7c1f]160 return os;
[839ccbb]161} // write
[51b73452]162
[09687aa]163int fmt( ofstream & os, const char format[], ... ) {
[5d125e4]164 va_list args;
[829c907]165 va_start( args, format );
[6c5d92f]166 int len = vfprintf( (FILE *)(os.file$), format, args );
[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
[e474cf09]179inline void acquire( ofstream & os ) {
[6c5d92f]180 lock( os.lock$ );
181 if ( ! os.acquired$ ) os.acquired$ = true;
182 else unlock( os.lock$ );
[e474cf09]183} // acquire
184
185inline void release( ofstream & os ) {
[6c5d92f]186 unlock( os.lock$ );
[e474cf09]187} // release
188
[6c5d92f]189void ?{}( osacquire & acq, ofstream & os ) { &acq.os = &os; lock( os.lock$ ); }
[e474cf09]190void ^?{}( osacquire & acq ) { release( acq.os ); }
191
[fd8f88f]192static ofstream soutFile = { (FILE *)stdout };
[a87d40b]193ofstream & sout = soutFile, & stdout = soutFile;
[fd8f88f]194static ofstream serrFile = { (FILE *)stderr };
[a87d40b]195ofstream & serr = serrFile, & stderr = serrFile;
[51b73452]196
[e474cf09]197static ofstream lsoutFile = { (FILE *)stdout };
198ofstream & lsout = lsoutFile;
199
[fd8f88f]200static ofstream exitFile = { (FILE *)stdout };
[65240bb]201ofstream & exit = exitFile;
[fd8f88f]202static ofstream abortFile = { (FILE *)stderr };
[65240bb]203ofstream & abort = abortFile;
[5cb2b8c]204
[f451177]205ofstream & nl( ofstream & os ) {
206 nl$( os ); // call basic_ostream nl
207 flush( os );
208 return os;
209 // (ofstream &)(os | '\n');
210 // setPrt$( os, false ); // turn off
211 // setNL$( os, true );
212 // flush( os );
213 // return sepOff( os ); // prepare for next line
214} // nl
[90c3b1c]215
[00e9be9]216
[8d321f9]217// *********************************** ifstream ***********************************
[65240bb]218
[51b73452]219
[09687aa]220// private
221void ?{}( ifstream & is, void * file ) {
[6c5d92f]222 is.file$ = file;
223 is.nlOnOff$ = false;
224 is.acquired$ = false;
[65240bb]225} // ?{}
[09687aa]226
227// public
[6c5d92f]228void ?{}( ifstream & is ) { is.file$ = 0p; }
[51b73452]229
[e3fea42]230void ?{}( ifstream & is, const char name[], const char mode[] ) {
[8da74119]231 open( is, name, mode );
[65240bb]232} // ?{}
233
[e3fea42]234void ?{}( ifstream & is, const char name[] ) {
[8da74119]235 open( is, name, "r" );
[65240bb]236} // ?{}
[09687aa]237
[4cae032]238void ^?{}( ifstream & is ) {
239 close( is );
240} // ^?{}
241
[6c5d92f]242void nlOn( ifstream & os ) { os.nlOnOff$ = true; }
243void nlOff( ifstream & os ) { os.nlOnOff$ = false; }
244bool getANL( ifstream & os ) { return os.nlOnOff$; }
[0efb269]245
[b431515]246bool fail( ifstream & is ) {
[6c5d92f]247 return is.file$ == 0p || ferror( (FILE *)(is.file$) );
[6ba0659]248} // fail
249
[00e9be9]250void clear( ifstream & is ) {
251 clearerr( (FILE *)(is.file$) );
252} // clear
253
[e474cf09]254void ends( ifstream & is ) {
[6c5d92f]255 if ( is.acquired$ ) { is.acquired$ = false; release( is ); }
[e474cf09]256} // ends
257
[00e9be9]258bool eof( ifstream & is ) {
[6c5d92f]259 return feof( (FILE *)(is.file$) );
[6ba0659]260} // eof
261
[e3fea42]262void open( ifstream & is, const char name[], const char mode[] ) {
[8a25be9]263 FILE * file = fopen( name, mode );
[93c2e0a]264 #ifdef __CFA_DEBUG__
[d1a9ff5]265 if ( file == 0p ) {
[8d321f9]266 throw (Open_Failure){ is };
267 // abort | IO_MSG "open input file \"" | name | "\"" | nl | strerror( errno );
[93c2e0a]268 } // if
269 #endif // __CFA_DEBUG__
[6c5d92f]270 is.file$ = file;
[90c3b1c]271} // open
[6ba0659]272
[e3fea42]273void open( ifstream & is, const char name[] ) {
[8da74119]274 open( is, name, "r" );
275} // open
276
[09687aa]277void close( ifstream & is ) {
[6c5d92f]278 if ( (FILE *)(is.file$) == 0p ) return;
279 if ( (FILE *)(is.file$) == (FILE *)stdin ) return;
[90c3b1c]280
[6c5d92f]281 if ( fclose( (FILE *)(is.file$) ) == EOF ) {
[ba0d2ea]282 throw (Close_Failure){ is };
283 // abort | IO_MSG "close input" | nl | strerror( errno );
[356189a]284 } // if
[6c5d92f]285 is.file$ = 0p;
[90c3b1c]286} // close
287
[00e9be9]288ifstream & read( ifstream & is, char data[], size_t size ) {
[6ba0659]289 if ( fail( is ) ) {
[ba0d2ea]290 throw (Read_Failure){ is };
291 // abort | IO_MSG "attempt read I/O on failed stream";
[6ba0659]292 } // if
293
[6c5d92f]294 if ( fread( data, size, 1, (FILE *)(is.file$) ) == 0 ) {
[ba0d2ea]295 throw (Read_Failure){ is };
296 // abort | IO_MSG "read" | nl | strerror( errno );
[6ba0659]297 } // if
[86bd7c1f]298 return is;
[6ba0659]299} // read
[356189a]300
[09687aa]301ifstream &ungetc( ifstream & is, char c ) {
[6ba0659]302 if ( fail( is ) ) {
[ff2a33e]303 abort | IO_MSG "attempt ungetc I/O on failed stream";
[6ba0659]304 } // if
[51b73452]305
[6c5d92f]306 if ( ungetc( c, (FILE *)(is.file$) ) == EOF ) {
[ff2a33e]307 abort | IO_MSG "ungetc" | nl | strerror( errno );
[6ba0659]308 } // if
309 return is;
310} // ungetc
[51b73452]311
[09687aa]312int fmt( ifstream & is, const char format[], ... ) {
[5d125e4]313 va_list args;
[51b73452]314
[829c907]315 va_start( args, format );
[6c5d92f]316 int len = vfscanf( (FILE *)(is.file$), format, args );
[90c3b1c]317 if ( len == EOF ) {
[6c5d92f]318 if ( ferror( (FILE *)(is.file$) ) ) {
[ff2a33e]319 abort | IO_MSG "invalid read";
[90c3b1c]320 } // if
321 } // if
[5d125e4]322 va_end( args );
[90c3b1c]323 return len;
[829c907]324} // fmt
[51b73452]325
[e474cf09]326inline void acquire( ifstream & is ) {
[6c5d92f]327 lock( is.lock$ );
328 if ( ! is.acquired$ ) is.acquired$ = true;
329 else unlock( is.lock$ );
[e474cf09]330} // acquire
331
332inline void release( ifstream & is ) {
[6c5d92f]333 unlock( is.lock$ );
[e474cf09]334} // release
335
[6c5d92f]336void ?{}( isacquire & acq, ifstream & is ) { &acq.is = &is; lock( is.lock$ ); }
[e474cf09]337void ^?{}( isacquire & acq ) { release( acq.is ); }
338
[fd8f88f]339static ifstream sinFile = { (FILE *)stdin };
[a87d40b]340ifstream & sin = sinFile, & stdin = sinFile;
[86bd7c1f]341
[91e52be]342
[8d321f9]343// *********************************** exceptions ***********************************
[91e52be]344
345
[ba0d2ea]346static vtable(Open_Failure) Open_Failure_vt;
[a5a6a1a8]347
348// exception I/O constructors
[8d321f9]349void ?{}( Open_Failure & this, ofstream & ostream ) {
[ba0d2ea]350 this.virtual_table = &Open_Failure_vt;
[91e52be]351 this.ostream = &ostream;
[8d321f9]352 this.tag = 1;
[a5a6a1a8]353} // ?{}
354
[8d321f9]355void ?{}( Open_Failure & this, ifstream & istream ) {
[ba0d2ea]356 this.virtual_table = &Open_Failure_vt;
[91e52be]357 this.istream = &istream;
[8d321f9]358 this.tag = 0;
[a5a6a1a8]359} // ?{}
360
361
[ba0d2ea]362static vtable(Close_Failure) Close_Failure_vt;
363
364// exception I/O constructors
365void ?{}( Close_Failure & this, ofstream & ostream ) {
366 this.virtual_table = &Close_Failure_vt;
367 this.ostream = &ostream;
368 this.tag = 1;
369} // ?{}
370
371void ?{}( Close_Failure & this, ifstream & istream ) {
372 this.virtual_table = &Close_Failure_vt;
373 this.istream = &istream;
374 this.tag = 0;
375} // ?{}
376
377
378static vtable(Write_Failure) Write_Failure_vt;
379
380// exception I/O constructors
381void ?{}( Write_Failure & this, ofstream & ostream ) {
382 this.virtual_table = &Write_Failure_vt;
383 this.ostream = &ostream;
384 this.tag = 1;
385} // ?{}
386
387void ?{}( Write_Failure & this, ifstream & istream ) {
388 this.virtual_table = &Write_Failure_vt;
389 this.istream = &istream;
390 this.tag = 0;
391} // ?{}
392
393
394static vtable(Read_Failure) Read_Failure_vt;
395
396// exception I/O constructors
397void ?{}( Read_Failure & this, ofstream & ostream ) {
398 this.virtual_table = &Read_Failure_vt;
399 this.ostream = &ostream;
400 this.tag = 1;
401} // ?{}
402
403void ?{}( Read_Failure & this, ifstream & istream ) {
404 this.virtual_table = &Read_Failure_vt;
405 this.istream = &istream;
406 this.tag = 0;
407} // ?{}
408
409// void throwOpen_Failure( ofstream & ostream ) {
410// Open_Failure exc = { ostream };
411// }
412
413// void throwOpen_Failure( ifstream & istream ) {
414// Open_Failure exc = { istream };
415// }
[91e52be]416
[86bd7c1f]417// Local Variables: //
418// tab-width: 4 //
419// End: //
Note: See TracBrowser for help on using the repository browser.