mirror of
https://github.com/TomHarte/CLK.git
synced 2026-04-26 19:17:52 +00:00
Discovering that there is such a thing as P81 — a ZX81 file without the name omitted — added support for it. Extended FileHolder while I was here to retain the file name and be able to supply its extension, as my quick-fix test-the-last-character approach to o/p/80/81 discrimination stops working with p81 thrown into the mix and this feels like the correct factoring.
This commit is contained in:
+23
-23
@@ -12,26 +12,22 @@
|
||||
|
||||
using namespace Storage;
|
||||
|
||||
FileHolder::~FileHolder()
|
||||
{
|
||||
FileHolder::~FileHolder() {
|
||||
if(file_) fclose(file_);
|
||||
}
|
||||
|
||||
FileHolder::FileHolder(const char *file_name) : file_(nullptr)
|
||||
{
|
||||
stat(file_name, &file_stats_);
|
||||
FileHolder::FileHolder(const std::string &file_name) : file_(nullptr), name_(file_name) {
|
||||
stat(file_name.c_str(), &file_stats_);
|
||||
is_read_only_ = false;
|
||||
file_ = fopen(file_name, "rb+");
|
||||
if(!file_)
|
||||
{
|
||||
file_ = fopen(file_name.c_str(), "rb+");
|
||||
if(!file_) {
|
||||
is_read_only_ = true;
|
||||
file_ = fopen(file_name, "rb");
|
||||
file_ = fopen(file_name.c_str(), "rb");
|
||||
}
|
||||
if(!file_) throw ErrorCantOpen;
|
||||
}
|
||||
|
||||
bool FileHolder::check_signature(const char *signature, size_t length)
|
||||
{
|
||||
bool FileHolder::check_signature(const char *signature, size_t length) {
|
||||
if(!length) length = strlen(signature)+1;
|
||||
|
||||
// read and check the file signature
|
||||
@@ -41,8 +37,7 @@ bool FileHolder::check_signature(const char *signature, size_t length)
|
||||
return true;
|
||||
}
|
||||
|
||||
uint32_t FileHolder::fgetc32le()
|
||||
{
|
||||
uint32_t FileHolder::fgetc32le() {
|
||||
uint32_t result = (uint32_t)fgetc(file_);
|
||||
result |= (uint32_t)(fgetc(file_) << 8);
|
||||
result |= (uint32_t)(fgetc(file_) << 16);
|
||||
@@ -51,8 +46,7 @@ uint32_t FileHolder::fgetc32le()
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t FileHolder::fgetc24le()
|
||||
{
|
||||
uint32_t FileHolder::fgetc24le() {
|
||||
uint32_t result = (uint32_t)fgetc(file_);
|
||||
result |= (uint32_t)(fgetc(file_) << 8);
|
||||
result |= (uint32_t)(fgetc(file_) << 16);
|
||||
@@ -60,16 +54,14 @@ uint32_t FileHolder::fgetc24le()
|
||||
return result;
|
||||
}
|
||||
|
||||
uint16_t FileHolder::fgetc16le()
|
||||
{
|
||||
uint16_t FileHolder::fgetc16le() {
|
||||
uint16_t result = (uint16_t)fgetc(file_);
|
||||
result |= (uint16_t)(fgetc(file_) << 8);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t FileHolder::fgetc32be()
|
||||
{
|
||||
uint32_t FileHolder::fgetc32be() {
|
||||
uint32_t result = (uint32_t)(fgetc(file_) << 24);
|
||||
result |= (uint32_t)(fgetc(file_) << 16);
|
||||
result |= (uint32_t)(fgetc(file_) << 8);
|
||||
@@ -78,16 +70,14 @@ uint32_t FileHolder::fgetc32be()
|
||||
return result;
|
||||
}
|
||||
|
||||
uint16_t FileHolder::fgetc16be()
|
||||
{
|
||||
uint16_t FileHolder::fgetc16be() {
|
||||
uint16_t result = (uint16_t)(fgetc(file_) << 8);
|
||||
result |= (uint16_t)fgetc(file_);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void FileHolder::ensure_file_is_at_least_length(long length)
|
||||
{
|
||||
void FileHolder::ensure_file_is_at_least_length(long length) {
|
||||
fseek(file_, 0, SEEK_END);
|
||||
long bytes_to_write = length - ftell(file_);
|
||||
if(bytes_to_write > 0)
|
||||
@@ -98,3 +88,13 @@ void FileHolder::ensure_file_is_at_least_length(long length)
|
||||
delete[] empty;
|
||||
}
|
||||
}
|
||||
|
||||
std::string FileHolder::extension() {
|
||||
size_t pointer = name_.size() - 1;
|
||||
while(pointer > 0 && name_[pointer] != '.') pointer--;
|
||||
if(name_[pointer] == '.') pointer++;
|
||||
|
||||
std::string extension = name_.substr(pointer);
|
||||
std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
|
||||
return extension;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user