module System.IO.SaferFileHandles
(
File(..)
, Binary
, FilePath
, Template
#if MIN_VERSION_base(4,2,0)
, DefaultPermissions
#endif
, IOMode(..)
, ReadMode
, WriteMode
, AppendMode
, ReadWriteMode
, ReadModes
, WriteModes
, module Control.Monad.Trans.Region
, RegionalFileHandle
, openFile, withFile
, hFileSize
#ifdef __GLASGOW_HASKELL__
, hSetFileSize
#endif
, hIsEOF
, isEOF
, BufferMode(..)
, hSetBuffering
, hGetBuffering
, hFlush
, hGetPosn
, hSetPosn
, HandlePosn
, hSeek
, SeekMode(..)
#if !defined(__NHC__)
, hTell
#endif
, hIsOpen, hIsClosed
, hIsReadable, hIsWritable
, hIsSeekable
#if !defined(__NHC__)
, hIsTerminalDevice
, hSetEcho
, hGetEcho
#endif
#ifdef __GLASGOW_HASKELL__
, hShow
#endif
, hWaitForInput
, hReady
, hGetChar
, hGetLine
, hLookAhead
, hGetContents
, hPutChar
, hPutStr
, hPutStrLn
, hPrint
, interact
, putChar
, putStr
, putStrLn
, print
, getChar
, getLine
, getContents
, readIO
, readLn
, withBinaryFile, openBinaryFile
, hSetBinaryMode
, hPutBuf
, hGetBuf
#if !defined(__NHC__) && !defined(__HUGS__)
, hPutBufNonBlocking
, hGetBufNonBlocking
#endif
, openTempFile
, openBinaryTempFile
#if MIN_VERSION_base(4,2,0)
, openTempFileWithDefaultPermissions
, openBinaryTempFileWithDefaultPermissions
#endif
#if MIN_VERSION_base(4,2,0) && !defined(__NHC__) && !defined(__HUGS__)
, hSetEncoding
, hGetEncoding
, TextEncoding
, latin1
, utf8, utf8_bom
, utf16, utf16le, utf16be
, utf32, utf32le, utf32be
, localeEncoding
, mkTextEncoding
, hSetNewlineMode
, Newline(..)
, nativeNewline
, NewlineMode(..)
, noNewlineTranslation, universalNewlineMode, nativeNewlineMode
#endif
)
where
import Prelude ( Integer )
import Control.Monad ( return, (>>=), fail )
import Data.Function ( ($) )
import Data.Bool ( Bool(False, True) )
import Data.Char ( Char, String )
import Data.Int ( Int )
import Data.Maybe ( Maybe, fromJust )
import Text.Show ( Show )
import Text.Read ( Read )
import Foreign.Ptr ( Ptr )
import qualified System.IO as SIO
import Data.Function.Unicode ( (∘) )
import Control.Monad.CatchIO ( MonadCatchIO )
import Control.Monad.IO.Class ( MonadIO, liftIO )
import Control.Monad.Trans.Region
import Control.Monad.Trans.Region.Unsafe ( internalHandle )
#ifdef __HADDOCK__
import Control.Resource ( Resource )
#endif
import System.IO.ExplicitIOModes ( IOMode(..)
, ReadMode
, WriteMode
, AppendMode
, ReadWriteMode
, ReadModes
, WriteModes
, FilePath
, BufferMode(..)
, HandlePosn
, SeekMode(..)
#if MIN_VERSION_base(4,2,0) && !defined(__NHC__) && !defined(__HUGS__)
, TextEncoding
, latin1
, utf8, utf8_bom
, utf16, utf16le, utf16be
, utf32, utf32le, utf32be
, localeEncoding
, Newline(..)
, nativeNewline
, NewlineMode(..)
, noNewlineTranslation
, universalNewlineMode
, nativeNewlineMode
#endif
)
import qualified System.IO.ExplicitIOModes as E
import System.IO.SaferFileHandles.Internal ( RegionalFileHandle
, File(..)
, Binary
, Template
#if MIN_VERSION_base(4,2,0)
, DefaultPermissions
#endif
, mbFilePath
, wrap, wrap2, wrap3
)
openFile ∷ MonadCatchIO pr
⇒ FilePath
→ IOMode ioMode
→ RegionT s pr
(RegionalFileHandle ioMode (RegionT s pr))
openFile filePath ioMode = open $ File False filePath ioMode
withFile ∷ MonadCatchIO pr
⇒ FilePath
→ IOMode ioMode
→ (∀ s. RegionalFileHandle ioMode (RegionT s pr)
→ RegionT s pr α
)
→ pr α
withFile filePath ioMode = with $ File False filePath ioMode
hFileSize ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → cr Integer
hFileSize = wrap E.hFileSize
#ifdef __GLASGOW_HASKELL__
hSetFileSize ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → Integer → cr ()
hSetFileSize = wrap2 E.hSetFileSize
#endif
hIsEOF ∷ (pr `ParentOf` cr, MonadIO cr, ReadModes ioMode)
⇒ RegionalFileHandle ioMode pr → cr Bool
hIsEOF = wrap E.hIsEOF
isEOF ∷ MonadIO m ⇒ m Bool
isEOF = liftIO $ E.isEOF
hSetBuffering ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → BufferMode → cr ()
hSetBuffering = wrap2 E.hSetBuffering
hGetBuffering ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → cr BufferMode
hGetBuffering = wrap E.hGetBuffering
hFlush ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → cr ()
hFlush = wrap E.hFlush
hGetPosn ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → cr HandlePosn
hGetPosn = wrap E.hGetPosn
hSetPosn ∷ MonadIO m ⇒ HandlePosn → m ()
hSetPosn = liftIO ∘ E.hSetPosn
hSeek ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → SeekMode → Integer → cr ()
hSeek = wrap3 E.hSeek
#if !defined(__NHC__)
hTell ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → cr Integer
hTell = wrap E.hTell
#endif
hIsOpen ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → cr Bool
hIsOpen = wrap E.hIsOpen
hIsClosed ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → cr Bool
hIsClosed = wrap E.hIsClosed
hIsReadable ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → cr Bool
hIsReadable = wrap E.hIsReadable
hIsWritable ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → cr Bool
hIsWritable = wrap E.hIsWritable
hIsSeekable ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → cr Bool
hIsSeekable = wrap E.hIsSeekable
#if !defined(__NHC__)
hIsTerminalDevice ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → cr Bool
hIsTerminalDevice = wrap E.hIsTerminalDevice
hSetEcho ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → Bool → cr ()
hSetEcho = wrap2 E.hSetEcho
hGetEcho ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → cr Bool
hGetEcho = wrap E.hGetEcho
#endif
#ifdef __GLASGOW_HASKELL__
hShow ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → cr String
hShow = wrap E.hShow
#endif
hWaitForInput ∷ (pr `ParentOf` cr, MonadIO cr, ReadModes ioMode)
⇒ RegionalFileHandle ioMode pr → Int → cr Bool
hWaitForInput = wrap2 E.hWaitForInput
hReady ∷ (pr `ParentOf` cr, MonadIO cr, ReadModes ioMode)
⇒ RegionalFileHandle ioMode pr → cr Bool
hReady = wrap E.hReady
hGetChar ∷ (pr `ParentOf` cr, MonadIO cr, ReadModes ioMode)
⇒ RegionalFileHandle ioMode pr → cr Char
hGetChar = wrap E.hGetChar
hGetLine ∷ (pr `ParentOf` cr, MonadIO cr, ReadModes ioMode)
⇒ RegionalFileHandle ioMode pr → cr String
hGetLine = wrap E.hGetLine
hLookAhead ∷ (pr `ParentOf` cr, MonadIO cr, ReadModes ioMode)
⇒ RegionalFileHandle ioMode pr → cr Char
hLookAhead = wrap E.hLookAhead
hGetContents ∷ (pr `ParentOf` cr, MonadIO cr, ReadModes ioMode)
⇒ RegionalFileHandle ioMode pr → cr String
hGetContents = wrap E.hGetContents
hPutChar ∷ (pr `ParentOf` cr, MonadIO cr, WriteModes ioMode)
⇒ RegionalFileHandle ioMode pr → Char → cr ()
hPutChar = wrap2 E.hPutChar
hPutStr ∷ (pr `ParentOf` cr, MonadIO cr, WriteModes ioMode)
⇒ RegionalFileHandle ioMode pr → String → cr ()
hPutStr = wrap2 E.hPutStr
hPutStrLn ∷ (pr `ParentOf` cr, MonadIO cr, WriteModes ioMode)
⇒ RegionalFileHandle ioMode pr → String → cr ()
hPutStrLn = wrap2 E.hPutStrLn
hPrint ∷ (pr `ParentOf` cr, MonadIO cr, WriteModes ioMode, Show α)
⇒ RegionalFileHandle ioMode pr → α → cr ()
hPrint = wrap2 E.hPrint
interact ∷ MonadIO m ⇒ (String → String) → m ()
interact f = liftIO $ SIO.interact f
putChar ∷ MonadIO m ⇒ Char → m ()
putChar c = liftIO $ SIO.putChar c
putStr ∷ MonadIO m ⇒ String → m ()
putStr s = liftIO $ SIO.putStr s
putStrLn ∷ MonadIO m ⇒ String → m ()
putStrLn s = liftIO $ SIO.putStrLn s
print ∷ (MonadIO m, Show α) ⇒ α → m ()
print x = liftIO $ SIO.print x
getChar ∷ MonadIO m ⇒ m Char
getChar = liftIO SIO.getChar
getLine ∷ MonadIO m ⇒ m String
getLine = liftIO SIO.getLine
getContents ∷ MonadIO m ⇒ m String
getContents = liftIO SIO.getContents
readIO ∷ (MonadIO m, Read α) ⇒ String → m α
readIO s = liftIO $ SIO.readIO s
readLn ∷ (MonadIO m, Read α) ⇒ m α
readLn = liftIO SIO.readLn
withBinaryFile ∷ MonadCatchIO pr
⇒ FilePath
→ IOMode ioMode
→ (∀ s. RegionalFileHandle ioMode (RegionT s pr)
→ RegionT s pr α
)
→ pr α
withBinaryFile filePath ioMode = with $ File True filePath ioMode
openBinaryFile ∷ MonadCatchIO pr
⇒ FilePath
→ IOMode ioMode
→ RegionT s pr
(RegionalFileHandle ioMode (RegionT s pr))
openBinaryFile filePath ioMode = open $ File True filePath ioMode
hSetBinaryMode ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → Bool → cr ()
hSetBinaryMode = wrap2 E.hSetBinaryMode
hPutBuf ∷ (pr `ParentOf` cr, MonadIO cr, WriteModes ioMode)
⇒ RegionalFileHandle ioMode pr → Ptr α → Int → cr ()
hPutBuf = wrap3 E.hPutBuf
hGetBuf ∷ (pr `ParentOf` cr, MonadIO cr, ReadModes ioMode)
⇒ RegionalFileHandle ioMode pr → Ptr α → Int → cr Int
hGetBuf = wrap3 E.hGetBuf
#if !defined(__NHC__) && !defined(__HUGS__)
hPutBufNonBlocking ∷ (pr `ParentOf` cr, MonadIO cr, WriteModes ioMode)
⇒ RegionalFileHandle ioMode pr → Ptr α → Int → cr Int
hPutBufNonBlocking = wrap3 E.hPutBufNonBlocking
hGetBufNonBlocking ∷ (pr `ParentOf` cr, MonadIO cr, ReadModes ioMode)
⇒ RegionalFileHandle ioMode pr → Ptr α → Int → cr Int
hGetBufNonBlocking = wrap3 E.hGetBufNonBlocking
#endif
genOpenTempFile ∷ MonadCatchIO pr
⇒ Binary
→ FilePath
→ Template
→ RegionT s pr ( FilePath
, RegionalFileHandle ReadWriteMode (RegionT s pr)
)
genOpenTempFile binary filePath template = do
rh ← open $ TempFile binary filePath template
#if MIN_VERSION_base(4,2,0)
False
#endif
return (generatedFilePath rh, rh)
generatedFilePath ∷ RegionalFileHandle ioMode r → FilePath
generatedFilePath = fromJust ∘ mbFilePath ∘ internalHandle
openTempFile ∷ MonadCatchIO pr
⇒ FilePath
→ Template
→ RegionT s pr ( FilePath
, RegionalFileHandle ReadWriteMode (RegionT s pr)
)
openTempFile = genOpenTempFile False
openBinaryTempFile ∷
MonadCatchIO pr
⇒ FilePath
→ Template
→ RegionT s pr ( FilePath
, RegionalFileHandle ReadWriteMode (RegionT s pr)
)
openBinaryTempFile = genOpenTempFile True
#if MIN_VERSION_base(4,2,0)
genOpenTempFileWithDefaultPermissions ∷
MonadCatchIO pr
⇒ Binary
→ FilePath
→ Template
→ RegionT s pr ( FilePath
, RegionalFileHandle ReadWriteMode (RegionT s pr)
)
genOpenTempFileWithDefaultPermissions binary filePath template = do
rh ← open $ TempFile binary filePath template True
return (generatedFilePath rh, rh)
openTempFileWithDefaultPermissions ∷
MonadCatchIO pr
⇒ FilePath
→ Template
→ RegionT s pr ( FilePath
, RegionalFileHandle ReadWriteMode (RegionT s pr)
)
openTempFileWithDefaultPermissions = genOpenTempFileWithDefaultPermissions False
openBinaryTempFileWithDefaultPermissions ∷
MonadCatchIO pr
⇒ FilePath
→ Template
→ RegionT s pr ( FilePath
, RegionalFileHandle ReadWriteMode (RegionT s pr)
)
openBinaryTempFileWithDefaultPermissions = genOpenTempFileWithDefaultPermissions True
#endif
#if MIN_VERSION_base(4,2,0) && !defined(__NHC__) && !defined(__HUGS__)
hSetEncoding ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → TextEncoding → cr ()
hSetEncoding = wrap2 E.hSetEncoding
hGetEncoding ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → cr (Maybe TextEncoding)
hGetEncoding = wrap E.hGetEncoding
mkTextEncoding ∷ MonadIO m ⇒ String → m TextEncoding
mkTextEncoding = liftIO ∘ E.mkTextEncoding
hSetNewlineMode ∷ (pr `ParentOf` cr, MonadIO cr)
⇒ RegionalFileHandle ioMode pr → NewlineMode → cr ()
hSetNewlineMode = wrap2 E.hSetNewlineMode
#endif