Safe Haskell | None |
---|---|
Language | Haskell2010 |
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
- readFile' :: FilePath -> IO String
- type FilePath = String
- data IO a
- data Handle
- 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 ()
- stdout :: Handle
- data TextEncoding
- openFile :: FilePath -> IOMode -> IO Handle
- data IOMode
- hSetEncoding :: Handle -> TextEncoding -> IO ()
- hGetContents :: Handle -> IO String
- utf8 :: TextEncoding
- openBinaryFile :: FilePath -> IOMode -> IO Handle
- withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
- hGetContents' :: Handle -> IO String
- withBinaryFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
- hPutStr :: Handle -> String -> IO ()
- stderr :: Handle
- hClose :: Handle -> IO ()
- hGetBuffering :: Handle -> IO BufferMode
- hSetBuffering :: Handle -> BufferMode -> IO ()
- data BufferMode
- = NoBuffering
- | LineBuffering
- | BlockBuffering (Maybe Int)
- openTempFile :: FilePath -> String -> IO (FilePath, Handle)
- hFileSize :: Handle -> IO Integer
- hGetBuf :: Handle -> Ptr a -> Int -> IO Int
- char8 :: TextEncoding
- latin1 :: TextEncoding
- mkTextEncoding :: String -> IO TextEncoding
- utf16 :: TextEncoding
- utf16be :: TextEncoding
- utf16le :: TextEncoding
- utf32 :: TextEncoding
- utf32be :: TextEncoding
- utf32le :: TextEncoding
- utf8_bom :: TextEncoding
- hGetEcho :: Handle -> IO Bool
- hGetEncoding :: Handle -> IO (Maybe TextEncoding)
- hGetPosn :: Handle -> IO HandlePosn
- hIsClosed :: Handle -> IO Bool
- hIsEOF :: Handle -> IO Bool
- hIsOpen :: Handle -> IO Bool
- hIsReadable :: Handle -> IO Bool
- hIsSeekable :: Handle -> IO Bool
- hIsTerminalDevice :: 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 ()
- hSetNewlineMode :: Handle -> NewlineMode -> IO ()
- hSetPosn :: HandlePosn -> 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
- nativeNewline :: Newline
- nativeNewlineMode :: NewlineMode
- noNewlineTranslation :: NewlineMode
- universalNewlineMode :: NewlineMode
- stdin :: Handle
- fixIO :: (a -> IO a) -> IO a
- getContents' :: IO String
- hPrint :: Show a => Handle -> a -> IO ()
- hReady :: Handle -> IO Bool
- localeEncoding :: TextEncoding
- openBinaryTempFile :: FilePath -> String -> IO (FilePath, Handle)
- openBinaryTempFileWithDefaultPermissions :: FilePath -> String -> IO (FilePath, Handle)
- openTempFileWithDefaultPermissions :: FilePath -> String -> IO (FilePath, Handle)
- data SeekMode
- data HandlePosn
- data Newline
- data NewlineMode = NewlineMode {}
- 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 ())
- newTempFileWithin :: FilePath -> IO (FilePath, IO ())
- newTempDirWithin :: FilePath -> IO (FilePath, IO ())
- fileEq :: FilePath -> FilePath -> IO Bool
Documentation
Instances
MonadIO IO | |
Defined in Control.Monad.IO.Class | |
Alternative IO | |
Applicative IO | |
Functor IO | |
Monad IO | |
MonadPlus IO | |
MonadFail IO | |
Defined in GHC.Internal.Control.Monad.Fail | |
Quasi IO | |
Defined in Language.Haskell.TH.Syntax Methods qReport :: Bool -> String -> IO () qRecover :: IO a -> IO a -> IO a qLookupName :: Bool -> String -> IO (Maybe Name) 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] 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] | |
Quote IO | |
Defined in Language.Haskell.TH.Syntax | |
a ~ () => HPrintfType (IO a) | |
Defined in Text.Printf | |
a ~ () => PrintfType (IO a) | |
Defined in Text.Printf | |
Monoid a => Monoid (IO a) | |
Semigroup a => Semigroup (IO a) | |
appendFile :: FilePath -> String -> IO () #
getContents :: IO String #
data TextEncoding #
Instances
Show TextEncoding | |
Defined in GHC.Internal.IO.Encoding.Types Methods showsPrec :: Int -> TextEncoding -> ShowS show :: TextEncoding -> String showList :: [TextEncoding] -> ShowS |
Constructors
ReadMode | |
WriteMode | |
AppendMode | |
ReadWriteMode |
hSetEncoding :: Handle -> TextEncoding -> IO () #
hGetContents :: Handle -> IO String #
utf8 :: TextEncoding #
hGetContents' :: Handle -> IO String #
hGetBuffering :: Handle -> IO BufferMode #
hSetBuffering :: Handle -> BufferMode -> IO () #
data BufferMode #
Constructors
NoBuffering | |
LineBuffering | |
BlockBuffering (Maybe Int) |
Instances
Read BufferMode | |
Defined in GHC.Internal.IO.Handle.Types Methods readsPrec :: Int -> ReadS BufferMode # readList :: ReadS [BufferMode] # readPrec :: ReadPrec BufferMode # readListPrec :: ReadPrec [BufferMode] # | |
Show BufferMode | |
Defined in GHC.Internal.IO.Handle.Types Methods showsPrec :: Int -> BufferMode -> ShowS show :: BufferMode -> String showList :: [BufferMode] -> ShowS | |
Eq BufferMode | |
Defined in GHC.Internal.IO.Handle.Types | |
Ord BufferMode | |
Defined in GHC.Internal.IO.Handle.Types Methods compare :: BufferMode -> BufferMode -> Ordering (<) :: BufferMode -> BufferMode -> Bool (<=) :: BufferMode -> BufferMode -> Bool (>) :: BufferMode -> BufferMode -> Bool (>=) :: BufferMode -> BufferMode -> Bool max :: BufferMode -> BufferMode -> BufferMode min :: BufferMode -> BufferMode -> BufferMode |
char8 :: TextEncoding #
latin1 :: TextEncoding #
mkTextEncoding :: String -> IO TextEncoding #
utf16 :: TextEncoding #
utf16be :: TextEncoding #
utf16le :: TextEncoding #
utf32 :: TextEncoding #
utf32be :: TextEncoding #
utf32le :: TextEncoding #
hGetEncoding :: Handle -> IO (Maybe TextEncoding) #
hGetPosn :: Handle -> IO HandlePosn #
hIsReadable :: Handle -> IO Bool #
hIsSeekable :: Handle -> IO Bool #
hIsTerminalDevice :: Handle -> IO Bool #
hIsWritable :: Handle -> IO Bool #
hLookAhead :: Handle -> IO Char #
hSetBinaryMode :: Handle -> Bool -> IO () #
hSetFileSize :: Handle -> Integer -> IO () #
hSetNewlineMode :: Handle -> NewlineMode -> IO () #
hSetPosn :: HandlePosn -> IO () #
hGetBufNonBlocking :: Handle -> Ptr a -> Int -> IO Int #
hGetBufSome :: Handle -> Ptr a -> Int -> IO Int #
hPutBufNonBlocking :: Handle -> Ptr a -> Int -> IO Int #
hWaitForInput :: Handle -> Int -> IO Bool #
getContents' :: IO String #
Constructors
AbsoluteSeek | |
RelativeSeek | |
SeekFromEnd |
Instances
Enum SeekMode | |
Defined in GHC.Internal.IO.Device | |
Ix SeekMode | |
Defined in GHC.Internal.IO.Device | |
Read SeekMode | |
Show SeekMode | |
Eq SeekMode | |
Ord SeekMode | |
Defined in GHC.Internal.IO.Device |
data HandlePosn #
Instances
Show HandlePosn | |
Defined in GHC.Internal.IO.Handle Methods showsPrec :: Int -> HandlePosn -> ShowS show :: HandlePosn -> String showList :: [HandlePosn] -> ShowS | |
Eq HandlePosn | |
Defined in GHC.Internal.IO.Handle |
data NewlineMode #
Constructors
NewlineMode | |
Instances
Read NewlineMode | |
Defined in GHC.Internal.IO.Handle.Types Methods readsPrec :: Int -> ReadS NewlineMode # readList :: ReadS [NewlineMode] # readPrec :: ReadPrec NewlineMode # readListPrec :: ReadPrec [NewlineMode] # | |
Show NewlineMode | |
Defined in GHC.Internal.IO.Handle.Types Methods showsPrec :: Int -> NewlineMode -> ShowS show :: NewlineMode -> String showList :: [NewlineMode] -> ShowS | |
Eq NewlineMode | |
Defined in GHC.Internal.IO.Handle.Types | |
Ord NewlineMode | |
Defined in GHC.Internal.IO.Handle.Types Methods compare :: NewlineMode -> NewlineMode -> Ordering (<) :: NewlineMode -> NewlineMode -> Bool (<=) :: NewlineMode -> NewlineMode -> Bool (>) :: NewlineMode -> NewlineMode -> Bool (>=) :: NewlineMode -> NewlineMode -> Bool max :: NewlineMode -> NewlineMode -> NewlineMode min :: NewlineMode -> NewlineMode -> NewlineMode |
captureOutput :: IO a -> IO (String, a) Source #
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.
Strict reading
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)