-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCoreWrapper.mm
More file actions
138 lines (112 loc) · 4.13 KB
/
CoreWrapper.mm
File metadata and controls
138 lines (112 loc) · 4.13 KB
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
129
130
131
132
133
134
135
136
137
138
//
// CoreWrapper.m
// OpenDocument Reader
//
// Created by Thomas Taschauer on 09.02.19.
// Copyright © 2019 Thomas Taschauer. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "CoreWrapper.h"
#include <odr/document.hpp>
#include <odr/document_element.hpp>
#include <odr/file.hpp>
#include <odr/html.hpp>
#include <odr/html_service.hpp>
#include <odr/odr.hpp>
#include <odr/exceptions.hpp>
#include <odr/global_params.hpp>
#include <string>
#include <optional>
#include <string>
#include <optional>
@implementation CoreWrapper {
std::optional<odr::Document> document;
std::optional<odr::Html> html;
}
- (bool)translate:(NSString *)inputPath cache:(NSString *)cachePath into:(NSString *)outputPath with:(NSString *)password editable:(bool)editable {
@synchronized(self) {
try {
_errorCode = 0;
_pageNames = nil;
_pagePaths = nil;
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
std::string bundlePathCpp = std::string([bundlePath UTF8String]);
odr::GlobalParams::set_odr_core_data_path(bundlePathCpp + "/odrcore");
html.reset();
odr::HtmlConfig config;
config.editable = editable;
if (password == nil) {
password = @"";
}
auto inputPathC = [inputPath UTF8String];
auto inputPathCpp = std::string(inputPathC);
std::vector<odr::FileType> fileTypes;
try {
fileTypes = odr::types(inputPathCpp);
if (fileTypes.empty()) {
_errorCode = @(-5);
return false;
}
} catch (odr::UnsupportedFileType &e) {
_errorCode = @(-5);
return false;
}
if (std::find(fileTypes.begin(), fileTypes.end(), odr::FileType::portable_document_format) != fileTypes.end()) {
_errorCode = @(-5);
return false;
}
auto outputPathC = [outputPath UTF8String];
auto outputPathCpp = std::string(outputPathC);
auto cachePathC = [cachePath UTF8String];
auto cachePathCpp = std::string(cachePathC);
odr::DecodedFile file = odr::open(inputPathCpp);
if (file.password_encrypted()) {
try {
file = file.decrypt(std::string([password UTF8String]));
} catch (odr::WrongPasswordError &) {
_errorCode = @(-2);
return false;
}
}
if (!file.is_document_file()) {
_errorCode = @(-5);
return false;
}
document = file.document_file().document();
html = odr::html::translate(*document, cachePathCpp, config).bring_offline(outputPathCpp);
NSMutableArray *pageNames = [[NSMutableArray alloc] init];
NSMutableArray *pagePaths = [[NSMutableArray alloc] init];
for (auto &&page : html->pages()) {
[pageNames addObject:[NSString stringWithCString:page.name.c_str() encoding:[NSString defaultCStringEncoding]]];
[pagePaths addObject:[NSString stringWithCString:page.path.c_str() encoding:[NSString defaultCStringEncoding]]];
}
_pageNames = pageNames;
_pagePaths = pagePaths;
} catch (odr::UnknownFileType&) {
_errorCode = @(-5);
return false;
} catch (std::runtime_error &e) {
std::cout << e.what() << std::endl;
_errorCode = @(-3);
return false;
} catch (...) {
_errorCode = @(-3);
return false;
}
return true;
}
}
- (bool)backTranslate:(NSString *)diff into:(NSString *)outputPath {
@synchronized(self) {
try {
_errorCode = 0;
odr::html::edit(*document, [diff UTF8String]);
document->save([outputPath UTF8String]);
} catch (...) {
_errorCode = @(-3);
return false;
}
return true;
}
}
@end