source: libcfa/src/fstream.cfa @ b78129a

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

add feature to input streams to read/not-read newline characters

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