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