#include "../include/quickmedia/HtmlSearch.h" #include #include #include #include static char* get_file_content(const char *filepath, size_t *filesize) { FILE *file = fopen(filepath, "rb"); assert(file); fseek(file, 0, SEEK_END); *filesize = ftell(file); fseek(file, 0, SEEK_SET); char *buffer = malloc((*filesize) + 1); buffer[*filesize] = '\0'; fread(buffer, 1, *filesize, file); return buffer; } static int result_callback(QuickMediaMatchNode *node, void *userdata) { QuickMediaStringView href = quickmedia_html_node_get_attribute_value(node->node, "href"); QuickMediaStringView text = quickmedia_html_node_get_text(node); printf("a href: %.*s, node value: %.*s\n", (int)href.size, href.data, (int)text.size, text.data); return 0; } static int result_callback_nested_text(QuickMediaMatchNode *node, void *userdata) { QuickMediaStringView text = quickmedia_html_node_get_text(node); printf("text: %.*s\n", (int)text.size, text.data); return 0; } static void test_nested_nodes_get_text() { const char *html_source = "
hello

text

world
"; QuickMediaHtmlSearch html_search; quickmedia_html_search_init(&html_search, html_source, strlen(html_source)); quickmedia_html_find_nodes_xpath(&html_search, "//div[class='item']", result_callback_nested_text, NULL); quickmedia_html_search_deinit(&html_search); } int main() { size_t filesize = 0; char *file_content = get_file_content("test_files/test.html", &filesize); QuickMediaHtmlSearch html_search; int result = quickmedia_html_search_init(&html_search, file_content, filesize); if(result != 0) goto cleanup; result = quickmedia_html_find_nodes_xpath(&html_search, "//h3[class=\"story_name\"]//a", result_callback, NULL); /* Test that the object can be reused without reloading html doc */ result = quickmedia_html_find_nodes_xpath(&html_search, "//h3[class=\"story_name\"]//a", result_callback, NULL); cleanup: quickmedia_html_search_deinit(&html_search); free(file_content); test_nested_nodes_get_text(); return result; }