packages feed

zlib-enum (empty) → 0.0

raw patch · 4 files changed

+130/−0 lines, 4 filesdep +basedep +bytestringdep +enumeratorsetup-changed

Dependencies added: base, bytestring, enumerator, mtl, zlib-bindings

Files

+ Codec/Zlib/Enum.hs view
@@ -0,0 +1,80 @@+module Codec.Zlib.Enum (+    -- * Enumeratees+    compress, decompress,+    -- * Re-exported from zlib-bindings +    WindowBits, defaultWindowBits+) where++import Codec.Zlib+import Data.Enumerator as E+import Control.Monad.Trans (MonadIO, liftIO)+import Data.ByteString (ByteString)+import Control.Monad (join)++joinIO :: MonadIO m => IO (m (Step a m b)) -> Iteratee a m b+joinIO = Iteratee . join . liftIO++enumLoop :: Monad m =>+     ((Stream b -> Iteratee b m c) -> Iteratee b m c)+     -> (a -> (Stream b -> Iteratee b m c) -> Iteratee b m c)+     -> Enumeratee a b m c+enumLoop done more = checkDone loop where+    loop k = do maybe_x <- E.head+                case maybe_x of+                     Nothing -> return $$ done k+                     Just x  -> checkDone loop $$ more x k++-- |+-- Decompress (inflate) a stream of 'ByteString's. For example:+--      +-- >    run $ enumFile "test.z" $$ decompress defaultWindowBits $$ printChunks True++decompress :: MonadIO m+    => WindowBits -- ^ Zlib parameter (see the zlib-bindings package as well as the zlib C library)+    -> Enumeratee ByteString ByteString m ()+decompress config step0 = do+    inflate <- liftIO $ initInflate config+    let done k      = do lastChunk <- liftIO $ finishInflate inflate+                         k (Chunks [lastChunk])+        more x k    = joinIO $ withInflateInput inflate x (return . callback k)+    enumLoop done more step0++-- |+-- Compress (deflate) a stream of 'ByteString's. The 'WindowBits' also control+-- the format (zlib vs. gzip).++compress :: MonadIO m+    => Int        -- ^ Compression level+    -> WindowBits -- ^ Zlib parameter (see the zlib-bindings package as well as the zlib C library)+    -> Enumeratee ByteString ByteString m ()+compress level config step0 = do+    deflate <- liftIO $ initDeflate level config+    let done k    = joinIO $ finishDeflate deflate (return . callback k)+        more x k  = joinIO $ withDeflateInput deflate x (return . callback k)+    enumLoop done more step0++-- A callback function for withInflateInput / withDeflateInput+callback :: MonadIO m =>+    (Stream a -> Iteratee a m b) -> IO (Maybe a) -> m (Step a m b)++callback k pop = maybe done more =<< liftIO pop+  where+    done   = return (Continue k)+    more y = do step <- runIteratee (k (Chunks [y]))+                case step of+                     Continue k'    -> callback k' pop+                     other          -> return other++-- testInflate = do+--     h <- openBinaryFile "test-out" WriteMode+--     run $ enumFile "test.z"+--            $$ decompress defaultWindowBits+--            $$ iterHandle h+--     hClose h+-- +-- testDeflate = do+--     h <- openBinaryFile "test.z" WriteMode+--     run $ enumFile "test"+--            $$ compress 7 defaultWindowBits+--            $$ iterHandle h+--     hClose h
+ LICENSE view
@@ -0,0 +1,21 @@+Copyright 2011 Malte Sommerkorn.++Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the+"Software"), to deal in the Software without restriction, including+without limitation the rights to use, copy, modify, merge, publish,+distribute, sublicense, and/or sell copies of the Software, and to+permit persons to whom the Software is furnished to do so, subject to+the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ zlib-enum.cabal view
@@ -0,0 +1,27 @@+name:           zlib-enum+version:        0.0+license:        MIT+license-file:   LICENSE+author:         Malte Sommerkorn <malte.sommerkorn@googlemail.com>+maintainer:     Malte Sommerkorn <malte.sommerkorn@googlemail.com>+synopsis:       Enumerator interface for zlib compression+description:    zlib-enum is a stop-gap package to provide enumeratees for+                zlib compression/decompression.+category:       Codec, Compression, Enumerator+stability:      Experimental+cabal-version:  >= 1.6+build-type:     Simple+homepage:       http://github.com/maltem/zlib-enum++library+    build-depends: base                  >= 4 && < 5+                 , bytestring+                 , mtl                   >= 2+                 , enumerator            >= 0.4+                 , zlib-bindings+    exposed-modules: Codec.Zlib.Enum+    ghc-options:     -Wall++source-repository head+  type:     git+  location: git://github.com/maltem/zlib-enum.git