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