Hi,
I’ve got plans for a new project where I’m using the functions from libcurl, so I’m getting to grips with the foreign
system. It generally all seems quite straightforward but I wanted to ask about best practices when importing a function that has a variadic argument list.
The only function that’s given me a headache so far is curl_easy_setopt
where the 3rd parameter is variadic.
My solution currently is to write a wrapper which modifies the way this function is imported based on the argument type:
callback :: #type proc(buffer: cstring, size: u64, nmemb: u64, userdata: rawptr) -> u64
my_curl_easy_setopt :: proc(handle: rawptr, opt: int, param: $T) {
when T == callback {
foreign curl {
curl_easy_setopt :: proc(handle: rawptr, opt: int, param: callback) ---
}
curl_easy_setopt(handle, opt, param)
}
else {
foreign curl {
curl_easy_setopt :: proc(handle: rawptr, opt: int, param: rawptr) ---
}
curl_easy_setopt(handle, opt, param)
}
}
Is this the best approach? I don’t think I’ve seen any samples which do this.
I’ve created a github gist with complete sample code: Odin code that imports libcurl functions · GitHub