From 0d65dcd24c8090dcc719be599cd3ef4dc2220e9b Mon Sep 17 00:00:00 2001 From: frosty Date: Thu, 12 Mar 2026 18:05:09 -0400 Subject: refactor: put HTTP and XML logic into reusable modules --- src/Utility/XmlHelper.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/Utility/XmlHelper.c (limited to 'src/Utility/XmlHelper.c') diff --git a/src/Utility/XmlHelper.c b/src/Utility/XmlHelper.c new file mode 100644 index 0000000..4fed96a --- /dev/null +++ b/src/Utility/XmlHelper.c @@ -0,0 +1,65 @@ +#include "XmlHelper.h" +#include +#include + +SearchResult *xml_result_alloc(int count, int max_results) { + if (count <= 0 || max_results <= 0) { + return NULL; + } + int actual = (count < max_results) ? count : max_results; + return (SearchResult *)calloc(actual, sizeof(SearchResult)); +} + +void xml_result_free(SearchResult *results, int count) { + if (!results) { + return; + } + for (int i = 0; i < count; i++) { + free(results[i].url); + free(results[i].title); + free(results[i].snippet); + } + free(results); +} + +xmlXPathObjectPtr xml_xpath_eval(xmlXPathContextPtr ctx, const char *xpath) { + if (!ctx || !xpath) { + return NULL; + } + return xmlXPathEvalExpression((const xmlChar *)xpath, ctx); +} + +char *xml_node_content(xmlNodePtr node) { + if (!node) { + return NULL; + } + char *content = (char *)xmlNodeGetContent(node); + return content; +} + +char *xpath_text(xmlDocPtr doc, const char *xpath) { + if (!doc || !xpath) { + return NULL; + } + + xmlXPathContextPtr ctx = xmlXPathNewContext(doc); + if (!ctx) { + return NULL; + } + + xmlXPathObjectPtr obj = xmlXPathEvalExpression((const xmlChar *)xpath, ctx); + xmlXPathFreeContext(ctx); + + if (!obj || !obj->nodesetval || obj->nodesetval->nodeNr == 0) { + if (obj) + xmlXPathFreeObject(obj); + return NULL; + } + + xmlChar *content = xmlNodeGetContent(obj->nodesetval->nodeTab[0]); + char *result = content ? strdup((char *)content) : NULL; + if (content) + xmlFree(content); + xmlXPathFreeObject(obj); + return result; +} -- cgit v1.2.3