extra-1.7.7: Extra functions I use.

Safe HaskellNone
LanguageHaskell2010

System.IO.Extra

Contents

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

module System.IO

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 Source #

A strict version of readFile. When the string is produced, the entire file will have been read into memory and the file handle will have been closed. Closing the file handle does not rely on the garbage collector.

\(filter isHexDigit -> s) -> fmap (== s) $ withTempFile $ \file -> do writeFile file s; readFile' file

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)