module System.IO.Streams.File
(
withFileAsInput
, withFileAsInputStartingAt
, unsafeWithFileAsInputStartingAt
, withFileAsOutput
, withFileAsOutputExt
) where
import Control.Monad (unless)
import Data.ByteString (ByteString)
import Data.Int (Int64)
import System.IO (BufferMode (NoBuffering), IOMode (ReadMode, WriteMode), SeekMode (AbsoluteSeek), hSeek, hSetBuffering, withBinaryFile)
import System.IO.Streams.Handle (handleToInputStream, handleToOutputStream)
import System.IO.Streams.Internal (InputStream, OutputStream)
withFileAsInput :: FilePath
-> (InputStream ByteString -> IO a)
-> IO a
withFileAsInput = withFileAsInputStartingAt 0
withFileAsInputStartingAt
:: Int64
-> FilePath
-> (InputStream ByteString -> IO a)
-> IO a
withFileAsInputStartingAt idx fp m = withBinaryFile fp ReadMode go
where
go h = do
unless (idx == 0) $ hSeek h AbsoluteSeek $ toInteger idx
handleToInputStream h >>= m
unsafeWithFileAsInputStartingAt
:: Int64
-> FilePath
-> (InputStream ByteString -> IO a)
-> IO a
unsafeWithFileAsInputStartingAt = withFileAsInputStartingAt
withFileAsOutput
:: FilePath
-> (OutputStream ByteString -> IO a)
-> IO a
withFileAsOutput f = withFileAsOutputExt f WriteMode NoBuffering
withFileAsOutputExt
:: FilePath
-> IOMode
-> BufferMode
-> (OutputStream ByteString -> IO a)
-> IO a
withFileAsOutputExt fp iomode buffermode m = withBinaryFile fp iomode $ \h -> do
hSetBuffering h buffermode
handleToOutputStream h >>= m