OXYGENHUB
◆ main openmsx / src / cassette / CassetteImage.cc
1.5 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
e8555c89 Andy McCall 1
#include "CassetteImage.hh"
2
3
#include "FileOperations.hh"
4
#include "Filename.hh"
5
6
#include <cassert>
7
8
namespace openmsx {
9
10
std::string CassetteImage::getFirstFileTypeAsString() const
11
{
12
	using enum FileType;
13
	if (firstFileType == ASCII) {
14
		return "ASCII";
15
	} else if (firstFileType == BINARY) {
16
		return "binary";
17
	} else if (firstFileType == BASIC) {
18
		return "BASIC";
19
	} else {
20
		return "unknown";
21
	}
22
}
23
24
void CassetteImage::setFirstFileType(FileType type, const Filename& fileName)
25
{
26
	using enum FileType;
27
	if (type == UNKNOWN) {
28
		// see if there is a hint in the filename
29
		const auto& file = fileName.getResolved();
30
		auto fileStem = FileOperations::stem(file);
31
32
		auto containsInstructionAndCAS = [&](std::string_view instruction) {
33
			auto pos = fileStem.find(instruction);
34
			if (pos == std::string_view::npos) return false;
35
			return fileStem.find("CAS", pos + instruction.size()) != std::string_view::npos;
36
		};
37
38
		if (containsInstructionAndCAS("BLOAD")) {
39
			firstFileType = BINARY;
40
		} else if (fileStem.contains("CLOAD")) {
41
			// note: probably better to first check on CLOAD and then on RUN. Some filenames
42
			// contain hints like CLOAD + RUN.
43
			firstFileType = BASIC;
44
		} else if (containsInstructionAndCAS("RUN")) {
45
			firstFileType = ASCII;
46
		}
47
	} else {
48
		firstFileType = type;
49
	}
50
}
51
52
void CassetteImage::setSha1Sum(const Sha1Sum& sha1sum_)
53
{
54
	assert(sha1sum.empty());
55
	sha1sum = sha1sum_;
56
}
57
58
const Sha1Sum& CassetteImage::getSha1Sum() const
59
{
60
	assert(!sha1sum.empty());
61
	return sha1sum;
62
}
63
64
} // namespace openmsx