source: libcfa/src/fstream.cfa @ 5cb2b8c

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

update ofstream constructor

  • Property mode set to 100644
File size: 6.9 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 May 16 08:33:28 2019
13// Update Count     : 328
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 ) {
30        os.file = file;
31        os.sepDefault = true;
32        os.sepOnOff = false;
33        os.nlOnOff = true;
34        os.prt = false;
35        os.sawNL = false;
36        sepSet( os, " " );
37        sepSetCur( os, sepGet( os ) );
38        sepSetTuple( os, ", " );
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 };
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_) };
155ofstream & sout = soutFile;
156static ofstream serrFile = { (FILE *)(&_IO_2_1_stderr_) };
157ofstream & serr = serrFile;
158
159// static ofstream sexitFile = { (FILE *)(&_IO_2_1_stdout_) };
160// ofstream & sexit = sexitFile;
161// static ofstream sabortFile = { (FILE *)(&_IO_2_1_stderr_) };
162// ofstream & sabort = sabortFile;
163
164void nl( ofstream & os ) {
165        if ( getANL( os ) ) (ofstream &)(nl( os ));                     // implementation only
166        else setPrt( os, false );                                                       // turn off
167}
168
169//---------------------------------------
170
171// private
172void ?{}( ifstream & is, void * file ) {
173        is.file = file;
174        is.nlOnOff = false;
175}
176
177// public
178void ?{}( ifstream & is ) {     is.file = 0; }
179
180void ?{}( ifstream & is, const char * name, const char * mode ) {
181        open( is, name, mode );
182}
183void ?{}( ifstream & is, const char * name ) {
184        open( is, name, "r" );
185}
186
187void nlOn( ifstream & os ) { os.nlOnOff = true; }
188void nlOff( ifstream & os ) { os.nlOnOff = false; }
189bool getANL( ifstream & os ) { return os.nlOnOff; }
190
191int fail( ifstream & is ) {
192        return is.file == 0 || ferror( (FILE *)(is.file) );
193} // fail
194
195int eof( ifstream & is ) {
196        return feof( (FILE *)(is.file) );
197} // eof
198
199void open( ifstream & is, const char * name, const char * mode ) {
200        FILE * file = fopen( name, mode );
201        #ifdef __CFA_DEBUG__
202        if ( file == 0 ) {
203                abort( IO_MSG "open input file \"%s\", %s\n", name, strerror( errno ) );
204        } // if
205        #endif // __CFA_DEBUG__
206        is.file = file;
207} // open
208
209void open( ifstream & is, const char * name ) {
210        open( is, name, "r" );
211} // open
212
213void close( ifstream & is ) {
214        if ( (FILE *)(is.file) == stdin ) return;
215
216        if ( fclose( (FILE *)(is.file) ) == EOF ) {
217                abort( IO_MSG "close input %s", strerror( errno ) );
218        } // if
219} // close
220
221ifstream & read( ifstream & is, char * data, size_t size ) {
222        if ( fail( is ) ) {
223                abort( "attempt read I/O on failed stream\n" );
224        } // if
225
226        if ( fread( data, size, 1, (FILE *)(is.file) ) == 0 ) {
227                abort( IO_MSG "read %s", strerror( errno ) );
228        } // if
229        return is;
230} // read
231
232ifstream &ungetc( ifstream & is, char c ) {
233        if ( fail( is ) ) {
234                abort( "attempt ungetc I/O on failed stream\n" );
235        } // if
236
237        if ( ungetc( c, (FILE *)(is.file) ) == EOF ) {
238                abort( IO_MSG "ungetc %s", strerror( errno ) );
239        } // if
240        return is;
241} // ungetc
242
243int fmt( ifstream & is, const char format[], ... ) {
244        va_list args;
245
246        va_start( args, format );
247        int len = vfscanf( (FILE *)(is.file), format, args );
248        if ( len == EOF ) {
249                if ( ferror( (FILE *)(is.file) ) ) {
250                        abort( "invalid read\n" );
251                } // if
252        } // if
253        va_end( args );
254        return len;
255} // fmt
256
257
258static ifstream sinFile = { (FILE *)(&_IO_2_1_stdin_) };
259ifstream & sin = sinFile;
260
261// Local Variables: //
262// tab-width: 4 //
263// End: //
Note: See TracBrowser for help on using the repository browser.