summaryrefslogtreecommitdiffstats
path: root/fbreader/src/migration/FB2MigrationReader.cpp
diff options
context:
space:
mode:
authorMichele Calgaro <[email protected]>2024-05-11 21:28:48 +0900
committerMichele Calgaro <[email protected]>2024-05-11 21:28:48 +0900
commit2462d03f322261bd616721c2b2065c4004b36c9c (patch)
tree239947a0737bb8386703a1497f12c09aebd3080a /fbreader/src/migration/FB2MigrationReader.cpp
downloadtde-ebook-reader-2462d03f322261bd616721c2b2065c4004b36c9c.tar.gz
tde-ebook-reader-2462d03f322261bd616721c2b2065c4004b36c9c.zip
Initial import (as is) from Debian Snapshot's 'fbreader' source code (https://snapshot.debian.org/package/fbreader/0.99.4%2Bdfsg-6).
The Debian code is provided under GPL2 license. Signed-off-by: Michele Calgaro <[email protected]>
Diffstat (limited to 'fbreader/src/migration/FB2MigrationReader.cpp')
-rw-r--r--fbreader/src/migration/FB2MigrationReader.cpp106
1 files changed, 106 insertions, 0 deletions
diff --git a/fbreader/src/migration/FB2MigrationReader.cpp b/fbreader/src/migration/FB2MigrationReader.cpp
new file mode 100644
index 0000000..875c0a5
--- /dev/null
+++ b/fbreader/src/migration/FB2MigrationReader.cpp
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2004-2012 Geometer Plus <[email protected]>
+ *
+ * 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 <cstdlib>
+
+#include <ZLInputStream.h>
+#include <ZLUnicodeUtil.h>
+
+#include "FB2MigrationReader.h"
+#include "../formats/fb2/FB2TagManager.h"
+
+FB2MigrationReader::FB2MigrationReader(BookInfo &info, bool updateSeries) : myInfo(info), myUpdateSeries(updateSeries), myUpdateTags(info.TagsOption.value().empty()) {
+}
+
+void FB2MigrationReader::characterDataHandler(const char *text, std::size_t len) {
+ if (myReadState == READ_GENRE) {
+ myGenreBuffer.append(text, len);
+ }
+}
+
+void FB2MigrationReader::startElementHandler(int tag, const char **attributes) {
+ switch (tag) {
+ case _BODY:
+ interrupt();
+ break;
+ case _TITLE_INFO:
+ myReadState = READ_SOMETHING;
+ break;
+ case _GENRE:
+ if ((myReadState == READ_SOMETHING) && myUpdateTags) {
+ myReadState = READ_GENRE;
+ }
+ break;
+ case _SEQUENCE:
+ if ((myReadState == READ_SOMETHING) && myUpdateSeries) {
+ const char *name = attributeValue(attributes, "name");
+ if (name != 0) {
+ std::string seriesTitle = name;
+ ZLUnicodeUtil::utf8Trim(seriesTitle);
+ myInfo.SeriesTitleOption.setValue(seriesTitle);
+ const char *number = attributeValue(attributes, "number");
+ myInfo.IndexInSeriesOption.setValue((number != 0) ? std::string(number) : std::string());
+ }
+ }
+ break;
+ default:
+ break;
+ }
+}
+
+void FB2MigrationReader::endElementHandler(int tag) {
+ switch (tag) {
+ case _TITLE_INFO:
+ myReadState = READ_NOTHING;
+ break;
+ case _GENRE:
+ if (myReadState == READ_GENRE) {
+ ZLUnicodeUtil::utf8Trim(myGenreBuffer);
+ if (!myGenreBuffer.empty()) {
+ const std::vector<std::string> &tags =
+ FB2TagManager::Instance().humanReadableTags(myGenreBuffer);
+ if (!tags.empty()) {
+ myTags.insert(tags.begin(), tags.end());
+ } else {
+ myTags.insert(myGenreBuffer);
+ }
+ myGenreBuffer.erase();
+ }
+ myReadState = READ_SOMETHING;
+ }
+ break;
+ default:
+ break;
+ }
+}
+
+void FB2MigrationReader::doRead(const ZLFile &file) {
+ myReadState = READ_NOTHING;
+ readDocument(file);
+ if (myUpdateTags) {
+ std::string tagList;
+ for (std::set<std::string>::const_iterator it = myTags.begin(); it != myTags.end(); ++it) {
+ if (it != myTags.begin()) {
+ tagList += ",";
+ }
+ tagList += *it;
+ }
+ myInfo.TagsOption.setValue(tagList);
+ }
+}