mirror of
https://github.com/davidgiven/fluxengine.git
synced 2025-10-31 11:17:01 -07:00
Add helper flags to fe-read and fe-write for setting common parameters.
This commit is contained in:
@@ -218,6 +218,7 @@ void BoolFlag::set(const std::string& value)
|
||||
_value = false;
|
||||
else
|
||||
Error() << "can't parse '" << value << "'; try 'true' or 'false'";
|
||||
_callback(_value);
|
||||
}
|
||||
|
||||
const std::string HexIntFlag::defaultValueAsString() const
|
||||
|
||||
38
lib/flags.h
38
lib/flags.h
@@ -92,10 +92,12 @@ class ValueFlag : public Flag
|
||||
{
|
||||
public:
|
||||
ValueFlag(const std::vector<std::string>& names, const std::string helptext,
|
||||
const T defaultValue):
|
||||
const T defaultValue,
|
||||
std::function<void(const T&)> callback = [](const T&) {}):
|
||||
Flag(names, helptext),
|
||||
_defaultValue(defaultValue),
|
||||
_value(defaultValue)
|
||||
_value(defaultValue),
|
||||
_callback(callback)
|
||||
{}
|
||||
|
||||
const T& get() const
|
||||
@@ -114,38 +116,42 @@ public:
|
||||
protected:
|
||||
T _defaultValue;
|
||||
T _value;
|
||||
std::function<void(const T&)> _callback;
|
||||
};
|
||||
|
||||
class StringFlag : public ValueFlag<std::string>
|
||||
{
|
||||
public:
|
||||
StringFlag(const std::vector<std::string>& names, const std::string helptext,
|
||||
const std::string defaultValue = ""):
|
||||
ValueFlag(names, helptext, defaultValue)
|
||||
const std::string defaultValue = "",
|
||||
std::function<void(const std::string&)> callback = [](const std::string&) {}):
|
||||
ValueFlag(names, helptext, defaultValue, callback)
|
||||
{}
|
||||
|
||||
const std::string defaultValueAsString() const { return _defaultValue; }
|
||||
void set(const std::string& value) { _value = value; }
|
||||
void set(const std::string& value) { _value = value; _callback(_value); }
|
||||
};
|
||||
|
||||
class IntFlag : public ValueFlag<int>
|
||||
{
|
||||
public:
|
||||
IntFlag(const std::vector<std::string>& names, const std::string helptext,
|
||||
int defaultValue = 0):
|
||||
ValueFlag(names, helptext, defaultValue)
|
||||
int defaultValue = 0,
|
||||
std::function<void(const int&)> callback = [](const int&) {}):
|
||||
ValueFlag(names, helptext, defaultValue, callback)
|
||||
{}
|
||||
|
||||
const std::string defaultValueAsString() const { return std::to_string(_defaultValue); }
|
||||
void set(const std::string& value) { _value = std::stoi(value); }
|
||||
void set(const std::string& value) { _value = std::stoi(value); _callback(_value); }
|
||||
};
|
||||
|
||||
class HexIntFlag : public IntFlag
|
||||
{
|
||||
public:
|
||||
HexIntFlag(const std::vector<std::string>& names, const std::string helptext,
|
||||
int defaultValue = 0):
|
||||
IntFlag(names, helptext, defaultValue)
|
||||
int defaultValue = 0,
|
||||
std::function<void(const int&)> callback = [](const int&) {}):
|
||||
IntFlag(names, helptext, defaultValue, callback)
|
||||
{}
|
||||
|
||||
const std::string defaultValueAsString() const;
|
||||
@@ -155,20 +161,22 @@ class DoubleFlag : public ValueFlag<double>
|
||||
{
|
||||
public:
|
||||
DoubleFlag(const std::vector<std::string>& names, const std::string helptext,
|
||||
double defaultValue = 1.0):
|
||||
ValueFlag(names, helptext, defaultValue)
|
||||
double defaultValue = 1.0,
|
||||
std::function<void(const double&)> callback = [](const double&) {}):
|
||||
ValueFlag(names, helptext, defaultValue, callback)
|
||||
{}
|
||||
|
||||
const std::string defaultValueAsString() const { return std::to_string(_defaultValue); }
|
||||
void set(const std::string& value) { _value = std::stod(value); }
|
||||
void set(const std::string& value) { _value = std::stod(value); _callback(_value); }
|
||||
};
|
||||
|
||||
class BoolFlag : public ValueFlag<bool>
|
||||
{
|
||||
public:
|
||||
BoolFlag(const std::vector<std::string>& names, const std::string helptext,
|
||||
bool defaultValue = false):
|
||||
ValueFlag(names, helptext, defaultValue)
|
||||
bool defaultValue = false,
|
||||
std::function<void(const bool&)> callback = [](const bool&) {}):
|
||||
ValueFlag(names, helptext, defaultValue, callback)
|
||||
{}
|
||||
|
||||
const std::string defaultValueAsString() const { return _defaultValue ? "true" : "false"; }
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "imagereader/imagereader.h"
|
||||
#include "utils.h"
|
||||
#include "fmt/format.h"
|
||||
#include "proto.h"
|
||||
#include "lib/config.pb.h"
|
||||
#include <algorithm>
|
||||
#include <ctype.h>
|
||||
@@ -31,6 +32,32 @@ std::unique_ptr<ImageReader> ImageReader::create(const InputFileProto& config)
|
||||
return std::unique_ptr<ImageReader>();
|
||||
}
|
||||
|
||||
void ImageReader::updateConfigForFilename(const std::string& filename)
|
||||
{
|
||||
InputFileProto* f = config.mutable_input()->mutable_file();
|
||||
static const std::map<std::string, std::function<void(void)>> formats =
|
||||
{
|
||||
{".adf", [&]() { f->mutable_img(); }},
|
||||
{".jv3", [&]() { f->mutable_jv3(); }},
|
||||
{".d81", [&]() { f->mutable_img(); }},
|
||||
{".diskcopy", [&]() { f->mutable_diskcopy(); }},
|
||||
{".img", [&]() { f->mutable_img(); }},
|
||||
{".st", [&]() { f->mutable_img(); }},
|
||||
};
|
||||
|
||||
for (const auto& it : formats)
|
||||
{
|
||||
if (endsWith(filename, it.first))
|
||||
{
|
||||
it.second();
|
||||
f->set_filename(filename);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Error() << fmt::format("unrecognised image filename '{}'", filename);
|
||||
}
|
||||
|
||||
ImageReader::ImageReader(const InputFileProto& config):
|
||||
_config(config)
|
||||
{}
|
||||
|
||||
@@ -13,6 +13,7 @@ public:
|
||||
|
||||
public:
|
||||
static std::unique_ptr<ImageReader> create(const InputFileProto& config);
|
||||
static void updateConfigForFilename(const std::string& filename);
|
||||
|
||||
public:
|
||||
static std::unique_ptr<ImageReader> createDiskCopyImageReader(const InputFileProto& config);
|
||||
|
||||
@@ -6,23 +6,11 @@
|
||||
#include "imagewriter/imagewriter.h"
|
||||
#include "utils.h"
|
||||
#include "lib/config.pb.h"
|
||||
#include "proto.h"
|
||||
#include "fmt/format.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#if 0
|
||||
std::map<std::string, ImageWriter::Constructor> ImageWriter::formats =
|
||||
{
|
||||
{".adf", ImageWriter::createImgImageWriter},
|
||||
{".d64", ImageWriter::createD64ImageWriter},
|
||||
{".d81", ImageWriter::createImgImageWriter},
|
||||
{".diskcopy", ImageWriter::createDiskCopyImageWriter},
|
||||
{".img", ImageWriter::createImgImageWriter},
|
||||
{".ldbs", ImageWriter::createLDBSImageWriter},
|
||||
{".st", ImageWriter::createImgImageWriter},
|
||||
};
|
||||
#endif
|
||||
|
||||
std::unique_ptr<ImageWriter> ImageWriter::create(const OutputFileProto& config)
|
||||
{
|
||||
switch (config.format_case())
|
||||
@@ -43,11 +31,32 @@ std::unique_ptr<ImageWriter> ImageWriter::create(const OutputFileProto& config)
|
||||
return std::unique_ptr<ImageWriter>();
|
||||
}
|
||||
|
||||
//void ImageWriter::verifyImageSpec(const ImageSpec& spec)
|
||||
//{
|
||||
// if (!findConstructor(spec))
|
||||
// Error() << "unrecognised output image filename extension";
|
||||
//}
|
||||
void ImageWriter::updateConfigForFilename(const std::string& filename)
|
||||
{
|
||||
OutputFileProto* f = config.mutable_output()->mutable_file();
|
||||
static const std::map<std::string, std::function<void(void)>> formats =
|
||||
{
|
||||
{".adf", [&]() { f->mutable_img(); }},
|
||||
{".d64", [&]() { f->mutable_d64(); }},
|
||||
{".d81", [&]() { f->mutable_img(); }},
|
||||
{".diskcopy", [&]() { f->mutable_diskcopy(); }},
|
||||
{".img", [&]() { f->mutable_img(); }},
|
||||
{".ldbs", [&]() { f->mutable_ldbs(); }},
|
||||
{".st", [&]() { f->mutable_img(); }},
|
||||
};
|
||||
|
||||
for (const auto& it : formats)
|
||||
{
|
||||
if (endsWith(filename, it.first))
|
||||
{
|
||||
it.second();
|
||||
f->set_filename(filename);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Error() << fmt::format("unrecognised image filename '{}'", filename);
|
||||
}
|
||||
|
||||
ImageWriter::ImageWriter(const OutputFileProto& config):
|
||||
_config(config)
|
||||
|
||||
@@ -12,6 +12,7 @@ public:
|
||||
|
||||
public:
|
||||
static std::unique_ptr<ImageWriter> create(const OutputFileProto& config);
|
||||
static void updateConfigForFilename(const std::string& filename);
|
||||
|
||||
static std::unique_ptr<ImageWriter> createImgImageWriter(
|
||||
const OutputFileProto& config);
|
||||
|
||||
@@ -19,13 +19,40 @@
|
||||
|
||||
static FlagGroup flags;
|
||||
|
||||
static StringFlag sourceFlux(
|
||||
{ "-s", "--source" },
|
||||
"flux file to read from",
|
||||
"",
|
||||
[](const auto& value)
|
||||
{
|
||||
config.mutable_input()->mutable_disk()->set_fluxfile(value);
|
||||
});
|
||||
|
||||
static IntFlag sourceDrive(
|
||||
{ "-D", "--drive" },
|
||||
"drive to write from",
|
||||
0,
|
||||
[](const auto& value)
|
||||
{
|
||||
config.mutable_input()->mutable_disk()->mutable_drive()->set_drive(value);
|
||||
});
|
||||
|
||||
static StringFlag destImage(
|
||||
{ "-o", "--output" },
|
||||
"destination image to write",
|
||||
"",
|
||||
[](const auto& value)
|
||||
{
|
||||
ImageWriter::updateConfigForFilename(value);
|
||||
});
|
||||
|
||||
extern const std::map<std::string, std::string> readables;
|
||||
|
||||
int mainRead(int argc, const char* argv[])
|
||||
{
|
||||
flags.parseFlagsWithConfigFiles(argc, argv, readables);
|
||||
|
||||
if (!config.has_input() || !config.has_output())
|
||||
if (!config.input().has_disk() || !config.output().has_file())
|
||||
Error() << "incomplete config (did you remember to specify the format?)";
|
||||
|
||||
std::unique_ptr<FluxSource> fluxSource(FluxSource::create(config.input().disk()));
|
||||
|
||||
@@ -19,13 +19,40 @@
|
||||
|
||||
static FlagGroup flags;
|
||||
|
||||
static StringFlag sourceImage(
|
||||
{ "-i", "--input" },
|
||||
"source image to read from",
|
||||
"",
|
||||
[](const auto& value)
|
||||
{
|
||||
ImageReader::updateConfigForFilename(value);
|
||||
});
|
||||
|
||||
static StringFlag destFlux(
|
||||
{ "-d", "--dest" },
|
||||
"flux file to write to",
|
||||
"",
|
||||
[](const auto& value)
|
||||
{
|
||||
config.mutable_output()->mutable_disk()->set_fluxfile(value);
|
||||
});
|
||||
|
||||
static IntFlag destDrive(
|
||||
{ "-D", "--drive" },
|
||||
"drive to write to",
|
||||
0,
|
||||
[](const auto& value)
|
||||
{
|
||||
config.mutable_output()->mutable_disk()->mutable_drive()->set_drive(value);
|
||||
});
|
||||
|
||||
extern const std::map<std::string, std::string> writables;
|
||||
|
||||
int mainWrite(int argc, const char* argv[])
|
||||
{
|
||||
flags.parseFlagsWithConfigFiles(argc, argv, writables);
|
||||
|
||||
if (!config.has_input() || !config.has_output())
|
||||
if (!config.input().has_disk() || !config.output().has_file())
|
||||
Error() << "incomplete config (did you remember to specify the format?)";
|
||||
|
||||
std::unique_ptr<ImageReader> reader(ImageReader::create(config.input().file()));
|
||||
|
||||
Reference in New Issue
Block a user