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 :: FilePath -> (InputStream ByteString -> IO a) -> IO a
withFileAsInput = Int64 -> FilePath -> (InputStream ByteString -> IO a) -> IO a
forall a.
Int64 -> FilePath -> (InputStream ByteString -> IO a) -> IO a
withFileAsInputStartingAt 0
withFileAsInputStartingAt
:: Int64
-> FilePath
-> (InputStream ByteString -> IO a)
-> IO a
withFileAsInputStartingAt :: Int64 -> FilePath -> (InputStream ByteString -> IO a) -> IO a
withFileAsInputStartingAt idx :: Int64
idx fp :: FilePath
fp m :: InputStream ByteString -> IO a
m = FilePath -> IOMode -> (Handle -> IO a) -> IO a
forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
withBinaryFile FilePath
fp IOMode
ReadMode Handle -> IO a
go
where
go :: Handle -> IO a
go h :: Handle
h = do
Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int64
idx Int64 -> Int64 -> Bool
forall a. Eq a => a -> a -> Bool
== 0) (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Handle -> SeekMode -> Integer -> IO ()
hSeek Handle
h SeekMode
AbsoluteSeek (Integer -> IO ()) -> Integer -> IO ()
forall a b. (a -> b) -> a -> b
$ Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger Int64
idx
Handle -> IO (InputStream ByteString)
handleToInputStream Handle
h IO (InputStream ByteString)
-> (InputStream ByteString -> IO a) -> IO a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= InputStream ByteString -> IO a
m
unsafeWithFileAsInputStartingAt
:: Int64
-> FilePath
-> (InputStream ByteString -> IO a)
-> IO a
unsafeWithFileAsInputStartingAt :: Int64 -> FilePath -> (InputStream ByteString -> IO a) -> IO a
unsafeWithFileAsInputStartingAt = Int64 -> FilePath -> (InputStream ByteString -> IO a) -> IO a
forall a.
Int64 -> FilePath -> (InputStream ByteString -> IO a) -> IO a
withFileAsInputStartingAt
withFileAsOutput
:: FilePath
-> (OutputStream ByteString -> IO a)
-> IO a
withFileAsOutput :: FilePath -> (OutputStream ByteString -> IO a) -> IO a
withFileAsOutput f :: FilePath
f = FilePath
-> IOMode
-> BufferMode
-> (OutputStream ByteString -> IO a)
-> IO a
forall a.
FilePath
-> IOMode
-> BufferMode
-> (OutputStream ByteString -> IO a)
-> IO a
withFileAsOutputExt FilePath
f IOMode
WriteMode BufferMode
NoBuffering
withFileAsOutputExt
:: FilePath
-> IOMode
-> BufferMode
-> (OutputStream ByteString -> IO a)
-> IO a
withFileAsOutputExt :: FilePath
-> IOMode
-> BufferMode
-> (OutputStream ByteString -> IO a)
-> IO a
withFileAsOutputExt fp :: FilePath
fp iomode :: IOMode
iomode buffermode :: BufferMode
buffermode m :: OutputStream ByteString -> IO a
m = FilePath -> IOMode -> (Handle -> IO a) -> IO a
forall r. FilePath -> IOMode -> (Handle -> IO r) -> IO r
withBinaryFile FilePath
fp IOMode
iomode ((Handle -> IO a) -> IO a) -> (Handle -> IO a) -> IO a
forall a b. (a -> b) -> a -> b
$ \h :: Handle
h -> do
Handle -> BufferMode -> IO ()
hSetBuffering Handle
h BufferMode
buffermode
Handle -> IO (OutputStream ByteString)
handleToOutputStream Handle
h IO (OutputStream ByteString)
-> (OutputStream ByteString -> IO a) -> IO a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= OutputStream ByteString -> IO a
m