[Haskell] reading binary files
Bulat Ziganshin
bulat.ziganshin at gmail.com
Thu Apr 6 09:52:09 EDT 2006
Hello minh,
Thursday, April 6, 2006, 12:41:32 PM, you wrote:
> the code you give in the mail is +/- what i thought about ... is-it
> the fastest way ?
if you will say about your task and speed requirements, i will say how
you can do it. the fastest way is to use asm :)
> 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 ...
it was on advertising :) in Real World (tm) the things are more
complex ;) on the other side, you can write something like this
(using my library):
data TiffHeader = Tiff { id : Word8,
height : Word16,
width : Word16,
....
}
main = do h <- openBinaryFD "filename" ReadMode
header <- get h
print (height header, width header)
and got what you want. here, all fields will be read as big-endian, to
read them as little-endian you should use:
main = do h <- openBinaryFD "filename" ReadMode >>= openByteAlignedLE
> 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).
with my lib, you can either read data directly from file (through
implicit 512-byte buffer) or read whole file into the automatically
allocated buffer with `readFromFile`. what is better - depends on what
you plan to do with rest of file
>> -- Create new MemBuf filled with data from file
>> h <- readFromFile "test"
--
Best regards,
Bulat mailto:Bulat.Ziganshin at gmail.com
More information about the Haskell
mailing list