From 3e25e61778cc9fe427bf68f45de43f26985b12c3 Mon Sep 17 00:00:00 2001 From: Jannis Harder Date: Fri, 23 Dec 2022 17:22:24 +0100 Subject: aiger: Use new JSON code for writing aiger witness map files --- kernel/json.cc | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) (limited to 'kernel/json.cc') diff --git a/kernel/json.cc b/kernel/json.cc index 6ea873329..59f782e5e 100644 --- a/kernel/json.cc +++ b/kernel/json.cc @@ -23,13 +23,38 @@ USING_YOSYS_NAMESPACE void PrettyJson::emit_to_log() { - targets.push_back([](const char *raw_json) { log("%s", raw_json); }); + struct LogTarget : public Target { + void emit(const char *data) override { log("%s", data); } + }; + + targets.push_back(std::unique_ptr(new LogTarget)); } void PrettyJson::append_to_string(std::string &target) { - std::string *target_p = ⌖ - targets.push_back([=](const char *raw_json) { *target_p += raw_json; }); + struct AppendStringTarget : public Target { + std::string ⌖ + AppendStringTarget(std::string &target) : target(target) {} + void emit(const char *data) override { target += data; } + }; + + targets.push_back(std::unique_ptr(new AppendStringTarget(target))); +} + +bool PrettyJson::write_to_file(const std::string &path) +{ + struct WriteFileTarget : public Target { + std::ofstream target; + void emit(const char *data) override { target << data; } + void flush() override { target.flush(); } + }; + + auto target = std::unique_ptr(new WriteFileTarget); + target->target.open(path); + if (target->target.fail()) + return false; + targets.push_back(std::unique_ptr(target.release())); + return true; } void PrettyJson::line() @@ -42,7 +67,13 @@ void PrettyJson::line() void PrettyJson::raw(const char *raw_json) { for (auto &target : targets) - target(raw_json); + target->emit(raw_json); +} + +void PrettyJson::flush() +{ + for (auto &target : targets) + target->flush(); } void PrettyJson::begin_object() @@ -110,8 +141,10 @@ void PrettyJson::begin_value() void PrettyJson::end_value() { - if (state.empty()) + if (state.empty()) { raw("\n"); + flush(); + } } void PrettyJson::value_json(const Json &value) -- cgit v1.2.3