module Data.Iteratee.IO.Handle(
enumHandle,
enumHandleRandom,
fileDriverHandle,
fileDriverRandomHandle,
)
where
import Data.Iteratee.Base.StreamChunk (ReadableChunk (..))
import Data.Iteratee.Base
import Data.Iteratee.Binary()
import Data.Iteratee.IO.Base
import Data.Int
import Control.Exception.Extensible
import Foreign.Ptr
import Foreign.Marshal.Alloc
import System.IO
enumHandle :: ReadableChunk s el => Handle -> EnumeratorGM s el IO a
enumHandle h iter' = IM $ allocaBytes (fromIntegral buffer_size) $ loop iter'
where
buffer_size = 4096
loop iter@Done{} _p = return iter
loop _iter@Seek{} _p = error "enumH is not compatabile with seek IO"
loop iter@(Cont step) p = do
n <- try $ hGetBuf h p buffer_size
case n of
Left ex -> unIM $ step (Error $ show (ex :: SomeException))
Right 0 -> return iter
Right n' -> do
s <- readFromPtr p (fromIntegral n')
im <- unIM $ step (Chunk s)
loop im p
enumHandleRandom :: ReadableChunk s el => Handle -> EnumeratorGM s el IO a
enumHandleRandom h iter =
IM $ allocaBytes (fromIntegral buffer_size) (loop (0,0) iter)
where
buffer_size = 4096
loop :: (ReadableChunk s el) =>
(FileOffset,Int) ->
IterateeG s el IO a ->
Ptr el ->
IO (IterateeG s el IO a)
loop _pos iter'@Done{} _p = return iter'
loop pos@(off,len) (Seek off' c) p |
off <= off' && off' < off + fromIntegral len =
do
let local_off = fromIntegral $ off' off
s <- readFromPtr (p `plusPtr` local_off) (len local_off)
im <- unIM $ c (Chunk s)
loop pos im p
loop _pos iter'@(Seek off c) p = do
off' <- (try $ hSeek h AbsoluteSeek
(fromIntegral off)) :: IO (Either SomeException ())
case off' of
Left _errno -> unIM $ enumErr "IO error" iter'
Right _ -> loop (off,0) (Cont c) p
loop (off,len) _iter' _p | off `seq` len `seq` False = undefined
loop (off,len) iter'@(Cont step) p = do
n <- (try $ hGetBuf h p buffer_size) :: IO (Either SomeException Int)
case n of
Left _errno -> unIM $ step (Error "IO error")
Right 0 -> return iter'
Right n' -> do
s <- readFromPtr p (fromIntegral n')
im <- unIM $ step (Chunk s)
loop (off + fromIntegral len,fromIntegral n') im p
fileDriverHandle :: ReadableChunk s el =>
IterateeGM s el IO a ->
FilePath ->
IO (Either (String, a) a)
fileDriverHandle iter filepath = do
h <- openBinaryFile filepath ReadMode
result <- unIM $ (enumHandle h >. enumEof) ==<< iter
hClose h
print_res result
where
print_res (Done a (Error err)) = return $ Left (err, a)
print_res (Done a _) = return $ Right a
print_res (Cont _) = return $ Left ("Iteratee unfinished", undefined)
print_res (Seek _ _) = return $ Left ("Iteratee unfinished", undefined)
fileDriverRandomHandle :: ReadableChunk s el =>
IterateeGM s el IO a ->
FilePath ->
IO (Either (String, a) a)
fileDriverRandomHandle iter filepath = do
h <- openBinaryFile filepath ReadMode
result <- unIM $ (enumHandleRandom h >. enumEof) ==<< iter
hClose h
print_res result
where
print_res (Done a (Error err)) = return $ Left (err, a)
print_res (Done a _) = return $ Right a
print_res (Cont _) = return $ Left ("Iteratee unfinished", undefined)
print_res (Seek _ _) = return $ Left ("Iteratee unfinished", undefined)