Permalink
Browse files

Reorder functions, and re-export defaultWindowBits

  • Loading branch information...
1 parent 66f85ba commit f19545b2cab0039c4283c8523538dc447fa68c8c maltem committed Jan 2, 2011
Showing with 34 additions and 34 deletions.
  1. +34 −34 Codec/Zlib/Enum.hs
View
@@ -2,7 +2,7 @@ module Codec.Zlib.Enum (
-- * Enumeratees
compress, decompress,
-- * Re-exported from zlib-bindings
- WindowBits
+ WindowBits, defaultWindowBits
) where
import Codec.Zlib
@@ -23,18 +23,6 @@ decompress config step0 = do
inflate <- liftIO $ initInflate config
checkDone (continue . goInflate inflate) 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
- checkDone (continue . goDeflate deflate) step0
-
goInflate :: MonadIO m
=> Inflate
-- ^ Zlib state
@@ -52,6 +40,35 @@ goInflate inflate k stream = case stream of
inflateOne = withInflateInput inflate x (return . callback k)
in checkDone cont $$ joinIO inflateOne
+-- |
+-- 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
+ checkDone (continue . goDeflate deflate) step0
+
+goDeflate :: MonadIO m
+ => Deflate
+ -- ^ Zlib state
+ -> (Stream ByteString -> Iteratee ByteString m b)
+ -- ^ Continuation to be called on the next chunk of compressed data
+ -> (Stream ByteString)
+ -- ^ Uncompressed data
+ -> Iteratee ByteString m (Step ByteString m b)
+
+goDeflate deflate k stream = case stream of
+ EOF -> let callFinish = finishDeflate deflate (return . callback k)
+ in return $$ joinIO callFinish
+ (Chunks []) -> continue (goDeflate deflate k)
+ (Chunks (x:xs)) -> let cont k' = goDeflate deflate k' (Chunks xs)
+ deflateOne = withDeflateInput deflate x (return . callback k)
+ in checkDone cont $$ joinIO deflateOne
+
-- A callback function for withInflateInput / withDeflateInput
callback :: MonadIO m =>
(Stream a -> Iteratee a m b) -> IO (Maybe a) -> m (Step a m b)
@@ -64,6 +81,10 @@ callback k pop = maybe done more =<< liftIO pop
Continue k' -> callback k' pop
other -> return other
+-- Conversion utility
+joinIO :: MonadIO m => IO (m (Step a m b)) -> Iteratee a m b
+joinIO = Iteratee . join . liftIO
+
-- testInflate = do
-- h <- openBinaryFile "test-out" WriteMode
-- run $ enumFile "test.z"
@@ -77,24 +98,3 @@ callback k pop = maybe done more =<< liftIO pop
-- $$ enumDeflate 7 defaultWindowBits
-- $$ iterHandle h
-- hClose h
-
--- Conversion utility
-joinIO :: MonadIO m => IO (m (Step a m b)) -> Iteratee a m b
-joinIO = Iteratee . join . liftIO
-
-goDeflate :: MonadIO m
- => Deflate
- -- ^ Zlib state
- -> (Stream ByteString -> Iteratee ByteString m b)
- -- ^ Continuation to be called on the next chunk of compressed data
- -> (Stream ByteString)
- -- ^ Uncompressed data
- -> Iteratee ByteString m (Step ByteString m b)
-
-goDeflate deflate k stream = case stream of
- EOF -> let callFinish = finishDeflate deflate (return . callback k)
- in return $$ joinIO callFinish
- (Chunks []) -> continue (goDeflate deflate k)
- (Chunks (x:xs)) -> let cont k' = goDeflate deflate k' (Chunks xs)
- deflateOne = withDeflateInput deflate x (return . callback k)
- in checkDone cont $$ joinIO deflateOne

0 comments on commit f19545b

Please sign in to comment.