1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the libetonyek 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/librevenge.h>
15 #include <librevenge-generators/librevenge-generators.h>
16 #include <librevenge-stream/librevenge-stream.h>
17
18 #include <libetonyek/libetonyek.h>
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #ifndef VERSION
25 #define VERSION "UNKNOWN VERSION"
26 #endif
27
28 #define TOOL "pages2text"
29
30 namespace
31 {
32
printUsage()33 int printUsage()
34 {
35 printf("`" TOOL "' converts Apple Pages documents to plain text.\n");
36 printf("\n");
37 printf("Usage: " TOOL " [OPTION] INPUT\n");
38 printf("\n");
39 printf("Options:\n");
40 printf("\t--help show this help message\n");
41 printf("\t--version show version information\n");
42 printf("\n");
43 printf("Report bugs to <https://bugs.documentfoundation.org/>.\n");
44 return -1;
45 }
46
printVersion()47 int printVersion()
48 {
49 printf(TOOL " " VERSION "\n");
50 return 0;
51 }
52
53 } // anonymous namespace
54
55 using libetonyek::EtonyekDocument;
56
main(int argc,char * argv[])57 int main(int argc, char *argv[])
58 {
59 if (argc < 2)
60 return printUsage();
61
62 char *szInputFile = nullptr;
63 bool isInfo = false;
64
65 for (int i = 1; i < argc; i++)
66 {
67 if (!strcmp(argv[i], "--info"))
68 isInfo = true;
69 else if (!strcmp(argv[i], "--version"))
70 return printVersion();
71 else if (!szInputFile && strncmp(argv[i], "--", 2))
72 szInputFile = argv[i];
73 else
74 return printUsage();
75 }
76
77 if (!szInputFile)
78 return printUsage();
79
80 using std::shared_ptr;
81
82 shared_ptr<librevenge::RVNGInputStream> input;
83 if (librevenge::RVNGDirectoryStream::isDirectory(szInputFile))
84 input.reset(new librevenge::RVNGDirectoryStream(szInputFile));
85 else
86 input.reset(new librevenge::RVNGFileStream(szInputFile));
87
88 EtonyekDocument::Type type = EtonyekDocument::TYPE_UNKNOWN;
89 const EtonyekDocument::Confidence confidence = EtonyekDocument::isSupported(input.get(), &type);
90 if ((EtonyekDocument::CONFIDENCE_NONE == confidence) || (EtonyekDocument::TYPE_PAGES != type))
91 {
92 fprintf(stderr, "ERROR: Unsupported file format!\n");
93 return 1;
94 }
95
96 if (EtonyekDocument::CONFIDENCE_SUPPORTED_PART == confidence)
97 input.reset(librevenge::RVNGDirectoryStream::createForParent(szInputFile));
98
99 librevenge::RVNGString output;
100 librevenge::RVNGTextTextGenerator documentGenerator(output, isInfo);
101
102 if (!EtonyekDocument::parse(input.get(), &documentGenerator))
103 {
104 fprintf(stderr, "ERROR: Parsing failed!\n");
105 return 1;
106 }
107
108 if (output.empty())
109 {
110 fprintf(stderr, "ERROR: No output was produced!\n");
111 return 1;
112 }
113
114 printf("%s", output.cstr());
115
116 return 0;
117 }
118
119 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */
120