Next: , Previous: Tutorial-Callbacks, Up: Tutorial


4.10 A complete FFI?

With all options finally set and a medium-level interface developed, we can finish the definition and retrieve http://www.cliki.net/CFFI, as is done in the tutorial.

  (defcfun "curl_easy_perform" curl-code
    (handle easy-handle))
  cffi-user> (with-output-to-string (contents)
               (let ((*easy-write-procedure*
                       (lambda (string)
                         (write-string string contents))))
                 (declare (special *easy-write-procedure*))
                 (curl-easy-perform *easy-handle*)))
  => "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"
  ...
  Now fear, comprehensively</P>
  "

Of course, that itself is slightly unwieldy, so you may want to define a function around it that simply retrieves a URL. I will leave synthesis of all the relevant REPL forms presented thus far into a single function as an exercise for the reader.

The remaining sections of this tutorial explore some advanced features of CFFI; the definition of new types will receive special attention. Some of these features are essential for particular foreign function calls; some are very helpful when trying to develop a Lispy interface to C.