module Util.IOExtras(
catchEOF,
catchAlreadyExists,
catchDoesNotExist,
catchErrorCalls,
hGetLineR,
simpleModifyIORef,
) where
import System.IO.Error
import System.IO
import Data.IORef
import Control.Exception
catchEOF :: IO a -> IO (Maybe a)
catchEOF action = catchGeneral isEOFError action
catchAlreadyExists :: IO a -> IO (Maybe a)
catchAlreadyExists action = catchGeneral isAlreadyExistsError action
catchDoesNotExist :: IO a -> IO (Maybe a)
catchDoesNotExist action = catchGeneral isDoesNotExistError action
catchGeneral :: (IOError -> Bool) -> IO a -> IO (Maybe a)
catchGeneral discriminator action =
do
result <- tryJust
(\ excep ->
case ioErrors excep of
Nothing -> Nothing
Just ioError ->
if discriminator ioError
then
Just ()
else
Nothing
)
action
case result of
Left () -> return Nothing
Right success -> return (Just success)
catchErrorCalls :: IO a -> IO (Either String a)
catchErrorCalls action = tryJust errorCalls action
hGetLineR :: Read a => Handle -> IO a
hGetLineR handle =
do
line <- hGetLine handle
return (read line)
simpleModifyIORef :: IORef a -> (a -> (a,b)) -> IO b
simpleModifyIORef = atomicModifyIORef