openMSX
DiskPartition.cc
Go to the documentation of this file.
1 #include "DiskPartition.hh"
2 #include "DiskImageUtils.hh"
3 #include "Filename.hh"
4 #include "strCat.hh"
5 #include <cassert>
6 
7 namespace openmsx {
8 
9 using namespace DiskImageUtils;
10 
11 static DiskName getDiskName(SectorAccessibleDisk* disk, unsigned partition)
12 {
13  if (auto* dsk = dynamic_cast<Disk*>(disk)) {
14  return DiskName(dsk->getName().getFilename(),
15  strCat(':', partition));
16  } else {
17  return DiskName(Filename("dummy"));
18  }
19 }
20 
22  std::shared_ptr<SectorAccessibleDisk> owned_)
23  : SectorBasedDisk(getDiskName(&disk, partition))
24  , parent(disk)
25  , owned(std::move(owned_))
26 {
27  assert(!owned || (owned.get() == &disk));
28 
29  if (partition == 0) {
30  start = 0;
31  setNbSectors(disk.getNbSectors());
32  } else {
33  checkValidPartition(disk, partition); // throws
34  SectorBuffer buf;
35  disk.readSector(0, buf);
36  auto& p = buf.pt.part[31 - partition];
37  start = p.start;
38  setNbSectors(p.size);
39  }
40 }
41 
43  size_t start_, size_t length)
44  : SectorBasedDisk(getDiskName(nullptr, 0))
45  , parent(parent_)
46  , start(start_)
47 {
48  setNbSectors(length);
49 }
50 
51 void DiskPartition::readSectorImpl(size_t sector, SectorBuffer& buf)
52 {
53  parent.readSector(start + sector, buf);
54 }
55 
56 void DiskPartition::writeSectorImpl(size_t sector, const SectorBuffer& buf)
57 {
58  parent.writeSector(start + sector, buf);
59 }
60 
61 bool DiskPartition::isWriteProtectedImpl() const
62 {
63  return parent.isWriteProtected();
64 }
65 
66 } // namespace openmsx
void readSector(size_t sector, SectorBuffer &buf)
T length(const vecN< N, T > &x)
Definition: gl_vec.hh:343
Abstract class for disk images that only represent the logical sector information (so not the raw tra...
STL namespace.
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void partition(SectorAccessibleDisk &disk, const std::vector< unsigned > &sizes)
Write a partition table to the given disk and format each partition.
std::string strCat(Ts &&...ts)
Definition: strCat.hh:577
DiskPartition(SectorAccessibleDisk &disk, unsigned partition, std::shared_ptr< SectorAccessibleDisk > owned=nullptr)
Return a partition (as a SectorbasedDisk) from another Disk.
void setNbSectors(size_t num)
void checkValidPartition(SectorAccessibleDisk &disk, unsigned partition)
Checks whether the disk is partitioned the specified partition exists throws a CommandException if on...
void writeSector(size_t sector, const SectorBuffer &buf)