Skip to content

Commit 86771cd

Browse files
committed
cpp compiles
1 parent 2664ae9 commit 86771cd

3 files changed

Lines changed: 86 additions & 8 deletions

File tree

app/src/main/cpp/CoreWrapper.cpp

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
#include <odr/html.hpp>
66
#include <odr/open_document_reader.hpp>
77
#include <odr/exceptions.hpp>
8+
#include <odr/http_server.hpp>
89

910
#include <android/log.h>
1011

1112
#include <string>
1213
#include <optional>
1314

14-
std::optional<odr::Html> html;
15+
std::optional<odr::Html> s_html;
1516

1617
JNIEXPORT jobject JNICALL
1718
Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jobject instance,
@@ -119,7 +120,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jobject
119120
config.text_document_margin = true;
120121
}
121122

122-
html = odr::OpenDocumentReader::html(inputPathCpp, [&passwordCpp]() -> std::string {
123+
s_html = odr::OpenDocumentReader::html(inputPathCpp, [&passwordCpp]() -> std::string {
123124
if (passwordCpp.has_value()) {
124125
return passwordCpp.value();
125126
}
@@ -128,7 +129,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jobject
128129

129130
{
130131
const auto extensionCpp = odr::OpenDocumentReader::type_to_string(
131-
html->file_type());
132+
s_html->file_type());
132133
const auto extensionC = extensionCpp.c_str();
133134
jstring extension = env->NewStringUTF(extensionC);
134135

@@ -137,7 +138,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jobject
137138
env->SetObjectField(result, extensionField, extension);
138139
}
139140

140-
for (auto &&page: html->pages()) {
141+
for (auto &&page: s_html->pages()) {
141142
jstring pageName = env->NewStringUTF(page.name.c_str());
142143
env->CallBooleanMethod(pageNames, addMethod, pageName);
143144

@@ -192,7 +193,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env,
192193

193194
const auto htmlDiffC = env->GetStringUTFChars(htmlDiff, &isCopy);
194195

195-
const auto extension = odr::OpenDocumentReader::type_to_string(html->file_type());
196+
const auto extension = odr::OpenDocumentReader::type_to_string(s_html->file_type());
196197
const auto outputPathCpp = outputPathPrefixCpp + "." + extension;
197198
const char *outputPathC = outputPathCpp.c_str();
198199
jstring outputPath = env->NewStringUTF(outputPathC);
@@ -201,7 +202,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env,
201202
env->SetObjectField(result, outputPathField, outputPath);
202203

203204
try {
204-
html->edit(htmlDiffC);
205+
s_html->edit(htmlDiffC);
205206

206207
env->ReleaseStringUTFChars(htmlDiff, htmlDiffC);
207208
} catch (...) {
@@ -212,7 +213,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env,
212213
}
213214

214215
try {
215-
html->save(outputPathCpp);
216+
s_html->save(outputPathCpp);
216217
} catch (...) {
217218
env->SetIntField(result, errorField, -7);
218219
return result;
@@ -229,5 +230,46 @@ Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env,
229230
JNIEXPORT void JNICALL
230231
Java_at_tomtasche_reader_background_CoreWrapper_closeNative(JNIEnv *env, jobject instance,
231232
jobject options) {
232-
html.reset();
233+
s_html.reset();
234+
}
235+
236+
std::optional<odr::HttpServer> s_server;
237+
238+
JNIEXPORT void JNICALL
239+
Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env, jobject instance) {
240+
odr::HttpServer::Config config;
241+
s_server = odr::HttpServer(config);
242+
}
243+
244+
JNIEXPORT jstring JNICALL
245+
Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jobject instance, jobject options) {
246+
jboolean isCopy;
247+
248+
jclass optionsClass = env->GetObjectClass(options);
249+
jfieldID inputPathField = env->GetFieldID(optionsClass, "inputPath", "Ljava/lang/String;");
250+
auto inputPath = (jstring) env->GetObjectField(options, inputPathField);
251+
252+
const auto inputPathC = env->GetStringUTFChars(inputPath, &isCopy);
253+
auto inputPathCpp = std::string(inputPathC, env->GetStringUTFLength(inputPath));
254+
env->ReleaseStringUTFChars(inputPath, inputPathC);
255+
256+
odr::DecodePreference decode_preference;
257+
decode_preference.engine_priority = {
258+
odr::DecoderEngine::poppler, odr::DecoderEngine::wvware, odr::DecoderEngine::odr};
259+
odr::DecodedFile file = odr::OpenDocumentReader::open(inputPathCpp, decode_preference);
260+
261+
std::string id = s_server->host_file(file);
262+
263+
return env->NewStringUTF(id.c_str());
264+
}
265+
266+
JNIEXPORT void JNICALL
267+
Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative(JNIEnv *env, jobject instance) {
268+
s_server->listen("0.0.0.0", 8080);
269+
}
270+
271+
JNIEXPORT void JNICALL
272+
Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative(JNIEnv *env, jobject instance) {
273+
s_server->stop();
274+
s_server.reset();
233275
}

app/src/main/cpp/CoreWrapper.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ extern "C" {
1313

1414
JNIEXPORT void JNICALL
1515
Java_at_tomtasche_reader_background_CoreWrapper_closeNative(JNIEnv *env, jobject instance, jobject options);
16+
17+
JNIEXPORT void JNICALL
18+
Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env, jobject instance);
19+
20+
JNIEXPORT jstring JNICALL
21+
Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jobject instance, jobject options);
22+
23+
JNIEXPORT void JNICALL
24+
Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative(JNIEnv *env, jobject instance);
25+
26+
JNIEXPORT void JNICALL
27+
Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative(JNIEnv *env, jobject instance);
1628
}
1729

1830
#endif //ANDROID_CORE_WRAPPER_H

app/src/main/java/at/tomtasche/reader/background/CoreWrapper.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,30 @@ public void close() {
7878

7979
private native void closeNative(CoreOptions options);
8080

81+
public void createServer() {
82+
createServerNative();
83+
}
84+
85+
private native void createServerNative();
86+
87+
public String hostFile(CoreOptions options) {
88+
return hostFileNative(options);
89+
}
90+
91+
private native String hostFileNative(CoreOptions options);
92+
93+
public void listenServer() {
94+
listenServerNative();
95+
}
96+
97+
private native void listenServerNative();
98+
99+
public void stopServer() {
100+
stopServerNative();
101+
}
102+
103+
private native void stopServerNative();
104+
81105
public static class CoreOptions {
82106

83107
public boolean ooxml;

0 commit comments

Comments
 (0)