on master help sign in
Data.Conduit.Binary
0321607Michael Snoyman
conduit/Data/Conduit.hs+1−3
@@ -47,7 +47,7 @@ data SourceInside m a = SourceInside
47 { sourcePull :: ResourceT m (Stream a)
47 { sourcePull :: ResourceT m (Stream a)
48 , sourcePush :: [a] -> ResourceT m ()
48 , sourcePush :: [a] -> ResourceT m ()
49 }
49 }
50−newtype Source m a = Source (ResourceT m (SourceInside m a))
50+newtype Source m a = Source { unSource :: ResourceT m (SourceInside m a) }
51
51
52type SinkInsideData a m b = Stream a -> ResourceT m (SinkResult a b)
52type SinkInsideData a m b = Stream a -> ResourceT m (SinkResult a b)
53data SinkInside a m b =
53data SinkInside a m b =
@@ -201,8 +201,6 @@ Source msource $= pipe = Source $ do
201 sourcePush source leftover
201 sourcePush source leftover
202 return result
202 return result
203 return source'
203 return source'
204− where
205− unSource (Source s) = s
206
204
207infixr 0 =$
205infixr 0 =$
208
206
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 @@
1Name: conduit
1Name: conduit
2Version: 0.0.0
2Version: 0.0.0
3−Synopsis: Put something in here.
3+Synopsis: A pull-based approach to streaming data.
4−-- Description:
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.
5License: BSD3
5License: BSD3
6License-file: LICENSE
6License-file: LICENSE
7Author: Michael Snoyman
7Author: Michael Snoyman
8Maintainer: michael@snoyman.com
8Maintainer: michael@snoyman.com
9−-- Copyright:
10Category: Data
9Category: Data
11Build-type: Simple
10Build-type: Simple
12−-- Extra-source-files:
13−Cabal-version: >=1.2
11+Cabal-version: >=1.8
12+Homepage: http://github.com/yesodweb/wai
14
13
15Library
14Library
16 Exposed-modules: Data.Conduit
15 Exposed-modules: Data.Conduit
16+ Data.Conduit.Binary
17 Data.Conduit.List
17 Data.Conduit.List
18 Control.Monad.Trans.Resource
18 Control.Monad.Trans.Resource
19 Build-depends: base >= 4 && < 5
19 Build-depends: base >= 4 && < 5
@@ -22,3 +22,10 @@ Library
22 , monad-control >= 0.3 && < 0.4
22 , monad-control >= 0.3 && < 0.4
23 , containers
23 , containers
24 , transformers >= 0.2.2 && < 0.3
24 , 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