diff options
author | Aleksi Lindeman <dec05eba@protonmail.com> | 2019-05-25 23:42:40 +0200 |
---|---|---|
committer | Aleksi Lindeman <dec05eba@protonmail.com> | 2019-05-25 23:42:42 +0200 |
commit | b2fd63ad96469b450eb98a8034073f5a8c1cb95c (patch) | |
tree | 26a921d5e1cec7c001d2384f2a949e1026e6375b | |
parent | c7500d39c6bae0084196ca7a7395ac85fff09928 (diff) |
Fix tests etc
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | include/quickmedia/HtmlSearch.h | 13 | ||||
-rw-r--r-- | src/HtmlSearch.c | 19 | ||||
-rw-r--r-- | tests/main.c | 4 |
4 files changed, 18 insertions, 20 deletions
@@ -1 +1 @@ -Html search using xpath, written in C +Html search using non-standard xpath, written in C. See tests/main.c diff --git a/include/quickmedia/HtmlSearch.h b/include/quickmedia/HtmlSearch.h index 09a17e2..b3b2eaa 100644 --- a/include/quickmedia/HtmlSearch.h +++ b/include/quickmedia/HtmlSearch.h @@ -17,11 +17,17 @@ typedef struct { const void *doc; } QuickMediaHtmlSearch; -/* Returns NULL if attribute doesn't exist or if it doesn't have any value */ +/* + Returns NULL if attribute doesn't exist or if it doesn't have any value. + The result is only valid within the callback function scope. +*/ const char* quickmedia_html_node_get_attribute_value(QuickMediaHtmlNode *self, const char *attribute_name); -/* Returns StringView where data is NULL and size is 0 if node doesn't have any text */ -QuickMediaStringView quickmedia_html_node_get_text(QuickMediaHtmlNode *self); +/* + Returns NULL if the node doesn't have any text. + The result is only valid within the callback function scope. +*/ +const char* quickmedia_html_node_get_text(QuickMediaHtmlNode *self); /* @node is only valid within the callback function scope */ typedef void (*QuickMediaHtmlSearchResultCallback)(QuickMediaHtmlNode *node, void *userdata); @@ -29,6 +35,7 @@ typedef void (*QuickMediaHtmlSearchResultCallback)(QuickMediaHtmlNode *node, voi int quickmedia_html_search_init(QuickMediaHtmlSearch *self, const char *html_source); void quickmedia_html_search_deinit(QuickMediaHtmlSearch *self); +/* Non-standard xpath. Doesn't use '@' symbol for accessing properties */ int quickmedia_html_find_nodes_xpath(QuickMediaHtmlSearch *self, const char *xpath, QuickMediaHtmlSearchResultCallback result_callback, void *userdata); #ifdef __cplusplus diff --git a/src/HtmlSearch.c b/src/HtmlSearch.c index dcbb8f2..abb04d6 100644 --- a/src/HtmlSearch.c +++ b/src/HtmlSearch.c @@ -75,28 +75,19 @@ const char* quickmedia_html_node_get_attribute_value(QuickMediaHtmlNode *self, c return tidyAttrValue(attr); } -QuickMediaStringView quickmedia_html_node_get_text(QuickMediaHtmlNode *self) { - QuickMediaStringView string_view; - string_view.data = NULL; - string_view.size = 0; - - if(self->text) { - string_view.data = (const char*)((TidyBuffer*)self->text)->bp; - string_view.size = ((TidyBuffer*)self->text)->size; - return string_view; - } +const char* quickmedia_html_node_get_text(QuickMediaHtmlNode *self) { + if(self->text) + return (const char*)((TidyBuffer*)self->text)->bp; TidyNode child_node = tidyGetChild(self->node); if(tidyNodeGetType(child_node) != TidyNode_Text) - return string_view; + return NULL; self->text = malloc(sizeof(TidyBuffer)); tidyBufInit(self->text); tidyNodeGetText(self->doc, child_node, self->text); - string_view.data = (const char*)((TidyBuffer*)self->text)->bp; - string_view.size = ((TidyBuffer*)self->text)->size; - return string_view; + return (const char*)((TidyBuffer*)self->text)->bp; } static int quickmedia_html_find_nodes(QuickMediaHtmlSearch *self, QuickMediaNodeSearch *search_data, QuickMediaHtmlSearchResultCallback result_callback, void *userdata) { diff --git a/tests/main.c b/tests/main.c index 4d16ad6..7888ea1 100644 --- a/tests/main.c +++ b/tests/main.c @@ -20,8 +20,8 @@ static char* get_file_content(const char *filepath) { static void result_callback(QuickMediaHtmlNode *node, void *userdata) { const char *href = quickmedia_html_node_get_attribute_value(node, "href"); - QuickMediaStringView text = quickmedia_html_node_get_text(node); - printf("a href: %s, node value: %.*s\n", href, text.size, text.data); + const char *text = quickmedia_html_node_get_text(node); + printf("a href: %s, node value: %s\n", href, text); } int main(int argc, char **argv) { |