Skip to content

Commit 94230b9

Browse files
rodolphehrodolpheh
andauthored
Export synchronous wget functions (#14)
Co-authored-by: rodolpheh <[email protected]>
1 parent 67f606a commit 94230b9

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/zemscripten.zig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,27 @@ pub fn log(
197197
else => emscripten_console_log(@ptrCast(msg.ptr)),
198198
}
199199
}
200+
201+
// Networking
202+
extern fn emscripten_wget(url: [*c]const u8, file: [*c]const u8) c_int;
203+
extern fn emscripten_wget_data(url: [*c]const u8, pbuffer: **anyopaque, pnum: *c_int, perror: *c_int) void;
204+
205+
pub fn wget(url: [:0]const u8, file: [:0]const u8) !void {
206+
if (emscripten_wget(url.ptr, file.ptr) != 0) {
207+
return error.WgetError;
208+
}
209+
}
210+
211+
pub fn wget_data(url: [:0]const u8) ![]u8 {
212+
var buffer_ptr: *anyopaque = undefined;
213+
var len: c_int = undefined;
214+
var err: c_int = undefined;
215+
216+
emscripten_wget_data(url.ptr, &buffer_ptr, &len, &err);
217+
218+
if (err != 0) {
219+
return error.WgetError;
220+
}
221+
222+
return @as([*]u8, @ptrCast(buffer_ptr))[0..@intCast(len)];
223+
}

0 commit comments

Comments
 (0)