From 4407dd12bd6b1abca915ee225c741c5739fc831d Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 28 Oct 2020 14:08:06 +0100 Subject: [PATCH] Move mimetypeCounter parsing in its own function. --- include/tools/otherTools.h | 4 ++++ src/reader.cpp | 21 ++++----------------- src/tools/otherTools.cpp | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/include/tools/otherTools.h b/include/tools/otherTools.h index 2f95f3fc2..7f80dbb9a 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 8d6b5a471..bda070cc3 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 e4ae850be..7d61f7a83 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; +}