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(string(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  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
openmsx::HD::getImageName
const Filename & getImageName() const
Definition: HD.hh:28
openmsx::CommandException
Definition: CommandException.hh:9
openmsx::HD::getName
const std::string & getName() const
Definition: HD.hh:27
FileException.hh
openmsx::Scheduler
Definition: Scheduler.hh:34
openmsx::CommandController
Definition: CommandController.hh:18
openmsx::Completer::completeFileName
static void completeFileName(std::vector< std::string > &tokens, const FileContext &context, const RANGE &extra)
Definition: Completer.hh:139
TclObject.hh
openmsx::StateChangeDistributor
Definition: StateChangeDistributor.hh:15
openmsx::SectorAccessibleDisk::isWriteProtected
bool isWriteProtected() const
Definition: SectorAccessibleDisk.cc:125
openmsx::HD
Definition: HD.hh:22
openmsx::Filename::getResolved
const std::string & getResolved() const
Definition: Filename.hh:27
openmsx::HDCommand::execute
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
openmsx::userFileContext
FileContext userFileContext(string_view savePath)
Definition: FileContext.cc:164
openmsx::HDCommand::HDCommand
HDCommand(CommandController &commandController, StateChangeDistributor &stateChangeDistributor, Scheduler &scheduler, HD &hd, BooleanSetting &powerSetting)
Definition: HDCommand.cc:16
BooleanSetting.hh
span
Definition: span.hh:126
openmsx::TclObject::addListElement
void addListElement(T t)
Definition: TclObject.hh:121
openmsx::Keys::getName
string getName(KeyCode keyCode)
Translate key code to key name.
Definition: Keys.cc:740
openmsx::filename
constexpr const char *const filename
Definition: FirmwareSwitch.cc:10
openmsx::FileException
Definition: FileException.hh:9
FileContext.hh
openmsx::HDCommand::help
std::string help(const std::vector< std::string > &tokens) const override
Print help for this command.
Definition: HDCommand.cc:70
HD.hh
span::size
constexpr index_type size() const noexcept
Definition: span.hh:296
openmsx::BooleanSetting
Definition: BooleanSetting.hh:9
openmsx::RecordedCommand
Commands that directly influence the MSX state should send and events so that they can be recorded by...
Definition: RecordedCommand.hh:46
openmsx::makeTclList
TclObject makeTclList(Args &&... args)
Definition: TclObject.hh:280
HDCommand.hh
openmsx::HDCommand::needRecord
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
openmsx::TclObject
Definition: TclObject.hh:22
openmsx::HD::switchImage
void switchImage(const Filename &filename)
Definition: HD.cc:85
openmsx::HDCommand::tabCompletion
void tabCompletion(std::vector< std::string > &tokens) const override
Attempt tab completion for this command.
Definition: HDCommand.cc:75
openmsx::Filename
This class represents a filename.
Definition: Filename.hh:18
openmsx::MSXException::getMessage
const std::string & getMessage() const &
Definition: MSXException.hh:23
CommandException.hh
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::BooleanSetting::getBoolean
bool getBoolean() const noexcept
Definition: BooleanSetting.hh:17