source: src/libcfa/fstream.c@ 71d0eab

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 71d0eab was 09687aa, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

complete conversion of iostream/fstream to use references

  • Property mode set to 100644
File size: 6.0 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 Dec 7 08:35:01 2017
13// Update Count : 270
14//
15
16#include "fstream"
17
18#include <stdio.h> // vfprintf, vfscanf
19#include <stdlib.h> // exit
20#include <stdarg.h> // varargs
21#include <string.h> // strlen
22#include <stdbool.h> // true/false
23#include <float.h> // DBL_DIG, LDBL_DIG
24#include <complex.h> // creal, cimag
25#include <assert.h>
26
27#define IO_MSG "I/O error: "
28
29void ?{}( ofstream & os, void * file, _Bool sepDefault, _Bool sepOnOff, const char * separator, const char * tupleSeparator ) {
30 os.file = file;
31 os.sepDefault = sepDefault;
32 os.sepOnOff = sepOnOff;
33 sepSet( os, separator );
34 sepSetCur( os, sepGet( os ) );
35 sepSetTuple( os, tupleSeparator );
36}
37
38// private
39_Bool sepPrt( ofstream & os ) { setNL( os, false ); return os.sepOnOff; }
40void sepReset( ofstream & os ) { os.sepOnOff = os.sepDefault; }
41void sepReset( ofstream & os, _Bool reset ) { os.sepDefault = reset; os.sepOnOff = os.sepDefault; }
42const char * sepGetCur( ofstream & os ) { return os.sepCur; }
43void sepSetCur( ofstream & os, const char * sepCur ) { os.sepCur = sepCur; }
44_Bool getNL( ofstream & os ) { return os.sawNL; }
45void setNL( ofstream & os, _Bool state ) { os.sawNL = state; }
46
47// public
48void ?{}( ofstream & os ) {}
49
50void ?{}( ofstream & os, const char * name, const char * mode ) {
51 open( os, name, mode );
52}
53
54void sepOn( ofstream & os ) { os.sepOnOff = ! getNL( os ); }
55void sepOff( ofstream & os ) { os.sepOnOff = false; }
56
57_Bool sepDisable( ofstream & os ) {
58 _Bool temp = os.sepDefault;
59 os.sepDefault = false;
60 sepReset( os );
61 return temp;
62} // sepDisable
63
64_Bool sepEnable( ofstream & os ) {
65 _Bool temp = os.sepDefault;
66 os.sepDefault = true;
67 if ( os.sepOnOff ) sepReset( os ); // start of line ?
68 return temp;
69} // sepEnable
70
71const char * sepGet( ofstream & os ) { return os.separator; }
72void sepSet( ofstream & os, const char * s ) {
73 assert( s );
74 strncpy( os.separator, s, sepSize - 1 );
75 os.separator[sepSize - 1] = '\0';
76} // sepSet
77
78const char * sepGetTuple( ofstream & os ) { return os.tupleSeparator; }
79void sepSetTuple( ofstream & os, const char * s ) {
80 assert( s );
81 strncpy( os.tupleSeparator, s, sepSize - 1 );
82 os.tupleSeparator[sepSize - 1] = '\0';
83} // sepSet
84
85int fail( ofstream & os ) {
86 return ferror( (FILE *)(os.file) );
87} // fail
88
89int flush( ofstream & os ) {
90 return fflush( (FILE *)(os.file) );
91} // flush
92
93void open( ofstream & os, const char * name, const char * mode ) {
94 FILE *file = fopen( name, mode );
95 if ( file == 0 ) { // do not change unless successful
96 fprintf( stderr, IO_MSG "open output file \"%s\", ", name );
97 perror( 0 );
98 exit( EXIT_FAILURE );
99 } // if
100 (os){ file, true, false, " ", ", " };
101} // open
102
103void close( ofstream & os ) {
104 if ( (FILE *)(os.file) == stdout || (FILE *)(os.file) == stderr ) return;
105
106 if ( fclose( (FILE *)(os.file) ) == EOF ) {
107 perror( IO_MSG "close output" );
108 } // if
109} // close
110
111ofstream & write( ofstream & os, const char * data, unsigned long int size ) {
112 if ( fail( os ) ) {
113 fprintf( stderr, "attempt write I/O on failed stream\n" );
114 exit( EXIT_FAILURE );
115 } // if
116
117 if ( fwrite( data, 1, size, (FILE *)(os.file) ) != size ) {
118 perror( IO_MSG "write" );
119 exit( EXIT_FAILURE );
120 } // if
121 return os;
122} // write
123
124int fmt( ofstream & os, const char format[], ... ) {
125 va_list args;
126 va_start( args, format );
127 int len = vfprintf( (FILE *)(os.file), format, args );
128 if ( len == EOF ) {
129 if ( ferror( (FILE *)(os.file) ) ) {
130 fprintf( stderr, "invalid write\n" );
131 exit( EXIT_FAILURE );
132 } // if
133 } // if
134 va_end( args );
135
136 sepReset( os ); // reset separator
137 return len;
138} // fmt
139
140static ofstream soutFile = { (FILE *)(&_IO_2_1_stdout_), true, false, " ", ", " };
141ofstream & sout = soutFile;
142static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_), true, false, " ", ", " };
143ofstream & serr = serrFile;
144
145
146//---------------------------------------
147
148// private
149void ?{}( ifstream & is, void * file ) {
150 is.file = file;
151}
152
153// public
154void ?{}( ifstream & is ) {}
155
156void ?{}( ifstream & is, const char * name ) {
157 open( is, name );
158}
159
160int fail( ifstream & is ) {
161 return ferror( (FILE *)(is.file) );
162} // fail
163
164int eof( ifstream & is ) {
165 return feof( (FILE *)(is.file) );
166} // eof
167
168void open( ifstream & is, const char * name ) {
169 FILE *file = fopen( name, "r" );
170 if ( file == 0 ) { // do not change unless successful
171 fprintf( stderr, IO_MSG "open input file \"%s\", ", name );
172 perror( 0 );
173 exit( EXIT_FAILURE );
174 } // if
175 is.file = file;
176} // open
177
178void close( ifstream & is ) {
179 if ( (FILE *)(is.file) == stdin ) return;
180
181 if ( fclose( (FILE *)(is.file) ) == EOF ) {
182 perror( IO_MSG "close input" );
183 } // if
184} // close
185
186ifstream & read( ifstream & is, char * data, unsigned long int size ) {
187 if ( fail( is ) ) {
188 fprintf( stderr, "attempt read I/O on failed stream\n" );
189 exit( EXIT_FAILURE );
190 } // if
191
192 if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
193 perror( IO_MSG "read" );
194 exit( EXIT_FAILURE );
195 } // if
196 return is;
197} // read
198
199ifstream &ungetc( ifstream & is, char c ) {
200 if ( fail( is ) ) {
201 fprintf( stderr, "attempt ungetc I/O on failed stream\n" );
202 exit( EXIT_FAILURE );
203 } // if
204
205 if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
206 perror( IO_MSG "ungetc" );
207 exit( EXIT_FAILURE );
208 } // if
209 return is;
210} // ungetc
211
212int fmt( ifstream & is, const char format[], ... ) {
213 va_list args;
214
215 va_start( args, format );
216 int len = vfscanf( (FILE *)(is.file), format, args );
217 if ( len == EOF ) {
218 if ( ferror( (FILE *)(is.file) ) ) {
219 fprintf( stderr, "invalid read\n" );
220 exit( EXIT_FAILURE );
221 } // if
222 } // if
223 va_end( args );
224 return len;
225} // fmt
226
227
228static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
229ifstream & sin = sinFile;
230
231// Local Variables: //
232// tab-width: 4 //
233// End: //
Note: See TracBrowser for help on using the repository browser.