source: src/libcfa/fstream.c @ 89e1b16

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 89e1b16 was cb91437, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

additions to tuple separator for sout

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