added txt support
This commit is contained in:
1 parent
3c0037cfc9
commit
64a70bc4cf
22 files changed
+800
-186
No files matched your search
@@ -0,0 +1,119 @@
|
||||
#include "TxtReader.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QRegularExpression>
|
||||
#include <QTextStream>
|
||||
|
||||
#include <uchardet/uchardet.h>
|
||||
#include <iconv.h>
|
||||
|
||||
static QString decodeWithIconv(const QByteArray &raw, const char *charset)
|
||||
{
|
||||
iconv_t cd = iconv_open("UTF-8", charset);
|
||||
if (cd == reinterpret_cast<iconv_t>(-1))
|
||||
return {};
|
||||
|
||||
// Worst case: each byte becomes 4 UTF-8 bytes
|
||||
QByteArray out(raw.size() * 4, '\0');
|
||||
char *inBuf = const_cast<char *>(raw.constData());
|
||||
size_t inLeft = raw.size();
|
||||
char *outBuf = out.data();
|
||||
size_t outLeft = out.size();
|
||||
|
||||
size_t result = iconv(cd, &inBuf, &inLeft, &outBuf, &outLeft);
|
||||
iconv_close(cd);
|
||||
|
||||
if (result == static_cast<size_t>(-1))
|
||||
return {};
|
||||
|
||||
out.resize(out.size() - outLeft);
|
||||
return QString::fromUtf8(out);
|
||||
}
|
||||
|
||||
bool TxtReader::open(const QString &filePath)
|
||||
{
|
||||
QFile file(filePath);
|
||||
if (!file.exists()) {
|
||||
m_error = QStringLiteral("File not found: ") + filePath;
|
||||
return false;
|
||||
}
|
||||
if (!file.open(QIODevice::ReadOnly)) {
|
||||
m_error = QStringLiteral("Cannot open file: ") + file.errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
QByteArray raw = file.readAll();
|
||||
file.close();
|
||||
|
||||
uchardet_t ud = uchardet_new();
|
||||
if (uchardet_handle_data(ud, raw.constData(), raw.size()) == 0) {
|
||||
uchardet_data_end(ud);
|
||||
const char *charset = uchardet_get_charset(ud);
|
||||
if (charset && charset[0] != '\0') {
|
||||
auto qtEncoding = QStringConverter::encodingForName(charset);
|
||||
if (qtEncoding) {
|
||||
QTextStream stream(raw);
|
||||
stream.setEncoding(qtEncoding.value());
|
||||
m_content = stream.readAll();
|
||||
} else {
|
||||
m_content = decodeWithIconv(raw, charset);
|
||||
}
|
||||
uchardet_delete(ud);
|
||||
if (!m_content.isEmpty()) {
|
||||
m_title = QFileInfo(filePath).completeBaseName();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
uchardet_delete(ud);
|
||||
|
||||
// Default: UTF-8
|
||||
m_content = QString::fromUtf8(raw);
|
||||
m_title = QFileInfo(filePath).completeBaseName();
|
||||
return true;
|
||||
}
|
||||
|
||||
QString TxtReader::lastError()
|
||||
{
|
||||
return m_error;
|
||||
}
|
||||
|
||||
int TxtReader::chapterCount()
|
||||
{
|
||||
return m_content.isEmpty() ? 0 : 1;
|
||||
}
|
||||
|
||||
Chapter TxtReader::loadChapter(int index)
|
||||
{
|
||||
if (index != 0 || m_content.isEmpty())
|
||||
return Chapter{index, {}, {}};
|
||||
|
||||
// Split on blank lines to create paragraphs; single newlines become <br>
|
||||
QString html;
|
||||
const QStringList blocks = m_content.split(QRegularExpression("\\n\\n+"));
|
||||
for (const QString &block : blocks) {
|
||||
QString escaped = block.toHtmlEscaped().trimmed();
|
||||
if (escaped.isEmpty())
|
||||
continue;
|
||||
escaped.replace('\n', QStringLiteral("<br>\n"));
|
||||
html += QStringLiteral("<p>") + escaped + QStringLiteral("</p>\n");
|
||||
}
|
||||
|
||||
return Chapter{0, m_title, html};
|
||||
}
|
||||
|
||||
QList<TocEntry> TxtReader::tableOfContents()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
QString TxtReader::title()
|
||||
{
|
||||
return m_title;
|
||||
}
|
||||
|
||||
QString TxtReader::author()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef TXTREADER_H
|
||||
#define TXTREADER_H
|
||||
|
||||
#include "backend/FormatReader.h"
|
||||
|
||||
class TxtReader : public FormatReader {
|
||||
public:
|
||||
bool open(const QString &filePath) override;
|
||||
QString lastError() override;
|
||||
int chapterCount() override;
|
||||
Chapter loadChapter(int index) override;
|
||||
QList<TocEntry> tableOfContents() override;
|
||||
QString title() override;
|
||||
QString author() override;
|
||||
|
||||
private:
|
||||
QString m_content;
|
||||
QString m_title;
|
||||
QString m_error;
|
||||
};
|
||||
|
||||
#endif // TXTREADER_H
|
||||
Reference in new issue
Block a user