Move setting the flux source into Config.

This commit is contained in:
dg
2023-05-11 22:47:00 +00:00
parent 0c7f9e0888
commit d74ed71023
15 changed files with 122 additions and 127 deletions

View File

@@ -4,6 +4,7 @@
#include "lib/logger.h" #include "lib/logger.h"
#include <fstream> #include <fstream>
#include <google/protobuf/text_format.h> #include <google/protobuf/text_format.h>
#include <regex>
static Config config; static Config config;
@@ -141,3 +142,77 @@ void Config::applyOption(const OptionProto& option)
(*this)->MergeFrom(option.config()); (*this)->MergeFrom(option.config());
} }
void Config::setFluxSource(std::string filename)
{
_readState = IO_FLUX;
static const std::vector<std::pair<std::regex,
std::function<void(const std::string&, FluxSourceProto*)>>>
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);
}

View File

@@ -31,6 +31,14 @@ public:
class Config class Config
{ {
public:
enum IOState
{
IO_NONE,
IO_FLUX,
IO_IMAGE
};
public: public:
ConfigProto* operator -> () const; ConfigProto* operator -> () const;
operator ConfigProto* () const; operator ConfigProto* () const;
@@ -55,6 +63,14 @@ public:
const OptionProto& findOption(const std::string& option); const OptionProto& findOption(const std::string& option);
bool isOptionValid(const OptionProto& option); bool isOptionValid(const OptionProto& option);
void applyOption(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(); extern Config& globalConfig();

View File

@@ -22,33 +22,32 @@ enum SupportStatus
// NEXT_TAG: 27 // NEXT_TAG: 27
message ConfigProto message ConfigProto
{ {
optional string shortname = 24; optional string shortname = 1;
optional string comment = 8; optional string comment = 2;
optional bool is_extension = 13; optional bool is_extension = 3;
repeated string documentation = 23; repeated string documentation = 4;
optional SupportStatus read_support_status = 25 [ default = UNSUPPORTED ]; optional SupportStatus read_support_status = 5 [ default = UNSUPPORTED ];
optional SupportStatus write_support_status = 26 [ 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 ImageWriterProto image_writer = 9;
optional FluxSourceProto flux_source = 10; optional FluxSourceProto flux_source = 10;
optional FluxSinkProto flux_sink = 11; optional FluxSinkProto flux_sink = 11;
optional DriveProto drive = 15; optional DriveProto drive = 12;
optional EncoderProto encoder = 3; optional EncoderProto encoder = 13;
optional DecoderProto decoder = 4; optional DecoderProto decoder = 14;
optional UsbProto usb = 5; optional UsbProto usb = 15;
optional RangeProto tracks = 6; optional RangeProto tracks = 16;
optional RangeProto heads = 7; optional RangeProto heads = 17;
optional FilesystemProto filesystem = 17; optional FilesystemProto filesystem = 18;
repeated OptionProto option = 20; repeated OptionProto option = 19;
repeated OptionGroupProto option_group = 22; repeated OptionGroupProto option_group = 20;
} }
message OptionRequirementProto message OptionRequirementProto

View File

@@ -5,14 +5,6 @@
#include "lib/config.pb.h" #include "lib/config.pb.h"
#include "proto.h" #include "proto.h"
#include "utils.h" #include "utils.h"
#include <regex>
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> FluxSource::create(const FluxSourceProto& config) std::unique_ptr<FluxSource> FluxSource::create(const FluxSourceProto& config)
{ {
@@ -51,80 +43,6 @@ std::unique_ptr<FluxSource> FluxSource::create(const FluxSourceProto& config)
} }
} }
void FluxSource::updateConfigForFilename(
FluxSourceProto* proto, const std::string& filename)
{
static const std::vector<std::pair<std::regex,
std::function<void(const std::string&, FluxSourceProto*)>>>
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 class TrivialFluxSourceIterator : public FluxSourceIterator
{ {
public: public:

View File

@@ -56,8 +56,6 @@ public:
const DiskFlux& flux); const DiskFlux& flux);
static std::unique_ptr<FluxSource> create(const FluxSourceProto& spec); static std::unique_ptr<FluxSource> create(const FluxSourceProto& spec);
static void updateConfigForFilename(
FluxSourceProto* proto, const std::string& filename);
public: public:
virtual std::unique_ptr<FluxSourceIterator> readFlux( virtual std::unique_ptr<FluxSourceIterator> readFlux(

View File

@@ -18,8 +18,7 @@ static StringFlag sourceFlux({"--source", "-s"},
"", "",
[](const auto& value) [](const auto& value)
{ {
FluxSource::updateConfigForFilename( globalConfig().setFluxSource(value);
globalConfig()->mutable_flux_source(), value);
}); });
static IntFlag trackFlag({"--cylinder", "-c"}, "Track to read.", 0); static IntFlag trackFlag({"--cylinder", "-c"}, "Track to read.", 0);

View File

@@ -22,8 +22,7 @@ static StringFlag sourceFlux({"-s", "--source"},
"", "",
[](const auto& value) [](const auto& value)
{ {
FluxSource::updateConfigForFilename( globalConfig().setFluxSource(value);
globalConfig()->mutable_flux_source(), value);
}); });
static StringFlag destFlux({"-d", "--dest"}, static StringFlag destFlux({"-d", "--dest"},

View File

@@ -17,8 +17,7 @@ static StringFlag sourceFlux({"--source", "-s"},
"", "",
[](const auto& value) [](const auto& value)
{ {
FluxSource::updateConfigForFilename( globalConfig().setFluxSource(value);
globalConfig()->mutable_flux_source(), value);
}); });
static StringFlag destFlux({"--dest", "-d"}, static StringFlag destFlux({"--dest", "-d"},

View File

@@ -22,8 +22,7 @@ static StringFlag sourceFlux({"-s", "--source"},
"", "",
[](const auto& value) [](const auto& value)
{ {
FluxSource::updateConfigForFilename( globalConfig().setFluxSource(value);
globalConfig()->mutable_flux_source(), value);
}); });
static StringFlag destImage({"-o", "--output"}, static StringFlag destImage({"-o", "--output"},

View File

@@ -12,8 +12,7 @@ static StringFlag sourceFlux({"-s", "--source"},
"", "",
[](const auto& value) [](const auto& value)
{ {
FluxSource::updateConfigForFilename( globalConfig().setFluxSource(value);
globalConfig()->mutable_flux_source(), value);
}); });
int mainRpm(int argc, const char* argv[]) int mainRpm(int argc, const char* argv[])

View File

@@ -12,8 +12,7 @@ static StringFlag sourceFlux({"-s", "--source"},
"", "",
[](const auto& value) [](const auto& value)
{ {
FluxSource::updateConfigForFilename( globalConfig().setFluxSource(value);
globalConfig()->mutable_flux_source(), value);
}); });
static IntFlag track({"--cylinder", "-c"}, "track to seek to", 0); static IntFlag track({"--cylinder", "-c"}, "track to seek to", 0);

View File

@@ -34,8 +34,7 @@ static StringFlag destFlux({"--dest", "-d"},
{ {
FluxSink::updateConfigForFilename( FluxSink::updateConfigForFilename(
globalConfig()->mutable_flux_sink(), value); globalConfig()->mutable_flux_sink(), value);
FluxSource::updateConfigForFilename( globalConfig().setFluxSource(value);
globalConfig()->mutable_flux_source(), value);
}); });
static StringFlag destTracks({"--cylinders", "-c"}, static StringFlag destTracks({"--cylinders", "-c"},

View File

@@ -32,8 +32,7 @@ static StringFlag flux({"-f", "--flux"},
"", "",
[](const auto& value) [](const auto& value)
{ {
FluxSource::updateConfigForFilename( globalConfig().setFluxSource(value);
globalConfig()->mutable_flux_source(), value);
FluxSink::updateConfigForFilename( FluxSink::updateConfigForFilename(
globalConfig()->mutable_flux_sink(), value); globalConfig()->mutable_flux_sink(), value);
}); });

View File

@@ -245,8 +245,7 @@ public:
std::string filename = _selectedDrive ? "drive:1" : "drive:0"; std::string filename = _selectedDrive ? "drive:1" : "drive:0";
FluxSink::updateConfigForFilename( FluxSink::updateConfigForFilename(
globalConfig()->mutable_flux_sink(), filename); globalConfig()->mutable_flux_sink(), filename);
FluxSource::updateConfigForFilename( globalConfig().setFluxSource(filename);
globalConfig()->mutable_flux_source(), filename);
break; break;
} }
@@ -255,9 +254,7 @@ public:
{ {
FluxSink::updateConfigForFilename( FluxSink::updateConfigForFilename(
globalConfig()->mutable_flux_sink(), _selectedFluxfilename); globalConfig()->mutable_flux_sink(), _selectedFluxfilename);
FluxSource::updateConfigForFilename( globalConfig().setFluxSource(_selectedFluxfilename);
globalConfig()->mutable_flux_source(),
_selectedFluxfilename);
break; break;
} }

View File

@@ -109,13 +109,13 @@ static void test_config(void)
ConfigProto config; ConfigProto config;
const std::string text = R"M( const std::string text = R"M(
flux_sink {
drive { }
}
image_reader { image_reader {
filename: "filename" filename: "filename"
} }
flux_sink {
drive { }
}
)M"; )M";
google::protobuf::TextFormat::MergeFromString(text, &config); google::protobuf::TextFormat::MergeFromString(text, &config);