aboutsummaryrefslogtreecommitdiff
path: root/src/Utility/XmlHelper.c
diff options
context:
space:
mode:
authorfrosty <gabriel@bwaaa.monster>2026-03-12 18:05:09 -0400
committerfrosty <gabriel@bwaaa.monster>2026-03-12 18:05:09 -0400
commit0d65dcd24c8090dcc719be599cd3ef4dc2220e9b (patch)
tree4fc3eaf09d7a41b6b96ccee9637b2e8bdff77f6c /src/Utility/XmlHelper.c
parentc802a4784ab70e0a7512dac0419727fdefacd75c (diff)
downloadomnisearch-0d65dcd24c8090dcc719be599cd3ef4dc2220e9b.tar.gz
refactor: put HTTP and XML logic into reusable modules
Diffstat (limited to 'src/Utility/XmlHelper.c')
-rw-r--r--src/Utility/XmlHelper.c65
1 files changed, 65 insertions, 0 deletions
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 <stdlib.h>
+#include <string.h>
+
+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;
+}