OXYGENHUB
◆ main openmsx / src / fdc / DiskPartition.cc
1.5 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#include "DiskPartition.hh"
2
3
#include "DiskImageUtils.hh"
4
#include "Filename.hh"
5
6
#include "strCat.hh"
7
8
#include <cassert>
9
10
namespace openmsx {
11
12
using namespace DiskImageUtils;
13
14
[[nodiscard]] static DiskName getDiskName(SectorAccessibleDisk* disk, unsigned partition)
15
{
16
	if (const auto* dsk = dynamic_cast<Disk*>(disk)) {
17
		return {dsk->getName().getFilename(),
18
		        strCat(':', partition)};
19
	} else {
20
		return DiskName(Filename("dummy"));
21
	}
22
}
23
24
DiskPartition::DiskPartition(SectorAccessibleDisk& disk, unsigned partition,
25
                             std::shared_ptr<SectorAccessibleDisk> owned_)
26
	: SectorBasedDisk(getDiskName(&disk, partition))
27
	, parent(disk)
28
	, owned(std::move(owned_))
29
{
30
	assert(!owned || (owned.get() == &disk));
31
32
	if (partition == 0) {
33
		start = 0;
34
		setNbSectors(disk.getNbSectors());
35
	} else {
36
		SectorBuffer buf;
37
		const Partition& p = getPartition(disk, partition, buf); // throws
38
		start = p.start;
39
		setNbSectors(p.size);
40
	}
41
}
42
43
DiskPartition::DiskPartition(SectorAccessibleDisk& parent_,
44
                             size_t start_, size_t length)
45
	: SectorBasedDisk(getDiskName(nullptr, 0))
46
	, parent(parent_)
47
	, start(start_)
48
{
49
	setNbSectors(length);
50
}
51
52
void DiskPartition::readSectorImpl(size_t sector, SectorBuffer& buf)
53
{
54
	parent.readSector(start + sector, buf);
55
}
56
57
void DiskPartition::writeSectorImpl(size_t sector, const SectorBuffer& buf)
58
{
59
	parent.writeSector(start + sector, buf);
60
}
61
62
bool DiskPartition::isWriteProtectedImpl() const
63
{
64
	return parent.isWriteProtected();
65
}
66
67
} // namespace openmsx