Skip to content

Commit

Permalink
Implementation of 'compress' based on Michael's 'decompress'
Browse files Browse the repository at this point in the history
  • Loading branch information
tanimoto committed Mar 19, 2011
1 parent f7bf0d8 commit 8e3d934
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions Codec/Zlib/Enum.hs
Expand Up @@ -11,6 +11,7 @@ import Control.Monad.Trans (MonadIO, liftIO, lift)
import Data.ByteString (ByteString) import Data.ByteString (ByteString)
import Control.Monad (join) import Control.Monad (join)


{--
joinIO :: MonadIO m => IO (m (Step a m b)) -> Iteratee a m b joinIO :: MonadIO m => IO (m (Step a m b)) -> Iteratee a m b
joinIO = Iteratee . join . liftIO joinIO = Iteratee . join . liftIO
Expand All @@ -23,6 +24,7 @@ enumLoop done more = checkDone loop where
case maybe_x of case maybe_x of
Nothing -> return $$ done k Nothing -> return $$ done k
Just x -> checkDone loop $$ more x k Just x -> checkDone loop $$ more x k
--}


-- | -- |
-- Decompress (inflate) a stream of 'ByteString's. For example: -- Decompress (inflate) a stream of 'ByteString's. For example:
Expand All @@ -33,20 +35,20 @@ decompress :: MonadIO m
=> WindowBits -- ^ Zlib parameter (see the zlib-bindings package as well as the zlib C library) => WindowBits -- ^ Zlib parameter (see the zlib-bindings package as well as the zlib C library)
-> Enumeratee ByteString ByteString m a -> Enumeratee ByteString ByteString m a
decompress config inner = do decompress config inner = do
fzstr <- liftIO $ initInflate config inf <- liftIO $ initInflate config
decompress' fzstr inner decompress' inf inner


decompress' :: MonadIO m => Inflate -> Enumeratee ByteString ByteString m b decompress' :: MonadIO m => Inflate -> Enumeratee ByteString ByteString m b
decompress' fzstr (Continue k) = do decompress' inf (Continue k) = do
x <- E.head x <- E.head
case x of case x of
Nothing -> do Nothing -> do
chunk <- liftIO $ finishInflate fzstr chunk <- liftIO $ finishInflate inf
lift $ runIteratee $ k $ Chunks [chunk] lift $ runIteratee $ k $ Chunks [chunk]
Just bs -> do Just bs -> do
chunks <- liftIO $ withInflateInput fzstr bs $ go id chunks <- liftIO $ withInflateInput inf bs $ go id
step <- lift $ runIteratee $ k $ Chunks chunks step <- lift $ runIteratee $ k $ Chunks chunks
decompress' fzstr step decompress' inf step
where where
go front pop = do go front pop = do
x <- pop x <- pop
Expand All @@ -59,6 +61,35 @@ decompress' _ step = return step
-- Compress (deflate) a stream of 'ByteString's. The 'WindowBits' also control -- Compress (deflate) a stream of 'ByteString's. The 'WindowBits' also control
-- the format (zlib vs. gzip). -- the format (zlib vs. gzip).


compress
:: MonadIO m
=> Int
-> WindowBits
-> Enumeratee ByteString ByteString m a
compress level config inner = do
def <- liftIO $ initDeflate level config
compress' def inner

compress' :: MonadIO m => Deflate -> Enumeratee ByteString ByteString m b
compress' def (Continue k) = do
x <- E.head
case x of
Nothing -> do
chunks <- liftIO $ finishDeflate def $ go id
lift $ runIteratee $ k $ Chunks chunks
Just bs -> do
chunks <- liftIO $ withDeflateInput def bs $ go id
step <- lift $ runIteratee $ k $ Chunks chunks
compress' def step
where
go front pop = do
x <- pop
case x of
Nothing -> return $ front []
Just y -> go (front . (:) y) pop
compress' _ step = return step

{--
compress :: MonadIO m compress :: MonadIO m
=> Int -- ^ Compression level => Int -- ^ Compression level
-> WindowBits -- ^ Zlib parameter (see the zlib-bindings package as well as the zlib C library) -> WindowBits -- ^ Zlib parameter (see the zlib-bindings package as well as the zlib C library)
Expand All @@ -80,6 +111,7 @@ callback k pop = runIteratee . k . Chunks =<< liftIO (go id)
case x of case x of
Nothing -> return $ front [] Nothing -> return $ front []
Just y -> go (front . (:) y) Just y -> go (front . (:) y)
--}


-- testInflate = do -- testInflate = do
-- h <- openBinaryFile "test-out" WriteMode -- h <- openBinaryFile "test-out" WriteMode
Expand Down

0 comments on commit 8e3d934

Please sign in to comment.