@@ -136,15 +136,27 @@ int main(int argc, char **argv)
136
136
exit (exit_failure);
137
137
}
138
138
139
+ // Check for testdata file
140
+ fstream fdata;
141
+ if ( argc>optind+1 ) {
142
+ char *datafile = argv[optind+1 ];
143
+ ios_base::openmode mode = generate ? ios_base::out|ios_base::trunc : ios_base::in;
144
+ fdata = fstream (datafile, mode);
145
+ if ( fdata.fail () ) {
146
+ cerr << " Error opening '" << datafile << " '.\n " ;
147
+ exit (exit_failure);
148
+ }
149
+ }
150
+ iostream& data = fdata.is_open () ? static_cast <iostream&>(fdata)
151
+ : (generate ? static_cast <iostream&>(cout)
152
+ : static_cast <iostream&>(cin) );
153
+
139
154
// Set options for checksyntax
140
155
int options=0 ;
141
156
if (whitespace_ok) options |= opt_whitespace_ok;
142
157
if (debugging ) options |= opt_debugging;
143
158
if (quiet ) options |= opt_quiet;
144
159
145
- // Check for testdata file and check syntax
146
- bool testdata_ok = 0 ;
147
-
148
160
init_checktestdata (prog, options);
149
161
150
162
// Parse presets after initialization to have debugging available
@@ -153,35 +165,15 @@ int main(int argc, char **argv)
153
165
exit (exit_failure);
154
166
}
155
167
156
- if ( argc<=optind+1 ) {
157
- if ( generate ) {
158
- gentestdata (cout);
159
- } else {
160
- testdata_ok = checksyntax (cin);
161
- }
168
+ bool testdata_ok = false ;
169
+ if ( generate ) {
170
+ gentestdata (data);
162
171
} else {
163
- if ( generate ) {
164
- char *datafile = argv[optind+1 ];
165
- ofstream fout (datafile);
166
- if ( fout.fail () ) {
167
- cerr << " Error opening '" << datafile << " '.\n " ;
168
- exit (exit_failure);
169
- }
170
- gentestdata (fout);
171
- fout.close ();
172
- } else {
173
- char *datafile = argv[optind+1 ];
174
- ifstream fin (datafile);
175
- if ( fin.fail () ) {
176
- cerr << " Error opening '" << datafile << " '.\n " ;
177
- exit (exit_failure);
178
- }
179
- testdata_ok = checksyntax (fin);
180
- fin.close ();
181
- }
172
+ testdata_ok = checksyntax (data);
182
173
}
183
174
184
175
prog.close ();
176
+ if ( fdata.is_open () ) fdata.close ();
185
177
186
178
if ( !generate ) {
187
179
if ( !testdata_ok ) {
0 commit comments