Beautify a bit the code.

No real change. Just do less code or use higher level API.
This commit is contained in:
Matthieu Gautier 2017-04-07 00:39:02 +02:00
parent 94670847ef
commit 37f29da63e
1 changed files with 85 additions and 109 deletions

View File

@ -98,12 +98,12 @@ namespace kiwix {
std::map<const std::string, unsigned int> Reader::parseCounterMetadata() const { std::map<const std::string, unsigned int> Reader::parseCounterMetadata() const {
std::map<const std::string, unsigned int> counters; std::map<const std::string, unsigned int> counters;
string content, mimeType, item, counterString; string mimeType, item, counterString;
unsigned int contentLength, counter; unsigned int counter;
string counterUrl = "/M/Counter";
this->getContentByUrl(counterUrl, content, contentLength, mimeType); zim::Article article = this->zimFileHandler->getArticle('M',"Counter");
stringstream ssContent(content);
stringstream ssContent(article.getData());
while(getline(ssContent, item, ';')) { while(getline(ssContent, item, ';')) {
stringstream ssItem(item); stringstream ssItem(item);
@ -175,15 +175,13 @@ namespace kiwix {
/* Return a page url from a title */ /* Return a page url from a title */
bool Reader::getPageUrlFromTitle(const string &title, string &url) const { bool Reader::getPageUrlFromTitle(const string &title, string &url) const {
/* Extract the content from the zim file */ /* Extract the content from the zim file */
std::pair<bool, zim::File::const_iterator> resultPair = zimFileHandler->findxByTitle('A', title); zim::Article article = this->zimFileHandler->getArticleByTitle('A', title);
/* Test if the article was found */ if ( ! article.good() )
if (resultPair.first == true) { {
return false;
}
/* Get the article */
zim::Article article = *resultPair.second;
/* If redirect */
unsigned int loopCounter = 0; unsigned int loopCounter = 0;
while (article.isRedirect() && loopCounter++<42) { while (article.isRedirect() && loopCounter++<42) {
article = article.getRedirectArticle(); article = article.getRedirectArticle();
@ -193,9 +191,6 @@ namespace kiwix {
return true; return true;
} }
return false;
}
/* Return an URL from a title*/ /* Return an URL from a title*/
string Reader::getRandomPageUrl() const { string Reader::getRandomPageUrl() const {
zim::Article article; zim::Article article;
@ -208,7 +203,7 @@ namespace kiwix {
article = zimFileHandler->getArticle(idx); article = zimFileHandler->getArticle(idx);
} while (article.getLongUrl() == mainPageUrl); } while (article.getLongUrl() == mainPageUrl);
return article.getLongUrl().c_str(); return article.getLongUrl();
} }
/* Return the welcome page URL */ /* Return the welcome page URL */
@ -358,9 +353,7 @@ namespace kiwix {
string Reader::getFirstPageUrl() const { string Reader::getFirstPageUrl() const {
zim::size_type firstPageOffset = zimFileHandler->getNamespaceBeginOffset('A'); zim::size_type firstPageOffset = zimFileHandler->getNamespaceBeginOffset('A');
zim::Article article = zimFileHandler->getArticle(firstPageOffset); zim::Article article = zimFileHandler->getArticle(firstPageOffset);
url = article.getLongUrl(); return article.getLongUrl();
return url;
} }
bool Reader::parseUrl(const string &url, char *ns, string &title) const { bool Reader::parseUrl(const string &url, char *ns, string &title) const {
@ -394,56 +387,44 @@ namespace kiwix {
/* Return article by url */ /* Return article by url */
bool Reader::getArticleObjectByDecodedUrl(const string &url, zim::Article &article) const { bool Reader::getArticleObjectByDecodedUrl(const string &url, zim::Article &article) const {
bool retVal = false; if (this->zimFileHandler == NULL) {
return false;
if (this->zimFileHandler != NULL) { }
/* Parse the url */ /* Parse the url */
char ns = 0; char ns = 0;
string titleStr; string urlStr;
this->parseUrl(url, &ns, titleStr); this->parseUrl(url, &ns, urlStr);
/* Main page */ /* Main page */
if (titleStr.empty() && ns == 0) { if (urlStr.empty() && ns == 0) {
this->parseUrl(this->getMainPageUrl(), &ns, titleStr); this->parseUrl(this->getMainPageUrl(), &ns, urlStr);
} }
/* Extract the content from the zim file */ /* Extract the content from the zim file */
std::pair<bool, zim::File::const_iterator> resultPair = zimFileHandler->findx(ns, titleStr); article = zimFileHandler->getArticle(ns, urlStr);
return article.good();
/* Test if the article was found */
if (resultPair.first == true) {
article = zimFileHandler->getArticle(resultPair.second.getIndex());
retVal = true;
}
}
return retVal;
} }
/* Return the mimeType without the content */ /* Return the mimeType without the content */
bool Reader::getMimeTypeByUrl(const string &url, string &mimeType) const { bool Reader::getMimeTypeByUrl(const string &url, string &mimeType) const {
bool retVal = false; if (this->zimFileHandler == NULL) {
return false;
if (this->zimFileHandler != NULL) { }
zim::Article article; zim::Article article;
if (this->getArticleObjectByDecodedUrl(url, article)) { if (this->getArticleObjectByDecodedUrl(url, article)) {
try { try {
mimeType = string(article.getMimeType().data(), article.getMimeType().size()); mimeType = article.getMimeType();
} catch (exception &e) { } catch (exception &e) {
cerr << "Unable to get the mimetype for " << url << ":" << e.what() << endl; cerr << "Unable to get the mimetype for " << url << ":" << e.what() << endl;
mimeType = "application/octet-stream"; mimeType = "application/octet-stream";
} }
retVal = true; return true;
} else { } else {
mimeType = ""; mimeType = "";
return false;
} }
}
return retVal;
} }
/* Get a content from a zim file */ /* Get a content from a zim file */
@ -466,14 +447,14 @@ namespace kiwix {
} }
bool Reader::getContentByDecodedUrl(const string &url, string &content, unsigned int &contentLength, string &contentType, string &baseUrl) const { bool Reader::getContentByDecodedUrl(const string &url, string &content, unsigned int &contentLength, string &contentType, string &baseUrl) const {
bool retVal = false;
content=""; content="";
contentType=""; contentType="";
contentLength = 0; contentLength = 0;
if (this->zimFileHandler != NULL) {
zim::Article article; zim::Article article;
if (this->getArticleObjectByDecodedUrl(url, article)) { if ( ! this->getArticleObjectByDecodedUrl(url, article)) {
return false;
}
/* If redirect */ /* If redirect */
unsigned int loopCounter = 0; unsigned int loopCounter = 0;
@ -507,12 +488,7 @@ namespace kiwix {
/* Get the data length */ /* Get the data length */
contentLength = article.getArticleSize(); contentLength = article.getArticleSize();
/* Set return value */ return true;
retVal = true;
}
}
return retVal;
} }
/* Check if an article exists */ /* Check if an article exists */