Refactor using Fable
This commit is contained in:
90 files changed
+2080
-2267
No files matched your search
@@ -0,0 +1,88 @@
|
||||
#include "Fb2ByteSource.h"
|
||||
|
||||
#include "backend/formats/Fb2Reader.h"
|
||||
|
||||
#include <QCryptographicHash>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QXmlStreamReader>
|
||||
|
||||
bool Fb2ByteSource::open(const QString &path)
|
||||
{
|
||||
m_path.clear();
|
||||
m_hash.clear();
|
||||
|
||||
if (!QFileInfo::exists(path))
|
||||
return false;
|
||||
|
||||
m_path = path;
|
||||
return true;
|
||||
}
|
||||
|
||||
QByteArray Fb2ByteSource::rawBytes() const
|
||||
{
|
||||
if (m_path.isEmpty())
|
||||
return {};
|
||||
QFile f(m_path);
|
||||
if (!f.open(QIODevice::ReadOnly))
|
||||
return {};
|
||||
return f.readAll();
|
||||
}
|
||||
|
||||
QStringList Fb2ByteSource::listEntries() const
|
||||
{
|
||||
const QByteArray raw = rawBytes();
|
||||
if (raw.isEmpty())
|
||||
return {};
|
||||
|
||||
QStringList ids;
|
||||
// Decode first so parsing works for encodings Qt 6 does not natively
|
||||
// detect (e.g. Windows-1251); see Fb2Reader::decodeRawBytes.
|
||||
QXmlStreamReader xml(Fb2Reader::decodeRawBytes(raw));
|
||||
while (!xml.atEnd()) {
|
||||
xml.readNext();
|
||||
if (xml.isStartElement() && xml.name() == QLatin1String("binary"))
|
||||
ids.append(xml.attributes().value(QLatin1String("id")).toString());
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
QByteArray Fb2ByteSource::readEntry(const QString &binaryId) const
|
||||
{
|
||||
if (binaryId.isEmpty())
|
||||
return {};
|
||||
const QByteArray raw = rawBytes();
|
||||
if (raw.isEmpty())
|
||||
return {};
|
||||
|
||||
QXmlStreamReader xml(Fb2Reader::decodeRawBytes(raw));
|
||||
while (!xml.atEnd()) {
|
||||
xml.readNext();
|
||||
if (!xml.isStartElement() || xml.name() != QLatin1String("binary"))
|
||||
continue;
|
||||
if (xml.attributes().value(QLatin1String("id")) != binaryId)
|
||||
continue;
|
||||
|
||||
// Accumulate text (base64 data may span multiple character events)
|
||||
QString base64;
|
||||
while (!xml.atEnd()) {
|
||||
xml.readNext();
|
||||
if (xml.isCharacters())
|
||||
base64 += xml.text();
|
||||
else
|
||||
break;
|
||||
}
|
||||
return QByteArray::fromBase64(base64.toLatin1());
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
QByteArray Fb2ByteSource::contentHash() const
|
||||
{
|
||||
if (m_hash.isEmpty() && !m_path.isEmpty()) {
|
||||
const QByteArray raw = rawBytes();
|
||||
if (!raw.isEmpty())
|
||||
m_hash = QCryptographicHash::hash(raw, QCryptographicHash::Md5);
|
||||
}
|
||||
return m_hash;
|
||||
}
|
||||
Reference in new issue
Block a user