Move setting the image writer into Config.

This commit is contained in:
dg
2023-05-11 23:06:24 +00:00
parent 08e9e508cc
commit 790f0a42e3
8 changed files with 41 additions and 49 deletions

View File

@@ -322,3 +322,38 @@ void Config::setImageReader(std::string filename)
error("unrecognised image filename '{}'", filename);
}
void Config::setImageWriter(std::string filename)
{
static const std::map<std::string, std::function<void(ImageWriterProto*)>>
formats = {
// clang-format off
{".adf", [](auto* proto) { proto->set_type(ImageWriterProto::IMG); }},
{".d64", [](auto* proto) { proto->set_type(ImageWriterProto::D64); }},
{".d81", [](auto* proto) { proto->set_type(ImageWriterProto::IMG); }},
{".d88", [](auto* proto) { proto->set_type(ImageWriterProto::D88); }},
{".diskcopy", [](auto* proto) { proto->set_type(ImageWriterProto::DISKCOPY); }},
{".dsk", [](auto* proto) { proto->set_type(ImageWriterProto::IMG); }},
{".img", [](auto* proto) { proto->set_type(ImageWriterProto::IMG); }},
{".imd", [](auto* proto) { proto->set_type(ImageWriterProto::IMD); }},
{".ldbs", [](auto* proto) { proto->set_type(ImageWriterProto::LDBS); }},
{".nsi", [](auto* proto) { proto->set_type(ImageWriterProto::NSI); }},
{".raw", [](auto* proto) { proto->set_type(ImageWriterProto::RAW); }},
{".st", [](auto* proto) { proto->set_type(ImageWriterProto::IMG); }},
{".vgi", [](auto* proto) { proto->set_type(ImageWriterProto::IMG); }},
{".xdf", [](auto* proto) { proto->set_type(ImageWriterProto::IMG); }},
// clang-format on
};
for (const auto& it : formats)
{
if (endsWith(filename, it.first))
{
it.second((*this)->mutable_image_writer());
(*this)->mutable_image_writer()->set_filename(filename);
return;
}
}
error("unrecognised image filename '{}'", filename);
}

View File

@@ -70,6 +70,7 @@ public:
void setFluxSink(std::string value);
void setCopyFluxTo(std::string value);
void setImageReader(std::string value);
void setImageWriter(std::string value);
private:
IOState _readState = IO_NONE;

View File

@@ -45,42 +45,6 @@ std::unique_ptr<ImageWriter> ImageWriter::create(const ImageWriterProto& config)
}
}
void ImageWriter::updateConfigForFilename(
ImageWriterProto* proto, const std::string& filename)
{
static const std::map<std::string, std::function<void(ImageWriterProto*)>>
formats = {
// clang-format off
{".adf", [](auto* proto) { proto->set_type(ImageWriterProto::IMG); }},
{".d64", [](auto* proto) { proto->set_type(ImageWriterProto::D64); }},
{".d81", [](auto* proto) { proto->set_type(ImageWriterProto::IMG); }},
{".d88", [](auto* proto) { proto->set_type(ImageWriterProto::D88); }},
{".diskcopy", [](auto* proto) { proto->set_type(ImageWriterProto::DISKCOPY); }},
{".dsk", [](auto* proto) { proto->set_type(ImageWriterProto::IMG); }},
{".img", [](auto* proto) { proto->set_type(ImageWriterProto::IMG); }},
{".imd", [](auto* proto) { proto->set_type(ImageWriterProto::IMD); }},
{".ldbs", [](auto* proto) { proto->set_type(ImageWriterProto::LDBS); }},
{".nsi", [](auto* proto) { proto->set_type(ImageWriterProto::NSI); }},
{".raw", [](auto* proto) { proto->set_type(ImageWriterProto::RAW); }},
{".st", [](auto* proto) { proto->set_type(ImageWriterProto::IMG); }},
{".vgi", [](auto* proto) { proto->set_type(ImageWriterProto::IMG); }},
{".xdf", [](auto* proto) { proto->set_type(ImageWriterProto::IMG); }},
// clang-format on
};
for (const auto& it : formats)
{
if (endsWith(filename, it.first))
{
it.second(proto);
proto->set_filename(filename);
return;
}
}
error("unrecognised image filename '{}'", filename);
}
ImageWriter::ImageWriter(const ImageWriterProto& config): _config(config) {}
void ImageWriter::writeCsv(const Image& image, const std::string& filename)

View File

@@ -12,8 +12,6 @@ public:
public:
static std::unique_ptr<ImageWriter> create(const ImageWriterProto& config);
static void updateConfigForFilename(
ImageWriterProto* proto, const std::string& filename);
static std::unique_ptr<ImageWriter> createD64ImageWriter(
const ImageWriterProto& config);

View File

@@ -30,8 +30,7 @@ static StringFlag destImage({"-o", "--output"},
"",
[](const auto& value)
{
ImageWriter::updateConfigForFilename(
globalConfig()->mutable_image_writer(), value);
globalConfig().setImageWriter(value);
});
static StringFlag copyFluxTo({"--copy-flux-to"},

View File

@@ -22,8 +22,7 @@ static StringFlag image({"-i", "--image"},
[](const auto& value)
{
globalConfig().setImageReader(value);
ImageWriter::updateConfigForFilename(
globalConfig()->mutable_image_writer(), value);
globalConfig().setImageWriter(value);
});
static StringFlag flux({"-f", "--flux"},

View File

@@ -259,9 +259,7 @@ public:
case SELECTEDSOURCE_IMAGE:
{
globalConfig().setImageReader(_selectedImagefilename);
ImageWriter::updateConfigForFilename(
globalConfig()->mutable_image_writer(),
_selectedImagefilename);
globalConfig().setImageWriter(_selectedImagefilename);
break;
}
}

View File

@@ -138,8 +138,7 @@ public:
return;
globalConfig().setImageReader(filename.ToStdString());
ImageWriter::updateConfigForFilename(
globalConfig()->mutable_image_writer(), filename.ToStdString());
globalConfig().setImageWriter(filename.ToStdString());
visualiser->Clear();
_currentDisk = nullptr;
@@ -261,8 +260,7 @@ public:
if (filename.empty())
return;
ImageWriter::updateConfigForFilename(
globalConfig()->mutable_image_writer(), filename.ToStdString());
globalConfig().setImageWriter(filename.ToStdString());
auto image = _currentDisk->image;