Few simplification/beautification of the code

This commit is contained in:
kelson42 2014-08-30 10:37:17 -06:00
parent 11a5594816
commit 63ec186e37

View File

@ -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<string> libraryPaths = kiwix::split(libraryPath, ";");
vector<string>::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<string> 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 += "<h3><a href=\"#\">" + currentBook.title + "</a></h3> \
<table style=\"overflow-x: hidden; overflow-y: hidden; margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;\"><tr> \
<td style=\"background-repeat: no-repeat; background-image: url(data:" + currentBook.faviconMimeType+ ";base64," + currentBook.favicon + ")\"><div style=\"width: 50px\"></div></td> \
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>\n \
<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 +
"<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) \
</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> \
<tr><td>Author: " + currentBook.creator + "</td><td>Language: " + currentBook.language + "</td></tr> \
<tr><td>Publisher: " + (currentBook.publisher.empty() ? "unknown" : currentBook.publisher ) + "</td><td></td></tr> \
</table> \
</td></tr> \
</table>";
"<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>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>\n \
<tr><td>Publisher: " + (currentBook.publisher.empty() ? "unknown" : currentBook.publisher ) + "</td><td></td></tr>\n \
</table>\n \
</td></tr>\n \
</table>\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;