HDCommand.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. namespace openmsx {
  9. using std::string;
  10. using std::vector;
  11. // class HDCommand
  12. HDCommand::HDCommand(CommandController& commandController_,
  13. StateChangeDistributor& stateChangeDistributor_,
  14. Scheduler& scheduler_, HD& hd_,
  15. BooleanSetting& powerSetting_)
  16. : RecordedCommand(commandController_, stateChangeDistributor_,
  17. scheduler_, hd_.getName())
  18. , hd(hd_)
  19. , powerSetting(powerSetting_)
  20. {
  21. }
  22. void HDCommand::execute(span<const TclObject> tokens, TclObject& result,
  23. EmuTime::param /*time*/)
  24. {
  25. if (tokens.size() == 1) {
  26. result.addListElement(hd.getName() + ':',
  27. hd.getImageName().getResolved());
  28. if (hd.isWriteProtected()) {
  29. TclObject options = makeTclList("readonly");
  30. result.addListElement(options);
  31. }
  32. } else if ((tokens.size() == 2) ||
  33. ((tokens.size() == 3) && tokens[1] == "insert")) {
  34. if (powerSetting.getBoolean()) {
  35. throw CommandException(
  36. "Can only change hard disk image when MSX "
  37. "is powered down.");
  38. }
  39. int fileToken = 1;
  40. if (tokens[1] == "insert") {
  41. if (tokens.size() > 2) {
  42. fileToken = 2;
  43. } else {
  44. throw CommandException(
  45. "Missing argument to insert subcommand");
  46. }
  47. }
  48. try {
  49. Filename filename(string(tokens[fileToken].getString()),
  50. userFileContext());
  51. hd.switchImage(filename);
  52. // Note: the diskX command doesn't do this either,
  53. // so this has not been converted to TclObject style here
  54. // return filename;
  55. } catch (FileException& e) {
  56. throw CommandException("Can't change hard disk image: ",
  57. e.getMessage());
  58. }
  59. } else {
  60. throw CommandException("Too many or wrong arguments.");
  61. }
  62. }
  63. string HDCommand::help(const vector<string>& /*tokens*/) const
  64. {
  65. return hd.getName() + ": change the hard disk image for this hard disk drive\n";
  66. }
  67. void HDCommand::tabCompletion(vector<string>& tokens) const
  68. {
  69. vector<const char*> extra;
  70. if (tokens.size() < 3) {
  71. extra = { "insert" };
  72. }
  73. completeFileName(tokens, userFileContext(), extra);
  74. }
  75. bool HDCommand::needRecord(span<const TclObject> tokens) const
  76. {
  77. return tokens.size() > 1;
  78. }
  79. } // namespace openmsx