[Haskell] reading binary files

minh thu noteed at gmail.com
Thu Apr 6 04:41:32 EDT 2006

thanks a lot !

the code you give in the mail is +/- what i thought about ... is-it
the fastest way ?

thank you also for your lib, i will read it later in the day.

when i said "would have to be the same" in previous mail, it's because
i like to see haskell as a really high-level abstraction where you
just say what you want, but not in which way to do it. in the examples
(mines or yours), the programmer has a lot of choices ...

another problem is that you have to allocate a buffer before reading
the file, but (i m not a os expert) i think there is already some kind
of plumbing (maybe another buffer) to read file. so maybe that in all
cases, we're losing efficiency when reading a file ?
also, if our buffer allocation is mandatory, the compiler could put
the right code for us (i.e. choose the best buffer length).

again thx,
minh thu

2006/4/6, Bulat Ziganshin <bulat.ziganshin at gmail.com>:
> Hello minh,
> Wednesday, April 5, 2006, 10:41:02 PM, you wrote:
> > but in 1/, i have to choose between different kind of array
> > representation (and i dont know which one is better) and it seems to
> > me that the resulting code (compiled) would have to be the same.
> no, the code will be slightly different. IOUArray will allocate space
> in the GHC's heap, while malloc - in the C heap (ghc's heap is
> additional storey on the C heap)
> btw, `getElems` is VERY INEEFECIENT way - it will convert entire array
> to the list before return
> > for example, the couples (hGet*,peek/readArray) could be written in one line;
> > also, one line for the reading/reconstructing more-than-one-Word8 value.
> > is it already possible ?
> > would it be interesting to add such capabilities to haskell ? (i think so)
> > i can try to add it but i need some pointers about how to do it.
> i don't see much problems here, just add peek16LE and other procedures
> like it and you can use trivial code:
> idLength <- peek8 a 1
> x <- peek16LE a 8
> peek8 a i = do (x::Word8) <- peekByteOff a i
>               return (fromIntegral x)
> peek16LE a i = do (x::Word8) <- peekByteOff a i
>                  (y::Word8) <- peekByteOff a (i+1)
>                  return (fromIntegral x + fromIntegral y * 256 )
> there are a couple of binary I/O libs (including my own one :) ), i
> just don't think you need such power here. of course, if you want to
> read data sequentially, binary i/o lib will be preferable. with my lib
> you can write smth like this:
>          -- Create new MemBuf filled with data from file
>          h <- readFromFile "test"
>          -- Read header fields sequentially
>          idLength <- getWord8 h
>          x <- getWord16le h
>          ....
> i attached here a part of my library docs where this described in much
> more details :)
> the lib itself is at http://freearc.narod.ru/Streams.tar.gz
> --
> Best regards,
>  Bulat                            mailto:Bulat.Ziganshin at gmail.com

More information about the Haskell mailing list