diff options
Diffstat (limited to 'src/lib.c')
-rw-r--r-- | src/lib.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/lib.c b/src/lib.c new file mode 100644 index 0000000..2c3e34a --- /dev/null +++ b/src/lib.c @@ -0,0 +1,33 @@ +static htmlDocPtr parseHtmlDocument(const char * d, const char * b /* base url */) { + if (!b) + b = ""; + htmlParserCtxtPtr parser_context = htmlNewParserCtxt(); + htmlDocPtr document = htmlCtxtReadMemory(parser_context, d, strlen(d), b, NULL /* encoding */, HTML_PARSE_NOWARNING | HTML_PARSE_NOERROR | HTML_PARSE_RECOVER); + htmlFreeParserCtxt(parser_context); + return document; +} +static xmlXPathObjectPtr findNodes(htmlDocPtr document, const char * xpath_query) { + xmlXPathContextPtr xpath_ctx = xmlXPathNewContext(document); + xmlXPathObjectPtr nodes = xmlXPathEvalExpression(BAD_CAST xpath_query, xpath_ctx); + xmlXPathFreeContext(xpath_ctx); + return nodes; +} +typedef void (*node_function_t)(xmlNodePtr node, void * data); +static void eachNode(xmlXPathObjectPtr nodes, node_function_t f, void * data) { + xmlNodeSetPtr nodeset = nodes->nodesetval; + int i, size = nodeset->nodeNr; + for (i = 0; i < size; i++) { + xmlNodePtr cur; + cur = (xmlNodePtr)nodeset->nodeTab[i]; + f(cur, data); + } +} +void printLinkNode(xmlNodePtr node, void * data) { + if (node->type == XML_ELEMENT_NODE) { + xmlAttrPtr href = xmlHasProp(node, BAD_CAST "href"); + if (href) { + printf("-> Link to '%s'\n", xmlGetProp(node, BAD_CAST "href")); + } + } +} + |