3
0
Fork 0

Few simplification/beautification of the code

small_fixes
kelson42 11 years ago
parent 11a5594816
commit 63ec186e37

@ -192,9 +192,11 @@ static int accessHandlerCallback(void *cls,
const char* tmpGetValue = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "content"); const char* tmpGetValue = MHD_lookup_connection_value(connection, MHD_GET_ARGUMENT_KIND, "content");
humanReadableBookId = (tmpGetValue != NULL ? string(tmpGetValue) : ""); humanReadableBookId = (tmpGetValue != NULL ? string(tmpGetValue) : "");
} else { } 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()) { 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) { if (libraryFlag) {
vector<string> libraryPaths = kiwix::split(libraryPath, ";"); vector<string> libraryPaths = kiwix::split(libraryPath, ";");
vector<string>::iterator itr; vector<string>::iterator itr;
for ( itr = libraryPaths.begin(); itr != libraryPaths.end(); ++itr ) { for ( itr = libraryPaths.begin(); itr != libraryPaths.end(); ++itr ) {
if (!itr->empty()) { if (!itr->empty()) {
bool retVal = false; 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; cerr << "Unable to add the ZIM file '" << zimPath << "' to the internal library." << endl;
exit(1); exit(1);
} else if (!indexPath.empty()) { } else if (!indexPath.empty()) {
vector<string> booksIds = libraryManager.getBooksIds();
kiwix::supportedIndexType indexType = kiwix::UNKNOWN;
/* Try with the XapianSearcher */
try { try {
new kiwix::XapianSearcher(indexPath); 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) */ /* Instanciate the ZIM index (if necessary) */
kiwix::Searcher *searcher = NULL; kiwix::Searcher *searcher = NULL;
if (!indexPath.empty()) { if (!indexPath.empty()) {
bool hasSearchIndex = false;
/* Try to load the search */
try { try {
if (currentBook.indexType == kiwix::XAPIAN) {
searcher = new kiwix::XapianSearcher(indexPath); 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->setProtocolPrefix("/"); searcher->setProtocolPrefix("/");
searcher->setSearchProtocolPrefix("/search?"); searcher->setSearchProtocolPrefix("/search?");
searcher->setContentHumanReadableId(humanReadableId); searcher->setContentHumanReadableId(humanReadableId);
searchers[humanReadableId] = searcher; 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); libraryManager.getBookById(*itr, currentBook);
if (!currentBook.path.empty() && readers.find(currentBook.getHumanReadableIdFromPath()) != readers.end()) { if (!currentBook.path.empty() && readers.find(currentBook.getHumanReadableIdFromPath()) != readers.end()) {
welcomeBooksHtml += "<h3><a href=\"#\">" + currentBook.title + "</a></h3> \ welcomeBooksHtml += "<h3><a href=\"#\">" + currentBook.title + "</a></h3>\n \
<table style=\"overflow-x: hidden; overflow-y: hidden; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;\"><tr> \ <table style=\"overflow-x: hidden; overflow-y: hidden; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;\"><tr>\n \
<td style=\"background-repeat: no-repeat; background-image: url(data:" + currentBook.faviconMimeType+ ";base64," + currentBook.favicon + ")\"><div style=\"width: 50px\"></div></td> \ <td style=\"background-repeat: no-repeat; background-image: url(data:" + currentBook.faviconMimeType+ ";base64," + currentBook.favicon + ")\"><div style=\"width: 50px\"></div></td>\n \
<td style=\"width: 100%;\">" + currentBook.description + <td style=\"width: 100%;\">" + currentBook.description +
"<br/><table style=\"font-size: small; color: grey; width: 100%;\">" + "<br/><table style=\"font-size: small; color: grey; width: 100%;\">" +
"<tr><td style=\"width: 50%\">Size: " + kiwix::beautifyFileSize(atoi(currentBook.size.c_str())) + " (" + kiwix::beautifyInteger(atoi(currentBook.articleCount.c_str())) + " articles, " + kiwix::beautifyInteger(atoi(currentBook.mediaCount.c_str())) + " medias) \ "<tr><td style=\"width: 50%\">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 \
</td><td>Created: " + currentBook.date + "</td><td style=\"vertical-align: bottom;\" rowspan=\"3\"><form action=\"/" + currentBook.getHumanReadableIdFromPath() + "/\" method=\"GET\"><input style=\"align: right; right: 0px; float:right; width: 100%; height: 60px; font-weight: bold;\" type=\"submit\" value=\"Load\" /></form></td></tr> \ </td><td>Date: " + currentBook.date + "</td><td style=\"vertical-align: bottom;\" rowspan=\"3\"><form action=\"/" + currentBook.getHumanReadableIdFromPath() + "/\" method=\"GET\"><input style=\"align: right; right: 0px; float:right; width: 100%; height: 60px; font-weight: bold;\" type=\"submit\" value=\"Load\" /></form></td></tr>\n \
<tr><td>Author: " + currentBook.creator + "</td><td>Language: " + currentBook.language + "</td></tr> \ <tr><td>Author: " + currentBook.creator + "</td><td>Language: " + currentBook.language + "</td></tr>\n \
<tr><td>Publisher: " + (currentBook.publisher.empty() ? "unknown" : currentBook.publisher ) + "</td><td></td></tr> \ <tr><td>Publisher: " + (currentBook.publisher.empty() ? "unknown" : currentBook.publisher ) + "</td><td></td></tr>\n \
</table> \ </table>\n \
</td></tr> \ </td></tr>\n \
</table>"; </table>\n\n";
} }
} }
@ -645,6 +633,7 @@ int main(int argc, char **argv) {
if (pid < 0) { if (pid < 0) {
exit(1); exit(1);
} }
/* If we got a good PID, then /* If we got a good PID, then
we can exit the parent process. */ we can exit the parent process. */
if (pid > 0) { if (pid > 0) {

Loading…
Cancel
Save