on fa75413 help sign in
streamly/examples/Split.hs
44 lines 1.6 kB history raw
1import qualified Streamly.Internal.Prelude as S
2import qualified Streamly.Data.Fold as FL
3-- import qualified Streamly.Memory.Array as A
4import qualified Streamly.Internal.Memory.ArrayStream as AS
5import qualified Streamly.Internal.FileSystem.Handle as IFH
6import qualified Streamly.FileSystem.Handle as FH
7import qualified System.IO as FH
8-- import qualified Streamly.FileSystem.FD as FH
9-- import qualified Streamly.Data.Unicode.Stream as US
11import Control.Monad.IO.Class (liftIO)
12import Control.Monad.State.Strict (StateT(..), get, put)
13import Data.Char (ord)
14import System.Environment (getArgs)
15import System.IO (IOMode(..), hSeek, SeekMode(..))
16import Data.Function ((&))
18newHandle :: StateT (Maybe (FH.Handle, Int)) IO FH.Handle
19newHandle = do
20 old <- get
21 idx <- case old of
22 Nothing -> return 0
23 Just (h, i) -> liftIO (FH.hClose h) >> return (i + 1)
24 h <- liftIO $ FH.openFile ("dst-xyz-" ++ show idx ++ ".txt") WriteMode
25 put (Just (h, idx))
26 return h
28-- XXX reduce the input stream to a stream of file names
29-- The fold can return the file name/handle after it is done.
30-- similarly the files can written to directories and we can generate a stream
31-- of directory names.
32splitFile :: FH.Handle -> IO ()
33splitFile inHandle =
34 S.unfold FH.read inHandle
35 & S.liftInner
36 & S.chunksOf2 (180 * 1024 * 1024) newHandle IFH.write2
37 & S.evalStateT Nothing -- generate new handle for each iteration
38 & S.drain
40main :: IO ()
41main = do
42 name <- fmap head getArgs
43 src <- FH.openFile name ReadMode
44 splitFile src