mirror of
https://github.com/davidgiven/fluxengine.git
synced 2025-10-31 11:17:01 -07:00
Move setting the flux source into Config.
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include "lib/logger.h"
|
||||
#include <fstream>
|
||||
#include <google/protobuf/text_format.h>
|
||||
#include <regex>
|
||||
|
||||
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<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);
|
||||
}
|
||||
|
||||
16
lib/config.h
16
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();
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -5,14 +5,6 @@
|
||||
#include "lib/config.pb.h"
|
||||
#include "proto.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)
|
||||
{
|
||||
@@ -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
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -56,8 +56,6 @@ public:
|
||||
const DiskFlux& flux);
|
||||
|
||||
static std::unique_ptr<FluxSource> create(const FluxSourceProto& spec);
|
||||
static void updateConfigForFilename(
|
||||
FluxSourceProto* proto, const std::string& filename);
|
||||
|
||||
public:
|
||||
virtual std::unique_ptr<FluxSourceIterator> readFlux(
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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"},
|
||||
|
||||
@@ -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"},
|
||||
|
||||
@@ -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"},
|
||||
|
||||
@@ -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[])
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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"},
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user