[Haskell-cafe] Restricted file reading monad

George Pollard porges at porg.es
Wed Oct 1 20:56:21 EDT 2008


Hello all,

I'm currently working on a (toy) ID3 [1] tag reader, which made me think
of a library which might be quite useful.

The structure of an ID3 tag goes something like this:

Header: 
- total size of tag
- other header info
A series of frames, each with:
- total size of frame
- other header info
- frame data

Since the ID3 tag as a whole has size information, I need to pass that
into the frame-reading functions to ensure that I never read past the
end of the total header. This exact same requirement is present within
the frame-reading function itself; it must read the frame data without
reading past the end of the frame.

It would be nice to have some kind of monad or similar so that I could
do:

> readTag = do
> 	size <- -- read size
> 	-- read other header info
> 	withLimit size $ -- read frames

then the read-frames function would be able to do its work without
having to worry about size-checking all the time, as this would be
implicit. (And similarly with the frame-reading functions as well.) This
could easily be extended to:

> f x = readManyWithLimit size readerFunction

where readManyWithLimit would return a list of results of applying the
reader function as many times as possible within the size limit (and
possibly returning some kind of error if the reader function 'bails
early' and doesn't gobble right up to the end of the size limit).

The thing is, I've not implemented a monad of any substantial size so I
don't have a clue where to start. Calling it FileReader (for want of a
better name), I guess it would almost be the same as a parsing monad, so
the type would have a reference to the file Handle and the current index
into the file, as well as the last point that it's allowed to read up
to.

So, umm... a rather open-ended question, but how would I go about this?

[1]: http://www.id3.org/Developer_Information
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20081002/a145d907/attachment.bin


More information about the Haskell-Cafe mailing list