Added fetchAhead function

darcs-hash:20090225040209-a4fee-816a8cef5c9edeea5b35fd5fe75ca5b0ec36ee17
This commit is contained in:
Henning Guenther 2009-02-24 20:02:09 -08:00
parent 5528bf1a55
commit 7b2af16c03

View File

@ -19,6 +19,7 @@ import System.IO
class (Monad m,Throws DecodingException m) => ByteSource m where class (Monad m,Throws DecodingException m) => ByteSource m where
sourceEmpty :: m Bool sourceEmpty :: m Bool
fetchWord8 :: m Word8 fetchWord8 :: m Word8
fetchAhead :: m a -> m a
fetchWord16be :: m Word16 fetchWord16be :: m Word16
fetchWord16be = do fetchWord16be = do
w1 <- fetchWord8 w1 <- fetchWord8
@ -94,6 +95,7 @@ instance Throws DecodingException Get where
instance ByteSource Get where instance ByteSource Get where
sourceEmpty = isEmpty sourceEmpty = isEmpty
fetchWord8 = getWord8 fetchWord8 = getWord8
fetchAhead = lookAhead
fetchWord16be = getWord16be fetchWord16be = getWord16be
fetchWord16le = getWord16le fetchWord16le = getWord16le
fetchWord32be = getWord32be fetchWord32be = getWord32be
@ -113,6 +115,11 @@ instance ByteSource (State [Char]) where
c:cs -> do c:cs -> do
put cs put cs
return (fromIntegral $ ord c) return (fromIntegral $ ord c)
fetchAhead act = do
chs <- get
res <- act
put chs
return res
instance Monad (Either DecodingException) where instance Monad (Either DecodingException) where
return = Right return = Right
@ -128,6 +135,11 @@ instance ByteSource (StateT [Char] (Either DecodingException)) where
c:cs -> do c:cs -> do
put cs put cs
return (fromIntegral $ ord c) return (fromIntegral $ ord c)
fetchAhead act = do
chs <- get
res <- act
put chs
return res
instance Throws DecodingException (State BS.ByteString) where instance Throws DecodingException (State BS.ByteString) where
throwException = throw throwException = throw
@ -137,18 +149,33 @@ instance ByteSource (State BS.ByteString) where
fetchWord8 = State (\str -> case BS.uncons str of fetchWord8 = State (\str -> case BS.uncons str of
Nothing -> throw UnexpectedEnd Nothing -> throw UnexpectedEnd
Just (c,cs) -> (c,cs)) Just (c,cs) -> (c,cs))
fetchAhead act = do
str <- get
res <- act
put str
return res
instance ByteSource (StateT BS.ByteString (Either DecodingException)) where instance ByteSource (StateT BS.ByteString (Either DecodingException)) where
sourceEmpty = gets BS.null sourceEmpty = gets BS.null
fetchWord8 = StateT (\str -> case BS.uncons str of fetchWord8 = StateT (\str -> case BS.uncons str of
Nothing -> Left UnexpectedEnd Nothing -> Left UnexpectedEnd
Just ns -> Right ns) Just ns -> Right ns)
fetchAhead act = do
chs <- get
res <- act
put chs
return res
instance ByteSource (StateT LBS.ByteString (Either DecodingException)) where instance ByteSource (StateT LBS.ByteString (Either DecodingException)) where
sourceEmpty = gets LBS.null sourceEmpty = gets LBS.null
fetchWord8 = StateT (\str -> case LBS.uncons str of fetchWord8 = StateT (\str -> case LBS.uncons str of
Nothing -> Left UnexpectedEnd Nothing -> Left UnexpectedEnd
Just ns -> Right ns) Just ns -> Right ns)
fetchAhead act = do
chs <- get
res <- act
put chs
return res
instance ByteSource (ReaderT Handle IO) where instance ByteSource (ReaderT Handle IO) where
sourceEmpty = do sourceEmpty = do
@ -158,4 +185,10 @@ instance ByteSource (ReaderT Handle IO) where
h <- ask h <- ask
liftIO $ do liftIO $ do
ch <- hGetChar h ch <- hGetChar h
return (fromIntegral $ ord ch) return (fromIntegral $ ord ch)
fetchAhead act = do
h <- ask
pos <- liftIO $ hGetPosn h
res <- act
liftIO $ hSetPosn pos
return res