-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
improve translate-c for libcurl simple example #988
Comments
You can use libcurl in zig too. I wonder how far this will get you:
|
not very far :(
|
this issue is a duplicate of #910 but I'm going to repurpose it to be an issue to improve translate-c of this curl example. |
This comment has been minimized.
This comment has been minimized.
The curl sample file now fails with:
|
The example above now translates to: pub export fn main() c_int {
var curl: ?*CURL = undefined;
var res: CURLcode = undefined;
curl = curl_easy_init();
if (curl != null) {
_ = curl_easy_setopt(curl, @intToEnum(CURLoption, CURLOPT_URL), "https://example.com");
_ = curl_easy_setopt(curl, @intToEnum(CURLoption, CURLOPT_FOLLOWLOCATION), @as(c_long, 1));
res = curl_easy_perform(curl);
if (@enumToInt(res) != @bitCast(c_uint, CURLE_OK)) _ = fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
return 0;
} It would be nice if Zig could figure out to improve the enum handling in this way instead: pub export fn main() c_int {
var curl: ?*CURL = undefined;
var res: CURLcode = undefined;
curl = curl_easy_init();
if (curl != null) {
_ = curl_easy_setopt(curl, .CURLOPT_URL, "https://example.com");
_ = curl_easy_setopt(curl, .CURLOPT_FOLLOWLOCATION, @as(c_long, 1));
res = curl_easy_perform(curl);
if (res != .CURLE_OK) _ = fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
return 0;
} But I think it's safe to say that this issue is solved. |
Just wanted to comment here for anyone trying to use libcurl that enum handling already works in the way @andrewrk mentioned. const std = @import("std");
const c = @cImport(@cInclude("curl/curl.h"));
fn wrap(result: anytype) !void {
switch (@enumToInt(result)) {
c.CURLE_OK => return,
else => unreachable,
}
}
pub fn main() !void {
const curl = c.curl_easy_init();
if (curl == null) return error.InitFailed;
defer c.curl_easy_cleanup(curl);
try wrap(c.curl_easy_setopt(curl, .CURLOPT_URL, "https://client.tlsfingerprint.io:8443/"));
try wrap(c.curl_easy_setopt(curl, .CURLOPT_FOLLOWLOCATION, @as(c_long, 1)));
if (c.curl_easy_perform(curl) == .CURLE_OK) {
std.debug.print("It works!\n", .{});
}
} |
For example, with C you can do this:
https://github.com/curl/curl/blob/master/docs/examples/simple.c
and other languages:
http://rosettacode.org/wiki/HTTP
The text was updated successfully, but these errors were encountered: