extra-1.8: Extra functions I use.
Safe HaskellNone
LanguageHaskell2010

System.IO.Extra

Description

More IO functions. The functions include ones for reading files with specific encodings, strictly reading files, and writing files with encodings. There are also some simple temporary file functions, more advanced alternatives can be found in the exceptions package.

Synopsis

Documentation

readFile' :: FilePath -> IO String #

type FilePath = String #

data IO a #

Instances

Instances details
MonadIO IO 
Instance details

Defined in Control.Monad.IO.Class

Methods

liftIO :: IO a -> IO a

Alternative IO 
Instance details

Defined in GHC.Internal.Base

Methods

empty :: IO a

(<|>) :: IO a -> IO a -> IO a

some :: IO a -> IO [a]

many :: IO a -> IO [a]

Applicative IO 
Instance details

Defined in GHC.Internal.Base

Methods

pure :: a -> IO a

(<*>) :: IO (a -> b) -> IO a -> IO b

liftA2 :: (a -> b -> c) -> IO a -> IO b -> IO c

(*>) :: IO a -> IO b -> IO b

(<*) :: IO a -> IO b -> IO a

Functor IO 
Instance details

Defined in GHC.Internal.Base

Methods

fmap :: (a -> b) -> IO a -> IO b #

(<$) :: a -> IO b -> IO a #

Monad IO 
Instance details

Defined in GHC.Internal.Base

Methods

(>>=) :: IO a -> (a -> IO b) -> IO b #

(>>) :: IO a -> IO b -> IO b #

return :: a -> IO a #

MonadPlus IO 
Instance details

Defined in GHC.Internal.Base

Methods

mzero :: IO a #

mplus :: IO a -> IO a -> IO a #

MonadFail IO 
Instance details

Defined in GHC.Internal.Control.Monad.Fail

Methods

fail :: String -> IO a #

Quasi IO 
Instance details

Defined in Language.Haskell.TH.Syntax

Methods

qNewName :: String -> IO Name

qReport :: Bool -> String -> IO ()

qRecover :: IO a -> IO a -> IO a

qLookupName :: Bool -> String -> IO (Maybe Name)

qReify :: Name -> IO Info

qReifyFixity :: Name -> IO (Maybe Fixity)

qReifyType :: Name -> IO Type

qReifyInstances :: Name -> [Type] -> IO [Dec]

qReifyRoles :: Name -> IO [Role]

qReifyAnnotations :: Data a => AnnLookup -> IO [a]

qReifyModule :: Module -> IO ModuleInfo

qReifyConStrictness :: Name -> IO [DecidedStrictness]

qLocation :: IO Loc

qRunIO :: IO a -> IO a

qGetPackageRoot :: IO FilePath

qAddDependentFile :: FilePath -> IO ()

qAddTempFile :: String -> IO FilePath

qAddTopDecls :: [Dec] -> IO ()

qAddForeignFilePath :: ForeignSrcLang -> String -> IO ()

qAddModFinalizer :: Q () -> IO ()

qAddCorePlugin :: String -> IO ()

qGetQ :: Typeable a => IO (Maybe a)

qPutQ :: Typeable a => a -> IO ()

qIsExtEnabled :: Extension -> IO Bool

qExtsEnabled :: IO [Extension]

qPutDoc :: DocLoc -> String -> IO ()

qGetDoc :: DocLoc -> IO (Maybe String)

Quote IO 
Instance details

Defined in Language.Haskell.TH.Syntax

Methods

newName :: String -> IO Name

a ~ () => HPrintfType (IO a) 
Instance details

Defined in Text.Printf

Methods

hspr :: Handle -> String -> [UPrintf] -> IO a

a ~ () => PrintfType (IO a) 
Instance details

Defined in Text.Printf

Methods

spr :: String -> [UPrintf] -> IO a

Monoid a => Monoid (IO a) 
Instance details

Defined in GHC.Internal.Base

Methods

mempty :: IO a #

mappend :: IO a -> IO a -> IO a #

mconcat :: [IO a] -> IO a #

Semigroup a => Semigroup (IO a) 
Instance details

Defined in GHC.Internal.Base

Methods

(<>) :: IO a -> IO a -> IO a #

sconcat :: NonEmpty (IO a) -> IO a

stimes :: Integral b => b -> IO a -> IO a

data Handle #

Instances

Instances details
Show Handle 
Instance details

Defined in GHC.Internal.IO.Handle.Types

Methods

showsPrec :: Int -> Handle -> ShowS

show :: Handle -> String

showList :: [Handle] -> ShowS

Eq Handle 
Instance details

Defined in GHC.Internal.IO.Handle.Types

Methods

(==) :: Handle -> Handle -> Bool

(/=) :: Handle -> Handle -> Bool

appendFile :: FilePath -> String -> IO () #

getChar :: IO Char #

getContents :: IO String #

getLine :: IO String #

interact :: (String -> String) -> IO () #

putChar :: Char -> IO () #

putStr :: String -> IO () #

putStrLn :: String -> IO () #

readFile :: FilePath -> IO String #

readIO :: Read a => String -> IO a #

readLn :: Read a => IO a #

writeFile :: FilePath -> String -> IO () #

print :: Show a => a -> IO () #

hFlush :: Handle -> IO () #

data TextEncoding #

Instances

Instances details
Show TextEncoding 
Instance details

Defined in GHC.Internal.IO.Encoding.Types

Methods

showsPrec :: Int -> TextEncoding -> ShowS

show :: TextEncoding -> String

showList :: [TextEncoding] -> ShowS

data IOMode #

Instances

Instances details
Enum IOMode 
Instance details

Defined in GHC.Internal.IO.IOMode

Ix IOMode 
Instance details

Defined in GHC.Internal.IO.IOMode

Methods

range :: (IOMode, IOMode) -> [IOMode]

index :: (IOMode, IOMode) -> IOMode -> Int

unsafeIndex :: (IOMode, IOMode) -> IOMode -> Int

inRange :: (IOMode, IOMode) -> IOMode -> Bool

rangeSize :: (IOMode, IOMode) -> Int

unsafeRangeSize :: (IOMode, IOMode) -> Int

Read IOMode 
Instance details

Defined in GHC.Internal.IO.IOMode

Show IOMode 
Instance details

Defined in GHC.Internal.IO.IOMode

Methods

showsPrec :: Int -> IOMode -> ShowS

show :: IOMode -> String

showList :: [IOMode] -> ShowS

Eq IOMode 
Instance details

Defined in GHC.Internal.IO.IOMode

Methods

(==) :: IOMode -> IOMode -> Bool

(/=) :: IOMode -> IOMode -> Bool

Ord IOMode 
Instance details

Defined in GHC.Internal.IO.IOMode

Methods

compare :: IOMode -> IOMode -> Ordering

(<) :: IOMode -> IOMode -> Bool

(<=) :: IOMode -> IOMode -> Bool

(>) :: IOMode -> IOMode -> Bool

(>=) :: IOMode -> IOMode -> Bool

max :: IOMode -> IOMode -> IOMode

min :: IOMode -> IOMode -> IOMode

hGetContents :: Handle -> IO String #

withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r #

hGetContents' :: Handle -> IO String #

withBinaryFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r #

hPutStr :: Handle -> String -> IO () #

hClose :: Handle -> IO () #

data BufferMode #

Constructors

NoBuffering 
LineBuffering 
BlockBuffering (Maybe Int) 

Instances

Instances details
Read BufferMode 
Instance details

Defined in GHC.Internal.IO.Handle.Types

Show BufferMode 
Instance details

Defined in GHC.Internal.IO.Handle.Types

Methods

showsPrec :: Int -> BufferMode -> ShowS

show :: BufferMode -> String

showList :: [BufferMode] -> ShowS

Eq BufferMode 
Instance details

Defined in GHC.Internal.IO.Handle.Types

Methods

(==) :: BufferMode -> BufferMode -> Bool

(/=) :: BufferMode -> BufferMode -> Bool

Ord BufferMode 
Instance details

Defined in GHC.Internal.IO.Handle.Types

hFileSize :: Handle -> IO Integer #

hGetBuf :: Handle -> Ptr a -> Int -> IO Int #

hGetEcho :: Handle -> IO Bool #

hIsClosed :: Handle -> IO Bool #

hIsEOF :: Handle -> IO Bool #

hIsOpen :: Handle -> IO Bool #

hIsReadable :: Handle -> IO Bool #

hIsSeekable :: Handle -> IO Bool #

hIsWritable :: Handle -> IO Bool #

hLookAhead :: Handle -> IO Char #

hSeek :: Handle -> SeekMode -> Integer -> IO () #

hSetBinaryMode :: Handle -> Bool -> IO () #

hSetEcho :: Handle -> Bool -> IO () #

hSetFileSize :: Handle -> Integer -> IO () #

hShow :: Handle -> IO String #

hTell :: Handle -> IO Integer #

isEOF :: IO Bool #

hGetBufNonBlocking :: Handle -> Ptr a -> Int -> IO Int #

hGetBufSome :: Handle -> Ptr a -> Int -> IO Int #

hGetChar :: Handle -> IO Char #

hGetLine :: Handle -> IO String #

hPutBuf :: Handle -> Ptr a -> Int -> IO () #

hPutBufNonBlocking :: Handle -> Ptr a -> Int -> IO Int #

hPutChar :: Handle -> Char -> IO () #

hPutStrLn :: Handle -> String -> IO () #

hWaitForInput :: Handle -> Int -> IO Bool #

fixIO :: (a -> IO a) -> IO a #

getContents' :: IO String #

hPrint :: Show a => Handle -> a -> IO () #

hReady :: Handle -> IO Bool #

data SeekMode #

Instances

Instances details
Enum SeekMode 
Instance details

Defined in GHC.Internal.IO.Device

Ix SeekMode 
Instance details

Defined in GHC.Internal.IO.Device

Read SeekMode 
Instance details

Defined in GHC.Internal.IO.Device

Show SeekMode 
Instance details

Defined in GHC.Internal.IO.Device

Methods

showsPrec :: Int -> SeekMode -> ShowS

show :: SeekMode -> String

showList :: [SeekMode] -> ShowS

Eq SeekMode 
Instance details

Defined in GHC.Internal.IO.Device

Methods

(==) :: SeekMode -> SeekMode -> Bool

(/=) :: SeekMode -> SeekMode -> Bool

Ord SeekMode 
Instance details

Defined in GHC.Internal.IO.Device

Methods

compare :: SeekMode -> SeekMode -> Ordering

(<) :: SeekMode -> SeekMode -> Bool

(<=) :: SeekMode -> SeekMode -> Bool

(>) :: SeekMode -> SeekMode -> Bool

(>=) :: SeekMode -> SeekMode -> Bool

max :: SeekMode -> SeekMode -> SeekMode

min :: SeekMode -> SeekMode -> SeekMode

data HandlePosn #

Instances

Instances details
Show HandlePosn 
Instance details

Defined in GHC.Internal.IO.Handle

Methods

showsPrec :: Int -> HandlePosn -> ShowS

show :: HandlePosn -> String

showList :: [HandlePosn] -> ShowS

Eq HandlePosn 
Instance details

Defined in GHC.Internal.IO.Handle

Methods

(==) :: HandlePosn -> HandlePosn -> Bool

(/=) :: HandlePosn -> HandlePosn -> Bool

data Newline #

Constructors

LF 
CRLF 

Instances

Instances details
Read Newline 
Instance details

Defined in GHC.Internal.IO.Handle.Types

Show Newline 
Instance details

Defined in GHC.Internal.IO.Handle.Types

Methods

showsPrec :: Int -> Newline -> ShowS

show :: Newline -> String

showList :: [Newline] -> ShowS

Eq Newline 
Instance details

Defined in GHC.Internal.IO.Handle.Types

Methods

(==) :: Newline -> Newline -> Bool

(/=) :: Newline -> Newline -> Bool

Ord Newline 
Instance details

Defined in GHC.Internal.IO.Handle.Types

Methods

compare :: Newline -> Newline -> Ordering

(<) :: Newline -> Newline -> Bool

(<=) :: Newline -> Newline -> Bool

(>) :: Newline -> Newline -> Bool

(>=) :: Newline -> Newline -> Bool

max :: Newline -> Newline -> Newline

min :: Newline -> Newline -> Newline

data NewlineMode #

Constructors

NewlineMode 

Instances

Instances details
Read NewlineMode 
Instance details

Defined in GHC.Internal.IO.Handle.Types

Show NewlineMode 
Instance details

Defined in GHC.Internal.IO.Handle.Types

Methods

showsPrec :: Int -> NewlineMode -> ShowS

show :: NewlineMode -> String

showList :: [NewlineMode] -> ShowS

Eq NewlineMode 
Instance details

Defined in GHC.Internal.IO.Handle.Types

Methods

(==) :: NewlineMode -> NewlineMode -> Bool

(/=) :: NewlineMode -> NewlineMode -> Bool

Ord NewlineMode 
Instance details

Defined in GHC.Internal.IO.Handle.Types

captureOutput :: IO a -> IO (String, a) Source #

Capture the stdout and stderr of a computation.

captureOutput (print 1) == pure ("1\n",())

withBuffering :: Handle -> BufferMode -> IO a -> IO a Source #

Execute an action with a custom BufferMode, a wrapper around hSetBuffering.

Read encoding

readFileEncoding :: TextEncoding -> FilePath -> IO String Source #

Like readFile, but setting an encoding.

readFileUTF8 :: FilePath -> IO String Source #

Like readFile, but with the encoding utf8.

readFileBinary :: FilePath -> IO String Source #

Like readFile, but for binary files.

Strict reading

readFile' :: FilePath -> IO String #

readFileEncoding' :: TextEncoding -> FilePath -> IO String Source #

A strict version of readFileEncoding, see readFile' for details.

readFileUTF8' :: FilePath -> IO String Source #

A strict version of readFileUTF8, see readFile' for details.

readFileBinary' :: FilePath -> IO String Source #

A strict version of readFileBinary, see readFile' for details.

Write with encoding

writeFileEncoding :: TextEncoding -> FilePath -> String -> IO () Source #

Write a file with a particular encoding.

writeFileUTF8 :: FilePath -> String -> IO () Source #

Write a file with the utf8 encoding.

\s -> withTempFile $ \file -> do writeFileUTF8 file s; fmap (== s) $ readFileUTF8' file

writeFileBinary :: FilePath -> String -> IO () Source #

Write a binary file.

\(ASCIIString s) -> withTempFile $ \file -> do writeFileBinary file s; fmap (== s) $ readFileBinary' file

Temporary files

withTempFile :: (FilePath -> IO a) -> IO a Source #

Create a temporary file in the temporary directory. The file will be deleted after the action completes (provided the file is not still open). The FilePath will not have any file extension, will exist, and will be zero bytes long. If you require a file with a specific name, use withTempDir.

withTempFile doesFileExist == pure True
(doesFileExist =<< withTempFile pure) == pure False
withTempFile readFile' == pure ""

withTempDir :: (FilePath -> IO a) -> IO a Source #

Create a temporary directory inside the system temporary directory. The directory will be deleted after the action completes.

withTempDir doesDirectoryExist == pure True
(doesDirectoryExist =<< withTempDir pure) == pure False
withTempDir listFiles == pure []

newTempFile :: IO (FilePath, IO ()) Source #

Provide a function to create a temporary file, and a way to delete a temporary file. Most users should use withTempFile which combines these operations.

newTempDir :: IO (FilePath, IO ()) Source #

Provide a function to create a temporary directory, and a way to delete a temporary directory. Most users should use withTempDir which combines these operations.

newTempFileWithin :: FilePath -> IO (FilePath, IO ()) Source #

Like newTempFile but using a custom temporary directory.

newTempDirWithin :: FilePath -> IO (FilePath, IO ()) Source #

Like newTempDir but using a custom temporary directory.

File comparison

fileEq :: FilePath -> FilePath -> IO Bool Source #

Returns True if both files have the same content. Raises an error if either file is missing.

fileEq "does_not_exist1" "does_not_exist2" == undefined
fileEq "does_not_exist" "does_not_exist" == undefined
withTempFile $ \f1 -> fileEq "does_not_exist" f1 == undefined
withTempFile $ \f1 -> withTempFile $ \f2 -> fileEq f1 f2
withTempFile $ \f1 -> withTempFile $ \f2 -> writeFile f1 "a" >> writeFile f2 "a" >> fileEq f1 f2
withTempFile $ \f1 -> withTempFile $ \f2 -> writeFile f1 "a" >> writeFile f2 "b" >> notM (fileEq f1 f2)