1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the libe-book project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include <memory>
11 #include <stdio.h>
12 #include <string.h>
13
14 #include <librevenge-generators/librevenge-generators.h>
15 #include <librevenge-stream/librevenge-stream.h>
16
17 #include <libe-book/libe-book.h>
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #ifndef VERSION
24 #define VERSION "UNKNOWN VERSION"
25 #endif
26
27 namespace
28 {
29
printUsage()30 int printUsage()
31 {
32 printf("`ebook2text' converts e-books to plain text.\n");
33 printf("Supported formats include FictionBook 2, BBeB and various Palm-based formats.\n");
34 printf("\n");
35 printf("Usage: ebook2text [OPTION] FILE\n");
36 printf("\n");
37 printf("Options:\n");
38 printf("\t--info display document metadata instead of the text\n");
39 printf("\t--help show this help message\n");
40 printf("\t--version show version information\n");
41 printf("\n");
42 printf("Report bugs to <https://sourceforge.net/p/libebook/tickets/> or <https://bugs.documentfoundation.org/>.\n");
43 return -1;
44 }
45
printVersion()46 int printVersion()
47 {
48 printf("ebook2text %s\n", VERSION);
49 return 0;
50 }
51
52 } // anonymous namespace
53
54 using libebook::EBOOKDocument;
55
main(int argc,char * argv[])56 int main(int argc, char *argv[])
57 {
58 if (argc < 2)
59 return printUsage();
60
61 char *szInputFile = nullptr;
62 bool isInfo = false;
63
64 for (int i = 1; i < argc; i++)
65 {
66 if (!strcmp(argv[i], "--info"))
67 isInfo = true;
68 else if (!strcmp(argv[i], "--version"))
69 return printVersion();
70 else if (!szInputFile && strncmp(argv[i], "--", 2))
71 szInputFile = argv[i];
72 else
73 return printUsage();
74 }
75
76 if (!szInputFile)
77 return printUsage();
78
79 std::shared_ptr<librevenge::RVNGInputStream> input;
80
81 if (librevenge::RVNGDirectoryStream::isDirectory(szInputFile))
82 input.reset(new librevenge::RVNGDirectoryStream(szInputFile));
83 else
84 input.reset(new librevenge::RVNGFileStream(szInputFile));
85
86 EBOOKDocument::Type type = EBOOKDocument::TYPE_UNKNOWN;
87 EBOOKDocument::Confidence confidence = EBOOKDocument::isSupported(input.get(), &type);
88
89 if (EBOOKDocument::CONFIDENCE_SUPPORTED_PART == confidence)
90 {
91 input.reset(librevenge::RVNGDirectoryStream::createForParent(szInputFile));
92 confidence = EBOOKDocument::isSupported(input.get(), &type);
93 }
94
95 if ((EBOOKDocument::CONFIDENCE_EXCELLENT != confidence) && (EBOOKDocument::CONFIDENCE_WEAK != confidence))
96 {
97 fprintf(stderr, "'%s' has not been recognized as any of the supported formats\n", szInputFile);
98 return 1;
99 }
100
101 librevenge::RVNGString document;
102 librevenge::RVNGTextTextGenerator documentGenerator(document, isInfo);
103
104 if (EBOOKDocument::RESULT_OK != EBOOKDocument::parse(input.get(), &documentGenerator, type))
105 return 1;
106
107 printf("%s", document.cstr());
108
109 return 0;
110 }
111
112 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */
113