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



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.



--
__Pascal Bourguignon__ http://www.informatimago.com/

NEW GRAND UNIFIED THEORY DISCLAIMER: The manufacturer may
technically be entitled to claim that this product is
ten-dimensional. However, the consumer is reminded that this
confers no legal rights above and beyond those applicable to
three-dimensional objects, since the seven new dimensions are
"rolled up" into such a small "area" that they cannot be
detected.
.



Relevant Pages