binary files in haskell

Sven Panne [email protected]
Sat, 27 Jan 2001 18:35:49 +0100


[ Seems I *can't* mail in a sane way anymore, so please excuse my last
  half-baked one. But at least I've learned now that Ctrl-RET means
  "send immediately" in Netscape. Great idea! Here my next try... ]

Timmy Douglas wrote:
> I was wondering if anyone here has a solution for writing binary files
> that works in hugs and ghc that doesn't require getting libs.

I dont' know exactly about the current status of Hugs' libs, but I fear
the answer to your question is "no".

> I looked at this page:
> http://haskell.cs.yale.edu/ghc/docs/latest/set/sec-ioexts.html [...]

... and that was exactly the right place, see section "Bulk transfers":
(http://haskell.cs.yale.edu/ghc/docs/latest/set/sec-ioexts.html#SEC-BULK-TRANSFERS)

   hGetBuf       :: Handle -> Addr -> Int -> IO Int
   hGetBufFull   :: Handle -> Addr -> Int -> IO Int

   hPutBuf       :: Handle -> Addr -> Int -> IO Int
   hPutBufFull   :: Handle -> Addr -> Int -> IO ()

These action read/write a given amount of bytes to/from a buffer. There
hasn't been much consensus about a higher level interface yet, but you
can have a look at my variant sitting on top of this:

   http://cvs.haskell.org/cgi-bin/cvsweb.cgi/fptools/hslibs/OpenGL/GL/GL_BinaryIO.hs?rev=1.2&content-type=text/x-cvsweb-markup

Not the nicest and most efficient code on earth, but it should be enough
to get the idea. Note that in more recent (CVS) versions of GHC, `Addr'
(which is deprecated now) has been replaced by `Ptr a'.

Any ideas & suggestions for a good binary I/O interface are highly welcome!

Cheers,
   Sven "The Typo" P.