summaryrefslogtreecommitdiffstats
path: root/reader/src/formats/tcr
diff options
context:
space:
mode:
authorMichele Calgaro <michele.calgaro@yahoo.it>2024-06-07 23:30:05 +0900
committerMichele Calgaro <michele.calgaro@yahoo.it>2024-06-07 23:30:05 +0900
commit17b259df9cb6b28779d4881b2b6c805ee2e48eea (patch)
tree5ed61937459cb7081089111b0242c01ec178f1f3 /reader/src/formats/tcr
parent1cba8bce178eb2d6719c6f7f21e2c9352c5513a6 (diff)
downloadtde-ebook-reader-17b259df9cb6b28779d4881b2b6c805ee2e48eea.tar.gz
tde-ebook-reader-17b259df9cb6b28779d4881b2b6c805ee2e48eea.zip
Rename to tde-ebook-reader
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
Diffstat (limited to 'reader/src/formats/tcr')
-rw-r--r--reader/src/formats/tcr/PPLBookReader.cpp129
-rw-r--r--reader/src/formats/tcr/PPLBookReader.h51
-rw-r--r--reader/src/formats/tcr/TcrPlugin.cpp82
-rw-r--r--reader/src/formats/tcr/TcrPlugin.h43
-rw-r--r--reader/src/formats/tcr/TcrStream.cpp125
-rw-r--r--reader/src/formats/tcr/TcrStream.h47
6 files changed, 477 insertions, 0 deletions
diff --git a/reader/src/formats/tcr/PPLBookReader.cpp b/reader/src/formats/tcr/PPLBookReader.cpp
new file mode 100644
index 0000000..9b7d271
--- /dev/null
+++ b/reader/src/formats/tcr/PPLBookReader.cpp
@@ -0,0 +1,129 @@
+/*
+ * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include <cstring>
+#include <cctype>
+
+#include "PPLBookReader.h"
+#include <ZLInputStream.h>
+
+static const std::size_t BUFFER_SIZE = 2048;
+
+PPLBookReader::PPLBookReader(BookModel &model, const std::string &encoding) : EncodedTextReader(encoding), myModelReader(model) {
+ myBuffer = new char[BUFFER_SIZE + 1];
+}
+
+PPLBookReader::~PPLBookReader() {
+ delete[] myBuffer;
+}
+
+bool PPLBookReader::currentParagraphIsEmpty() const {
+ const char *ptr = myCurrentParagraph.data();
+ const char *end = ptr + myCurrentParagraph.length();
+ for (; ptr < end; ++ptr) {
+ if (!std::isspace((unsigned char)*ptr)) {
+ return false;
+ }
+ }
+ return true;
+}
+
+void PPLBookReader::addParagraph() {
+ static const std::string END_OF_TEXT = "<* >";
+ if (!myCurrentParagraph.empty()) {
+ if (currentParagraphIsEmpty()) {
+ ++myEmptyLineCounter;
+ if (myEmptyLineCounter >= 2) {
+ myModelReader.beginParagraph(ZLTextParagraph::EMPTY_LINE_PARAGRAPH);
+ myModelReader.endParagraph();
+ }
+ } else if (myEmptyLineCounter < 2) {
+ myModelReader.beginParagraph();
+ myModelReader.addControl(TITLE, true);
+ myModelReader.addData(myCurrentParagraph);
+ myModelReader.endParagraph();
+ } else if (myCurrentParagraph[0] == 9) {
+ myModelReader.beginParagraph();
+ myModelReader.addData(myCurrentParagraph);
+ myModelReader.endParagraph();
+ } else if ((myCurrentParagraph.length() >= 2) &&
+ (myCurrentParagraph[0] == '*') &&
+ (myCurrentParagraph[1] == ' ')) {
+ myCurrentParagraph.erase(0, 2);
+ myModelReader.insertEndOfSectionParagraph();
+ myModelReader.beginContentsParagraph();
+ myModelReader.addContentsData(myCurrentParagraph);
+ myModelReader.endContentsParagraph();
+ myModelReader.beginParagraph();
+ myModelReader.addControl(SECTION_TITLE, true);
+ myModelReader.addData(myCurrentParagraph);
+ myModelReader.endParagraph();
+ } else if (myCurrentParagraph.substr(0, 4) != END_OF_TEXT) {
+ myModelReader.beginParagraph();
+ myModelReader.addControl(SUBTITLE, true);
+ myModelReader.addData(myCurrentParagraph);
+ myModelReader.endParagraph();
+ }
+ myCurrentParagraph.erase();
+ }
+}
+
+bool PPLBookReader::readDocument(ZLInputStream &stream) {
+ if (!stream.open()) {
+ return false;
+ }
+
+ myModelReader.setMainTextModel();
+ myModelReader.pushKind(REGULAR);
+ myCurrentParagraph.erase();
+ myEmptyLineCounter = 0;
+
+ // "PPL\r\n"
+ stream.seek(5, false);
+
+ std::size_t size;
+ do {
+ size = stream.read(myBuffer, BUFFER_SIZE);
+ myBuffer[size] = '\0';
+
+ const char *start = myBuffer;
+ const char *end = myBuffer + size;
+ const char *eol;
+ do {
+ eol = std::strchr(start, '\n');
+ if (eol != 0) {
+ if (start < eol) {
+ myConverter->convert(myCurrentParagraph, start, eol);
+ }
+ addParagraph();
+ start = eol + 1;
+ } else {
+ if (start < end) {
+ myConverter->convert(myCurrentParagraph, start, end);
+ }
+ }
+ } while (eol != 0);
+ } while (size == BUFFER_SIZE);
+
+ addParagraph();
+
+ stream.close();
+
+ return true;
+}
diff --git a/reader/src/formats/tcr/PPLBookReader.h b/reader/src/formats/tcr/PPLBookReader.h
new file mode 100644
index 0000000..98c7f9d
--- /dev/null
+++ b/reader/src/formats/tcr/PPLBookReader.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef __PPLBOOKREADER_H__
+#define __PPLBOOKREADER_H__
+
+#include <shared_ptr.h>
+#include <ZLEncodingConverter.h>
+#include "../../bookmodel/BookReader.h"
+#include "../EncodedTextReader.h"
+
+class ZLInputStream;
+class BookModel;
+
+class PPLBookReader : public EncodedTextReader {
+
+public:
+ PPLBookReader(BookModel &model, const std::string &encoding);
+ ~PPLBookReader();
+
+ bool readDocument(ZLInputStream &stream);
+
+private:
+ bool currentParagraphIsEmpty() const;
+ void addParagraph();
+
+private:
+ BookReader myModelReader;
+
+ char *myBuffer;
+ std::string myCurrentParagraph;
+ int myEmptyLineCounter;
+};
+
+#endif /* __PPLBOOKREADER_H__ */
diff --git a/reader/src/formats/tcr/TcrPlugin.cpp b/reader/src/formats/tcr/TcrPlugin.cpp
new file mode 100644
index 0000000..8ee0f14
--- /dev/null
+++ b/reader/src/formats/tcr/TcrPlugin.cpp
@@ -0,0 +1,82 @@
+/*
+ * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include <ZLFile.h>
+#include <ZLInputStream.h>
+
+#include "TcrPlugin.h"
+#include "TcrStream.h"
+#include "PPLBookReader.h"
+#include "../util/TextFormatDetector.h"
+#include "../txt/TxtBookReader.h"
+#include "../html/HtmlBookReader.h"
+#include "../txt/PlainTextFormat.h"
+
+#include "../../bookmodel/BookModel.h"
+#include "../../library/Book.h"
+
+bool TcrPlugin::acceptsFile(const ZLFile &file) const {
+ return file.extension() == "tcr";
+}
+
+bool TcrPlugin::readMetaInfo(Book &book) const {
+ shared_ptr<ZLInputStream> stream = new TcrStream(book.file());
+ detectEncodingAndLanguage(book, *stream);
+ if (book.encoding().empty()) {
+ return false;
+ }
+
+ return true;
+}
+
+bool TcrPlugin::readLanguageAndEncoding(Book &book) const {
+ (void)book;
+ return true;
+}
+
+bool TcrPlugin::readModel(BookModel &model) const {
+ const Book &book = *model.book();
+ const ZLFile &file = book.file();
+
+ shared_ptr<ZLInputStream> stream = new TcrStream(file);
+
+ PlainTextFormat format(file);
+ if (!format.initialized()) {
+ PlainTextFormatDetector detector;
+ detector.detect(*stream, format);
+ }
+
+ const std::string &encoding = book.encoding();
+ if (TextFormatDetector().isPPL(*stream)) {
+ PPLBookReader(model, encoding).readDocument(*stream);
+ } else if (TextFormatDetector().isHtml(*stream)) {
+ HtmlBookReader("", model, format, encoding).readDocument(*stream);
+ } else {
+ TxtBookReader(model, format, encoding).readDocument(*stream);
+ }
+ return true;
+}
+
+FormatInfoPage *TcrPlugin::createInfoPage(ZLOptionsDialog &dialog, const ZLFile &file) {
+ shared_ptr<ZLInputStream> stream = new TcrStream(file);
+ if (TextFormatDetector().isPPL(*stream)) {
+ return 0;
+ }
+ return new PlainTextInfoPage(dialog, file, ZLResourceKey("Text"), !TextFormatDetector().isHtml(*stream));
+}
diff --git a/reader/src/formats/tcr/TcrPlugin.h b/reader/src/formats/tcr/TcrPlugin.h
new file mode 100644
index 0000000..9655892
--- /dev/null
+++ b/reader/src/formats/tcr/TcrPlugin.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef __TCRPLUGIN_H__
+#define __TCRPLUGIN_H__
+
+#include "../FormatPlugin.h"
+
+class TcrPlugin : public FormatPlugin {
+
+public:
+ TcrPlugin();
+ ~TcrPlugin();
+
+ bool providesMetaInfo() const;
+ bool acceptsFile(const ZLFile &file) const;
+ bool readMetaInfo(Book &book) const;
+ bool readLanguageAndEncoding(Book &book) const;
+ bool readModel(BookModel &model) const;
+ FormatInfoPage *createInfoPage(ZLOptionsDialog &dialog, const ZLFile &file);
+};
+
+inline TcrPlugin::TcrPlugin() {}
+inline TcrPlugin::~TcrPlugin() {}
+inline bool TcrPlugin::providesMetaInfo() const { return false; }
+
+#endif /* __TCRPLUGIN_H__ */
diff --git a/reader/src/formats/tcr/TcrStream.cpp b/reader/src/formats/tcr/TcrStream.cpp
new file mode 100644
index 0000000..cf4e540
--- /dev/null
+++ b/reader/src/formats/tcr/TcrStream.cpp
@@ -0,0 +1,125 @@
+/*
+ * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include <cstring>
+#include <algorithm>
+
+#include <ZLFile.h>
+#include <ZLZDecompressor.h>
+
+#include "TcrStream.h"
+
+TcrStream::TcrStream(const ZLFile &file) : myBase(file.inputStream()) {
+}
+
+TcrStream::~TcrStream() {
+ close();
+}
+
+bool TcrStream::open() {
+ close();
+ if (myBase.isNull() || !myBase->open()) {
+ return false;
+ }
+
+ char header[9];
+ if (myBase->read(header, 9) != 9 || std::strncmp(header, "!!8-Bit!!", 9) != 0) {
+ myBase->close();
+ return false;
+ }
+
+ unsigned char entryLength;
+ char entryBuffer[255];
+ for (int i = 0; i < 256; ++i) {
+ if (myBase->read((char*)&entryLength, 1) != 1 ||
+ (entryLength > 0 && myBase->read(entryBuffer, entryLength) != entryLength)) {
+ myBase->close();
+ return false;
+ }
+ if (entryLength > 0) {
+ myDictionary[i].append(entryBuffer, entryLength);
+ }
+ }
+
+ return true;
+}
+
+void TcrStream::close() {
+ if (!myBase.isNull()) {
+ myBase->close();
+ }
+ for (int i = 0; i < 256; ++i) {
+ myDictionary[i].erase();
+ }
+ myBuffer.erase();
+}
+
+std::size_t TcrStream::read(char *buffer, std::size_t maxSize) {
+ std::size_t size = 0;
+ if (myBuffer.length() > 0) {
+ size += std::min(maxSize, myBuffer.length());
+ if (buffer != 0) {
+ std::strncpy(buffer, myBuffer.data(), size);
+ }
+ myBuffer.erase(0, size);
+ }
+ while (size < maxSize) {
+ unsigned char index;
+ if (myBase->read((char*)&index, 1) != 1) {
+ break;
+ }
+ std::size_t len = myDictionary[index].length();
+ if (len > 0) {
+ std::size_t freeSize = maxSize - size;
+ if (buffer != 0) {
+ std::strncpy(buffer + size, myDictionary[index].data(), std::min(len, freeSize));
+ }
+ size += std::min(len, freeSize);
+ if (len > freeSize) {
+ myBuffer = myDictionary[index].substr(freeSize);
+ }
+ }
+ }
+ myOffset += size;
+ return size;
+}
+
+void TcrStream::seek(int offset, bool absoluteOffset) {
+ if (absoluteOffset) {
+ offset -= this->offset();
+ }
+ if (offset > 0) {
+ read(0, offset);
+ } else if (offset < 0) {
+ offset += this->offset();
+ open();
+ if (offset >= 0) {
+ read(0, offset);
+ }
+ }
+}
+
+std::size_t TcrStream::offset() const {
+ return myOffset;
+}
+
+std::size_t TcrStream::sizeOfOpened() {
+ // TODO: implement
+ return 0;
+}
diff --git a/reader/src/formats/tcr/TcrStream.h b/reader/src/formats/tcr/TcrStream.h
new file mode 100644
index 0000000..0a9d212
--- /dev/null
+++ b/reader/src/formats/tcr/TcrStream.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef __TCRSTREAM_H__
+#define __TCRSTREAM_H__
+
+#include <ZLInputStream.h>
+
+class ZLFile;
+
+class TcrStream : public ZLInputStream {
+
+public:
+ TcrStream(const ZLFile &file);
+ virtual ~TcrStream();
+ bool open();
+ virtual void close();
+
+ std::size_t read(char *buffer, std::size_t maxSize);
+ void seek(int offset, bool absoluteOffset);
+ std::size_t offset() const;
+ std::size_t sizeOfOpened();
+
+protected:
+ std::string myDictionary[256];
+ std::string myBuffer;
+ shared_ptr<ZLInputStream> myBase;
+ std::size_t myOffset;
+};
+
+#endif /* __TCRSTREAM_H__ */