extra-1.4.2: Extra functions I use.

Safe Haskell | None
Language | Haskell2010

System.IO.Extra

Contents

- Read encoding
- Strict reading
- Write with encoding
- Temporary files

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

- module System.IO
- captureOutput :: IO a -> IO (String, a)
- withBuffering :: Handle -> BufferMode -> IO a -> IO a
- readFileEncoding :: TextEncoding -> FilePath -> IO String
- readFileUTF8 :: FilePath -> IO String
- readFileBinary :: FilePath -> IO String
- readFile' :: FilePath -> IO String
- readFileEncoding' :: TextEncoding -> FilePath -> IO String
- readFileUTF8' :: FilePath -> IO String
- readFileBinary' :: FilePath -> IO String
- writeFileEncoding :: TextEncoding -> FilePath -> String -> IO ()
- writeFileUTF8 :: FilePath -> String -> IO ()
- writeFileBinary :: FilePath -> String -> IO ()
- withTempFile :: (FilePath -> IO a) -> IO a
- withTempDir :: (FilePath -> IO a) -> IO a
- newTempFile :: IO (FilePath, IO ())
- newTempDir :: IO (FilePath, IO ())

# Documentation

module System.IO

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

Capture the `stdout` and `stderr` of a computation.

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

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

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

# Read encoding

readFileEncoding :: TextEncoding -> FilePath -> IO String

Like `readFile`, but setting an encoding.

readFileUTF8 :: FilePath -> IO String

Like `readFile`, but with the encoding `utf8`.

readFileBinary :: FilePath -> IO String

Like `readFile`, but for binary files.

# Strict reading

readFile' :: FilePath -> IO String

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

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

readFileUTF8' :: FilePath -> IO String

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

readFileBinary' :: FilePath -> IO String

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

# Write with encoding

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

Write a file with a particular encoding.

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

Write a file with the `utf8` encoding.

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

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

Write a binary file.

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

# Temporary files

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

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 == return True
    (doesFileExist =<< withTempFile return) == return False
    withTempFile readFile' == return ""

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

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

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

newTempFile :: IO (FilePath, IO ())

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 ())

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.