aboutsummaryrefslogtreecommitdiff
path: root/src/main.zig
blob: 916742f7ad540bc27e6e25a7649de186943427bc (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
const std = @import("std");
const c = @import("c.zig");

const example = @embedFile("example.html");

pub fn main() !void {
    var html_search: c.QuickMediaHtmlSearch = undefined;
    if(c.quickmedia_html_search_init(&html_search, example.ptr, example.len) != 0)
        return error.FailedToInitHtmlSearch;
    defer c.quickmedia_html_search_deinit(&html_search);

    if(c.quickmedia_html_find_nodes_xpath(&html_search, "//div[class='cool']", resultCallback, null) != 0)
        return error.FailedToFindNodes;
}

fn resultCallback(node: [*c]c.QuickMediaMatchNode, userdata: ?*anyopaque) callconv(.C) c_int {
    _ = userdata;
    const text = stringViewToSlice(c.quickmedia_html_node_get_text(node));
    std.debug.print("found match, text: {s}\n", .{text});
    return 0;
}

fn stringViewToSlice(view: c.QuickMediaStringView) []const u8 {
    if(view.size == 0) {
        return "";
    } else {
        return view.data[0..view.size];
    }
}