aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-05-25 02:48:33 +0200
committerdec05eba <dec05eba@protonmail.com>2020-07-06 06:55:04 +0200
commit2f9cf2385a90920b1bcbe6cefa899f33c615e2a5 (patch)
tree7fce39645c4cb580ae4be66a6ed193612bc0d322
parentc17412cce925ce226d3835a2e59b4d9f31b5b3ed (diff)
Cleanup after finished
-rw-r--r--include/quickmedia/NodeSearch.h3
-rw-r--r--src/HtmlSearch.c14
-rw-r--r--src/NodeSearch.c2
-rw-r--r--src/XpathParser.c3
-rw-r--r--tests/main.c3
5 files changed, 13 insertions, 12 deletions
diff --git a/include/quickmedia/NodeSearch.h b/include/quickmedia/NodeSearch.h
index b512296..f06354b 100644
--- a/include/quickmedia/NodeSearch.h
+++ b/include/quickmedia/NodeSearch.h
@@ -4,6 +4,7 @@
typedef struct {
char *name;
char *value;
+ int defined;
} QuickMediaNodeSearchParam;
typedef struct QuickMediaNodeSearch QuickMediaNodeSearch;
@@ -12,8 +13,6 @@ struct QuickMediaNodeSearch {
char *name; /* optional */
int recursive;
QuickMediaNodeSearchParam param; /* optional */
- int param_defined;
-
QuickMediaNodeSearch *child; /* optional */
};
diff --git a/src/HtmlSearch.c b/src/HtmlSearch.c
index e59dc1e..c3608dd 100644
--- a/src/HtmlSearch.c
+++ b/src/HtmlSearch.c
@@ -41,7 +41,7 @@ static void find_child_nodes(TidyDoc tdoc, TidyNode node, const QuickMediaNodeSe
} while(0)
/* If we search without param, then it's a match */
- if(!search_data->param_defined) {
+ if(!search_data->param.defined) {
on_match();
continue;
}
@@ -123,8 +123,12 @@ static int quickmedia_html_find_nodes(const char *html_source, QuickMediaNodeSea
int quickmedia_html_find_nodes_xpath(const char *html_source, const char *xpath, QuickMediaHtmlSearchResultCallback result_callback, void *userdata) {
QuickMediaNodeSearch search_data;
- int xpath_result = quickmedia_parse_xpath(xpath, &search_data);
- if(xpath_result != 0)
- return xpath_result;
- return quickmedia_html_find_nodes(html_source, &search_data, result_callback, userdata);
+ 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);
+ cleanup:
+ quickmedia_node_search_deinit(&search_data);
+ return result;
}
diff --git a/src/NodeSearch.c b/src/NodeSearch.c
index 198b8cd..0a36215 100644
--- a/src/NodeSearch.c
+++ b/src/NodeSearch.c
@@ -4,6 +4,7 @@
void quickmedia_node_search_param_init(QuickMediaNodeSearchParam *self) {
self->name = NULL;
self->value = NULL;
+ self->defined = 0;
}
static void quickmedia_node_search_param_deinit(QuickMediaNodeSearchParam *self) {
@@ -17,7 +18,6 @@ void quickmedia_node_search_init(QuickMediaNodeSearch *self) {
self->name = NULL;
self->recursive = 0;
quickmedia_node_search_param_init(&self->param);
- self->param_defined = 0;
self->child = NULL;
}
diff --git a/src/XpathParser.c b/src/XpathParser.c
index 24e1d6e..4326e85 100644
--- a/src/XpathParser.c
+++ b/src/XpathParser.c
@@ -35,6 +35,7 @@ static int xpath_parse_param(QuickMediaXpathParser *self, QuickMediaNodeSearchPa
if(token != QUICKMEDIA_XPATH_TOKEN_CLOSING_BRACKET)
return -4;
+ result->defined = 1;
return 0;
}
@@ -55,8 +56,6 @@ static int xpath_parse_node(QuickMediaXpathParser *self, QuickMediaNodeSearch *r
if(param_result < 0) {
quickmedia_node_search_deinit(result);
return param_result;
- } else if(param_result == 0) {
- result->param_defined = 1;
}
result->child = malloc(sizeof(QuickMediaNodeSearch));
diff --git a/tests/main.c b/tests/main.c
index eb1abc7..5b697a9 100644
--- a/tests/main.c
+++ b/tests/main.c
@@ -24,8 +24,7 @@ static void result_callback(QuickMediaHtmlNode *node, void *userdata) {
printf("a href: %s, node value: %.*s\n", href, text.size, text.data);
}
-int main(int argc, char **argv)
-{
+int main(int argc, char **argv) {
char *file_content = get_file_content("test_files/test.html");
int result = quickmedia_html_find_nodes_xpath(file_content, "//h3[class=\"story_name\"]//a", result_callback, NULL);
free(file_content);