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 
9 namespace openmsx {
10 
11 // class HDCommand
12 
14  StateChangeDistributor& stateChangeDistributor_,
15  Scheduler& scheduler_, HD& hd_,
16  BooleanSetting& powerSetting_)
17  : RecordedCommand(commandController_, stateChangeDistributor_,
18  scheduler_, hd_.getName())
19  , hd(hd_)
20  , powerSetting(powerSetting_)
21 {
22 }
23 
25  EmuTime::param /*time*/)
26 {
27  if (tokens.size() == 1) {
28  result.addListElement(tmpStrCat(hd.getName(), ':'),
29  hd.getImageName().getResolved());
30 
31  if (hd.isWriteProtected()) {
32  TclObject options = makeTclList("readonly");
33  result.addListElement(options);
34  }
35  } else if ((tokens.size() == 2) ||
36  ((tokens.size() == 3) && tokens[1] == "insert")) {
37  if (powerSetting.getBoolean()) {
38  throw CommandException(
39  "Can only change hard disk image when MSX "
40  "is powered down.");
41  }
42  int fileToken = 1;
43  if (tokens[1] == "insert") {
44  if (tokens.size() > 2) {
45  fileToken = 2;
46  } else {
47  throw CommandException(
48  "Missing argument to insert subcommand");
49  }
50  }
51  try {
52  Filename filename(tokens[fileToken].getString(),
53  userFileContext());
55  // Note: the diskX command doesn't do this either,
56  // so this has not been converted to TclObject style here
57  // return filename;
58  } catch (FileException& e) {
59  throw CommandException("Can't change hard disk image: ",
60  e.getMessage());
61  }
62  } else {
63  throw CommandException("Too many or wrong arguments.");
64  }
65 }
66 
67 std::string HDCommand::help(span<const TclObject> /*tokens*/) const
68 {
69  return hd.getName() + ": change the hard disk image for this hard disk drive\n";
70 }
71 
72 void HDCommand::tabCompletion(std::vector<std::string>& tokens) const
73 {
74  using namespace std::literals;
75  static constexpr std::array extra = {"insert"sv};
77  (tokens.size() < 3) ? extra : span<const std::string_view>{});
78 
79 }
80 
82 {
83  return tokens.size() > 1;
84 }
85 
86 } // 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
std::string help(span< const TclObject > tokens) const override
Print help for this command.
Definition: HDCommand.cc:67
void tabCompletion(std::vector< std::string > &tokens) const override
Attempt tab completion for this command.
Definition: HDCommand.cc:72
void execute(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:24
bool needRecord(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:81
HDCommand(CommandController &commandController, StateChangeDistributor &stateChangeDistributor, Scheduler &scheduler, HD &hd, BooleanSetting &powerSetting)
Definition: HDCommand.cc:13
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
const std::string & getMessage() const &
Definition: MSXException.hh:23
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:129
Definition: span.hh:126
constexpr index_type size() const noexcept
Definition: span.hh:296
std::string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:741
This file implemented 3 utility functions:
Definition: Autofire.cc:9
constexpr const char *const filename
FileContext userFileContext(string_view savePath)
Definition: FileContext.cc:173
TclObject makeTclList(Args &&... args)
Definition: TclObject.hh:290
TemporaryString tmpStrCat(Ts &&... ts)
Definition: strCat.hh:659