module Bio.Iteratee.IO(
defaultBufSize
,enumFile
,enumFileRandom
,enumFd
,enumFdRandom
)
where
import Bio.Iteratee.Iteratee
import Bio.Prelude hiding ( bracket, loop )
import Control.Monad.Catch
import Control.Monad.IO.Class
import System.IO (SeekMode(..))
import qualified Data.ByteString as B
defaultBufSize :: Int
defaultBufSize = 2*1024*1024
enumFd :: MonadIO m => Int -> Fd -> Enumerator Bytes m a
enumFd bufsize fd = loop
where
loop iter = runIter iter idoneM onCont
onCont k j@(Just _) = return (icont k j)
onCont k Nothing = do
s <- liftIO $ fdGet bufsize fd
if B.null s then return $ liftI k
else loop . k $ Chunk s
enumFdRandom :: MonadIO m => Int -> Fd -> Enumerator Bytes m a
enumFdRandom bs fd = loop
where
loop iter = runIter iter idoneM onCont
onCont k Nothing = do
s <- liftIO $ fdGet bs fd
if B.null s then return $ liftI k
else loop . k $ Chunk s
onCont k j@(Just e) = case fromException e of
Just (SeekException off) -> do
liftIO . void $ fdSeek fd AbsoluteSeek (fromIntegral off)
loop $ liftI k
Nothing -> return (icont k j)
enumFile' :: (MonadIO m, MonadMask m) =>
(Int -> Fd -> Enumerator s m a)
-> Int
-> FilePath
-> Enumerator s m a
enumFile' enumf bufsize filepath iter = bracket
(liftIO $ openFd filepath ReadOnly Nothing defaultFileFlags)
(liftIO . closeFd)
(flip (enumf bufsize) iter)
enumFile ::
(MonadIO m, MonadMask m)
=> Int
-> FilePath
-> Enumerator Bytes m a
enumFile = enumFile' enumFd
enumFileRandom ::
(MonadIO m, MonadMask m)
=> Int
-> FilePath
-> Enumerator Bytes m a
enumFileRandom = enumFile' enumFdRandom