1 {-#LANGUAGE RankNTypes#-}
22 import qualified System.IO as IO
23 import Control.Exception (throwIO, try)
24 import Foreign.C.Error (Errno(Errno), ePIPE)
25 import qualified GHC.IO.Exception as G
26 import Data.Text (Text)
27 import qualified Data.Text as T
28 import qualified Data.Text.IO as T
30 import qualified Pipes.Safe.Prelude as Safe
31 import Pipes.Safe (MonadSafe(..))
32 import Prelude hiding (readFile, writeFile)
35 Where pipes @IO@ replaces lazy @IO@, @Producer Text IO r@ replaces lazy 'Text'.
36 This module exports some convenient functions for producing and consuming
37 pipes 'Text' in @IO@, namely, 'readFile', 'writeFile', 'fromHandle', 'toHandle',
38 'stdin' and 'stdout'. Some caveats described below.
40 The main points are as in
41 <https://hackage.haskell.org/package/pipes-bytestring-1.0.0/docs/Pipes-ByteString.html Pipes.ByteString>:
43 A 'Handle' can be associated with a 'Producer' or 'Consumer' according
44 as it is read or written to.
47 > import qualified Pipes.Text as Text
48 > import qualified Pipes.Text.IO as Text
52 > withFile "inFile.txt" ReadMode $ \hIn ->
53 > withFile "outFile.txt" WriteMode $ \hOut ->
54 > runEffect $ Text.fromHandle hIn >-> Text.toHandle hOut
56 To stream from files, the following is perhaps more Prelude-like (note that it uses Pipes.Safe):
59 > import qualified Pipes.Text as Text
60 > import qualified Pipes.Text.IO as Text
63 > main = runSafeT $ runEffect $ Text.readFile "inFile.txt" >-> Text.writeFile "outFile.txt"
65 Finally, you can stream to and from 'stdin' and 'stdout' using the predefined 'stdin'
66 and 'stdout' pipes, as with the following \"echo\" program:
68 > main = runEffect $ Text.stdin >-> Text.stdout
76 The operations exported here are a convenience, like the similar operations in
77 @Data.Text.IO@ (or rather, @Data.Text.Lazy.IO@, since, again, @Producer Text m r@ is
78 'effectful text' and something like the pipes equivalent of lazy Text.)
80 * Like the functions in @Data.Text.IO@, they attempt to work with the system encoding.
82 * Like the functions in @Data.Text.IO@, they significantly slower than ByteString operations. Where
83 you know what encoding you are working with, use @Pipes.ByteString@ and @Pipes.Text.Encoding@ instead,
84 e.g. @view utf8 Bytes.stdin@ instead of @Text.stdin@
86 * Like the functions in @Data.Text.IO@ , they use Text exceptions, not the standard Pipes protocols.
90 > view utf8 . Bytes.fromHandle :: Handle -> Producer Text IO (Producer ByteString m ())
92 yields a stream of Text, and follows
93 standard pipes protocols by reverting to (i.e. returning) the underlying byte stream
94 upon reaching any decoding error. (See especially the pipes-binary package.)
96 By contrast, something like
98 > Text.fromHandle :: Handle -> Producer Text IO ()
100 supplies a stream of text returning '()', which is convenient for many tasks,
101 but violates the pipes @pipes-binary@ approach to decoding errors and
102 throws an exception of the kind characteristic of the @text@ library instead.
107 {-| Convert a 'IO.Handle' into a text stream using a text size
108 determined by the good sense of the text library. Note with the remarks
109 at the head of this module that this
110 is slower than @view utf8 (Pipes.ByteString.fromHandle h)@
111 but uses the system encoding and has other nice @Data.Text.IO@ features
114 fromHandle :: MonadIO m => IO.Handle -> Producer Text m ()
115 fromHandle h = go where
116 go = do txt <- liftIO (T.hGetChunk h)
117 if T.null txt then return ()
120 {-# INLINABLE fromHandle#-}
122 -- | Stream text from 'stdin'
123 stdin :: MonadIO m => Producer Text m ()
124 stdin = fromHandle IO.stdin
128 {-| Stream text from a file in the simple fashion of @Data.Text.IO@
130 >>> runSafeT $ runEffect $ Text.readFile "hello.hs" >-> Text.map toUpper >-> hoist lift Text.stdout
131 MAIN = PUTSTRLN "HELLO WORLD"
134 readFile :: MonadSafe m => FilePath -> Producer Text m ()
135 readFile file = Safe.withFile file IO.ReadMode fromHandle
136 {-# INLINE readFile #-}
139 {-| Stream lines of text from a file
141 readFileLines :: MonadSafe m => FilePath -> Producer Text m ()
142 readFileLines file = Safe.withFile file IO.ReadMode fromHandleLines
144 fromHandleLines :: MonadIO m => IO.Handle -> Producer Text m ()
145 fromHandleLines h = go where
146 getLine :: IO (Either G.IOException Text)
147 getLine = try (T.hGetLine h)
149 go = do txt <- liftIO getLine
152 Right y -> do yield y
154 {-# INLINE readFileLines #-}
157 {-| Stream text to 'stdout'
159 Unlike 'toHandle', 'stdout' gracefully terminates on a broken output pipe.
161 Note: For best performance, it might be best just to use @(for source (liftIO . putStr))@
162 instead of @(source >-> stdout)@ .
164 stdout :: MonadIO m => Consumer' Text m ()
169 x <- liftIO $ try (T.putStr txt)
171 Left (G.IOError { G.ioe_type = G.ResourceVanished
172 , G.ioe_errno = Just ioe })
175 Left e -> liftIO (throwIO e)
177 {-# INLINABLE stdout #-}
180 {-| Convert a text stream into a 'Handle'
182 Note: again, for best performance, where possible use
183 @(for source (liftIO . hPutStr handle))@ instead of @(source >-> toHandle handle)@.
185 toHandle :: MonadIO m => IO.Handle -> Consumer' Text m r
186 toHandle h = for cat (liftIO . T.hPutStr h)
187 {-# INLINABLE toHandle #-}
191 -- | Stream text into a file. Uses @pipes-safe@.
192 writeFile :: (MonadSafe m) => FilePath -> Consumer' Text m ()
193 writeFile file = Safe.withFile file IO.WriteMode toHandle
194 {-# INLINE writeFile #-}