Version section bin files

This commit is contained in:
Dave Allie 2025-12-06 22:04:59 +11:00
parent dd6e649d74
commit bb151caee7
No known key found for this signature in database
GPG Key ID: F2FDDB3AD8D0276F
4 changed files with 31 additions and 20 deletions

View File

@ -7,6 +7,9 @@
#include "EpubHtmlParserSlim.h"
#include "Page.h"
#include "Serialization.h"
constexpr uint8_t SECTION_FILE_VERSION = 2;
void Section::onPageComplete(const Page* page) {
Serial.printf("Page %d complete - free mem: %lu\n", pageCount, ESP.getFreeHeap());
@ -21,7 +24,14 @@ void Section::onPageComplete(const Page* page) {
delete page;
}
bool Section::hasCache() {
void Section::writeCacheMetadata() const {
std::ofstream outputFile(("/sd" + cachePath + "/section.bin").c_str());
serialization::writePod(outputFile, SECTION_FILE_VERSION);
serialization::writePod(outputFile, pageCount);
outputFile.close();
}
bool Section::loadCacheMetadata() {
if (!SD.exists(cachePath.c_str())) {
return false;
}
@ -31,14 +41,18 @@ bool Section::hasCache() {
return false;
}
File sectionFile = SD.open(sectionFilePath.c_str(), FILE_READ);
uint8_t pageCountBytes[2] = {0, 0};
sectionFile.read(pageCountBytes, 2);
sectionFile.close();
pageCount = pageCountBytes[0] + (pageCountBytes[1] << 8);
std::ifstream inputFile(("/sd" + sectionFilePath).c_str());
uint8_t version;
serialization::readPod(inputFile, version);
if (version != SECTION_FILE_VERSION) {
inputFile.close();
SD.remove(sectionFilePath.c_str());
Serial.printf("Section state file: Unknown version %u\n", version);
return false;
}
serialization::readPod(inputFile, pageCount);
inputFile.close();
Serial.printf("Loaded cache: %d pages\n", pageCount);
return true;
}
@ -86,16 +100,12 @@ bool Section::persistPageDataToSD() {
return false;
}
File sectionFile = SD.open((cachePath + "/section.bin").c_str(), FILE_WRITE, true);
const uint8_t pageCountBytes[2] = {static_cast<uint8_t>(pageCount & 0xFF),
static_cast<uint8_t>((pageCount >> 8) & 0xFF)};
sectionFile.write(pageCountBytes, 2);
sectionFile.close();
writeCacheMetadata();
return true;
}
void Section::renderPage() {
void Section::renderPage() const {
if (0 <= currentPage && currentPage < pageCount) {
const auto filePath = "/sd" + cachePath + "/page_" + std::to_string(currentPage) + ".bin";
std::ifstream inputFile(filePath);

View File

@ -21,9 +21,10 @@ class Section {
cachePath = epub->getCachePath() + "/" + std::to_string(spineIndex);
}
~Section() = default;
bool hasCache();
void writeCacheMetadata() const;
bool loadCacheMetadata();
void setupCacheDir() const;
void clearCache() const;
bool persistPageDataToSD();
void renderPage();
void renderPage() const;
};

View File

@ -6,12 +6,12 @@
#include <fstream>
constexpr uint8_t STATE_VERSION = 1;
constexpr uint8_t STATE_FILE_VERSION = 1;
constexpr char STATE_FILE[] = "/sd/.crosspoint/state.bin";
bool CrossPointState::saveToFile() const {
std::ofstream outputFile(STATE_FILE);
serialization::writePod(outputFile, STATE_VERSION);
serialization::writePod(outputFile, STATE_FILE_VERSION);
serialization::writeString(outputFile, openEpubPath);
outputFile.close();
return true;
@ -22,7 +22,7 @@ bool CrossPointState::loadFromFile() {
uint8_t version;
serialization::readPod(inputFile, version);
if (version != STATE_VERSION) {
if (version != STATE_FILE_VERSION) {
Serial.printf("CrossPointState: Unknown version %u\n", version);
inputFile.close();
return false;

View File

@ -148,7 +148,7 @@ void EpubReaderScreen::renderPage() {
const auto filepath = epub->getSpineItem(currentSpineIndex);
Serial.printf("Loading file: %s, index: %d\n", filepath.c_str(), currentSpineIndex);
section = new Section(epub, currentSpineIndex, renderer);
if (!section->hasCache()) {
if (!section->loadCacheMetadata()) {
Serial.println("Cache not found, building...");
{