From: Rob Malouf

Subject: Re: Sharing streams with foreign (C) functions

Date: 1998-2-24 16:12

> Is it possible to share/send a stream with a foreign function? I > would like to have with-open-file open a file, then give the stream > (or some mapping of that stream) to a C function that expects a (C) > stream (e.g., something that would result from the C function fopen). > This C function would put output to that stream and then return to the > calling CL function.
Here's what I've been using to do just that. So far I haven't had any trouble with it, but I make no guarantees. The basic ideas is to flush out all pending output to the stream, get its file descriptor, duplicate it, open it as a stdio stream, do the I/O, and close the stream. Rob Malouf <stanford.edu at malouf> ====================================== (ff:defforeign-list '((dup :arguments (integer) :return-type :integer :unconverted-entry-name "dup") (fdopen :arguments (integer string) :return-type :integer :unconverted-entry-name "fdopen") (fclose :arguments (integer) :return-type :void :unconverted-entry-name "fclose") (c-image-gif :arguments (integer integer) :return-type :void :unconverted-entry-name "gdImageGif"))) (defun image-gif (image stream) (force-output stream) (let* ((fd (dup (excl:stream-output-fn stream))) (out (fdopen fd "w"))) (c-image-gif image out) (fclose out)))