mirror of
https://github.com/daveallie/crosspoint-reader.git
synced 2026-02-08 08:37:38 +03:00
Compare commits
5 Commits
5cd56af350
...
3a49a3d343
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3a49a3d343 | ||
|
|
05d1a03fe4 | ||
|
|
a4242ab11e | ||
|
|
1bcc9068c1 | ||
|
|
c22a2b56fb |
@ -22,9 +22,11 @@ OpdsParser::~OpdsParser() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpdsParser::push(const char* xmlData, const size_t length) {
|
size_t OpdsParser::write(uint8_t c) { return write(&c, 1); }
|
||||||
|
|
||||||
|
size_t OpdsParser::write(const uint8_t* xmlData, const size_t length) {
|
||||||
if (errorOccured) {
|
if (errorOccured) {
|
||||||
return;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
XML_SetUserData(parser, this);
|
XML_SetUserData(parser, this);
|
||||||
@ -32,7 +34,7 @@ void OpdsParser::push(const char* xmlData, const size_t length) {
|
|||||||
XML_SetCharacterDataHandler(parser, characterData);
|
XML_SetCharacterDataHandler(parser, characterData);
|
||||||
|
|
||||||
// Parse in chunks to avoid large buffer allocations
|
// Parse in chunks to avoid large buffer allocations
|
||||||
const char* currentPos = xmlData;
|
const char* currentPos = reinterpret_cast<const char*>(xmlData);
|
||||||
size_t remaining = length;
|
size_t remaining = length;
|
||||||
constexpr size_t chunkSize = 1024;
|
constexpr size_t chunkSize = 1024;
|
||||||
|
|
||||||
@ -43,7 +45,7 @@ void OpdsParser::push(const char* xmlData, const size_t length) {
|
|||||||
Serial.printf("[%lu] [OPDS] Couldn't allocate memory for buffer\n", millis());
|
Serial.printf("[%lu] [OPDS] Couldn't allocate memory for buffer\n", millis());
|
||||||
XML_ParserFree(parser);
|
XML_ParserFree(parser);
|
||||||
parser = nullptr;
|
parser = nullptr;
|
||||||
return;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
const size_t toRead = remaining < chunkSize ? remaining : chunkSize;
|
const size_t toRead = remaining < chunkSize ? remaining : chunkSize;
|
||||||
@ -55,15 +57,16 @@ void OpdsParser::push(const char* xmlData, const size_t length) {
|
|||||||
XML_ErrorString(XML_GetErrorCode(parser)));
|
XML_ErrorString(XML_GetErrorCode(parser)));
|
||||||
XML_ParserFree(parser);
|
XML_ParserFree(parser);
|
||||||
parser = nullptr;
|
parser = nullptr;
|
||||||
return;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
currentPos += toRead;
|
currentPos += toRead;
|
||||||
remaining -= toRead;
|
remaining -= toRead;
|
||||||
}
|
}
|
||||||
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpdsParser::finish() {
|
void OpdsParser::flush() {
|
||||||
if (XML_Parse(parser, nullptr, 0, XML_TRUE) != XML_STATUS_OK) {
|
if (XML_Parse(parser, nullptr, 0, XML_TRUE) != XML_STATUS_OK) {
|
||||||
errorOccured = true;
|
errorOccured = true;
|
||||||
XML_ParserFree(parser);
|
XML_ParserFree(parser);
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <Print.h>
|
||||||
#include <expat.h>
|
#include <expat.h>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -42,7 +43,7 @@ using OpdsBook = OpdsEntry;
|
|||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
class OpdsParser {
|
class OpdsParser final : public Print {
|
||||||
public:
|
public:
|
||||||
OpdsParser();
|
OpdsParser();
|
||||||
~OpdsParser();
|
~OpdsParser();
|
||||||
@ -51,10 +52,15 @@ class OpdsParser {
|
|||||||
OpdsParser(const OpdsParser&) = delete;
|
OpdsParser(const OpdsParser&) = delete;
|
||||||
OpdsParser& operator=(const OpdsParser&) = delete;
|
OpdsParser& operator=(const OpdsParser&) = delete;
|
||||||
|
|
||||||
void push(const char* xmlData, size_t length);
|
size_t write(uint8_t) override;
|
||||||
void finish();
|
size_t write(const uint8_t*, size_t) override;
|
||||||
|
|
||||||
|
void flush() override;
|
||||||
|
|
||||||
bool error() const;
|
bool error() const;
|
||||||
|
|
||||||
|
operator bool() { return !error(); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the parsed entries (both navigation and book entries).
|
* Get the parsed entries (both navigation and book entries).
|
||||||
* @return Vector of OpdsEntry entries
|
* @return Vector of OpdsEntry entries
|
||||||
|
|||||||
@ -8,14 +8,8 @@ int OpdsParserStream::peek() { abort(); }
|
|||||||
|
|
||||||
int OpdsParserStream::read() { abort(); }
|
int OpdsParserStream::read() { abort(); }
|
||||||
|
|
||||||
size_t OpdsParserStream::write(uint8_t c) {
|
size_t OpdsParserStream::write(uint8_t c) { return parser.write(c); }
|
||||||
parser.push(reinterpret_cast<const char*>(&c), 1);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t OpdsParserStream::write(const uint8_t* buffer, size_t size) {
|
size_t OpdsParserStream::write(const uint8_t* buffer, size_t size) { return parser.write(buffer, size); }
|
||||||
parser.push(reinterpret_cast<const char*>(buffer), size);
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
OpdsParserStream::~OpdsParserStream() { parser.finish(); }
|
OpdsParserStream::~OpdsParserStream() { parser.flush(); }
|
||||||
|
|||||||
@ -277,7 +277,7 @@ void OpdsBookBrowserActivity::fetchFeed(const std::string& path) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parser.error()) {
|
if (!parser) {
|
||||||
state = BrowserState::ERROR;
|
state = BrowserState::ERROR;
|
||||||
errorMessage = "Failed to parse feed";
|
errorMessage = "Failed to parse feed";
|
||||||
updateRequired = true;
|
updateRequired = true;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user