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