source: libcfa/src/fstream.cfa @ a539fc3

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since a539fc3 was fd8f88f, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

remove special FILE names, like _IO_2_1_stdout_

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