From bc0b7338ed2d4c126c15eaf57aeb84655e0e1cc3 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 25 May 2019 03:58:20 +0200 Subject: Allow reusing doc for multiple xpath searches --- src/HtmlSearch.c | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/HtmlSearch.c b/src/HtmlSearch.c index c3608dd..7868f32 100644 --- a/src/HtmlSearch.c +++ b/src/HtmlSearch.c @@ -99,35 +99,42 @@ const QuickMediaStringView quickmedia_html_node_get_text(QuickMediaHtmlNode *sel return string_view; } -static int quickmedia_html_find_nodes(const char *html_source, QuickMediaNodeSearch *search_data, QuickMediaHtmlSearchResultCallback result_callback, void *userdata) { - assert(html_source); +static int quickmedia_html_find_nodes(QuickMediaHtmlSearch *self, QuickMediaNodeSearch *search_data, QuickMediaHtmlSearchResultCallback result_callback, void *userdata) { assert(search_data); assert(result_callback); - if(!html_source || !search_data || !result_callback) + if(!search_data || !result_callback) return -1; + + TidyNode root_node = tidyGetRoot(self->doc); + find_child_nodes(self->doc, root_node, search_data, result_callback, userdata); + return 0; +} - TidyDoc tdoc = tidyCreate(); - tidyOptSetBool(tdoc, TidyShowWarnings, no); - /* tidyOptSetBool(tdoc, TidyForceOutput, yes); */ - int rc = tidyParseString( tdoc, html_source); - if(rc < 0) { - tidyRelease(tdoc); - return rc; +int quickmedia_html_search_init(QuickMediaHtmlSearch *self, const char *html_source) { + self->doc = tidyCreate(); + tidyOptSetBool(self->doc, TidyShowWarnings, no); + /* tidyOptSetBool(self->doc, TidyForceOutput, yes); */ + if(tidyParseString(self->doc, html_source) < 0) { + tidyRelease(self->doc); + self->doc = NULL; } - - TidyNode root_node = tidyGetRoot(tdoc); - find_child_nodes(tdoc, root_node, search_data, result_callback, userdata); - tidyRelease(tdoc); return 0; } -int quickmedia_html_find_nodes_xpath(const char *html_source, const char *xpath, QuickMediaHtmlSearchResultCallback result_callback, void *userdata) { +void quickmedia_html_search_deinit(QuickMediaHtmlSearch *self) { + if(self->doc) { + tidyRelease(self->doc); + self->doc = NULL; + } +} + +int quickmedia_html_find_nodes_xpath(QuickMediaHtmlSearch *self, const char *xpath, QuickMediaHtmlSearchResultCallback result_callback, void *userdata) { QuickMediaNodeSearch search_data; quickmedia_node_search_init(&search_data); int result = quickmedia_parse_xpath(xpath, &search_data); if(result != 0) goto cleanup; - result = quickmedia_html_find_nodes(html_source, &search_data, result_callback, userdata); + result = quickmedia_html_find_nodes(self, &search_data, result_callback, userdata); cleanup: quickmedia_node_search_deinit(&search_data); return result; -- cgit v1.2.3