module System.IO.Exception.BinaryFile where
import System.IO.Exception.File (EIO, close, )
import Control.Monad.Exception.Synchronous (bracketT, )
import System.IO.Straight (ioToExceptionalSIO, )
import System.IO (Handle, IOMode, )
import qualified System.IO as IO
import Data.Word (Word8, )
import Data.Char (ord, chr, )
open :: FilePath -> IOMode -> EIO Handle
open :: FilePath -> IOMode -> EIO Handle
open name :: FilePath
name mode :: IOMode
mode =
IO Handle -> EIO Handle
forall a. IO a -> ExceptionalT IOException SIO a
ioToExceptionalSIO (IO Handle -> EIO Handle) -> IO Handle -> EIO Handle
forall a b. (a -> b) -> a -> b
$ FilePath -> IOMode -> IO Handle
IO.openBinaryFile FilePath
name IOMode
mode
with ::
FilePath -> IOMode -> (Handle -> EIO r) -> EIO r
with :: FilePath -> IOMode -> (Handle -> EIO r) -> EIO r
with name :: FilePath
name mode :: IOMode
mode =
EIO Handle
-> (Handle -> ExceptionalT IOException SIO ())
-> (Handle -> EIO r)
-> EIO r
forall (m :: * -> *) e h a.
Monad m =>
ExceptionalT e m h
-> (h -> ExceptionalT e m ())
-> (h -> ExceptionalT e m a)
-> ExceptionalT e m a
bracketT (FilePath -> IOMode -> EIO Handle
open FilePath
name IOMode
mode) Handle -> ExceptionalT IOException SIO ()
close
getByte :: Handle -> EIO Word8
getByte :: Handle -> EIO Word8
getByte h :: Handle
h =
IO Word8 -> EIO Word8
forall a. IO a -> ExceptionalT IOException SIO a
ioToExceptionalSIO (IO Word8 -> EIO Word8) -> IO Word8 -> EIO Word8
forall a b. (a -> b) -> a -> b
$ (Char -> Word8) -> IO Char -> IO Word8
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> (Char -> Int) -> Char -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Int
ord) (IO Char -> IO Word8) -> IO Char -> IO Word8
forall a b. (a -> b) -> a -> b
$ Handle -> IO Char
IO.hGetChar Handle
h
putByte :: Handle -> Word8 -> EIO ()
putByte :: Handle -> Word8 -> ExceptionalT IOException SIO ()
putByte h :: Handle
h c :: Word8
c =
IO () -> ExceptionalT IOException SIO ()
forall a. IO a -> ExceptionalT IOException SIO a
ioToExceptionalSIO (IO () -> ExceptionalT IOException SIO ())
-> IO () -> ExceptionalT IOException SIO ()
forall a b. (a -> b) -> a -> b
$ Handle -> Char -> IO ()
IO.hPutChar Handle
h (Int -> Char
chr (Int -> Char) -> (Word8 -> Int) -> Word8 -> Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8 -> Char) -> Word8 -> Char
forall a b. (a -> b) -> a -> b
$ Word8
c)