Skip to content

Commit f3e0b15

Browse files
committed
introduce output_filet
This introduces a new class output_filet, which * given a filename, opens the file for writing, and provides an std::ostream for it; if this fails, an appropriate exception is thrown. * given "-", provides a pointer to std::cout
1 parent 81ec9d7 commit f3e0b15

11 files changed

+123
-110
lines changed

src/ebmc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ SRC = \
2020
live_signal.cpp \
2121
main.cpp \
2222
neural_liveness.cpp \
23+
output_file.cpp \
2324
output_verilog.cpp \
2425
random_traces.cpp \
2526
ranking_function.cpp \

src/ebmc/ebmc_base.cpp

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Author: Daniel Kroening, [email protected]
1212
#include <util/config.h>
1313
#include <util/expr_util.h>
1414
#include <util/string2int.h>
15-
#include <util/unicode.h>
1615

1716
#include <solvers/prop/literal_expr.h>
1817
#include <solvers/sat/satcheck.h>
@@ -29,10 +28,10 @@ Author: Daniel Kroening, [email protected]
2928
#include "ebmc_error.h"
3029
#include "ebmc_solver_factory.h"
3130
#include "ebmc_version.h"
31+
#include "output_file.h"
3232
#include "report_results.h"
3333

3434
#include <chrono>
35-
#include <fstream>
3635
#include <iostream>
3736

3837
/*******************************************************************\
@@ -277,20 +276,13 @@ int ebmc_baset::do_bit_level_bmc()
277276
{
278277
if(cmdline.isset("outfile"))
279278
{
280-
const std::string filename = cmdline.get_value("outfile");
281-
std::ofstream out(widen_if_needed(filename));
279+
auto outfile = output_filet{cmdline.get_value("outfile")};
282280

283-
if(!out)
284-
{
285-
message.error() << "Failed to open `" << filename << "'"
286-
<< messaget::eom;
287-
return 1;
288-
}
289-
290-
message.status() << "Writing DIMACS CNF to `" << filename << "'"
281+
message.status() << "Writing DIMACS CNF to `" << outfile.name() << "'"
291282
<< messaget::eom;
292283

293-
dimacs_cnf_writert dimacs_cnf_writer{out, message.get_message_handler()};
284+
dimacs_cnf_writert dimacs_cnf_writer{
285+
outfile.stream(), message.get_message_handler()};
294286

295287
return do_bit_level_bmc(dimacs_cnf_writer, true);
296288
}

src/ebmc/ebmc_error.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ Author: Daniel Kroening, [email protected]
99
#ifndef EBMC_ERROR_H
1010
#define EBMC_ERROR_H
1111

12+
#include <util/source_location.h>
13+
1214
#include <sstream>
13-
#include <string>
1415

1516
class ebmc_errort
1617
{

src/ebmc/neural_liveness.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ Author: Daniel Kroening, [email protected]
1313
#include <util/string2int.h>
1414
#include <util/tempdir.h>
1515
#include <util/tempfile.h>
16-
#include <util/unicode.h>
1716

1817
#include <temporal-logic/temporal_expr.h>
1918
#include <verilog/sva_expr.h>
2019

2120
#include "ebmc_error.h"
2221
#include "ebmc_solver_factory.h"
2322
#include "live_signal.h"
23+
#include "output_file.h"
2424
#include "random_traces.h"
2525
#include "ranking_function.h"
2626
#include "report_results.h"
@@ -199,13 +199,10 @@ neural_livenesst::dump_vcd_files(temp_dirt &temp_dir)
199199
[&, trace_nr = 0ull, outfile_prefix](trans_tracet trace) mutable -> void {
200200
namespacet ns(transition_system.symbol_table);
201201
auto filename = outfile_prefix + std::to_string(trace_nr + 1);
202-
std::ofstream out(widen_if_needed(filename));
203-
204-
if(!out)
205-
throw ebmc_errort() << "failed to write trace to " << filename;
206-
207-
message.progress() << "*** Writing " << filename << messaget::eom;
208-
show_trans_trace_vcd(trace, message, ns, out);
202+
auto outfile = output_filet{filename};
203+
message.progress() << "*** Writing to " << outfile.name()
204+
<< messaget::eom;
205+
show_trans_trace_vcd(trace, message, ns, outfile.stream());
209206
};
210207
}
211208

src/ebmc/output_file.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*******************************************************************\
2+
3+
Module: Output File Container
4+
5+
Author: Daniel Kroening, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#include "output_file.h"
10+
11+
#include <util/unicode.h>
12+
13+
#include "ebmc_error.h"
14+
15+
#include <fstream>
16+
#include <iostream>
17+
18+
output_filet::output_filet(std::string __file_name)
19+
: _name(std::move(__file_name))
20+
{
21+
if(_name == "-")
22+
{
23+
_stream = &std::cout;
24+
delete_required = false;
25+
_name = "stdout";
26+
}
27+
else
28+
{
29+
_stream = new std::ofstream(widen_if_needed(_name));
30+
if(!_stream)
31+
throw ebmc_errort() << "failed to open " << _name;
32+
delete_required = true;
33+
}
34+
}
35+
36+
output_filet::~output_filet()
37+
{
38+
if(delete_required)
39+
delete _stream;
40+
}

src/ebmc/output_file.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*******************************************************************\
2+
3+
Module: Output File Container
4+
5+
Author: Daniel Kroening, [email protected]
6+
7+
\*******************************************************************/
8+
9+
#ifndef EBMC_OUTPUT_FILE_H
10+
#define EBMC_OUTPUT_FILE_H
11+
12+
#include <iosfwd>
13+
#include <string>
14+
15+
class output_filet final
16+
{
17+
public:
18+
/// Create a stream to the given file name,
19+
/// our stdout if "-".
20+
/// Throws ebmc_errort() if the file cannot be opened.
21+
explicit output_filet(std::string __file_name);
22+
~output_filet();
23+
24+
std::ostream &stream()
25+
{
26+
return *_stream;
27+
}
28+
29+
// the name of the file, or "stdout"
30+
const std::string &name()
31+
{
32+
return _name;
33+
}
34+
35+
protected:
36+
std::string _name;
37+
bool delete_required;
38+
std::ostream *_stream;
39+
};
40+
41+
#endif

src/ebmc/random_traces.cpp

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Author: Daniel Kroening, [email protected]
2222

2323
#include "ebmc_base.h"
2424
#include "ebmc_error.h"
25+
#include "output_file.h"
2526
#include "waveform.h"
2627

2728
#include <algorithm>
@@ -182,15 +183,12 @@ int random_traces(const cmdlinet &cmdline, message_handlert &message_handler)
182183
{
183184
PRECONDITION(outfile_prefix.has_value());
184185
auto filename = outfile_prefix.value() + std::to_string(trace_nr + 1);
185-
std::ofstream out(widen_if_needed(filename));
186+
auto outfile = output_filet{filename};
186187

187-
if(!out)
188-
throw ebmc_errort() << "failed to write trace to " << filename;
189-
190-
consolet::out() << "*** Writing " << filename << '\n';
188+
consolet::out() << "*** Writing " << outfile.name() << '\n';
191189

192190
messaget message(message_handler);
193-
show_trans_trace_vcd(trace, message, ns, out);
191+
show_trans_trace_vcd(trace, message, ns, outfile.stream());
194192
}
195193
else if(cmdline.isset("waveform"))
196194
{
@@ -294,23 +292,13 @@ int random_trace(const cmdlinet &cmdline, message_handlert &message_handler)
294292
else if(cmdline.isset("vcd"))
295293
{
296294
auto filename = cmdline.get_value("vcd");
297-
messaget message(message_handler);
298-
299-
if(filename == "-")
300-
{
301-
show_trans_trace_vcd(trace, message, ns, std::cout);
302-
}
303-
else
304-
{
305-
std::ofstream out(widen_if_needed(filename));
306-
307-
if(!out)
308-
throw ebmc_errort() << "failed to write trace to " << filename;
295+
auto outfile = output_filet{filename};
309296

297+
if(filename != "-")
310298
consolet::out() << "*** Writing " << filename << '\n';
311299

312-
show_trans_trace_vcd(trace, message, ns, out);
313-
}
300+
messaget message(message_handler);
301+
show_trans_trace_vcd(trace, message, ns, outfile.stream());
314302
}
315303
else // default
316304
{
@@ -352,13 +340,9 @@ void random_traces(
352340
auto consumer = [&, trace_nr = 0ull](trans_tracet trace) mutable -> void {
353341
namespacet ns(transition_system.symbol_table);
354342
auto filename = outfile_prefix + std::to_string(trace_nr + 1);
355-
std::ofstream out(widen_if_needed(filename));
356-
357-
if(!out)
358-
throw ebmc_errort() << "failed to write trace to " << filename;
359-
343+
auto outfile = output_filet{filename};
360344
messaget message(message_handler);
361-
show_trans_trace_vcd(trace, message, ns, out);
345+
show_trans_trace_vcd(trace, message, ns, outfile.stream());
362346

363347
trace_nr++;
364348
};

src/ebmc/report_results.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ Author: Daniel Kroening, [email protected]
1212
#include "report_results.h"
1313

1414
#include <util/json.h>
15-
#include <util/unicode.h>
1615
#include <util/xml.h>
1716

1817
#include "ebmc_error.h"
18+
#include "output_file.h"
1919
#include "waveform.h"
2020

21-
#include <fstream>
2221
#include <iostream>
2322

2423
/*******************************************************************\
@@ -42,9 +41,7 @@ void report_results(
4241
if(cmdline.isset("json-result"))
4342
{
4443
auto filename = cmdline.get_value("json-result");
45-
std::ofstream out(widen_if_needed(filename));
46-
if(!out)
47-
throw ebmc_errort() << "failed to open " << filename;
44+
auto outfile = output_filet{filename};
4845

4946
json_objectt json_results;
5047
auto &json_properties = json_results["properties"].make_array();
@@ -65,7 +62,7 @@ void report_results(
6562
json_properties.push_back(std::move(json_property));
6663
}
6764

68-
out << json_results;
65+
outfile.stream() << json_results;
6966
}
7067

7168
if(
@@ -167,10 +164,11 @@ void report_results(
167164
if(property.has_witness_trace())
168165
{
169166
std::string vcdfile = cmdline.get_value("vcd");
170-
std::ofstream vcd(widen_if_needed(vcdfile));
167+
auto outfile = output_filet{vcdfile};
171168

172169
messaget message(message_handler);
173-
show_trans_trace_vcd(property.witness_trace.value(), message, ns, vcd);
170+
show_trans_trace_vcd(
171+
property.witness_trace.value(), message, ns, outfile.stream());
174172

175173
break;
176174
}

src/ebmc/show_properties.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ Author: Daniel Kroening, [email protected]
88

99
#include <util/json.h>
1010
#include <util/json_irep.h>
11-
#include <util/unicode.h>
1211
#include <util/xml.h>
1312
#include <util/xml_irep.h>
1413

1514
#include "ebmc_base.h"
1615
#include "ebmc_error.h"
16+
#include "output_file.h"
1717

1818
#include <iostream>
1919

@@ -97,16 +97,6 @@ void ebmc_baset::json_properties(const std::string &file_name)
9797
json.push_back(std::move(json_property));
9898
}
9999

100-
if(file_name == "-")
101-
{
102-
std::cout << json;
103-
}
104-
else
105-
{
106-
std::ofstream out(widen_if_needed(file_name));
107-
if(!out)
108-
throw ebmc_errort() << "failed to open " << file_name;
109-
110-
out << json;
111-
}
100+
auto outfile = output_filet{file_name};
101+
outfile.stream() << json;
112102
}

src/ebmc/show_trans.cpp

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ Author: Daniel Kroening, [email protected]
88

99
#include "show_trans.h"
1010

11-
#include <util/unicode.h>
12-
1311
#include <verilog/expr2verilog.h>
1412

1513
#include "ebmc_base.h"
1614
#include "ebmc_version.h"
15+
#include "output_file.h"
1716
#include "output_verilog.h"
1817

19-
#include <fstream>
2018
#include <iostream>
2119

2220
/*******************************************************************\
@@ -268,17 +266,8 @@ int show_transt::show_trans_verilog_rtl()
268266
if(cmdline.isset("outfile"))
269267
{
270268
const std::string filename=cmdline.get_value("outfile");
271-
std::ofstream out(widen_if_needed(filename));
272-
273-
if(!out)
274-
{
275-
std::cerr << "Failed to open `"
276-
<< filename
277-
<< "'" << '\n';
278-
return 1;
279-
}
280-
281-
show_trans_verilog_rtl(out);
269+
auto outfile = output_filet{filename};
270+
show_trans_verilog_rtl(outfile.stream());
282271
}
283272
else
284273
show_trans_verilog_rtl(std::cout);
@@ -306,17 +295,8 @@ int show_transt::show_trans_verilog_netlist()
306295
if(cmdline.isset("outfile"))
307296
{
308297
const std::string filename=cmdline.get_value("outfile");
309-
std::ofstream out(widen_if_needed(filename));
310-
311-
if(!out)
312-
{
313-
std::cerr << "Failed to open `"
314-
<< filename
315-
<< "'" << '\n';
316-
return 1;
317-
}
318-
319-
show_trans_verilog_netlist(out);
298+
auto outfile = output_filet{filename};
299+
show_trans_verilog_netlist(outfile.stream());
320300
}
321301
else
322302
show_trans_verilog_netlist(std::cout);

0 commit comments

Comments
 (0)