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

View File

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

View File

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

View File

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