From 2bb080ee3b750b3e08808e959e9da0bb5c95e72a Mon Sep 17 00:00:00 2001 From: Veloman Yunkan Date: Tue, 8 Feb 2022 23:10:34 +0400 Subject: [PATCH] Cleared kiwix-search of libkiwix deprecated API --- src/searcher/kiwix-search.cpp | 65 ++++++++++++----------------------- 1 file changed, 22 insertions(+), 43 deletions(-) diff --git a/src/searcher/kiwix-search.cpp b/src/searcher/kiwix-search.cpp index 61f5806..7a58666 100644 --- a/src/searcher/kiwix-search.cpp +++ b/src/searcher/kiwix-search.cpp @@ -18,11 +18,16 @@ */ #include -#include -#include + +#include +#include + +#include #include "../version.h" +using namespace std; + void usage() { cout << "Usage: kiwix-search [OPTIONS] ZIM PATTERN" << endl << endl @@ -48,9 +53,6 @@ int main(int argc, char** argv) int option_index = 0; int c = 0; - kiwix::Searcher* searcher = NULL; - kiwix::Reader* reader = NULL; - /* Argument parsing */ while (42) { static struct option long_options[] @@ -96,47 +98,24 @@ int main(int argc, char** argv) /* Try to prepare the indexing */ try { - reader = new kiwix::Reader(zimPath); - } catch (...) { - /* Cannot open the zimPath, maybe it is a plain old xapian database - * directory */ - } - - if (reader) { - searcher = new kiwix::Searcher(); - bool contians_FTIndex=searcher->add_reader(reader); - if(!contians_FTIndex){ - std::cerr << "The Zim file does not contain a full-text index." << std::endl; - if(suggestionFlag){ - exit(0); - } - exit(1); - } - } else { - cerr << "Unable to search through zim '" << zimPath << "'." << endl; - exit(1); - } + zim::Archive archive(zimPath); - /* Start the indexing */ - if (searcher != NULL) { - string searchString(search); if (suggestionFlag) { - searcher->suggestions(searchString, verboseFlag); - } else { - searcher->search(searchString, 0, 10, verboseFlag); - } - kiwix::Result* p_result; - while ((p_result = searcher->getNextResult())) { - cout << p_result->get_title() << endl; - delete p_result; + zim::SuggestionSearcher searcher({archive}); + searcher.setVerbose(verboseFlag); + for (const auto& r : searcher.suggest(search).getResults(0, 10) ) { + cout << r.getTitle() << endl; + } + } else { + zim::Searcher searcher({archive}); + searcher.setVerbose(verboseFlag); + const zim::Query query(search); + for (const auto& r : searcher.search(query).getResults(0, 10) ) { + cout << r.getTitle() << endl; + } } - - delete searcher; - delete reader; - - // kiwix::XapianSearcher::terminate(); - } else { - cerr << "Unable instanciate the Kiwix searcher." << endl; + } catch ( const std::runtime_error& err) { + cerr << err.what() << endl; exit(1); }