From d74ed710230eb435f69ed87c941c7fb2add39db9 Mon Sep 17 00:00:00 2001 From: dg Date: Thu, 11 May 2023 22:47:00 +0000 Subject: [PATCH] Move setting the flux source into Config. --- lib/config.cc | 75 +++++++++++++++++++++++++++++++++ lib/config.h | 16 +++++++ lib/config.proto | 35 ++++++++------- lib/fluxsource/fluxsource.cc | 82 ------------------------------------ lib/fluxsource/fluxsource.h | 2 - src/fe-inspect.cc | 3 +- src/fe-rawread.cc | 3 +- src/fe-rawwrite.cc | 3 +- src/fe-read.cc | 3 +- src/fe-rpm.cc | 3 +- src/fe-seek.cc | 3 +- src/fe-write.cc | 3 +- src/fileutils.cc | 3 +- src/gui/idlepanel.cc | 7 +-- tests/proto.cc | 8 ++-- 15 files changed, 122 insertions(+), 127 deletions(-) diff --git a/lib/config.cc b/lib/config.cc index ec67623f..1790d91f 100644 --- a/lib/config.cc +++ b/lib/config.cc @@ -4,6 +4,7 @@ #include "lib/logger.h" #include #include +#include static Config config; @@ -141,3 +142,77 @@ void Config::applyOption(const OptionProto& option) (*this)->MergeFrom(option.config()); } + +void Config::setFluxSource(std::string filename) +{ + _readState = IO_FLUX; + + static const std::vector>> + formats = { + {std::regex("^(.*\\.flux)$"), + [](auto& s, auto* proto) + { + proto->set_type(FluxSourceProto::FLUX); + proto->mutable_fl2()->set_filename(s); + }}, + {std::regex("^(.*\\.scp)$"), + [](auto& s, auto* proto) + { + proto->set_type(FluxSourceProto::SCP); + proto->mutable_scp()->set_filename(s); + }}, + {std::regex("^(.*\\.a2r)$"), + [](auto& s, auto* proto) + { + proto->set_type(FluxSourceProto::A2R); + proto->mutable_a2r()->set_filename(s); + }}, + {std::regex("^(.*\\.cwf)$"), + [](auto& s, auto* proto) + { + proto->set_type(FluxSourceProto::CWF); + proto->mutable_cwf()->set_filename(s); + }}, + {std::regex("^erase:$"), + [](auto& s, auto* proto) + { + proto->set_type(FluxSourceProto::ERASE); + }}, + {std::regex("^kryoflux:(.*)$"), + [](auto& s, auto* proto) + { + proto->set_type(FluxSourceProto::KRYOFLUX); + proto->mutable_kryoflux()->set_directory(s); + }}, + {std::regex("^testpattern:(.*)"), + [](auto& s, auto* proto) + { + proto->set_type(FluxSourceProto::TEST_PATTERN); + }}, + {std::regex("^drive:(.*)"), + [](auto& s, auto* proto) + { + proto->set_type(FluxSourceProto::DRIVE); + globalConfig()->mutable_drive()->set_drive(std::stoi(s)); + }}, + {std::regex("^flx:(.*)$"), + [](auto& s, auto* proto) + { + proto->set_type(FluxSourceProto::FLX); + proto->mutable_flx()->set_directory(s); + }}, + }; + + for (const auto& it : formats) + { + std::smatch match; + if (std::regex_match(filename, match, it.first)) + { + it.second(match[1], (*this)->mutable_flux_source()); + return; + } + } + + error("unrecognised flux filename '{}'", filename); +} diff --git a/lib/config.h b/lib/config.h index f3b2728a..00b21083 100644 --- a/lib/config.h +++ b/lib/config.h @@ -31,6 +31,14 @@ public: class Config { +public: + enum IOState + { + IO_NONE, + IO_FLUX, + IO_IMAGE + }; + public: ConfigProto* operator -> () const; operator ConfigProto* () const; @@ -55,6 +63,14 @@ public: const OptionProto& findOption(const std::string& option); bool isOptionValid(const OptionProto& option); void applyOption(const OptionProto& option); + + /* Adjust overall inputs and outputs. */ + + void setFluxSource(std::string value); + +private: + IOState _readState = IO_NONE; + IOState _writeState = IO_NONE; }; extern Config& globalConfig(); diff --git a/lib/config.proto b/lib/config.proto index 16a481a0..03e0401b 100644 --- a/lib/config.proto +++ b/lib/config.proto @@ -22,33 +22,32 @@ enum SupportStatus // NEXT_TAG: 27 message ConfigProto { - optional string shortname = 24; - optional string comment = 8; - optional bool is_extension = 13; - repeated string documentation = 23; - optional SupportStatus read_support_status = 25 [ default = UNSUPPORTED ]; - optional SupportStatus write_support_status = 26 [ default = UNSUPPORTED ]; + optional string shortname = 1; + optional string comment = 2; + optional bool is_extension = 3; + repeated string documentation = 4; + optional SupportStatus read_support_status = 5 [ default = UNSUPPORTED ]; + optional SupportStatus write_support_status = 6 [ default = UNSUPPORTED ]; - optional LayoutProto layout = 18; + optional LayoutProto layout = 7; - optional ImageReaderProto image_reader = 12; + optional ImageReaderProto image_reader = 8; optional ImageWriterProto image_writer = 9; - optional FluxSourceProto flux_source = 10; optional FluxSinkProto flux_sink = 11; - optional DriveProto drive = 15; + optional DriveProto drive = 12; - optional EncoderProto encoder = 3; - optional DecoderProto decoder = 4; - optional UsbProto usb = 5; + optional EncoderProto encoder = 13; + optional DecoderProto decoder = 14; + optional UsbProto usb = 15; - optional RangeProto tracks = 6; - optional RangeProto heads = 7; + optional RangeProto tracks = 16; + optional RangeProto heads = 17; - optional FilesystemProto filesystem = 17; + optional FilesystemProto filesystem = 18; - repeated OptionProto option = 20; - repeated OptionGroupProto option_group = 22; + repeated OptionProto option = 19; + repeated OptionGroupProto option_group = 20; } message OptionRequirementProto diff --git a/lib/fluxsource/fluxsource.cc b/lib/fluxsource/fluxsource.cc index 71eea15b..1502a28e 100644 --- a/lib/fluxsource/fluxsource.cc +++ b/lib/fluxsource/fluxsource.cc @@ -5,14 +5,6 @@ #include "lib/config.pb.h" #include "proto.h" #include "utils.h" -#include - -static bool ends_with(const std::string& value, const std::string& ending) -{ - if (ending.size() > value.size()) - return false; - return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); -} std::unique_ptr FluxSource::create(const FluxSourceProto& config) { @@ -51,80 +43,6 @@ std::unique_ptr FluxSource::create(const FluxSourceProto& config) } } -void FluxSource::updateConfigForFilename( - FluxSourceProto* proto, const std::string& filename) -{ - - static const std::vector>> - formats = { - {std::regex("^(.*\\.flux)$"), - [](auto& s, auto* proto) - { - proto->set_type(FluxSourceProto::FLUX); - proto->mutable_fl2()->set_filename(s); - }}, - {std::regex("^(.*\\.scp)$"), - [](auto& s, auto* proto) - { - proto->set_type(FluxSourceProto::SCP); - proto->mutable_scp()->set_filename(s); - }}, - {std::regex("^(.*\\.a2r)$"), - [](auto& s, auto* proto) - { - proto->set_type(FluxSourceProto::A2R); - proto->mutable_a2r()->set_filename(s); - }}, - {std::regex("^(.*\\.cwf)$"), - [](auto& s, auto* proto) - { - proto->set_type(FluxSourceProto::CWF); - proto->mutable_cwf()->set_filename(s); - }}, - {std::regex("^erase:$"), - [](auto& s, auto* proto) - { - proto->set_type(FluxSourceProto::ERASE); - }}, - {std::regex("^kryoflux:(.*)$"), - [](auto& s, auto* proto) - { - proto->set_type(FluxSourceProto::KRYOFLUX); - proto->mutable_kryoflux()->set_directory(s); - }}, - {std::regex("^testpattern:(.*)"), - [](auto& s, auto* proto) - { - proto->set_type(FluxSourceProto::TEST_PATTERN); - }}, - {std::regex("^drive:(.*)"), - [](auto& s, auto* proto) - { - proto->set_type(FluxSourceProto::DRIVE); - globalConfig()->mutable_drive()->set_drive(std::stoi(s)); - }}, - {std::regex("^flx:(.*)$"), - [](auto& s, auto* proto) - { - proto->set_type(FluxSourceProto::FLX); - proto->mutable_flx()->set_directory(s); - }}, - }; - - for (const auto& it : formats) - { - std::smatch match; - if (std::regex_match(filename, match, it.first)) - { - it.second(match[1], proto); - return; - } - } - - error("unrecognised flux filename '{}'", filename); -} - class TrivialFluxSourceIterator : public FluxSourceIterator { public: diff --git a/lib/fluxsource/fluxsource.h b/lib/fluxsource/fluxsource.h index ee863c65..918b7f2c 100644 --- a/lib/fluxsource/fluxsource.h +++ b/lib/fluxsource/fluxsource.h @@ -56,8 +56,6 @@ public: const DiskFlux& flux); static std::unique_ptr create(const FluxSourceProto& spec); - static void updateConfigForFilename( - FluxSourceProto* proto, const std::string& filename); public: virtual std::unique_ptr readFlux( diff --git a/src/fe-inspect.cc b/src/fe-inspect.cc index c862af60..b8878dd4 100644 --- a/src/fe-inspect.cc +++ b/src/fe-inspect.cc @@ -18,8 +18,7 @@ static StringFlag sourceFlux({"--source", "-s"}, "", [](const auto& value) { - FluxSource::updateConfigForFilename( - globalConfig()->mutable_flux_source(), value); + globalConfig().setFluxSource(value); }); static IntFlag trackFlag({"--cylinder", "-c"}, "Track to read.", 0); diff --git a/src/fe-rawread.cc b/src/fe-rawread.cc index 2348b858..f2476981 100644 --- a/src/fe-rawread.cc +++ b/src/fe-rawread.cc @@ -22,8 +22,7 @@ static StringFlag sourceFlux({"-s", "--source"}, "", [](const auto& value) { - FluxSource::updateConfigForFilename( - globalConfig()->mutable_flux_source(), value); + globalConfig().setFluxSource(value); }); static StringFlag destFlux({"-d", "--dest"}, diff --git a/src/fe-rawwrite.cc b/src/fe-rawwrite.cc index 099a501d..719e1cc1 100644 --- a/src/fe-rawwrite.cc +++ b/src/fe-rawwrite.cc @@ -17,8 +17,7 @@ static StringFlag sourceFlux({"--source", "-s"}, "", [](const auto& value) { - FluxSource::updateConfigForFilename( - globalConfig()->mutable_flux_source(), value); + globalConfig().setFluxSource(value); }); static StringFlag destFlux({"--dest", "-d"}, diff --git a/src/fe-read.cc b/src/fe-read.cc index 1f529d44..7e51f4bf 100644 --- a/src/fe-read.cc +++ b/src/fe-read.cc @@ -22,8 +22,7 @@ static StringFlag sourceFlux({"-s", "--source"}, "", [](const auto& value) { - FluxSource::updateConfigForFilename( - globalConfig()->mutable_flux_source(), value); + globalConfig().setFluxSource(value); }); static StringFlag destImage({"-o", "--output"}, diff --git a/src/fe-rpm.cc b/src/fe-rpm.cc index dfd75b7a..46516b47 100644 --- a/src/fe-rpm.cc +++ b/src/fe-rpm.cc @@ -12,8 +12,7 @@ static StringFlag sourceFlux({"-s", "--source"}, "", [](const auto& value) { - FluxSource::updateConfigForFilename( - globalConfig()->mutable_flux_source(), value); + globalConfig().setFluxSource(value); }); int mainRpm(int argc, const char* argv[]) diff --git a/src/fe-seek.cc b/src/fe-seek.cc index afb6546c..bb5fb764 100644 --- a/src/fe-seek.cc +++ b/src/fe-seek.cc @@ -12,8 +12,7 @@ static StringFlag sourceFlux({"-s", "--source"}, "", [](const auto& value) { - FluxSource::updateConfigForFilename( - globalConfig()->mutable_flux_source(), value); + globalConfig().setFluxSource(value); }); static IntFlag track({"--cylinder", "-c"}, "track to seek to", 0); diff --git a/src/fe-write.cc b/src/fe-write.cc index 721299ae..96a74d03 100644 --- a/src/fe-write.cc +++ b/src/fe-write.cc @@ -34,8 +34,7 @@ static StringFlag destFlux({"--dest", "-d"}, { FluxSink::updateConfigForFilename( globalConfig()->mutable_flux_sink(), value); - FluxSource::updateConfigForFilename( - globalConfig()->mutable_flux_source(), value); + globalConfig().setFluxSource(value); }); static StringFlag destTracks({"--cylinders", "-c"}, diff --git a/src/fileutils.cc b/src/fileutils.cc index e9001c54..8e5b63c7 100644 --- a/src/fileutils.cc +++ b/src/fileutils.cc @@ -32,8 +32,7 @@ static StringFlag flux({"-f", "--flux"}, "", [](const auto& value) { - FluxSource::updateConfigForFilename( - globalConfig()->mutable_flux_source(), value); + globalConfig().setFluxSource(value); FluxSink::updateConfigForFilename( globalConfig()->mutable_flux_sink(), value); }); diff --git a/src/gui/idlepanel.cc b/src/gui/idlepanel.cc index c6c73daf..24159256 100644 --- a/src/gui/idlepanel.cc +++ b/src/gui/idlepanel.cc @@ -245,8 +245,7 @@ public: std::string filename = _selectedDrive ? "drive:1" : "drive:0"; FluxSink::updateConfigForFilename( globalConfig()->mutable_flux_sink(), filename); - FluxSource::updateConfigForFilename( - globalConfig()->mutable_flux_source(), filename); + globalConfig().setFluxSource(filename); break; } @@ -255,9 +254,7 @@ public: { FluxSink::updateConfigForFilename( globalConfig()->mutable_flux_sink(), _selectedFluxfilename); - FluxSource::updateConfigForFilename( - globalConfig()->mutable_flux_source(), - _selectedFluxfilename); + globalConfig().setFluxSource(_selectedFluxfilename); break; } diff --git a/tests/proto.cc b/tests/proto.cc index 29d44cfc..04850bc7 100644 --- a/tests/proto.cc +++ b/tests/proto.cc @@ -109,13 +109,13 @@ static void test_config(void) ConfigProto config; const std::string text = R"M( - flux_sink { - drive { } - } - image_reader { filename: "filename" } + + flux_sink { + drive { } + } )M"; google::protobuf::TextFormat::MergeFromString(text, &config);