From 63ec186e37eeea5fe19add807eafba05f0c8067c Mon Sep 17 00:00:00 2001 From: kelson42 Date: Sat, 30 Aug 2014 10:37:17 -0600 Subject: [PATCH] Few simplification/beautification of the code --- src/server/kiwix-serve.cpp | 87 +++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 49 deletions(-) diff --git a/src/server/kiwix-serve.cpp b/src/server/kiwix-serve.cpp index 5ca841f..3874c00 100644 --- a/src/server/kiwix-serve.cpp +++ b/src/server/kiwix-serve.cpp @@ -192,9 +192,11 @@ static int accessHandlerCallback(void *cls, const char* tmpGetValue = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "content"); humanReadableBookId = (tmpGetValue != NULL ? string(tmpGetValue) : ""); } else { - humanReadableBookId = urlStr.substr(1, urlStr.find("/", 1) != string::npos ? urlStr.find("/", 1) - 1 : urlStr.size() - 2); + humanReadableBookId = urlStr.substr(1, urlStr.find("/", 1) != string::npos ? + urlStr.find("/", 1) - 1 : urlStr.size() - 2); if (!humanReadableBookId.empty()) { - urlStr = urlStr.substr(urlStr.find("/", 1) != string::npos ? urlStr.find("/", 1) : humanReadableBookId.size()); + urlStr = urlStr.substr(urlStr.find("/", 1) != string::npos ? + urlStr.find("/", 1) : humanReadableBookId.size()); } } } @@ -518,6 +520,7 @@ int main(int argc, char **argv) { if (libraryFlag) { vector libraryPaths = kiwix::split(libraryPath, ";"); vector::iterator itr; + for ( itr = libraryPaths.begin(); itr != libraryPaths.end(); ++itr ) { if (!itr->empty()) { bool retVal = false; @@ -545,17 +548,13 @@ int main(int argc, char **argv) { cerr << "Unable to add the ZIM file '" << zimPath << "' to the internal library." << endl; exit(1); } else if (!indexPath.empty()) { - vector booksIds = libraryManager.getBooksIds(); - kiwix::supportedIndexType indexType = kiwix::UNKNOWN; - - /* Try with the XapianSearcher */ try { new kiwix::XapianSearcher(indexPath); - indexType = kiwix::XAPIAN; - } catch (...) { + } catch (...) { + cerr << "Unable to open the search index '" << indexPath << "'." << endl; } - - libraryManager.setBookIndex(booksIds[0], indexPath, indexType); + + libraryManager.setBookIndex(libraryManager.getBooksIds()[0], indexPath); } } @@ -587,25 +586,14 @@ int main(int argc, char **argv) { /* Instanciate the ZIM index (if necessary) */ kiwix::Searcher *searcher = NULL; if (!indexPath.empty()) { - bool hasSearchIndex = false; - - /* Try to load the search */ try { - if (currentBook.indexType == kiwix::XAPIAN) { - searcher = new kiwix::XapianSearcher(indexPath); - } else { - throw("Unknown index type"); - } - hasSearchIndex = true; - } catch (...) { - cerr << "Unable to open the search index '" << indexPath << "'." << endl; - } - - if (hasSearchIndex) { + searcher = new kiwix::XapianSearcher(indexPath); searcher->setProtocolPrefix("/"); searcher->setSearchProtocolPrefix("/search?"); searcher->setContentHumanReadableId(humanReadableId); searchers[humanReadableId] = searcher; + } catch (...) { + cerr << "Unable to open the search index '" << indexPath << "'." << endl; } } } @@ -618,18 +606,18 @@ int main(int argc, char **argv) { libraryManager.getBookById(*itr, currentBook); if (!currentBook.path.empty() && readers.find(currentBook.getHumanReadableIdFromPath()) != readers.end()) { - welcomeBooksHtml += "

" + currentBook.title + "

\ - \ - \ + welcomeBooksHtml += "

" + currentBook.title + "

\n \ +
\n \ + \n \ \ -
" + currentBook.description + "
" + - " \ - \ - \ -
Size: " + kiwix::beautifyFileSize(atoi(currentBook.size.c_str())) + " (" + kiwix::beautifyInteger(atoi(currentBook.articleCount.c_str())) + " articles, " + kiwix::beautifyInteger(atoi(currentBook.mediaCount.c_str())) + " medias) \ - Created: " + currentBook.date + "
Author: " + currentBook.creator + "Language: " + currentBook.language + "
Publisher: " + (currentBook.publisher.empty() ? "unknown" : currentBook.publisher ) + "
\ -
"; + "Size: " + kiwix::beautifyFileSize(atoi(currentBook.size.c_str())) + " (" + kiwix::beautifyInteger(atoi(currentBook.articleCount.c_str())) + " articles, " + kiwix::beautifyInteger(atoi(currentBook.mediaCount.c_str())) + " medias)\n \ + Date: " + currentBook.date + "
\n \ + Author: " + currentBook.creator + "Language: " + currentBook.language + "\n \ + Publisher: " + (currentBook.publisher.empty() ? "unknown" : currentBook.publisher ) + "\n \ + \n \ + \n \ + \n\n"; } } @@ -645,6 +633,7 @@ int main(int argc, char **argv) { if (pid < 0) { exit(1); } + /* If we got a good PID, then we can exit the parent process. */ if (pid > 0) { @@ -665,24 +654,24 @@ int main(int argc, char **argv) { /* Hard coded mimetypes */ extMimeTypes["html"] = "text/html"; - extMimeTypes["htm"] = "text/html"; - extMimeTypes["png"] = "image/png"; + extMimeTypes["htm"] = "text/html"; + extMimeTypes["png"] = "image/png"; extMimeTypes["tiff"] = "image/tiff"; - extMimeTypes["tif"] = "image/tiff"; + extMimeTypes["tif"] = "image/tiff"; extMimeTypes["jpeg"] = "image/jpeg"; - extMimeTypes["jpg"] = "image/jpeg"; - extMimeTypes["gif"] = "image/gif"; - extMimeTypes["svg"] = "image/svg+xml"; - extMimeTypes["txt"] = "text/plain"; - extMimeTypes["xml"] = "text/xml"; - extMimeTypes["pdf"] = "application/pdf"; - extMimeTypes["ogg"] = "application/ogg"; - extMimeTypes["js"] = "application/javascript"; - extMimeTypes["css"] = "text/css"; - extMimeTypes["otf"] = "application/vnd.ms-opentype"; - extMimeTypes["ttf"] = "application/font-ttf"; + extMimeTypes["jpg"] = "image/jpeg"; + extMimeTypes["gif"] = "image/gif"; + extMimeTypes["svg"] = "image/svg+xml"; + extMimeTypes["txt"] = "text/plain"; + extMimeTypes["xml"] = "text/xml"; + extMimeTypes["pdf"] = "application/pdf"; + extMimeTypes["ogg"] = "application/ogg"; + extMimeTypes["js"] = "application/javascript"; + extMimeTypes["css"] = "text/css"; + extMimeTypes["otf"] = "application/vnd.ms-opentype"; + extMimeTypes["ttf"] = "application/font-ttf"; extMimeTypes["woff"] = "application/font-woff"; - extMimeTypes["vtt"] = "text/vtt"; + extMimeTypes["vtt"] = "text/vtt"; /* Start the HTTP daemon */ void *page = NULL;