{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances #-}
module System.IO.Classes
(
module System.IO.Handle,
module System.IO.Error,
IsFilePath (..),
IsFile (..), getContents, putContents, withFile, readFile, writeFile, appendFile,
IsTextFile (..), getLine, putStr, putStrLn, gets, puts
)
where
import Prelude ()
import SDP.SafePrelude
import Data.FilePath
import qualified System.IO as IO
import System.IO.Handle
import System.IO.Error
import Control.Exception
default ()
class IsFilePath path
where
hOpen :: (MonadIO io) => IOMode -> path -> io Handle
hOpen IOMode
mode path
path = IOMode -> path -> BufferMode -> Bool -> io Handle
forall path (io :: * -> *).
(IsFilePath path, MonadIO io) =>
IOMode -> path -> BufferMode -> Bool -> io Handle
hOpenWith IOMode
mode path
path (Maybe Int -> BufferMode
BlockBuffering Maybe Int
forall a. Maybe a
Nothing) Bool
False
hOpenWith :: (MonadIO io) => IOMode -> path -> BufferMode -> Bool -> io Handle
hOpenTemp :: (MonadIO io) => path -> io (path, Handle)
class IsFile file
where
hGetContents :: (MonadIO io) => Handle -> io file
hPutContents :: (MonadIO io) => Handle -> file -> io ()
getContents :: (MonadIO io, IsFile file) => io file
getContents :: io file
getContents = Handle -> io file
forall file (io :: * -> *).
(IsFile file, MonadIO io) =>
Handle -> io file
hGetContents Handle
stdin
putContents :: (MonadIO io, IsFile file) => file -> io ()
putContents :: file -> io ()
putContents = Handle -> file -> io ()
forall file (io :: * -> *).
(IsFile file, MonadIO io) =>
Handle -> file -> io ()
hPutContents Handle
stdout
withFile :: (MonadIO io, IsFilePath path) => path -> IOMode -> (Handle -> IO a) -> io a
withFile :: path -> IOMode -> (Handle -> IO a) -> io a
withFile path
path IOMode
mode = IO a -> io a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> io a)
-> ((Handle -> IO a) -> IO a) -> (Handle -> IO a) -> io a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO Handle -> (Handle -> IO ()) -> (Handle -> IO a) -> IO a
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (IOMode -> path -> IO Handle
forall path (io :: * -> *).
(IsFilePath path, MonadIO io) =>
IOMode -> path -> io Handle
hOpen IOMode
mode path
path) Handle -> IO ()
forall (io :: * -> *). MonadIO io => Handle -> io ()
hClose
readFile :: (MonadIO io, IsFilePath p, IsFile f) => p -> io f
readFile :: p -> io f
readFile = IOMode -> p -> io Handle
forall path (io :: * -> *).
(IsFilePath path, MonadIO io) =>
IOMode -> path -> io Handle
hOpen IOMode
ReadMode (p -> io Handle) -> (Handle -> io f) -> p -> io f
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> Handle -> io f
forall file (io :: * -> *).
(IsFile file, MonadIO io) =>
Handle -> io file
hGetContents
writeFile :: (MonadIO io, IsFilePath p, IsFile f) => p -> f -> io ()
writeFile :: p -> f -> io ()
writeFile p
path f
file = p -> IOMode -> (Handle -> IO ()) -> io ()
forall (io :: * -> *) path a.
(MonadIO io, IsFilePath path) =>
path -> IOMode -> (Handle -> IO a) -> io a
withFile p
path IOMode
WriteMode (Handle -> f -> IO ()
forall file (io :: * -> *).
(IsFile file, MonadIO io) =>
Handle -> file -> io ()
`hPutContents` f
file)
appendFile :: (MonadIO io, IsFilePath p, IsFile f) => p -> f -> io ()
appendFile :: p -> f -> io ()
appendFile p
path f
file = p -> IOMode -> (Handle -> IO ()) -> io ()
forall (io :: * -> *) path a.
(MonadIO io, IsFilePath path) =>
path -> IOMode -> (Handle -> IO a) -> io a
withFile p
path IOMode
AppendMode (Handle -> f -> IO ()
forall file (io :: * -> *).
(IsFile file, MonadIO io) =>
Handle -> file -> io ()
`hPutContents` f
file)
class (IsFile text) => IsTextFile text
where
hGetLine :: (MonadIO io) => Handle -> io text
hPutStr :: (MonadIO io) => Handle -> text -> io ()
hPutStrLn :: (MonadIO io) => Handle -> text -> io ()
hPutStrLn Handle
hdl text
text = do Handle -> text -> io ()
forall text (io :: * -> *).
(IsTextFile text, MonadIO io) =>
Handle -> text -> io ()
hPutStr Handle
hdl text
text; Handle -> Char -> io ()
forall (io :: * -> *). MonadIO io => Handle -> Char -> io ()
hPutChar Handle
hdl Char
'\n'
getLine :: (MonadIO io, IsTextFile text) => io text
getLine :: io text
getLine = Handle -> io text
forall text (io :: * -> *).
(IsTextFile text, MonadIO io) =>
Handle -> io text
hGetLine Handle
stdin
putStr :: (MonadIO io, IsTextFile text) => text -> io ()
putStr :: text -> io ()
putStr = Handle -> text -> io ()
forall text (io :: * -> *).
(IsTextFile text, MonadIO io) =>
Handle -> text -> io ()
hPutStr Handle
stdout
putStrLn :: (MonadIO io, IsTextFile text) => text -> io ()
putStrLn :: text -> io ()
putStrLn = Handle -> text -> io ()
forall text (io :: * -> *).
(IsTextFile text, MonadIO io) =>
Handle -> text -> io ()
hPutStrLn Handle
stdout
gets :: (MonadIO io, IsTextFile text) => io text
gets :: io text
gets = io text
forall (io :: * -> *) text.
(MonadIO io, IsTextFile text) =>
io text
getLine
puts :: (MonadIO io, IsTextFile text) => text -> io ()
puts :: text -> io ()
puts = text -> io ()
forall (io :: * -> *) text.
(MonadIO io, IsTextFile text) =>
text -> io ()
putStrLn
instance IsFilePath FilePath
where
hOpen :: IOMode -> FilePath -> io Handle
hOpen = IO Handle -> io Handle
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO Handle -> io Handle)
-> (IOMode -> FilePath -> IO Handle)
-> IOMode
-> FilePath
-> io Handle
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... (FilePath -> IOMode -> IO Handle)
-> IOMode -> FilePath -> IO Handle
forall a b c. (a -> b -> c) -> b -> a -> c
flip FilePath -> IOMode -> IO Handle
IO.openFile
hOpenWith :: IOMode -> FilePath -> BufferMode -> Bool -> io Handle
hOpenWith IOMode
mode FilePath
path BufferMode
buf Bool
bin = do
Handle
h <- IOMode -> FilePath -> io Handle
forall path (io :: * -> *).
(IsFilePath path, MonadIO io) =>
IOMode -> path -> io Handle
hOpen IOMode
mode FilePath
path
Handle -> BufferMode -> io ()
forall (io :: * -> *). MonadIO io => Handle -> BufferMode -> io ()
hSetBuffering Handle
h BufferMode
buf
Handle -> Bool -> io ()
forall (io :: * -> *). MonadIO io => Handle -> Bool -> io ()
hSetBinaryMode Handle
h Bool
bin
Handle -> io Handle
forall (m :: * -> *) a. Monad m => a -> m a
return Handle
h
hOpenTemp :: FilePath -> io (FilePath, Handle)
hOpenTemp (FilePath
dir :/ FilePath
name) = IO (FilePath, Handle) -> io (FilePath, Handle)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (FilePath, Handle) -> io (FilePath, Handle))
-> IO (FilePath, Handle) -> io (FilePath, Handle)
forall a b. (a -> b) -> a -> b
$ (FilePath -> FilePath) -> (FilePath, Handle) -> (FilePath, Handle)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (FilePath
dir FilePath -> FilePath -> FilePath
:/) ((FilePath, Handle) -> (FilePath, Handle))
-> IO (FilePath, Handle) -> IO (FilePath, Handle)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> FilePath -> IO (FilePath, Handle)
IO.openTempFile FilePath
dir FilePath
name
instance IsFile String
where
hGetContents :: Handle -> io FilePath
hGetContents = IO FilePath -> io FilePath
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FilePath -> io FilePath)
-> (Handle -> IO FilePath) -> Handle -> io FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> IO FilePath
IO.hGetContents
hPutContents :: Handle -> FilePath -> io ()
hPutContents = IO () -> io ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> io ())
-> (Handle -> FilePath -> IO ()) -> Handle -> FilePath -> io ()
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... Handle -> FilePath -> IO ()
IO.hPutStr
instance IsTextFile String
where
hPutStrLn :: Handle -> FilePath -> io ()
hPutStrLn = IO () -> io ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> io ())
-> (Handle -> FilePath -> IO ()) -> Handle -> FilePath -> io ()
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... Handle -> FilePath -> IO ()
IO.hPutStrLn
hGetLine :: Handle -> io FilePath
hGetLine = IO FilePath -> io FilePath
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FilePath -> io FilePath)
-> (Handle -> IO FilePath) -> Handle -> io FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Handle -> IO FilePath
IO.hGetLine
hPutStr :: Handle -> FilePath -> io ()
hPutStr = IO () -> io ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> io ())
-> (Handle -> FilePath -> IO ()) -> Handle -> FilePath -> io ()
forall c d a b. (c -> d) -> (a -> b -> c) -> a -> b -> d
... Handle -> FilePath -> IO ()
IO.hPutStr