16/07/22 11:45:49.87 f6dSUOKk.net
>>7-11 >>25 >>28 >>30-32
定義はmacro使うと楽
macro c(restype, fnc, argtypes)
local args = [symbol("a", n) for n in 1:length(argtypes.args)]
quote
$(esc(fnc))($(args...)) = ccall($(string(fnc)), $restype, $argtypes, $(args...))
end
end
@c UInt32 printf (Ptr{Cchar},)
@c UInt32 printf (Ptr{Cchar}, UInt32,)
printf(pointer(Array{UInt8,1}([65,66,0,67,68,0,0])))
printf("%s\n", pointer(Array{UInt8,1}([65,66,0,67,68,0,0])))
macro m(lib, restype, fnc, argtypes)
local args = [symbol("a", n) for n in 1:length(argtypes.args)]
quote
$(esc(fnc))($(args...)) = ccall(($(string(fnc)), $(Expr(:quote, lib))), $restype, $argtypes, $(args...))
end
end
@m libcurl Cint curl_free (Ptr{Void},)
@m libcurl Ptr{Cchar} curl_getenv (Ptr{Cchar},)
@m libcurl Ptr{Cchar} curl_version ()
v = curl_getenv("TZ")
printf(v)
curl_free(v)
printf(curl_version())