aboutsummaryrefslogtreecommitdiff
path: root/tests/main.c
blob: ac3a081bcd4d798708395661e5e5b378e9dd2a61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "../include/quickmedia/HtmlSearch.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

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 = "<div class=\"item\">hello<h1>text</h1>world</div>";
    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;
}