openMSX
HDCommand.cc
Go to the documentation of this file.
1 #include "HDCommand.hh"
2 #include "HD.hh"
3 #include "FileContext.hh"
4 #include "FileException.hh"
5 #include "CommandException.hh"
6 #include "BooleanSetting.hh"
7 #include "TclObject.hh"
8 #include <array>
9 
10 namespace openmsx {
11 
12 // class HDCommand
13 
15  StateChangeDistributor& stateChangeDistributor_,
16  Scheduler& scheduler_, HD& hd_,
17  BooleanSetting& powerSetting_)
18  : RecordedCommand(commandController_, stateChangeDistributor_,
19  scheduler_, hd_.getName())
20  , hd(hd_)
21  , powerSetting(powerSetting_)
22 {
23 }
24 
25 void HDCommand::execute(std::span<const TclObject> tokens, TclObject& result,
26  EmuTime::param /*time*/)
27 {
28  if (tokens.size() == 1) {
29  result.addListElement(tmpStrCat(hd.getName(), ':'),
30  hd.getImageName().getResolved());
31 
32  if (hd.isWriteProtected()) {
33  TclObject options = makeTclList("readonly");
34  result.addListElement(options);
35  }
36  } else if ((tokens.size() == 2) ||
37  ((tokens.size() == 3) && tokens[1] == "insert")) {
38  if (powerSetting.getBoolean()) {
39  throw CommandException(
40  "Can only change hard disk image when MSX "
41  "is powered down.");
42  }
43  int fileToken = 1;
44  if (tokens[1] == "insert") {
45  if (tokens.size() > 2) {
46  fileToken = 2;
47  } else {
48  throw CommandException(
49  "Missing argument to insert subcommand");
50  }
51  }
52  try {
53  Filename filename(tokens[fileToken].getString(),
54  userFileContext());
56  // Note: the diskX command doesn't do this either,
57  // so this has not been converted to TclObject style here
58  // return filename;
59  } catch (FileException& e) {
60  throw CommandException("Can't change hard disk image: ",
61  e.getMessage());
62  }
63  } else {
64  throw CommandException("Too many or wrong arguments.");
65  }
66 }
67 
68 std::string HDCommand::help(std::span<const TclObject> /*tokens*/) const
69 {
70  return hd.getName() + ": change the hard disk image for this hard disk drive\n";
71 }
72 
73 void HDCommand::tabCompletion(std::vector<std::string>& tokens) const
74 {
75  using namespace std::literals;
76  static constexpr std::array extra = {"insert"sv};
78  (tokens.size() < 3) ? extra : std::span<const std::string_view>{});
79 
80 }
81 
82 bool HDCommand::needRecord(std::span<const TclObject> tokens) const
83 {
84  return tokens.size() > 1;
85 }
86 
87 } // namespace openmsx
bool getBoolean() const noexcept
static void completeFileName(std::vector< std::string > &tokens, const FileContext &context, const RANGE &extra)
Definition: Completer.hh:147
This class represents a filename.
Definition: Filename.hh:18
const std::string & getResolved() const &
Definition: Filename.hh:47
void tabCompletion(std::vector< std::string > &tokens) const override
Attempt tab completion for this command.
Definition: HDCommand.cc:73
bool needRecord(std::span< const TclObject > tokens) const override
It's possible that in some cases the command doesn't need to be recorded after all (e....
Definition: HDCommand.cc:82
void execute(std::span< const TclObject > tokens, TclObject &result, EmuTime::param time) override
This is like the execute() method of the Command class, it only has an extra time parameter.
Definition: HDCommand.cc:25
HDCommand(CommandController &commandController, StateChangeDistributor &stateChangeDistributor, Scheduler &scheduler, HD &hd, BooleanSetting &powerSetting)
Definition: HDCommand.cc:14
std::string help(std::span< const TclObject > tokens) const override
Print help for this command.
Definition: HDCommand.cc:68
const std::string & getName() const
Definition: HD.hh:27
void switchImage(const Filename &filename)
Definition: HD.cc:81
const Filename & getImageName() const
Definition: HD.hh:28
Commands that directly influence the MSX state should send and events so that they can be recorded by...
void addListElement(const T &t)
Definition: TclObject.hh:127
constexpr double e
Definition: Math.hh:18
std::string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:727
This file implemented 3 utility functions:
Definition: Autofire.cc:9
constexpr const char *const filename
FileContext userFileContext(string_view savePath)
Definition: FileContext.cc:171
TclObject makeTclList(Args &&... args)
Definition: TclObject.hh:281
TemporaryString tmpStrCat(Ts &&... ts)
Definition: strCat.hh:617