aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/HtmlSearch.c39
1 files changed, 23 insertions, 16 deletions
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;