From 94d6bef402afca218c83c7c7d945e48c054b131e Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Tue, 11 Jul 2023 15:44:27 +0530 Subject: [PATCH] Introduce readCategoriesFromFeed() Added a function to load categories stored in an OPDS stream --- include/tools.h | 9 +++++++++ src/tools/opdsParsingTools.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/include/tools.h b/include/tools.h index 4b8181660..cd415eee4 100644 --- a/include/tools.h +++ b/include/tools.h @@ -28,6 +28,7 @@ namespace kiwix { typedef std::pair LangNameCodePair; typedef std::vector FeedLanguages; +typedef std::vector FeedCategories; /** * Return the current directory. @@ -235,5 +236,13 @@ std::string beautifyFileSize(uint64_t number); * @return vector containing pairs of language code and their corresponding full language name. */ FeedLanguages readLanguagesFromFeed(const std::string& content); + +/** + * Load categories stored in an OPDS stream . + * + * @param content the OPDS stream. + * @return vector containing category strings. + */ +FeedCategories readCategoriesFromFeed(const std::string& content); } #endif // KIWIX_TOOLS_H diff --git a/src/tools/opdsParsingTools.cpp b/src/tools/opdsParsingTools.cpp index 4bedb483b..691c934c3 100644 --- a/src/tools/opdsParsingTools.cpp +++ b/src/tools/opdsParsingTools.cpp @@ -22,6 +22,20 @@ FeedLanguages parseLanguages(const pugi::xml_document& doc) return langs; } +FeedCategories parseCategories(const pugi::xml_document& doc) +{ + pugi::xml_node feedNode = doc.child("feed"); + FeedCategories categories; + + for (pugi::xml_node entryNode = feedNode.child("entry"); entryNode; + entryNode = entryNode.next_sibling("entry")) { + auto title = VALUE("title"); + categories.push_back(title); + } + + return categories; +} + } // unnamed namespace FeedLanguages readLanguagesFromFeed(const std::string& content) @@ -38,4 +52,19 @@ FeedLanguages readLanguagesFromFeed(const std::string& content) return FeedLanguages(); } +FeedCategories readCategoriesFromFeed(const std::string& content) +{ + pugi::xml_document doc; + pugi::xml_parse_result result + = doc.load_buffer((void*)content.data(), content.size()); + + FeedCategories categories; + if (result) { + categories = parseCategories(doc); + return categories; + } + + return categories; +} + } // namespace kiwix