source: libcfa/src/fstream.cfa@ 7a63486

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 7a63486 was 8a25be9, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

change exits to aborts to get stack trace

  • Property mode set to 100644
File size: 6.6 KB
Line 
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//
7// fstream.c --
8//
9// Author : Peter A. Buhr
10// Created On : Wed May 27 17:56:53 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Mar 28 17:39:53 2019
13// Update Count : 307
14//
15
16#include "fstream.hfa"
17
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
24#include <assert.h>
25#include <errno.h> // errno
26
27#define IO_MSG "I/O error: "
28
29void ?{}( ofstream & os, void * file, bool sepDefault, bool sepOnOff, bool nlOnOff, bool prt, const char * separator, const char * tupleSeparator ) {
30 os.file = file;
31 os.sepDefault = sepDefault;
32 os.sepOnOff = sepOnOff;
33 os.nlOnOff = nlOnOff;
34 os.prt = prt;
35 sepSet( os, separator );
36 sepSetCur( os, sepGet( os ) );
37 sepSetTuple( os, tupleSeparator );
38}
39
40// private
41bool sepPrt( ofstream & os ) { setNL( os, false ); return os.sepOnOff; }
42void sepReset( ofstream & os ) { os.sepOnOff = os.sepDefault; }
43void sepReset( ofstream & os, bool reset ) { os.sepDefault = reset; os.sepOnOff = os.sepDefault; }
44const char * sepGetCur( ofstream & os ) { return os.sepCur; }
45void sepSetCur( ofstream & os, const char * sepCur ) { os.sepCur = sepCur; }
46bool getNL( ofstream & os ) { return os.sawNL; }
47void setNL( ofstream & os, bool state ) { os.sawNL = state; }
48bool getANL( ofstream & os ) { return os.nlOnOff; }
49bool getPrt( ofstream & os ) { return os.prt; }
50void setPrt( ofstream & os, bool state ) { os.prt = state; }
51
52// public
53void ?{}( ofstream & os ) { os.file = 0; }
54
55void ?{}( ofstream & os, const char * name, const char * mode ) {
56 open( os, name, mode );
57}
58void ?{}( ofstream & os, const char * name ) {
59 open( os, name, "w" );
60}
61
62void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); }
63void sepOff( ofstream & os ) { os.sepOnOff = false; }
64
65bool sepDisable( ofstream & os ) {
66 bool temp = os.sepDefault;
67 os.sepDefault = false;
68 sepReset( os );
69 return temp;
70} // sepDisable
71
72bool sepEnable( ofstream & os ) {
73 bool temp = os.sepDefault;
74 os.sepDefault = true;
75 if ( os.sepOnOff ) sepReset( os ); // start of line ?
76 return temp;
77} // sepEnable
78
79void nlOn( ofstream & os ) { os.nlOnOff = true; }
80void nlOff( ofstream & os ) { os.nlOnOff = false; }
81
82const char * sepGet( ofstream & os ) { return os.separator; }
83void sepSet( ofstream & os, const char * s ) {
84 assert( s );
85 strncpy( os.separator, s, sepSize - 1 );
86 os.separator[sepSize - 1] = '\0';
87} // sepSet
88
89const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator; }
90void sepSetTuple( ofstream & os, const char * s ) {
91 assert( s );
92 strncpy( os.tupleSeparator, s, sepSize - 1 );
93 os.tupleSeparator[sepSize - 1] = '\0';
94} // sepSet
95
96int fail( ofstream & os ) {
97 return os.file == 0 || ferror( (FILE *)(os.file) );
98} // fail
99
100int flush( ofstream & os ) {
101 return fflush( (FILE *)(os.file) );
102} // flush
103
104void open( ofstream & os, const char * name, const char * mode ) {
105 FILE *file = fopen( name, mode );
106 #ifdef __CFA_DEBUG__
107 if ( file == 0 ) {
108 abort( IO_MSG "open output file \"%s\", %s", name, strerror( errno ) );
109 } // if
110 #endif // __CFA_DEBUG__
111 (os){ file, true, false, true, false, " ", ", " };
112} // open
113
114void open( ofstream & os, const char * name ) {
115 open( os, name, "w" );
116} // open
117
118void close( ofstream & os ) {
119 if ( (FILE *)(os.file) == stdout || (FILE *)(os.file) == stderr ) return;
120
121 if ( fclose( (FILE *)(os.file) ) == EOF ) {
122 abort( IO_MSG "close output %s", strerror( errno ) );
123 } // if
124} // close
125
126ofstream & write( ofstream & os, const char * data, size_t size ) {
127 if ( fail( os ) ) {
128 abort( "attempt write I/O on failed stream\n" );
129 } // if
130
131 if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) {
132 abort( IO_MSG "write %s", strerror( errno ) );
133 } // if
134 return os;
135} // write
136
137int fmt( ofstream & os, const char format[], ... ) {
138 va_list args;
139 va_start( args, format );
140 int len = vfprintf( (FILE *)(os.file), format, args );
141 if ( len == EOF ) {
142 if ( ferror( (FILE *)(os.file) ) ) {
143 abort( "invalid write\n" );
144 } // if
145 } // if
146 va_end( args );
147
148 setPrt( os, true ); // called in output cascade
149 sepReset( os ); // reset separator
150 return len;
151} // fmt
152
153static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), true, false, true, false, " ", ", " };
154ofstream & sout = soutFile;
155static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), true, false, true, false, " ", ", " };
156ofstream & serr = serrFile;
157
158
159//---------------------------------------
160
161// private
162void ?{}( ifstream & is, void * file ) {
163 is.file = file;
164}
165
166// public
167void ?{}( ifstream & is ) { is.file = 0; }
168
169void ?{}( ifstream & is, const char * name, const char * mode ) {
170 open( is, name, mode );
171}
172void ?{}( ifstream & is, const char * name ) {
173 open( is, name, "r" );
174}
175
176int fail( ifstream & is ) {
177 return is.file == 0 || ferror( (FILE *)(is.file) );
178} // fail
179
180int eof( ifstream & is ) {
181 return feof( (FILE *)(is.file) );
182} // eof
183
184void open( ifstream & is, const char * name, const char * mode ) {
185 FILE * file = fopen( name, mode );
186 #ifdef __CFA_DEBUG__
187 if ( file == 0 ) {
188 abort( IO_MSG "open input file \"%s\", %s\n", name, strerror( errno ) );
189 } // if
190 #endif // __CFA_DEBUG__
191 is.file = file;
192} // open
193
194void open( ifstream & is, const char * name ) {
195 open( is, name, "r" );
196} // open
197
198void close( ifstream & is ) {
199 if ( (FILE *)(is.file) == stdin ) return;
200
201 if ( fclose( (FILE *)(is.file) ) == EOF ) {
202 abort( IO_MSG "close input %s", strerror( errno ) );
203 } // if
204} // close
205
206ifstream & read( ifstream & is, char * data, size_t size ) {
207 if ( fail( is ) ) {
208 abort( "attempt read I/O on failed stream\n" );
209 } // if
210
211 if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
212 abort( IO_MSG "read %s", strerror( errno ) );
213 } // if
214 return is;
215} // read
216
217ifstream &ungetc( ifstream & is, char c ) {
218 if ( fail( is ) ) {
219 abort( "attempt ungetc I/O on failed stream\n" );
220 } // if
221
222 if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
223 abort( IO_MSG "ungetc %s", strerror( errno ) );
224 } // if
225 return is;
226} // ungetc
227
228int fmt( ifstream & is, const char format[], ... ) {
229 va_list args;
230
231 va_start( args, format );
232 int len = vfscanf( (FILE *)(is.file), format, args );
233 if ( len == EOF ) {
234 if ( ferror( (FILE *)(is.file) ) ) {
235 abort( "invalid read\n" );
236 } // if
237 } // if
238 va_end( args );
239 return len;
240} // fmt
241
242
243static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
244ifstream & sin = sinFile;
245
246// Local Variables: //
247// tab-width: 4 //
248// End: //
Note: See TracBrowser for help on using the repository browser.