Compare commits

..

No commits in common. "3a49a3d343a7733f6692ecf96be37e94b89a524a" and "5cd56af350f58339e6fe83a7d034353dfc034922" have entirely different histories.

4 changed files with 19 additions and 22 deletions

View File

@ -22,11 +22,9 @@ OpdsParser::~OpdsParser() {
}
}
size_t OpdsParser::write(uint8_t c) { return write(&c, 1); }
size_t OpdsParser::write(const uint8_t* xmlData, const size_t length) {
void OpdsParser::push(const char* xmlData, const size_t length) {
if (errorOccured) {
return length;
return;
}
XML_SetUserData(parser, this);
@ -34,7 +32,7 @@ size_t OpdsParser::write(const uint8_t* xmlData, const size_t length) {
XML_SetCharacterDataHandler(parser, characterData);
// Parse in chunks to avoid large buffer allocations
const char* currentPos = reinterpret_cast<const char*>(xmlData);
const char* currentPos = xmlData;
size_t remaining = length;
constexpr size_t chunkSize = 1024;
@ -45,7 +43,7 @@ size_t OpdsParser::write(const uint8_t* xmlData, const size_t length) {
Serial.printf("[%lu] [OPDS] Couldn't allocate memory for buffer\n", millis());
XML_ParserFree(parser);
parser = nullptr;
return length;
return;
}
const size_t toRead = remaining < chunkSize ? remaining : chunkSize;
@ -57,16 +55,15 @@ size_t OpdsParser::write(const uint8_t* xmlData, const size_t length) {
XML_ErrorString(XML_GetErrorCode(parser)));
XML_ParserFree(parser);
parser = nullptr;
return length;
return;
}
currentPos += toRead;
remaining -= toRead;
}
return length;
}
void OpdsParser::flush() {
void OpdsParser::finish() {
if (XML_Parse(parser, nullptr, 0, XML_TRUE) != XML_STATUS_OK) {
errorOccured = true;
XML_ParserFree(parser);

View File

@ -1,5 +1,4 @@
#pragma once
#include <Print.h>
#include <expat.h>
#include <string>
@ -43,7 +42,7 @@ using OpdsBook = OpdsEntry;
* }
* }
*/
class OpdsParser final : public Print {
class OpdsParser {
public:
OpdsParser();
~OpdsParser();
@ -52,15 +51,10 @@ class OpdsParser final : public Print {
OpdsParser(const OpdsParser&) = delete;
OpdsParser& operator=(const OpdsParser&) = delete;
size_t write(uint8_t) override;
size_t write(const uint8_t*, size_t) override;
void flush() override;
void push(const char* xmlData, size_t length);
void finish();
bool error() const;
operator bool() { return !error(); }
/**
* Get the parsed entries (both navigation and book entries).
* @return Vector of OpdsEntry entries

View File

@ -8,8 +8,14 @@ int OpdsParserStream::peek() { abort(); }
int OpdsParserStream::read() { abort(); }
size_t OpdsParserStream::write(uint8_t c) { return parser.write(c); }
size_t OpdsParserStream::write(uint8_t c) {
parser.push(reinterpret_cast<const char*>(&c), 1);
return 1;
}
size_t OpdsParserStream::write(const uint8_t* buffer, size_t size) { return parser.write(buffer, size); }
size_t OpdsParserStream::write(const uint8_t* buffer, size_t size) {
parser.push(reinterpret_cast<const char*>(buffer), size);
return size;
}
OpdsParserStream::~OpdsParserStream() { parser.flush(); }
OpdsParserStream::~OpdsParserStream() { parser.finish(); }

View File

@ -277,7 +277,7 @@ void OpdsBookBrowserActivity::fetchFeed(const std::string& path) {
}
}
if (!parser) {
if (parser.error()) {
state = BrowserState::ERROR;
errorMessage = "Failed to parse feed";
updateRequired = true;