source: src/libcfa/fstream.c @ f1a4ccb

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since f1a4ccb was 91c389a, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

change usages of assert to assert.h

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