From 831740fcacfe67b8124436460afa77af15493f93 Mon Sep 17 00:00:00 2001 From: Christian Segundo Date: Thu, 15 Jun 2023 23:27:24 +0200 Subject: one step closer --- src/main.zig | 37 +++++++++++++++++++++++++++---------- src/request.zig | 4 +--- test.c | 16 ++++++++++------ xmission.h | 3 +++ 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/main.zig b/src/main.zig index bdffa31..a52079a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -76,16 +76,33 @@ export fn c_session_get(client: ?*anyopaque, buf: [*]u8) c_int { return 0; } -//test "c api" { -//const clientOptions = transmission.ClientOptions{ -//.host = "192.168.0.2", -//.port = 9091, -//.https = false, -//}; -//var foo = c_client_init(clientOptions); -//_ = c_session_get(foo, undefined); -//c_client_deinit(foo); -//} +export fn c_torrent_get(client: ?*anyopaque) c_int { + var real_client: *transmission.Client = @ptrCast(*transmission.Client, @alignCast( + @alignOf(transmission.Client), + client.?, + )); + + const body = transmission.torrent_get_raw(real_client, null) catch |err| { + std.debug.print("error: {any}\n", .{err}); + unreachable; + }; + defer allocator.free(body); + + std.debug.print("body: {s}\n", .{body}); + return 0; +} + +test "c api" { + const clientOptions = transmission.ClientOptions{ + .host = "192.168.0.2", + .port = 9091, + .https = false, + }; + var foo = c_client_init(clientOptions); + _ = c_session_get(foo, undefined); + _ = c_torrent_get(foo, undefined); + c_client_deinit(foo); +} test "basic add functionality" { try testing.expect(add(3, 7) == 10); diff --git a/src/request.zig b/src/request.zig index 2633428..478999b 100644 --- a/src/request.zig +++ b/src/request.zig @@ -57,9 +57,7 @@ pub const TorrentIDs = union(enum) { many: []const usize, recently_active, - const Self = @This(); - - pub fn jsonStringify(self: Self, options: std.json.StringifyOptions, out_stream: anytype) !void { + pub fn jsonStringify(self: TorrentIDs, options: std.json.StringifyOptions, out_stream: anytype) !void { switch (self) { .single => |v| try std.json.stringify(v, options, out_stream), .many => |v| try std.json.stringify(v, options, out_stream), diff --git a/test.c b/test.c index 13a2af5..9766e8f 100644 --- a/test.c +++ b/test.c @@ -3,11 +3,15 @@ #include int main(int argc, char **argv) { - struct ClientOptions opts; - opts.host = "192.168.0.2"; - opts.port = 9091; + struct ClientOptions opts; + opts.host = "192.168.0.2"; + opts.port = 9091; + opts.https = false; + opts.user = NULL; + opts.passowrd = NULL; - c_client client = c_client_init(opts); - c_client_deinit(client); - return 0; + c_client client = c_client_init(opts); + c_torrent_get(client); + c_client_deinit(client); + return 0; } diff --git a/xmission.h b/xmission.h index be1d8d7..553aff6 100644 --- a/xmission.h +++ b/xmission.h @@ -16,5 +16,8 @@ typedef struct ClientOptions { } ClientOptions_t; c_client c_client_init(ClientOptions_t opts); + +int c_torrent_get(c_client); + void c_client_deinit(c_client); #endif -- cgit v1.2.3