WIP Use temp item file

This commit is contained in:
Dave Allie 2025-12-22 23:44:39 +11:00
parent 63f0acd852
commit 5bf603a38e
No known key found for this signature in database
GPG Key ID: F2FDDB3AD8D0276F
3 changed files with 23 additions and 13 deletions

View File

@ -5,8 +5,6 @@
#include <SD.h>
#include <ZipFile.h>
#include <map>
#include "Epub/FsHelpers.h"
#include "Epub/parsers/ContainerParser.h"
#include "Epub/parsers/ContentOpfParser.h"
@ -75,9 +73,9 @@ bool Epub::parseContentOpf(bool useCache) {
// Grab data from opfParser into epub
title = opfParser.title;
if (!opfParser.coverItemId.empty() && opfParser.items.count(opfParser.coverItemId) > 0) {
coverImageItem = opfParser.items.at(opfParser.coverItemId);
}
// if (!opfParser.coverItemId.empty() && opfParser.items.count(opfParser.coverItemId) > 0) {
// coverImageItem = opfParser.items.at(opfParser.coverItemId);
// }
if (!opfParser.tocNcxPath.empty()) {
tocNcxItem = opfParser.tocNcxPath;

View File

@ -1,6 +1,7 @@
#include "ContentOpfParser.h"
#include <HardwareSerial.h>
#include <Serialization.h>
#include <ZipFile.h>
namespace {
@ -94,11 +95,13 @@ void XMLCALL ContentOpfParser::startElement(void* userData, const XML_Char* name
if (self->state == IN_PACKAGE && (strcmp(name, "manifest") == 0 || strcmp(name, "opf:manifest") == 0)) {
self->state = IN_MANIFEST;
self->tempItemStore = SD.open("/.crosspoint/.tmp-items.bin", FILE_WRITE, true);
return;
}
if (self->state == IN_PACKAGE && (strcmp(name, "spine") == 0 || strcmp(name, "opf:spine") == 0)) {
self->state = IN_SPINE;
self->tempItemStore = SD.open("/.crosspoint/.tmp-items.bin", FILE_READ);
return;
}
@ -135,7 +138,10 @@ void XMLCALL ContentOpfParser::startElement(void* userData, const XML_Char* name
}
}
self->items[itemId] = href;
serialization::writeString(self->tempItemStore, itemId);
serialization::writeString(self->tempItemStore, href);
// // Write items down to SD card
// self->items[itemId] = href;
if (mediaType == MEDIA_TYPE_NCX) {
if (self->tocNcxPath.empty()) {
@ -156,13 +162,19 @@ void XMLCALL ContentOpfParser::startElement(void* userData, const XML_Char* name
if (strcmp(atts[i], "idref") == 0) {
const std::string idref = atts[i + 1];
// Resolve the idref to href using items map
if (self->items.count(idref) > 0) {
const std::string& href = self->items.at(idref);
self->tempItemStore.seek(0);
std::string itemId;
std::string href;
while (self->tempItemStore.available()) {
serialization::readString(self->tempItemStore, itemId);
serialization::readString(self->tempItemStore, href);
if (itemId == idref) {
self->cache->addSpineEntry(href);
}
break;
}
}
}
}
return;
}
}
@ -183,11 +195,13 @@ void XMLCALL ContentOpfParser::endElement(void* userData, const XML_Char* name)
if (self->state == IN_SPINE && (strcmp(name, "spine") == 0 || strcmp(name, "opf:spine") == 0)) {
self->state = IN_PACKAGE;
self->tempItemStore.close();
return;
}
if (self->state == IN_MANIFEST && (strcmp(name, "manifest") == 0 || strcmp(name, "opf:manifest") == 0)) {
self->state = IN_PACKAGE;
self->tempItemStore.close();
return;
}

View File

@ -1,8 +1,6 @@
#pragma once
#include <Print.h>
#include <map>
#include "Epub.h"
#include "Epub/SpineTocCache.h"
#include "expat.h"
@ -22,6 +20,7 @@ class ContentOpfParser final : public Print {
XML_Parser parser = nullptr;
ParserState state = START;
SpineTocCache* cache;
File tempItemStore;
static void startElement(void* userData, const XML_Char* name, const XML_Char** atts);
static void characterData(void* userData, const XML_Char* s, int len);
@ -31,7 +30,6 @@ class ContentOpfParser final : public Print {
std::string title;
std::string tocNcxPath;
std::string coverItemId;
std::map<std::string, std::string> items;
explicit ContentOpfParser(const std::string& baseContentPath, const size_t xmlSize, SpineTocCache* cache)
: baseContentPath(baseContentPath), remainingSize(xmlSize), cache(cache) {}