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(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().str(),
56  userFileContext());
57  hd.switchImage(filename);
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  vector<const char*> extra;
78  if (tokens.size() < 3) {
79  extra = { "insert" };
80  }
81  completeFileName(tokens, userFileContext(), extra);
82 }
83 
85 {
86  return tokens.size() > 1;
87 }
88 
89 } // namespace openmsx
void tabCompletion(std::vector< std::string > &tokens) const override
Attempt tab completion for this command.
Definition: HDCommand.cc:75
const std::string & getMessage() const &
Definition: MSXException.hh:23
Definition: span.hh:34
Commands that directly influence the MSX state should send and events so that they can be recorded by...
const Filename & getImageName() const
Definition: HD.hh:28
static void completeFileName(std::vector< std::string > &tokens, const FileContext &context, const RANGE &extra)
Definition: Completer.hh:138
bool getBoolean() const noexcept
FileContext userFileContext(string_view savePath)
Definition: FileContext.cc:160
const std::string & getName() const
Definition: HD.hh:27
This class represents a filename.
Definition: Filename.hh:17
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
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
bool needRecord(span< const TclObject > tokens) const override
It&#39;s possible that in some cases the command doesn&#39;t need to be recorded after all (e...
Definition: HDCommand.cc:84
HDCommand(CommandController &commandController, StateChangeDistributor &stateChangeDistributor, Scheduler &scheduler, HD &hd, BooleanSetting &powerSetting)
Definition: HDCommand.cc:16
void switchImage(const Filename &filename)
Definition: HD.cc:85
void addListElement(T t)
Definition: TclObject.hh:121
const std::string & getResolved() const
Definition: Filename.hh:27
string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:589
TclObject makeTclList(Args &&... args)
Definition: TclObject.hh:280
std::string help(const std::vector< std::string > &tokens) const override
Print help for this command.
Definition: HDCommand.cc:70