OXYGENHUB
◆ main openmsx / src / ide / HDCommand.cc
2.4 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
#include "HDCommand.hh"
2
3
#include "HD.hh"
4
5
#include "BooleanSetting.hh"
6
#include "CommandException.hh"
7
#include "FileContext.hh"
8
#include "FileException.hh"
9
#include "TclObject.hh"
10
11
#include <array>
12
13
namespace openmsx {
14
15
// class HDCommand
16
17
HDCommand::HDCommand(CommandController& commandController_,
18
                     StateChangeDistributor& stateChangeDistributor_,
19
                     Scheduler& scheduler_, HD& hd_,
20
                     const BooleanSetting& powerSetting_)
21
	: RecordedCommand(commandController_, stateChangeDistributor_,
22
	                  scheduler_, hd_.getName())
23
	, hd(hd_)
24
	, powerSetting(powerSetting_)
25
{
26
}
27
28
void HDCommand::execute(std::span<const TclObject> tokens, TclObject& result,
29
                        EmuTime /*time*/)
30
{
31
	if (tokens.size() == 1) {
32
		result.addListElement(tmpStrCat(hd.getName(), ':'),
33
		                      hd.getImageName().getResolved());
34
35
		if (hd.isWriteProtected()) {
36
			TclObject options = makeTclList("readonly");
37
			result.addListElement(options);
38
		}
39
	} else if ((tokens.size() == 2) ||
40
	           ((tokens.size() == 3) && tokens[1] == "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] == "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(),
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
std::string HDCommand::help(std::span<const TclObject> /*tokens*/) const
72
{
73
	return hd.getName() + ": change the hard disk image for this hard disk drive\n";
74
}
75
76
void HDCommand::tabCompletion(std::vector<std::string>& tokens) const
77
{
78
	using namespace std::literals;
79
	static constexpr std::array extra = {"insert"sv};
80
	completeFileName(tokens, userFileContext(),
81
		(tokens.size() < 3) ? extra : std::span<const std::string_view>{});
82
83
}
84
85
bool HDCommand::needRecord(std::span<const TclObject> tokens) const
86
{
87
	return tokens.size() > 1;
88
}
89
90
} // namespace openmsx