[Haskell-cafe] Re: [Haskell] installing streams library
Bulat Ziganshin
bulat.ziganshin at gmail.com
Wed May 31 04:27:02 EDT 2006
Hello Bulat,
Sunday, May 28, 2006, 2:44:37 PM, you wrote:
>> type PtrLen a = (Ptr a, Int)
>> encodePtrLen :: (Binary a) => a -> (PtrLen a -> IO b) -> IO b
>> decodePtr :: (Binary a) => Ptr a -> IO a
Finally i've implemented the following (you then would use
'withForeignPtr' to work with contents of ForeignPtr):
-- -----------------------------------------------------------------------------
-- Encode/decode contents of memory buffer
encodePtr :: (Binary a, Integral size) => a -> IO (ForeignPtr x, size)
encodePtrLE :: (Binary a, Integral size) => a -> IO (ForeignPtr x, size)
encodePtrBitAligned :: (Binary a, Integral size) => a -> IO (ForeignPtr x, size)
encodePtrBitAlignedLE :: (Binary a, Integral size) => a -> IO (ForeignPtr x, size)
encodePtr = encodePtr' openByteAligned
encodePtrLE = encodePtr' openByteAlignedLE
encodePtrBitAligned = encodePtr' openBitAligned
encodePtrBitAlignedLE = encodePtr' openBitAlignedLE
decodePtr :: (Binary a, Integral size) => Ptr x -> size -> IO a
decodePtrLE :: (Binary a, Integral size) => Ptr x -> size -> IO a
decodePtrBitAligned :: (Binary a, Integral size) => Ptr x -> size -> IO a
decodePtrBitAlignedLE :: (Binary a, Integral size) => Ptr x -> size -> IO a
decodePtr = decodePtr' openByteAligned
decodePtrLE = decodePtr' openByteAlignedLE
decodePtrBitAligned = decodePtr' openBitAligned
decodePtrBitAlignedLE = decodePtr' openBitAlignedLE
-- Universal function what encodes data with any alignment
encodePtr' open thedata = do
h <- createMemBuf 512 >>= open
put_ h thedata
vFlush h
vRewind h
(buf,size) <- vReceiveBuf h READING -- FIXME: MemBuf-implementation specific
fptr <- newForeignPtr finalizerFree (castPtr buf) -- FIXME: also MemBuf-implementation specific
return (fptr,size)
-- Universal function what decodes data written with any alignment
decodePtr' open ptr size = do
h <- openMemBuf ptr size >>= open
result <- get h
vClose h
return result
--
Best regards,
Bulat mailto:Bulat.Ziganshin at gmail.com
More information about the Haskell-Cafe
mailing list