diff options
author | dec05eba <dec05eba@protonmail.com> | 2023-08-19 14:21:44 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2023-08-19 14:21:44 +0200 |
commit | 16d19a5f0c77bef5ba8b66f1b1434b13b617a483 (patch) | |
tree | 2c12117aff1bcea46c658e6c803ef6ada7bf2572 | |
parent | 908d2ba2bc4a2c7f18e22df26cda8b619725e853 (diff) |
-rw-r--r-- | src/c.zig | 2 | ||||
-rw-r--r-- | src/example.html | 5 | ||||
-rw-r--r-- | src/main.zig | 26 |
3 files changed, 31 insertions, 2 deletions
@@ -1,3 +1,3 @@ pub usingnamespace @cImport({ - @cInclude("HtmlSearch.h"); + @cInclude("quickmedia/HtmlSearch.h"); });
\ No newline at end of file diff --git a/src/example.html b/src/example.html new file mode 100644 index 0000000..51bc51a --- /dev/null +++ b/src/example.html @@ -0,0 +1,5 @@ +<html> + <body> + <div class="cool">hello world</div> + </body> +</html>
\ No newline at end of file diff --git a/src/main.zig b/src/main.zig index 050e335..916742f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,5 +1,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]; + } }
\ No newline at end of file |