diff --git a/include/tools/otherTools.h b/include/tools/otherTools.h index 2f95f3fc..7f80dbb9 100644 --- a/include/tools/otherTools.h +++ b/include/tools/otherTools.h @@ -22,6 +22,8 @@ #include #include +#include +#include namespace pugi { class xml_node; @@ -41,6 +43,8 @@ namespace kiwix const std::string& tagName); bool convertStrToBool(const std::string& value); + using MimeCounterType = std::map; + MimeCounterType parseMimetypeCounter(const std::string& counterData); } #endif diff --git a/src/reader.cpp b/src/reader.cpp index 8d6b5a47..bda070cc 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -103,29 +103,16 @@ zim::File* Reader::getZimFileHandler() const { return this->zimFileHandler; } -std::map Reader::parseCounterMetadata() const -{ - std::map counters; - string mimeType, item, counterString; - unsigned int counter; +MimeCounterType Reader::parseCounterMetadata() const +{ zim::Article article = this->zimFileHandler->getArticle('M', "Counter"); if (article.good()) { - stringstream ssContent(article.getData()); - - while (getline(ssContent, item, ';')) { - stringstream ssItem(item); - getline(ssItem, mimeType, '='); - getline(ssItem, counterString, '='); - if (!counterString.empty() && !mimeType.empty()) { - sscanf(counterString.c_str(), "%u", &counter); - counters.insert(pair(mimeType, counter)); - } - } + return parseMimetypeCounter(article.getData()); } - return counters; + return MimeCounterType(); } /* Get the count of articles which can be indexed/displayed */ diff --git a/src/tools/otherTools.cpp b/src/tools/otherTools.cpp index e4ae850b..7d61f7a8 100644 --- a/src/tools/otherTools.cpp +++ b/src/tools/otherTools.cpp @@ -280,3 +280,24 @@ bool kiwix::convertStrToBool(const std::string& value) throw std::domain_error(ss.str()); } + +kiwix::MimeCounterType kiwix::parseMimetypeCounter(const std::string& counterData) +{ + kiwix::MimeCounterType counters; + std::string mimeType, item, counterString; + unsigned int counter; + + std::stringstream ssContent(counterData); + + while (getline(ssContent, item, ';')) { + std::stringstream ssItem(item); + getline(ssItem, mimeType, '='); + getline(ssItem, counterString, '='); + if (!counterString.empty() && !mimeType.empty()) { + sscanf(counterString.c_str(), "%u", &counter); + counters.insert(std::pair(mimeType, counter)); + } + } + + return counters; +}