extra-1.7.6: Extra functions I use.

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

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.

Like readFile, but setting an encoding.

Like readFile, but with the encoding utf8.

Like readFile, but for binary files.

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

# Write with encoding

Write a file with a particular encoding.

Write a file with the utf8 encoding.

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

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

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 []

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.

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.

Like newTempFile but using a custom temporary directory.

Like newTempDir but using a custom temporary directory.

# File comparison

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)