{- |
Reading records from streams
This is still only for demonstration and might be of not much use
and you should not rely on the interface.
-}moduleData.Accessor.BinaryReadwhereimportqualifiedData.Accessor.BasicasAccessorimportqualifiedControl.Monad.Trans.StateasStateimportControl.Monad.Trans.State(StateT,)importControl.Monad.Trans.Class(lift,)importControl.Monad(liftM,)importData.Word(Word8,)importData.Char(chr,)importPreludehiding(any)typeStream=[Word8]classCawhereany::ByteSourcesource=>sourceaclassMonadsource=>ByteSourcesourcewherereadWord8::sourceWord8classByteStreamswheregetWord8::Monadm=>s->m(Word8,s)instanceByteCompatiblebyte=>ByteStream[byte]wheregetWord8xs=casexsof(c:cs)->return(toBytec,cs)_->fail"ByteStream: no more byte available"classByteCompatiblebytewheretoByte::byte->Word8instanceByteCompatibleWord8wheretoByte=idinstance(ByteStreams,Monadm)=>ByteSource(StateTsm)wherereadWord8=doxs<-State.get(c,cs)<-lift(getWord8xs)State.putcsreturncinstanceCWord8whereany=readWord8instanceCCharwhereany=liftM(chr.fromIntegral)readWord8instanceCIntwhereany=doc0<-readWord8c1<-readWord8c2<-readWord8c3<-readWord8return(foldl1(\accd->acc*256+d)(mapfromIntegral[c0,c1,c2,c3]))newtypeParsersr=Parser{runParser::(r,s)->Maybe(r,s)}field::(ByteStreams,Ca)=>Accessor.Tra->Parsersrfieldf=Parser$uncurry(\r->State.runStateT$fmap(\x->Accessor.setfxr)any)record::[Parsersr]->Parsersrrecordps=Parser$flip(foldl(>>=))(maprunParserps).Just-- TODO: writer