module Bio.Iteratee.IO(
defaultBufSize
,enumFile
,enumFileRandom
,enumFd
,enumFdCatch
,enumFdRandom
)
where
import Bio.Iteratee.Iteratee
import Bio.Prelude hiding ( bracket )
import Control.Monad.Catch
import Control.Monad.IO.Class
import Data.ByteString.Internal (createAndTrim)
import System.IO (SeekMode(..))
defaultBufSize :: Int
defaultBufSize = 2*1024*1024
makefdCallback :: MonadIO m => Int -> Fd -> st -> m (Either SomeException ((Bool, st), Bytes))
makefdCallback bufsize fd st = do
s <- liftIO . createAndTrim bufsize $ \p ->
fromIntegral <$> fdReadBuf fd (castPtr p) (fromIntegral bufsize)
return $ Right ((True, st), s)
enumFd :: MonadIO m => Int -> Fd -> Enumerator Bytes m a
enumFd bufsize fd = enumFromCallback (makefdCallback bufsize fd) ()
enumFdCatch
:: (IException e, MonadIO m)
=> Int
-> Fd
-> (e -> m (Maybe EnumException))
-> Enumerator Bytes m a
enumFdCatch bufsize fd handler = enumFromCallbackCatch (makefdCallback bufsize fd) handler ()
enumFdRandom :: MonadIO m => Int -> Fd -> Enumerator Bytes m a
enumFdRandom bs fd iter = enumFdCatch bs fd handler iter
where
handler (SeekException off) =
Nothing <$ (liftIO . fdSeek fd AbsoluteSeek $ fromIntegral off)
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