|
| 1 | +/* |
| 2 | + * Copyright (C) 2013 Emmanuel Engelhart <[email protected]> |
| 3 | + * Copyright (C) 2017 Matthieu Gautier <[email protected]> |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation; either version 3 of the License, or |
| 8 | + * any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 18 | + * MA 02110-1301, USA. |
| 19 | + */ |
| 20 | + |
| 21 | +#include <jni.h> |
| 22 | +#include <exception> |
| 23 | +#include "org_kiwix_libzim_Searcher.h" |
| 24 | + |
| 25 | +#include <utils.h> |
| 26 | + |
| 27 | +#include <string> |
| 28 | + |
| 29 | +#include <zim/search.h> |
| 30 | + |
| 31 | +#define NATIVE_TYPE zim::Searcher |
| 32 | + |
| 33 | +JNIEXPORT void JNICALL Java_org_kiwix_libzim_Searcher_setNativeSearcher( |
| 34 | + JNIEnv* env, jobject thisObj, jobject archive) |
| 35 | +{ |
| 36 | + |
| 37 | + Lock l; |
| 38 | + auto cArchive = getPtr<zim::Archive>(env, archive); |
| 39 | + try { |
| 40 | + auto searcher = std::make_shared<zim::Searcher>(*cArchive); |
| 41 | + SET_PTR(searcher); |
| 42 | + } catch (std::exception& e) { |
| 43 | + LOG("Cannot create searcher"); |
| 44 | + LOG("%s", e.what()); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +JNIEXPORT void JNICALL |
| 50 | +Java_org_kiwix_kiwixlib_libzim_Searcher_dispose(JNIEnv* env, jobject thisObj) |
| 51 | +{ |
| 52 | + dispose<NATIVE_TYPE>(env, thisObj); |
| 53 | +} |
| 54 | + |
| 55 | +#define THIS GET_PTR(NATIVE_TYPE) |
| 56 | +#define GETTER(retType, name) JNIEXPORT retType JNICALL \ |
| 57 | +Java_org_kiwix_libzim_Searcher__##name (JNIEnv* env, jobject thisObj) \ |
| 58 | +{ \ |
| 59 | + return TO_JNI(THIS->name()); \ |
| 60 | +} |
| 61 | + |
| 62 | +METHOD(jobject, Searcher, addArchive, jobject archive) { |
| 63 | + auto cArchive = getPtr<zim::Archive>(env, archive); |
| 64 | + THIS->addArchive(*cArchive); |
| 65 | + return thisObj; |
| 66 | +} |
| 67 | + |
| 68 | +METHOD(jobject, Searcher, search, jobject query) { |
| 69 | + auto cQuery = getPtr<zim::Query>(env, query); |
| 70 | + auto obj = NEW_OBJECT("org/kiwix/libzim/Search"); |
| 71 | + SET_HANDLE(zim::Search, obj, THIS->search(*cQuery)); |
| 72 | + return obj; |
| 73 | +} |
| 74 | + |
| 75 | +METHOD(void, Searcher, setVerbose, jboolean verbose) { |
| 76 | + THIS->setVerbose(TO_C(verbose)); |
| 77 | +} |
| 78 | + |
0 commit comments