Skip to content

Commit 096c0cd

Browse files
committed
Split code to open data file open from actually running checktestdata.
1 parent 44058cc commit 096c0cd

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

checktestdata.cc

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,27 @@ int main(int argc, char **argv)
136136
exit(exit_failure);
137137
}
138138

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+
139154
// Set options for checksyntax
140155
int options=0;
141156
if (whitespace_ok) options |= opt_whitespace_ok;
142157
if (debugging ) options |= opt_debugging;
143158
if (quiet ) options |= opt_quiet;
144159

145-
// Check for testdata file and check syntax
146-
bool testdata_ok = 0;
147-
148160
init_checktestdata(prog, options);
149161

150162
// Parse presets after initialization to have debugging available
@@ -153,35 +165,15 @@ int main(int argc, char **argv)
153165
exit(exit_failure);
154166
}
155167

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);
162171
} 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);
182173
}
183174

184175
prog.close();
176+
if ( fdata.is_open() ) fdata.close();
185177

186178
if ( !generate ) {
187179
if ( !testdata_ok ) {

0 commit comments

Comments
 (0)