From 3b27b572ed0bd65e0e5a2d8d1debf78a17048c52 Mon Sep 17 00:00:00 2001 From: Sidharth Kapur Date: Fri, 5 Feb 2016 23:39:37 -0600 Subject: Add readFileLines function --- Pipes/Text/IO.hs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Pipes/Text/IO.hs b/Pipes/Text/IO.hs index b7ef5cd..bacb207 100644 --- a/Pipes/Text/IO.hs +++ b/Pipes/Text/IO.hs @@ -136,6 +136,24 @@ readFile file = Safe.withFile file IO.ReadMode fromHandle {-# INLINE readFile #-} +{-| Stream lines of text from a file +-} +readFileLines :: MonadSafe m => FilePath -> Producer Text m () +readFileLines file = Safe.withFile file IO.ReadMode fromHandleLines + where + fromHandleLines :: MonadIO m => IO.Handle -> Producer Text m () + fromHandleLines h = go where + getLine :: IO (Either G.IOException Text) + getLine = try (T.hGetLine h) + + go = do txt <- liftIO getLine + case txt of + Left e -> return () + Right y -> do yield y + go +{-# INLINE readFileLines #-} + + {-| Stream text to 'stdout' Unlike 'toHandle', 'stdout' gracefully terminates on a broken output pipe. -- cgit v1.2.3 From 0396816d948fcb60d3cb7664d5e532a0c64d5216 Mon Sep 17 00:00:00 2001 From: Sidharth Kapur Date: Fri, 5 Feb 2016 23:41:43 -0600 Subject: Export readFileLines function --- Pipes/Text/IO.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/Pipes/Text/IO.hs b/Pipes/Text/IO.hs index bacb207..4e0c4a9 100644 --- a/Pipes/Text/IO.hs +++ b/Pipes/Text/IO.hs @@ -13,6 +13,7 @@ module Pipes.Text.IO fromHandle , stdin , readFile + , readFileLines -- * Consumers , toHandle , stdout -- cgit v1.2.3 From 3412dff4538e0335d5703751efac5017ea02cabf Mon Sep 17 00:00:00 2001 From: Sidharth Kapur Date: Sat, 6 Feb 2016 11:18:37 -0600 Subject: Rename function to readFileLn, export fromHandleLn --- Pipes/Text/IO.hs | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/Pipes/Text/IO.hs b/Pipes/Text/IO.hs index 4e0c4a9..51c6926 100644 --- a/Pipes/Text/IO.hs +++ b/Pipes/Text/IO.hs @@ -11,9 +11,10 @@ module Pipes.Text.IO -- * Producers fromHandle + , fromHandleLn , stdin , readFile - , readFileLines + , readFileLn -- * Consumers , toHandle , stdout @@ -120,6 +121,19 @@ fromHandle h = go where go {-# INLINABLE fromHandle#-} + +fromHandleLn :: MonadIO m => IO.Handle -> Producer Text m () +fromHandleLn h = go where + getLine :: IO (Either G.IOException Text) + getLine = try (T.hGetLine h) + + go = do txt <- liftIO getLine + case txt of + Left e -> return () + Right y -> do yield y + go +{-# INLINABLE fromHandleLn #-} + -- | Stream text from 'stdin' stdin :: MonadIO m => Producer Text m () stdin = fromHandle IO.stdin @@ -139,20 +153,9 @@ readFile file = Safe.withFile file IO.ReadMode fromHandle {-| Stream lines of text from a file -} -readFileLines :: MonadSafe m => FilePath -> Producer Text m () -readFileLines file = Safe.withFile file IO.ReadMode fromHandleLines - where - fromHandleLines :: MonadIO m => IO.Handle -> Producer Text m () - fromHandleLines h = go where - getLine :: IO (Either G.IOException Text) - getLine = try (T.hGetLine h) - - go = do txt <- liftIO getLine - case txt of - Left e -> return () - Right y -> do yield y - go -{-# INLINE readFileLines #-} +readFileLn :: MonadSafe m => FilePath -> Producer Text m () +readFileLn file = Safe.withFile file IO.ReadMode fromHandleLn +{-# INLINE readFileLn #-} {-| Stream text to 'stdout' -- cgit v1.2.3