openMSX
HDCommand.cc
Go to the documentation of this file.
1 #include "HDCommand.hh"
2 #include "HD.hh"
3 #include "File.hh"
4 #include "FileContext.hh"
5 #include "FileException.hh"
6 #include "CommandException.hh"
7 #include "BooleanSetting.hh"
8 #include "TclObject.hh"
9 
10 namespace openmsx {
11 
12 using std::string;
13 using std::vector;
14 
15 // class HDCommand
16 
18  StateChangeDistributor& stateChangeDistributor,
19  Scheduler& scheduler, HD& hd_,
20  BooleanSetting& powerSetting_)
21  : RecordedCommand(commandController, stateChangeDistributor,
22  scheduler, hd_.getName())
23  , hd(hd_)
24  , powerSetting(powerSetting_)
25 {
26 }
27 
28 void HDCommand::execute(const std::vector<TclObject>& tokens, TclObject& result,
29  EmuTime::param /*time*/)
30 {
31  if (tokens.size() == 1) {
32  result.addListElement(hd.getName() + ':');
34 
35  TclObject options(result.getInterpreter());
36  if (hd.isWriteProtected()) {
37  options.addListElement("readonly");
38  }
39  if (options.getListLength() != 0) {
40  result.addListElement(options);
41  }
42  } else if ((tokens.size() == 2) ||
43  ((tokens.size() == 3) && tokens[1].getString() == "insert")) {
44  if (powerSetting.getValue()) {
45  throw CommandException(
46  "Can only change hard disk image when MSX "
47  "is powered down.");
48  }
49  int fileToken = 1;
50  if (tokens[1].getString() == "insert") {
51  if (tokens.size() > 2) {
52  fileToken = 2;
53  } else {
54  throw CommandException(
55  "Missing argument to insert subcommand");
56  }
57  }
58  try {
59  Filename filename(tokens[fileToken].getString().str(),
60  UserFileContext());
61  hd.switchImage(filename);
62  // Note: the diskX command doesn't do this either,
63  // so this has not been converted to TclObject style here
64  // return filename;
65  } catch (FileException& e) {
66  throw CommandException("Can't change hard disk image: " +
67  e.getMessage());
68  }
69  } else {
70  throw CommandException("Too many or wrong arguments.");
71  }
72 }
73 
74 string HDCommand::help(const vector<string>& /*tokens*/) const
75 {
76  return hd.getName() + ": change the hard disk image for this hard disk drive\n";
77 }
78 
79 void HDCommand::tabCompletion(vector<string>& tokens) const
80 {
81  vector<const char*> extra;
82  if (tokens.size() < 3) {
83  extra.push_back("insert");
84  }
85  completeFileName(tokens, UserFileContext(), extra);
86 }
87 
88 bool HDCommand::needRecord(const vector<string>& tokens) const
89 {
90  return tokens.size() > 1;
91 }
92 
93 } // namespace openmsx