From 76f2603810d40377c1d16a37181e77f7c494ede4 Mon Sep 17 00:00:00 2001 From: kelson42 Date: Fri, 8 Apr 2011 13:57:23 +0000 Subject: [PATCH] + usage of template file for displaying the results --- src/server/kiwix-serve.cpp | 44 +++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src/server/kiwix-serve.cpp b/src/server/kiwix-serve.cpp index a2af102..39e4d51 100644 --- a/src/server/kiwix-serve.cpp +++ b/src/server/kiwix-serve.cpp @@ -20,6 +20,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -29,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -233,18 +237,14 @@ static int accessHandlerCallback(void *cls, try { std::string patternString = string(pattern); - searcher->search(patternString, 30, verboseFlag); - content = "Kiwix search results

Results


    \n"; - while (searcher->getNextResult(urlStr, titleStr, scoreInt)) { - sprintf(scoreStr, "%d", scoreInt); - content += "
  1. " + titleStr+ " - " + scoreStr + "%
  2. \n"; - - } + searcher->search(patternString, 0, 25, verboseFlag); + content = "Kiwix search results\n"; + content += searcher->getHtml(); } catch (const std::exception& e) { std::cerr << e.what() << std::endl; } - content += "
\n"; + content += "\n"; mimeType = "text/html; charset=utf-8"; @@ -395,6 +395,10 @@ int main(int argc, char **argv) { void *page; + /* Chnage the current dir to binary dir */ + const char* currentDirectory = dirname(argv[0]); + chdir(currentDirectory); + /* Instanciate the ZIM file handler */ try { reader = new kiwix::Reader(zimPath); @@ -414,6 +418,30 @@ int main(int argc, char **argv) { cerr << "Unable to open the search index '" << zimPath << "' with the XapianSearcher." << endl; } + /* Try to load the result template */ + try { + fstream templateStream; + char rootPath[PATH_MAX]; + templateStream.open("../share/kiwix/static/results.tmpl", ifstream::in); + + if (templateStream.fail()) { + templateStream.open("../../static/results.tmpl", ifstream::in); + + if (templateStream.fail()) { + throw "Unable to find a result template file."; + } else { + realpath("../../static/results.tmpl", rootPath); + searcher->setResultTemplatePath(rootPath); + } + } else { + realpath("../share/kiwix/static/results.tmpl", rootPath); + searcher->setResultTemplatePath(rootPath); + } + } catch (...) { + cerr << "Unable to open the result template file." << endl; + exit(1); + } + /* Try with the CluceneSearcher */ if (!hasSearchIndex) { try {