Re: cffi: calls with variable number of arguments determined at runtime



On Thu, 01 Feb 2007 17:50:13 +0100, Pascal Bourguignon wrote:

Lars Rune Nøstdal <larsnostdal@xxxxxxxxx> writes:

cffi> (with-foreign-pointer-as-string (s 100)
(foreign-funcall "sprintf"
:pointer s
:string "i: %d"
:int 1234
:int))
"i: 1234"


;; ..works ok.. same with..


cffi> (defcfun "sprintf" :int
(str :pointer)
(control :string)
&rest)
sprintf
cffi> (with-foreign-pointer-as-string (s 100)
(sprintf s "i: %d" :int 1234))
"i: 1234"


;; but what if i do not know the types and arguments at compile time?
;; these are stupid examples and none of them work, but:


cffi> (with-foreign-pointer-as-string (s 100)
(let ((type-args* '(:int 1234)))
(sprintf s "i: %d" type-args*)))

Obviously, here you know the type, since you wrote %d, not %s or %f!

Now imagine you have this C function:

void format(const char* ctrlstring,int nargs,void** arguments){
...
}

How can you write it in C, if you don't know the type of the
arguments? The same as you'd do in lisp, and the same printf does,
you parse ctrlstring to collect the types.

Ok, but there are cases where there is no format string. Or - what I mean
is, the problem is after the "step" you describe. Even if I at runtime
determine:

* types
* values
* ..and of course then also number of these

...I cannot seem to find a way to build the function call (at runtime) and
call it with CFFI. Hm, does this make sense?

--
Lars Rune Nøstdal
http://nostdal.org/

.



Relevant Pages