on master help sign in
Data.Conduit.Binary
commit032160725d57e05e1235bfc601b3afed0d04c98f authorMichael Snoyman <michael@snoyman.com> dateFri 9 Dec 2011, 08:11 (15 years ago) parent8eac42a
3 files changed+50−8
conduit/Data/Conduit.hs+1−3 conduit/Data/Conduit/Binary.hs+37 conduit/conduit.cabal+12−5
conduit/Data/Conduit.hs+1−3
@@ -47,7 +47,7 @@ data SourceInside m a = SourceInside
4747 { sourcePull :: ResourceT m (Stream a)
4848 , sourcePush :: [a] -> ResourceT m ()
4949 }
50−newtype Source m a = Source (ResourceT m (SourceInside m a))
50+newtype Source m a = Source { unSource :: ResourceT m (SourceInside m a) }
5151
5252type SinkInsideData a m b = Stream a -> ResourceT m (SinkResult a b)
5353data SinkInside a m b =
@@ -201,8 +201,6 @@ Source msource $= pipe = Source $ do
201201 sourcePush source leftover
202202 return result
203203 return source'
204− where
205− unSource (Source s) = s
206204
207205infixr 0 =$
208206
conduit/Data/Conduit/Binary.hsnew file+37
@@ -0,0 +1,37 @@
1+{-# LANGUAGE FlexibleContexts #-}
2+module Data.Conduit.Binary
3+ ( sourceFile
4+ , sinkFile
5+ ) where
6+
7+import Prelude hiding (FilePath)
8+import System.IO (hClose)
9+import Filesystem.Path.CurrentOS (FilePath)
10+import qualified Data.ByteString as S
11+import Filesystem (openFile, IOMode (ReadMode, WriteMode))
12+import Data.Conduit
13+import Control.Monad.Trans.Control (MonadBaseControl)
14+import Control.Monad.Base (liftBase)
15+import Control.Monad.Trans.Resource (with, release)
16+
17+sourceFile :: MonadBaseControl IO m
18+ => FilePath
19+ -> Source m S.ByteString
20+sourceFile fp = Source $ do
21+ (key, handle) <- with (liftBase $ openFile fp ReadMode) (liftBase . hClose)
22+ unSource $ mkSource $ do
23+ bs <- liftBase $ S.hGetSome handle 50
24+ -- FIXME protection against being called after close?
25+ if S.null bs
26+ then release key >> return EOF
27+ else return $ Chunks [bs]
28+
29+sinkFile :: MonadBaseControl IO m
30+ => FilePath
31+ -> Sink S.ByteString m ()
32+sinkFile fp = Sink $ do
33+ (key, handle) <- with (liftBase $ openFile fp WriteMode) (liftBase . hClose)
34+ return $ SinkData $ \stream -> do
35+ case stream of
36+ EOF -> release key >> return (SinkResult [] (Just ()))
37+ Chunks bss -> mapM_ (liftBase . S.hPut handle) bss >> return (SinkResult [] Nothing)
conduit/conduit.cabal+12−5
@@ -1,19 +1,19 @@
11Name: conduit
22Version: 0.0.0
3−Synopsis: Put something in here.
4−-- Description:
3+Synopsis: A pull-based approach to streaming data.
4+Description: Conduits are an approach to the streaming data problem. It is meant as an alternative to enumerators/iterators, hoping to address the same issues with different trade-offs based on real-world experience with enumerators. See the README.md file for more details.
55License: BSD3
66License-file: LICENSE
77Author: Michael Snoyman
88Maintainer: michael@snoyman.com
9−-- Copyright:
109Category: Data
1110Build-type: Simple
12−-- Extra-source-files:
13−Cabal-version: >=1.2
11+Cabal-version: >=1.8
12+Homepage: http://github.com/yesodweb/wai
1413
1514Library
1615 Exposed-modules: Data.Conduit
16+ Data.Conduit.Binary
1717 Data.Conduit.List
1818 Control.Monad.Trans.Resource
1919 Build-depends: base >= 4 && < 5
@@ -22,3 +22,10 @@ Library
2222 , monad-control >= 0.3 && < 0.4
2323 , containers
2424 , transformers >= 0.2.2 && < 0.3
25+ , system-fileio >= 0.3.3 && < 0.4
26+ , system-filepath >= 0.4.3 && < 0.5
27+ , bytestring >= 0.9
28+
29+source-repository head
30+ type: git
31+ location: git://github.com/yesodweb/wai.git