-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlok_convert.cpp
84 lines (74 loc) · 2.26 KB
/
lok_convert.cpp
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
/**
* lok_convert - converter between LibreOffice document formats
*
* Written in 2016 by Mitsuya Shibata <[email protected]>
*
* To the extent possible under law, the author(s) have dedicated all copyright
* and related and neighboring rights to this software to the public domain
* worldwide. This software is distributed without any warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication along
* with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <LibreOfficeKit/LibreOfficeKit.hxx>
namespace lok_tools {
const std::string LO_PATH = "/usr/lib/libreoffice/program";
}
void usage(const char *name)
{
std::cerr << "Usage: " << name;
std::cerr << " [-p path-of-libreoffice] FROM TO" << std::endl;
}
int main(int argc, char **argv)
{
int opt;
std::string lo_path = lok_tools::LO_PATH;
while ((opt = getopt(argc, argv, "p:")) != -1) {
switch (opt) {
case 'p':
lo_path = std::string(optarg);
break;
default:
usage(argv[0]);
exit(EXIT_FAILURE);
}
}
if (argc - optind < 2) {
usage(argv[0]);
exit(EXIT_FAILURE);
}
const char *from_file = argv[optind++];
const char *to_file = argv[optind];
try {
lok::Office *lo = lok::lok_cpp_init(lo_path.c_str());
if (!lo) {
std::cerr << "Error: Failed to initialise LibreOfficeKit";
std::cerr << std::endl;
exit(EXIT_FAILURE);
}
lok::Document *doc = lo->documentLoad(from_file);
if (!doc) {
std::cerr << "Error: Failed to load document: ";
std::cerr << lo->getError() << std::endl;
delete lo;
exit(EXIT_FAILURE);
}
if (!doc->saveAs(to_file)) {
std::cerr << "Error: Failed to save document: ";
std::cerr << lo->getError() << std::endl;
delete doc;
delete lo;
exit(EXIT_FAILURE);
}
delete doc;
delete lo;
} catch (const std::exception & e) {
std::cerr << "Error: " << e.what() << std::endl;
exit(EXIT_FAILURE);
}
return 0;
}