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