1
0
mirror of https://github.com/TomHarte/CLK.git synced 2026-04-26 19:17:52 +00:00

Create a specific container for HDV files.

This commit is contained in:
Thomas Harte
2022-11-15 13:18:54 -05:00
parent c8a82933bc
commit 9b235a8f64
3 changed files with 129 additions and 2 deletions
+63
View File
@@ -0,0 +1,63 @@
//
// HDV.cpp
// Clock Signal
//
// Created by Thomas Harte on 14/11/2022.
// Copyright © 2022 Thomas Harte. All rights reserved.
//
#include "HDV.hpp"
#include <algorithm>
using namespace Storage::MassStorage;
HDV::HDV(const std::string &file_name, long start, long size):
file_(file_name),
file_start_(start),
image_size_(std::min(size, long(file_.stats().st_size)))
{
mapper_.set_drive_type(
Storage::MassStorage::Encodings::Apple::DriveType::SCSI,
size_t(size / 512)
);
}
size_t HDV::get_block_size() {
return 512;
}
size_t HDV::get_number_of_blocks() {
return mapper_.get_number_of_blocks();
}
std::vector<uint8_t> HDV::get_block(size_t address) {
const auto source_address = mapper_.to_source_address(address);
const auto file_offset = offset_for_block(source_address);
if(source_address >= 0) {
file_.seek(file_offset, SEEK_SET);
return mapper_.convert_source_block(source_address, file_.read(get_block_size()));
} else {
return mapper_.convert_source_block(source_address);
}
}
void HDV::set_block(size_t address, const std::vector<uint8_t> &data) {
const auto source_address = mapper_.to_source_address(address);
const auto file_offset = offset_for_block(source_address);
if(source_address >= 0 && file_offset >= 0) {
file_.seek(file_offset, SEEK_SET);
file_.write(data);
}
}
long HDV::offset_for_block(ssize_t address) {
if(address < 0) return -1;
const long offset = 512 * address;
if(offset > image_size_ - 512) return -1;
return file_start_ + offset;
}
+56
View File
@@ -0,0 +1,56 @@
//
// HDV.hpp
// Clock Signal
//
// Created by Thomas Harte on 14/11/2022.
// Copyright © 2022 Thomas Harte. All rights reserved.
//
#ifndef HDV_hpp
#define HDV_hpp
#include "../MassStorageDevice.hpp"
#include "../../FileHolder.hpp"
#include "../Encodings/AppleIIVolume.hpp"
#include <limits>
#include <vector>
namespace Storage {
namespace MassStorage {
/*!
Provides a @c MassStorageDevice containing an HDV image, which is a sector dump of
the ProDOS volume of an Apple II drive.
*/
class HDV: public MassStorageDevice {
public:
/*!
Constructs an HDV with the contents of the file named @c file_name within
the range given.
Raises an exception if the file name doesn't appear to identify a valid
Apple II mass storage image.
*/
HDV(const std::string &file_name, long start = 0, long size = std::numeric_limits<long>::max());
private:
FileHolder file_;
long file_start_, image_size_;
Storage::MassStorage::Encodings::AppleII::Mapper mapper_;
/// @returns -1 if @c address is out of range; the offset into the file at which
/// the block for @c address resides otherwise.
long offset_for_block(ssize_t address);
/* MassStorageDevices overrides. */
size_t get_block_size() final;
size_t get_number_of_blocks() final;
std::vector<uint8_t> get_block(size_t address) final;
void set_block(size_t address, const std::vector<uint8_t> &) final;
};
}
}
#endif /* HDV_hpp */