blaze-builder-enumerator-0.2.0.5/0000755000000000000000000000000012012134165014735 5ustar0000000000000000blaze-builder-enumerator-0.2.0.5/CHANGES0000644000000000000000000000143112012134165015727 0ustar0000000000000000* 0.2.0.5 Widened dependency on bytestring to allow for bytestring-0.10 * 0.2.0.4 Widened dependency on transformers to allow for transformers-0.3 * 0.2.0.3 Changed licence to BSD3 * 0.2.0.2 Widened blaze-builder dependencies. * 0.2.0.1 Removed left-over flag -fno-warn-unused-binds in .cabal file to allow blaze-builder-enumerator to be built with GHC 6.10. * 0.2.0.0 An enumeratee that allows to incrementally execute builders and pass-on the produced bytestrings to an inner iteratee. Complete rewrite by Simon Meier merging the builder enumeratee code by Michael Snoyman with the new buffered output support provided by blaze-builder-0.2.1.0. * 0.1 Original version by Thomas Sutton blaze-builder-enumerator-0.2.0.5/blaze-builder-enumerator.cabal0000644000000000000000000000254512012134165022627 0ustar0000000000000000Name : blaze-builder-enumerator Version : 0.2.0.5 Synopsis : Enumeratees for the incremental conversion of builders to bytestrings. Description : This package integrates the builders from the blaze-builder package with the enumerator package. It provides infrastructure and enumeratees for incrementally executing builders and pass the filled chunks to a bytestring iteratee. Author : Simon Meier Maintainer : Simon Meier Copyright : Copyright (c) 2010, 2011 Simon Meier original package by Thomas Sutton License : BSD3 License-file : LICENSE Build-type : Simple Cabal-version : >=1.6 Category : Data, Enumerator Stability : Experimental Bug-reports : https://github.com/meiersi/blaze-builder-enumerator Homepage : https://github.com/meiersi/blaze-builder-enumerator Extra-source-files: CHANGES Source-repository head Type: git Location: https://github.com/meiersi/blaze-builder-enumerator.git Library GHC-options: -Wall Build-depends: base >= 4 && < 5 , blaze-builder >= 0.2.1.4 && < 0.4 , bytestring >= 0.9 && < 0.11 , enumerator >= 0.4.3.1 && < 0.5 , transformers >= 0.2 && < 0.4 Exposed-modules: Blaze.ByteString.Builder.Enumerator blaze-builder-enumerator-0.2.0.5/LICENSE0000644000000000000000000000267612012134165015755 0ustar0000000000000000Copyright 2010, Thomas Sutton. All rights reserved. Copyright 2011, Simon Meier. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. blaze-builder-enumerator-0.2.0.5/Setup.hs0000644000000000000000000000005612012134165016372 0ustar0000000000000000import Distribution.Simple main = defaultMain blaze-builder-enumerator-0.2.0.5/Blaze/0000755000000000000000000000000012012134165015772 5ustar0000000000000000blaze-builder-enumerator-0.2.0.5/Blaze/ByteString/0000755000000000000000000000000012012134165020064 5ustar0000000000000000blaze-builder-enumerator-0.2.0.5/Blaze/ByteString/Builder/0000755000000000000000000000000012012134165021452 5ustar0000000000000000blaze-builder-enumerator-0.2.0.5/Blaze/ByteString/Builder/Enumerator.hs0000644000000000000000000001431712012134165024135 0ustar0000000000000000{-# LANGUAGE BangPatterns #-} ------------------------------------------------------------------------------ -- | -- Module : Blaze.ByteString.Builder.Enumerator -- Copyright : (c) 2010 Simon Meier -- License : BSD3 -- -- Maintainer : Simon Meier -- Stability : Experimental -- Portability : Tested on GHC only -- -- Infrastructure and enumeratees for the incremental execution of builders and -- passing on of the filled chunks as bytestrings to an inner iteratee. -- -- Note that the @Buffer@ code is likely to move/change in order to -- reconciliate it with the rest of the blaze-builder library. -- ------------------------------------------------------------------------------ module Blaze.ByteString.Builder.Enumerator ( -- * Buffers Buffer -- ** Status information , freeSize , sliceSize , bufferSize -- ** Creation and modification , allocBuffer , reuseBuffer , nextSlice -- ** Conversion to bytestings , unsafeFreezeBuffer , unsafeFreezeNonEmptyBuffer -- * Buffer allocation strategies , BufferAllocStrategy , allNewBuffersStrategy , reuseBufferStrategy -- * Enumeratees from builders to bytestrings , builderToByteString , unsafeBuilderToByteString , builderToByteStringWith ) where import qualified Data.ByteString as S import Data.Enumerator hiding (map) import Data.Monoid import Control.Monad.IO.Class import Blaze.ByteString.Builder.Internal import Blaze.ByteString.Builder.Internal.Types import Blaze.ByteString.Builder.Internal.Buffer ------------------------------------------------------------------------------ -- Enumeratees for converting builders incrementally to bytestrings ------------------------------------------------------------------------------ -- Simple default instances --------------------------- -- | Incrementally execute builders and pass on the filled chunks as -- bytestrings. builderToByteString :: MonadIO m => Enumeratee Builder S.ByteString m a builderToByteString = builderToByteStringWith (allNewBuffersStrategy defaultBufferSize) -- | Incrementally execute builders on the given buffer and pass on the filled -- chunks as bytestrings. Note that, if the given buffer is too small for the -- execution of a build step, a larger one will be allocated. -- -- WARNING: This enumeratee yields bytestrings that are NOT -- referentially transparent. Their content will be overwritten as soon -- as control is returned from the inner iteratee! unsafeBuilderToByteString :: MonadIO m => IO Buffer -- action yielding the inital buffer. -> Enumeratee Builder S.ByteString m a unsafeBuilderToByteString = builderToByteStringWith . reuseBufferStrategy -- | An enumeratee that incrementally executes builders and passes on the -- filled chunks as bytestrings to an inner iteratee. -- -- INV: All bytestrings passed to the inner iteratee are non-empty. -- -- based on the enumeratee code by Michael Snoyman -- builderToByteStringWith :: MonadIO m => BufferAllocStrategy -> Enumeratee Builder S.ByteString m a builderToByteStringWith (ioBuf0, nextBuf) step0 = do loop ioBuf0 step0 where loop ioBuf = checkDone $ continue . step ioBuf step :: MonadIO m => IO (Buffer) -> (Stream S.ByteString -> Iteratee S.ByteString m b) -> Stream Builder -> Iteratee Builder m (Step S.ByteString m b) step ioBuf k EOF = do buf <- liftIO ioBuf case unsafeFreezeNonEmptyBuffer buf of Nothing -> yield (Continue k) EOF Just bs -> k (Chunks [bs]) >>== flip yield EOF step ioBuf k0 (Chunks xs) = go (unBuilder (mconcat xs) (buildStep finalStep)) ioBuf k0 where finalStep !(BufRange pf _) = return $ Done pf () go bStep ioBuf k = do !buf <- liftIO ioBuf signal <- liftIO (execBuildStep bStep buf) case signal of Done op' _ -> continue $ step (return (updateEndOfSlice buf op')) k BufferFull minSize op' bStep' -> do let buf' = updateEndOfSlice buf op' {-# INLINE cont #-} cont k' = do -- sequencing the computation of the next buffer -- construction here ensures that the reference to the -- foreign pointer `fp` is lost as soon as possible. ioBuf' <- liftIO $ nextBuf minSize buf' go bStep' ioBuf' k' case unsafeFreezeNonEmptyBuffer buf' of Nothing -> cont k Just bs -> k (Chunks [bs]) >>== \step' -> case step' of Continue k' -> cont k' _ -> return step' -- FIXME: Check that we don't loose any input here! InsertByteString op' bs bStep' -> do let buf' = updateEndOfSlice buf op' bsk = maybe id (:) $ unsafeFreezeNonEmptyBuffer buf' k (Chunks (bsk [bs])) >>== \step' -> case step' of Continue k' -> do ioBuf' <- liftIO $ nextBuf 1 buf' go bStep' ioBuf' k' _ -> return step' -- FIXME: Check that we don't loose any input here! {- Old testing code: main :: IO () main = main1 >> main2 >> main3 main1 :: IO () main1 = do builder <- fromLazyByteString `fmap` L.readFile "test-input" withBinaryFile "test-output1" WriteMode $ \h -> run_ (go h builder) where go h builder = enumList 1 [builder] $$ joinI $ blaze $$ iterHandle h main2 :: IO () main2 = withBinaryFile "test-output2" WriteMode $ \h -> run_ (go h) where go h = enumFile "test-input" $$ joinI $ E.map fromByteString $$ joinI $ blaze $$ iterHandle h main3 :: IO () main3 = withBinaryFile "test-output3" WriteMode $ \h -> run_ (go h) where go h = enumList 1 (map S.singleton $ concat $ replicate 1000 [65..90]) $$ joinI $ E.map (mconcat . map fromWord8 . S.unpack) $$ joinI $ blaze $$ iterHandle h -}