Re: FLI and Unix file descriptors
On Mon, 11 Aug 2003 17:24:29 +0100, David Fox <davef@xanalys.com> wrote:
> I'm looking for a way to get the underlying Unix file descriptor
> from a LispWorks stream - something similar to what CMUCL has to
> offer:
>
> <http://www.mail-archive.com/cmucl-help@cons.org/msg01034.html>
>
> The closest thing I could find is IO:FILE-STREAM-FILE-HANDLE
> which is exported from the IO package but doesn't seem to be
> documented. Is this the way to go?
>
> The internal accessor (if you must) is
> STREAM::OS-FILE-HANDLE-STREAM-FILE-HANDLE. Depending on what you do
> with the file descriptor, it could be dangerous as that CMUCL
> message you reference suggests, so I think the absence of an
> exported accessor in LispWorks is intentional.
>
> What are you going to do with that fd? Anything that can't be
> achieved with Common Lisp?
As the subject line already suggested I'm working with the foreign
function interface - I don't think this could be easily done with CL
alone but I'll be happy if someone proves me wrong.
I'm trying to use parts of the GD library <http://www.boutell.com/gd/>
to generate PNG images. I use UFFI for the FFI stuff (I want a program
which runs at least on LW and CMUCL) and I have a working version for
the tutorial example given at
<http://www.boutell.com/gd/manual2.0.15.html#basics>.
It looks like this:
(defun foo ()
(with-foreign-object (im 'gd-image)
(setq im (gd-image-create 64 64))
(let* ((black (gd-image-color-allocate im 0 0 0))
(white (gd-image-color-allocate im 255 255 255)))
(gd-image-line im 0 0 63 63 white)
(with-open-file (s "/tmp/result.png" :direction :output :if-exists :supersede)
(gd-image-png-fd im (system:fd-stream-fd s)))
(gd-image-destroy im))))
This is for CMUCL but I was hoping to make it work with LW also.
Ah, by the way, all "gd-" function are direct calls to the
corresponding GD functions except for GD-IMAGE-PNG-FD which is my own
function. It looks like this:
void gdImagePngFD(gdImagePtr im, int fd) {
int fd2;
FILE *f;
fd2 = dup(fd);
f = fdopen(fd2, "w");
gdImagePng(im, f);
fclose(f);
}
I thought it might have been a good idea to "dup" the file descriptor
before I use it but maybe that's just naïve. The manpage isn't clear
about this (or I've missed it).
Of course, if I only wanted to work on files I could stop worrying
about Lisp streams and associated file handles and implement the whole
thing directly in C (accepting a C string for the file's
namestring). But eventually I want to able to write the images
directly to a socket stream (from my mod_lisp web server). That's why
I'm asking.
Is there a better, safer, Lisp-ier way to do it?
Thanks,
Edi.
________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs Email
Security System. For more information on a proactive email security
service working around the clock, around the globe, visit
http://www.messagelabs.com
________________________________________________________________________