Fix tg_termpos1 for 64-bit termpos
[xapian.git] / xapian-applications / omega / handler_libebook.cc
blobf3b5be4d0f50cf932d6ad20eb6ea08f06f22bcb2
1 /** @file
2 * @brief Extract text and metadata using libe-book.
3 */
4 /* Copyright (C) 2019 Bruno Baruffaldi
5 * Copyright (C) 2022,2023 Olly Betts
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20 * USA
22 #include <config.h>
23 #include "handler.h"
24 #include "stringutils.h"
26 #include <memory>
28 #include <librevenge-generators/librevenge-generators.h>
29 #include <librevenge-stream/librevenge-stream.h>
30 #include <libe-book/libe-book.h>
32 #define HANDLE_FIELD(START, END, FIELD, OUT...) \
33 handle_field((START), (END), (FIELD), (CONST_STRLEN(FIELD)), OUT)
35 using libebook::EBOOKDocument;
36 using namespace librevenge;
37 using namespace std;
39 // Handle a field for which we only take a single value - we avoid copying in
40 // this case.
41 static void
42 handle_field(const char* start,
43 const char* end,
44 const char* field,
45 size_t len,
46 const char*& out,
47 size_t& out_len)
49 if (size_t(end - start) > len && memcmp(start, field, len) == 0) {
50 start += len;
51 while (start != end && isspace(*start)) start++;
52 if (start != end && (end[-1] != '\r' || --end != start)) {
53 out = start;
54 out_len = end - start;
59 // Handle a field for which we process multiple instances. We just send each
60 // occurrence as we see it.
61 static void
62 handle_field(const char* start,
63 const char* end,
64 const char* field,
65 size_t len,
66 Field code)
68 if (size_t(end - start) > len && memcmp(start, field, len) == 0) {
69 start += len;
70 while (start != end && isspace(*start)) start++;
71 if (start != end && (end[-1] != '\r' || --end != start)) {
72 send_field(code, start, end - start);
77 static void
78 parse_metadata(const char* data, size_t len)
80 const char* author;
81 size_t author_len = 0;
83 const char* p = data;
84 const char* end = p + len;
86 while (p != end) {
87 const char* start = p;
88 p = static_cast<const char*>(memchr(p, '\n', end - start));
89 const char* eol;
90 if (p)
91 eol = p++;
92 else
93 p = eol = end;
94 if ((end - start) > 5 && memcmp(start, "meta:", 5) == 0) {
95 start += 5;
96 switch (*start) {
97 case 'i': {
98 // Use dc:creator in preference to meta:initial-creator.
99 if (!author_len)
100 HANDLE_FIELD(start, eol, "initial-creator",
101 author, author_len);
102 break;
104 case 'k': {
105 HANDLE_FIELD(start, eol, "keyword", FIELD_KEYWORDS);
106 break;
109 } else if ((end - start) > 3 && memcmp(start, "dc:", 3) == 0) {
110 start += 3;
111 switch (*start) {
112 case 'c': {
113 // Use dc:creator in preference to meta:initial-creator.
114 HANDLE_FIELD(start, eol, "creator", author, author_len);
115 break;
117 case 's': {
118 HANDLE_FIELD(start, eol, "subject", FIELD_KEYWORDS);
119 break;
121 case 't': {
122 HANDLE_FIELD(start, eol, "title", FIELD_TITLE);
123 break;
126 } else if ((end - start) > 8 && memcmp(start, "dcterms:", 8) == 0) {
127 start += 8;
128 HANDLE_FIELD(start, eol, "available", FIELD_KEYWORDS);
132 if (author_len) {
133 send_field(FIELD_AUTHOR, author, author_len);
137 bool
138 initialise()
140 return true;
143 void
144 extract(const string& filename, const string&)
146 unique_ptr<RVNGInputStream> input;
147 RVNGString dump, metadata;
148 const char* file = filename.c_str();
150 if (RVNGDirectoryStream::isDirectory(file))
151 input.reset(new RVNGDirectoryStream(file));
152 else
153 input.reset(new RVNGFileStream(file));
155 EBOOKDocument::Type type = EBOOKDocument::TYPE_UNKNOWN;
156 auto confidence = EBOOKDocument::isSupported(input.get(), &type);
158 if (confidence != EBOOKDocument::CONFIDENCE_EXCELLENT &&
159 confidence != EBOOKDocument::CONFIDENCE_WEAK) {
160 send_field(FIELD_ERROR, "Format not supported");
161 return;
164 // Extract metadata.
165 RVNGTextTextGenerator metadata_gen(metadata, true);
166 if (EBOOKDocument::parse(input.get(), &metadata_gen, type) !=
167 EBOOKDocument::RESULT_OK) {
168 send_field(FIELD_ERROR, "Failed to extract metadata");
169 return;
171 parse_metadata(metadata.cstr(), metadata.size());
173 // Extract body text.
174 RVNGTextTextGenerator content(dump, false);
175 if (EBOOKDocument::parse(input.get(), &content, type) !=
176 EBOOKDocument::RESULT_OK) {
177 send_field(FIELD_ERROR, "Failed to extract text");
178 return;
180 send_field(FIELD_BODY, dump.cstr(), dump.size());