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 pathconvert.cc
128 lines (110 loc) · 3.25 KB
/
convert.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* convert.cc - Convert documents using LibreOfficeKit
*
* Copyright (C) 2014,2015,2016,2018 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 "convert.h"
#include <cstdlib>
#include <exception>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sysexits.h>
#include <LibreOfficeKit/LibreOfficeKit.hxx>
#include "urlencode.h"
using namespace std;
using namespace lok;
// Install location for Debian packages:
#define LO_PATH_DEBIAN "/usr/lib/libreoffice/program"
// Install location for .deb files from libreoffice.org:
#define LO_PATH_LIBREOFFICEORG(V) "/opt/libreoffice"#V"/program"
const char * program = "<program>";
// Find a LibreOffice installation to use.
static const char *
get_lo_path()
{
const char * lo_path = getenv("LO_PATH");
if (!lo_path) {
struct stat sb;
#define CHECK_DIR(P) if (!lo_path && stat(P"/versionrc", &sb) == 0 && S_ISREG(sb.st_mode)) lo_path = P
CHECK_DIR(LO_PATH_DEBIAN);
CHECK_DIR(LO_PATH_LIBREOFFICEORG(6.1));
CHECK_DIR(LO_PATH_LIBREOFFICEORG(6.0));
CHECK_DIR(LO_PATH_LIBREOFFICEORG(5.4));
CHECK_DIR(LO_PATH_LIBREOFFICEORG(5.3));
CHECK_DIR(LO_PATH_LIBREOFFICEORG(5.2));
CHECK_DIR(LO_PATH_LIBREOFFICEORG(5.1));
CHECK_DIR(LO_PATH_LIBREOFFICEORG(5.0));
CHECK_DIR(LO_PATH_LIBREOFFICEORG(4.4));
CHECK_DIR(LO_PATH_LIBREOFFICEORG(4.3));
if (!lo_path) {
cerr << program << ": LibreOffice install not found\n"
"Set LO_PATH in the environment to the 'program' directory - e.g.:\n"
"LO_PATH=/opt/libreoffice/program\n"
"export LO_PATH" << endl;
_Exit(1);
}
}
return lo_path;
}
void *
convert_init()
{
Office * llo = NULL;
try {
const char * lo_path = get_lo_path();
llo = lok_cpp_init(lo_path);
if (!llo) {
cerr << program << ": Failed to initialise LibreOfficeKit" << endl;
return NULL;
}
return static_cast<void*>(llo);
} catch (const exception & e) {
delete llo;
cerr << program << ": LibreOfficeKit threw exception (" << e.what() << ")" << endl;
return NULL;
}
}
void
convert_cleanup(void * h_void)
{
Office * llo = static_cast<Office *>(h_void);
delete llo;
}
int
convert(void * h_void, bool url,
const char * input, const char * output,
const char * format, const char * options)
try {
if (!h_void) return 1;
Office * llo = static_cast<Office *>(h_void);
string input_url;
if (url) {
input_url = input;
} else {
url_encode_path(input_url, input);
}
Document * lodoc = llo->documentLoad(input_url.c_str(), options);
if (!lodoc) {
const char * errmsg = llo->getError();
cerr << program << ": LibreOfficeKit failed to load document (" << errmsg << ")" << endl;
return 1;
}
string output_url;
url_encode_path(output_url, output);
if (!lodoc->saveAs(output_url.c_str(), format, options)) {
const char * errmsg = llo->getError();
cerr << program << ": LibreOfficeKit failed to export (" << errmsg << ")" << endl;
delete lodoc;
return 1;
}
delete lodoc;
return 0;
} catch (const exception & e) {
cerr << program << ": LibreOfficeKit threw exception (" << e.what() << ")" << endl;
return 1;
}