chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Getting C file handle from port?


From: Peter Bex
Subject: Re: [Chicken-users] Getting C file handle from port?
Date: Sat, 20 Mar 2010 16:01:16 +0100
User-agent: Mutt/1.4.2.3i

On Sat, Mar 20, 2010 at 03:55:26PM +0100, Thomas Chust wrote:
> Hello Jeronimo,
> 
> internally CHICKEN uses the C stdio facilities for regular file ports
> anyway, so this is well possible: A regular file port is a block
> object containing the FILE * in its first slot and a set of port
> operation procedures specific for regular file ports in its third
> slot. You should first check for the correct type of the port, then
> extract the file pointer and use it.

That's pretty cool, I didn't know it did it like that.

>   (define (fputs data #!optional [port (current-output-port)])
>     (ensure stream-port? port)
>     ((foreign-lambda* void ([c-string data] [scheme-object port])
>        "fputs(data, (FILE *)C_block_item(port, 0));\n")
>      data port))

There's even a macro for that: C_port_file.  That will make your code
slightly more readable:

   (define (fputs data #!optional [port (current-output-port)])
     (ensure stream-port? port)
     ((foreign-lambda* void ([c-string data] [scheme-object port])
        "fputs(data, C_port_file(port));\n")
      data port))

Cheers,
Peter
-- 
http://sjamaan.ath.cx
--
"The process of preparing programs for a digital computer
 is especially attractive, not only because it can be economically
 and scientifically rewarding, but also because it can be an aesthetic
 experience much like composing poetry or music."
                                                        -- Donald Knuth




reply via email to

[Prev in Thread] Current Thread [Next in Thread]