blob: 4fed96a45f505e232fb6d4a1d33c50bc0c77fab6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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;
}
|