This repository has been archived by the owner on Aug 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlloconv.cc
112 lines (99 loc) · 2.22 KB
/
lloconv.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* lloconv.cc - Convert a document using LibreOfficeKit
*
* Copyright (C) 2014,2015,2016 Olly Betts
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <cstdlib>
#include <iostream>
#include <sysexits.h>
#include "convert.h"
using namespace std;
static void
usage()
{
cerr << "Usage: " << program << " [-u] [-f OUTPUT_FORMAT] [-o OPTIONS] INPUT_FILE OUTPUT_FILE\n\n";
cerr << " -u INPUT_FILE is a URL\n";
cerr << "Specifying options requires LibreOffice >= 4.3.0rc1\n\n";
cerr << "Known values for OUTPUT_FORMAT include:\n";
cerr << " For text documents: doc docx fodt html odt ott pdf txt xhtml\n\n";
cerr << "Known OPTIONS include: SkipImages\n";
cerr << flush;
}
int
main(int argc, char **argv)
{
program = argv[0];
if (argc < 3) {
usage();
_Exit(EX_USAGE);
}
const char * format = NULL;
const char * options = NULL;
bool url = false;
// FIXME: Use getopt() or something.
++argv;
--argc;
while (argv[0] && argv[0][0] == '-') {
switch (argv[0][1]) {
case '-':
if (argv[0][2] == '\0') {
// End of options.
++argv;
--argc;
goto last_option;
}
break;
case 'f':
if (argv[0][2]) {
format = argv[0] + 2;
++argv;
--argc;
} else {
format = argv[1];
argv += 2;
argc -= 2;
}
continue;
case 'o':
if (argv[0][2]) {
options = argv[0] + 2;
++argv;
--argc;
} else {
options = argv[1];
argv += 2;
argc -= 2;
}
continue;
case 'u':
if (argv[0][2] != '\0') {
break;
}
url = true;
++argv;
--argc;
continue;
}
cerr << "Option '" << argv[0] << "' unknown\n\n";
argc = -1;
break;
}
last_option:
if (argc != 2) {
usage();
_Exit(EX_USAGE);
}
const char * input = argv[0];
const char * output = argv[1];
void * handle = convert_init();
if (!handle) {
_Exit(EX_UNAVAILABLE);
}
int rc = convert(handle, url, input, output, format, options);
convert_cleanup(handle);
// Avoid segfault from LibreOffice by terminating swiftly.
_Exit(rc);
}