diff --git a/include/tools/otherTools.h b/include/tools/otherTools.h index a6dc47234..2f95f3fc2 100644 --- a/include/tools/otherTools.h +++ b/include/tools/otherTools.h @@ -21,6 +21,7 @@ #define KIWIX_OTHERTOOLS_H #include +#include namespace pugi { class xml_node; @@ -28,9 +29,18 @@ namespace pugi { namespace kiwix { -void sleep(unsigned int milliseconds); -std::string nodeToString(const pugi::xml_node& node); -std::string converta2toa3(const std::string& a2code); + void sleep(unsigned int milliseconds); + std::string nodeToString(const pugi::xml_node& node); + std::string converta2toa3(const std::string& a2code); + + /* + * Convert all format tag string to new format + */ + std::vector convertTags(const std::string& tags_str); + std::string getTagValueFromTagList(const std::vector& tagList, + const std::string& tagName); + bool convertStrToBool(const std::string& value); + } #endif diff --git a/src/reader.cpp b/src/reader.cpp index 87cedc550..d6874724a 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -22,6 +22,8 @@ #include +#include "tools/otherTools.h" + inline char hi(char v) { char hex[] = "0123456789abcdef"; @@ -352,43 +354,6 @@ string Reader::getLicense() const METADATA("License") } -std::vector convertTags(const std::string& tags_str) -{ - auto tags = split(tags_str, ";"); - std::vector tagsList; - bool picSeen(false), vidSeen(false), detSeen(false), indexSeen(false); - for (auto tag: tags) { - picSeen |= (tag == "nopic" || startsWith(tag, "_pictures:")); - vidSeen |= (tag == "novid" || startsWith(tag, "_videos:")); - detSeen |= (tag == "nodet" || startsWith(tag, "_details:")); - indexSeen |= startsWith(tag, "_ftindex"); - if (tag == "nopic") { - tagsList.push_back("_pictures:no"); - } else if (tag == "novid") { - tagsList.push_back("_videos:no"); - } else if (tag == "nodet") { - tagsList.push_back("_details:no"); - } else if (tag == "_ftindex") { - tagsList.push_back("_ftindex:yes"); - } else { - tagsList.push_back(tag); - } - } - if (!indexSeen) { - tagsList.push_back("_ftindex:no"); - } - if (!picSeen) { - tagsList.push_back("_pictures:yes"); - } - if (!vidSeen) { - tagsList.push_back("_videos:yes"); - } - if (!detSeen) { - tagsList.push_back("_details:yes"); - } - return tagsList; -} - string Reader::getTags(bool original) const { string tags_str; @@ -400,26 +365,6 @@ string Reader::getTags(bool original) const return join(tags, ";"); } -string getTagValueFromTagList(const std::vector& tagList, const std::string& tagName) -{ - for (auto tag: tagList) { - if (tag[0] == '_') { - auto delimPos = tag.find(':'); - if (delimPos == string::npos) { - // No delimiter... what to do ? - continue; - } - auto cTagName = tag.substr(1, delimPos-1); - auto cTagValue = tag.substr(delimPos+1); - if (cTagName == tagName) { - return cTagValue; - } - } - } - std::stringstream ss; - ss << tagName << " cannot be found"; - throw std::out_of_range(ss.str()); -} string Reader::getTagStr(const std::string& tagName) const { @@ -430,16 +375,7 @@ string Reader::getTagStr(const std::string& tagName) const bool Reader::getTagBool(const std::string& tagName) const { - auto tagValue = getTagStr(tagName); - if (tagValue == "yes") { - return true; - } else if (tagValue == "no") { - return false; - } else { - std::stringstream ss; - ss << "Tag value '" << tagValue << "' for " << tagName << " cannot be converted to bool."; - throw std::domain_error(ss.str()); - } + return convertStrToBool(getTagStr(tagName)); } string Reader::getRelation() const diff --git a/src/tools/otherTools.cpp b/src/tools/otherTools.cpp index 538d87104..e4ae850be 100644 --- a/src/tools/otherTools.cpp +++ b/src/tools/otherTools.cpp @@ -25,7 +25,10 @@ #include #endif +#include "tools/stringTools.h" + #include +#include #include @@ -204,3 +207,76 @@ std::string kiwix::nodeToString(const pugi::xml_node& node) std::string kiwix::converta2toa3(const std::string& a2code){ return codeisomapping.at(a2code); } + +std::vector kiwix::convertTags(const std::string& tags_str) +{ + auto tags = kiwix::split(tags_str, ";"); + std::vector tagsList; + bool picSeen(false), vidSeen(false), detSeen(false), indexSeen(false); + for (auto tag: tags) { + picSeen |= (tag == "nopic" || startsWith(tag, "_pictures:")); + vidSeen |= (tag == "novid" || startsWith(tag, "_videos:")); + detSeen |= (tag == "nodet" || startsWith(tag, "_details:")); + indexSeen |= kiwix::startsWith(tag, "_ftindex"); + if (tag == "nopic") { + tagsList.push_back("_pictures:no"); + } else if (tag == "novid") { + tagsList.push_back("_videos:no"); + } else if (tag == "nodet") { + tagsList.push_back("_details:no"); + } else if (tag == "_ftindex") { + tagsList.push_back("_ftindex:yes"); + } else { + tagsList.push_back(tag); + } + } + if (!indexSeen) { + tagsList.push_back("_ftindex:no"); + } + if (!picSeen) { + tagsList.push_back("_pictures:yes"); + } + if (!vidSeen) { + tagsList.push_back("_videos:yes"); + } + if (!detSeen) { + tagsList.push_back("_details:yes"); + } + return tagsList; +} + +std::string kiwix::getTagValueFromTagList( + const std::vector& tagList, const std::string& tagName) +{ + for (auto tag: tagList) { + if (tag[0] == '_') { + auto delimPos = tag.find(':'); + if (delimPos == std::string::npos) { + // No delimiter... what to do ? + continue; + } + auto cTagName = tag.substr(1, delimPos-1); + auto cTagValue = tag.substr(delimPos+1); + if (cTagName == tagName) { + return cTagValue; + } + } + } + std::stringstream ss; + ss << tagName << " cannot be found"; + throw std::out_of_range(ss.str()); +} + +bool kiwix::convertStrToBool(const std::string& value) +{ + if (value == "yes") { + return true; + } else if (value == "no") { + return false; + } + + std::stringstream ss; + ss << "Tag value '" << value << "' cannot be converted to bool."; + throw std::domain_error(ss.str()); +} +