-- | Simple resource management functions
{-# LANGUAGE RankNTypes #-}
module Pipes.Safe.Prelude (
-- * Handle management
withFile,
-- * String I/O-- $strings
readFile,
writeFile
) where
import Control.Monad.IO.Class (MonadIO(liftIO))
import Pipes (Producer', Consumer')
import Pipes.Safe (bracket, MonadSafe)
import qualified Pipes.Prelude as P
import qualified System.IO as IO
import Prelude hiding (readFile, writeFile)
-- | Acquire a 'IO.Handle' within 'MonadSafe'withFile :: (MonadSafe m) =>FilePath -> IO.IOMode -> (IO.Handle -> mr) -> mrwithFilefileioMode = bracket (liftIO$IO.openFilefileioMode) (liftIO.IO.hClose)
{-# INLINABLE withFile #-}{- $strings
Note that 'String's are very inefficient, and I will release future separate
packages with 'Data.ByteString.ByteString' and 'Data.Text.Text' operations.
I only provide these to allow users to test simple I/O without requiring any
additional library dependencies.
-}{-| Read lines from a file, automatically opening and closing the file as
necessary
-}readFile :: (MonadSafe m) =>FilePath -> Producer'Stringm()readFilefile = withFilefileIO.ReadModeP.fromHandle{-# INLINABLE readFile #-}{-| Write lines to a file, automatically opening and closing the file as
necessary
-}writeFile :: (MonadSafe m) =>FilePath -> Consumer'StringmrwriteFilefile = withFilefileIO.WriteModeP.toHandle{-# INLINABLE writeFile #-}