openMSX
HDImageCLI.cc
Go to the documentation of this file.
1#include "HDImageCLI.hh"
3#include "MSXException.hh"
4#include "ranges.hh"
5#include <utility>
6#include <vector>
7
8namespace openmsx {
9
10namespace {
11 struct IdImage {
12 IdImage(int i, std::string m)
13 : id(i), image(std::move(m)) {} // clang-15 workaround
14
15 int id;
16 std::string image;
17 };
18}
19static std::vector<IdImage> images;
20
22 : parser(parser_)
23{
25 // TODO: offer more options in case you want to specify 2 hard disk images?
26}
27
28void HDImageCLI::parseOption(const std::string& option, std::span<std::string>& cmdLine)
29{
30 // Machine has not been loaded yet. Only remember the image.
31 int id = option[3] - 'a';
32 images.emplace_back(id, getArgument(option, cmdLine));
33}
34
35std::string HDImageCLI::getImageForId(int id)
36{
37 // HD queries image. Return (and clear) the remembered value, or return
38 // an empty string.
39 std::string result;
40 if (auto it = ranges::find(images, id, &IdImage::id);
41 it != end(images)) {
42 result = std::move(it->image);
43 images.erase(it);
44 }
45 return result;
46}
47
49{
50 // After parsing all remembered values should be cleared. If not there
51 // was no hard disk as specified.
52 if (!images.empty()) {
53 auto hd = char(::toupper('a' + images.front().id));
54 throw MSXException("No hard disk ", hd, " present.");
55 }
56}
57
58std::string_view HDImageCLI::optionHelp() const
59{
60 return "Use hard disk image in argument for the IDE or SCSI extensions";
61}
62
63} // namespace openmsx
std::string image
Definition HDImageCLI.cc:16
uintptr_t id
static std::string getArgument(const std::string &option, std::span< std::string > &cmdLine)
Definition CLIOption.cc:9
void registerOption(const char *str, CLIOption &cliOption, ParsePhase phase=PHASE_LAST, unsigned length=2)
void parseDone() override
Definition HDImageCLI.cc:48
std::string_view optionHelp() const override
Definition HDImageCLI.cc:58
HDImageCLI(CommandLineParser &parser)
Definition HDImageCLI.cc:21
void parseOption(const std::string &option, std::span< std::string > &cmdLine) override
Definition HDImageCLI.cc:28
static std::string getImageForId(int id)
Definition HDImageCLI.cc:35
This file implemented 3 utility functions:
Definition Autofire.cc:11
auto find(InputRange &&range, const T &value)
Definition ranges.hh:162
STL namespace.
constexpr auto end(const zstring_view &x)