mirror of
https://github.com/gbdev/rgbds.git
synced 2026-08-19 21:34:31 +00:00
Use std::optional<uint64_t> instead of long with a special -1 sentinel for seekSize
This commit is contained in:
+1
-1
@@ -27,7 +27,7 @@ int xfclose(FILE *file);
|
||||
int xclose(int fd);
|
||||
|
||||
// Measure file size with `fseek` and `ftell` idiom
|
||||
long seekSize(FILE *file);
|
||||
std::optional<uint64_t> seekSize(FILE *file);
|
||||
|
||||
// Locale-independent character class functions
|
||||
bool isNewline(int c);
|
||||
|
||||
+3
-3
@@ -151,11 +151,11 @@ std::optional<std::string> act_ReadFile(std::string const &name, uint32_t maxLen
|
||||
Defer closeFile{[&] { xfclose(file); }};
|
||||
|
||||
size_t readSize = maxLen;
|
||||
if (long fileSize = seekSize(file); fileSize != -1) {
|
||||
if (std::optional<uint64_t> fileSize = seekSize(file); fileSize.has_value()) {
|
||||
// If the file is seekable and shorter than the max length,
|
||||
// just read as many bytes as there are
|
||||
if (static_cast<size_t>(fileSize) < readSize) {
|
||||
readSize = fileSize;
|
||||
if (*fileSize < readSize) {
|
||||
readSize = *fileSize;
|
||||
}
|
||||
// LCOV_EXCL_START
|
||||
} else if (errno != ESPIPE) {
|
||||
|
||||
+13
-11
@@ -977,13 +977,14 @@ bool sect_BinaryFile(std::string const &name, uint32_t startPos) {
|
||||
}
|
||||
Defer closeFile{[&] { xfclose(file); }};
|
||||
|
||||
if (long fileSize = seekSize(file); fileSize != -1) {
|
||||
if (startPos > static_cast<size_t>(fileSize)) {
|
||||
if (std::optional<uint64_t> fileSize = seekSize(file); fileSize.has_value()) {
|
||||
if (startPos > *fileSize) {
|
||||
error(
|
||||
"Specified start position (%" PRIu32 ") is greater than length of \"%s\" (%ld)",
|
||||
"Specified start position (%" PRIu32 ") is greater than length of \"%s\" (%" PRIu64
|
||||
")",
|
||||
startPos,
|
||||
name.c_str(),
|
||||
fileSize
|
||||
*fileSize
|
||||
);
|
||||
return false;
|
||||
}
|
||||
@@ -1037,23 +1038,24 @@ bool sect_BinaryFileSlice(std::string const &name, uint32_t startPos, uint32_t l
|
||||
}
|
||||
Defer closeFile{[&] { xfclose(file); }};
|
||||
|
||||
if (long fileSize = seekSize(file); fileSize != -1) {
|
||||
if (startPos > static_cast<size_t>(fileSize)) {
|
||||
if (std::optional<uint64_t> fileSize = seekSize(file); fileSize.has_value()) {
|
||||
if (startPos > *fileSize) {
|
||||
error(
|
||||
"Specified start position (%" PRIu32 ") is greater than length of \"%s\" (%ld)",
|
||||
"Specified start position (%" PRIu32 ") is greater than length of \"%s\" (%" PRIu64
|
||||
")",
|
||||
startPos,
|
||||
name.c_str(),
|
||||
fileSize
|
||||
*fileSize
|
||||
);
|
||||
return false;
|
||||
} else if (startPos + length > static_cast<size_t>(fileSize)) {
|
||||
} else if (startPos + length > *fileSize) {
|
||||
error(
|
||||
"Specified range in `INCBIN` file \"%s\" is out of bounds (%" PRIu32 " + %" PRIu32
|
||||
" > %ld)",
|
||||
" > %" PRIu64 ")",
|
||||
name.c_str(),
|
||||
startPos,
|
||||
length,
|
||||
fileSize
|
||||
*fileSize
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
+7
-6
@@ -6,6 +6,7 @@
|
||||
#include <deque>
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <optional>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -112,23 +113,23 @@ static uint32_t checkOverlaySize() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
long overlaySize = seekSize(overlayFile);
|
||||
std::optional<uint64_t> overlaySize = seekSize(overlayFile);
|
||||
|
||||
if (overlaySize == -1) {
|
||||
if (!overlaySize.has_value()) {
|
||||
warnx("Overlay file is not seekable, cannot check if properly formed");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (overlaySize % BANK_SIZE) {
|
||||
if (*overlaySize % BANK_SIZE) {
|
||||
warnx("Overlay file does not have a size multiple of 0x4000");
|
||||
} else if (options.is32kMode && overlaySize != 0x8000) {
|
||||
} else if (options.is32kMode && *overlaySize != 0x8000) {
|
||||
warnx("Overlay is not exactly 0x8000 bytes large");
|
||||
}
|
||||
if (overlaySize < 0x8000) {
|
||||
if (*overlaySize < 0x8000) {
|
||||
warnx("Overlay is less than 0x8000 bytes large");
|
||||
}
|
||||
|
||||
return (overlaySize + BANK_SIZE - 1) / BANK_SIZE;
|
||||
return (*overlaySize + BANK_SIZE - 1) / BANK_SIZE;
|
||||
}
|
||||
|
||||
// Expand `sections[SECTTYPE_ROMX]` to cover all the overlay banks.
|
||||
|
||||
+5
-5
@@ -25,18 +25,18 @@ int xclose(int fd) {
|
||||
return close(fd);
|
||||
}
|
||||
|
||||
long seekSize(FILE *file) {
|
||||
std::optional<uint64_t> seekSize(FILE *file) {
|
||||
if (fseek(file, 0, SEEK_END) != 0) {
|
||||
return -1;
|
||||
return std::nullopt;
|
||||
}
|
||||
long size = ftell(file);
|
||||
if (size < 0) {
|
||||
return -1;
|
||||
return std::nullopt;
|
||||
}
|
||||
if (fseek(file, 0, SEEK_SET) != 0) {
|
||||
return -1;
|
||||
return std::nullopt;
|
||||
}
|
||||
return size;
|
||||
return static_cast<uint64_t>(size);
|
||||
}
|
||||
|
||||
bool isNewline(int c) {
|
||||
|
||||
Reference in New Issue
Block a user