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() + ':');
33 
34  if (hd.isWriteProtected()) {
35  TclObject options;
36  options.addListElement("readonly");
37  result.addListElement(options);
38  }
39  } else if ((tokens.size() == 2) ||
40  ((tokens.size() == 3) && tokens[1].getString() == "insert")) {
41  if (powerSetting.getBoolean()) {
42  throw CommandException(
43  "Can only change hard disk image when MSX "
44  "is powered down.");
45  }
46  int fileToken = 1;
47  if (tokens[1].getString() == "insert") {
48  if (tokens.size() > 2) {
49  fileToken = 2;
50  } else {
51  throw CommandException(
52  "Missing argument to insert subcommand");
53  }
54  }
55  try {
56  Filename filename(tokens[fileToken].getString().str(),
57  UserFileContext());
58  hd.switchImage(filename);
59  // Note: the diskX command doesn't do this either,
60  // so this has not been converted to TclObject style here
61  // return filename;
62  } catch (FileException& e) {
63  throw CommandException("Can't change hard disk image: " +
64  e.getMessage());
65  }
66  } else {
67  throw CommandException("Too many or wrong arguments.");
68  }
69 }
70 
71 string HDCommand::help(const vector<string>& /*tokens*/) const
72 {
73  return hd.getName() + ": change the hard disk image for this hard disk drive\n";
74 }
75 
76 void HDCommand::tabCompletion(vector<string>& tokens) const
77 {
78  vector<const char*> extra;
79  if (tokens.size() < 3) {
80  extra = { "insert" };
81  }
82  completeFileName(tokens, UserFileContext(), extra);
83 }
84 
86 {
87  return tokens.size() > 1;
88 }
89 
90 } // namespace openmsx
void tabCompletion(std::vector< std::string > &tokens) const override
Attempt tab completion for this command.
Definition: HDCommand.cc:76
size_type size() const
Definition: array_ref.hh:61
const std::string & getName() const
Definition: HD.hh:27
Commands that directly influence the MSX state should send and events so that they can be recorded by...
void execute(array_ref< 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
const std::string & getResolved() const
Definition: Filename.hh:25
bool needRecord(array_ref< 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:85
static void completeFileName(std::vector< std::string > &tokens, const FileContext &context, const RANGE &extra)
Definition: Completer.hh:120
This class implements a subset of the proposal for std::array_ref (proposed for the next c++ standard...
Definition: array_ref.hh:19
const std::string & getMessage() const
Definition: MSXException.hh:14
This class represents a filename.
Definition: Filename.hh:17
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:7
HDCommand(CommandController &commandController, StateChangeDistributor &stateChangeDistributor, Scheduler &scheduler, HD &hd, BooleanSetting &powerSetting)
Definition: HDCommand.cc:16
void switchImage(const Filename &filename)
Definition: HD.cc:99
void addListElement(string_ref element)
Definition: TclObject.cc:120
const string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:373
const Filename & getImageName() const
Definition: HD.hh:28
std::string help(const std::vector< std::string > &tokens) const override
Print help for this command.
Definition: HDCommand.cc:71