{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -funbox-strict-fields #-}
module GHC.IO.BufferedIO (
BufferedIO(..),
readBuf, readBufNonBlocking, writeBuf, writeBufNonBlocking
) where
import GHC.Base
import GHC.Ptr
import Data.Word
import GHC.Num
import GHC.IO.Device as IODevice
import GHC.IO.Device as RawIO
import GHC.IO.Buffer
class BufferedIO dev where
newBuffer :: dev -> BufferState -> IO (Buffer Word8)
fillReadBuffer :: dev -> Buffer Word8 -> IO (Int, Buffer Word8)
fillReadBuffer0 :: dev -> Buffer Word8 -> IO (Maybe Int, Buffer Word8)
emptyWriteBuffer :: dev -> Buffer Word8 -> IO (Buffer Word8)
emptyWriteBuffer dev
_dev Buffer Word8
buf
= forall (m :: * -> *) a. Monad m => a -> m a
return Buffer Word8
buf{ bufL :: Int
bufL=Int
0, bufR :: Int
bufR=Int
0, bufState :: BufferState
bufState = BufferState
WriteBuffer }
flushWriteBuffer :: dev -> Buffer Word8 -> IO (Buffer Word8)
flushWriteBuffer0 :: dev -> Buffer Word8 -> IO (Int, Buffer Word8)
readBuf :: RawIO dev => dev -> Buffer Word8 -> IO (Int, Buffer Word8)
readBuf :: forall dev.
RawIO dev =>
dev -> Buffer Word8 -> IO (Int, Buffer Word8)
readBuf dev
dev Buffer Word8
bbuf = do
let bytes :: Int
bytes = forall e. Buffer e -> Int
bufferAvailable Buffer Word8
bbuf
let offset :: Word64
offset = forall e. Buffer e -> Word64
bufferOffset Buffer Word8
bbuf
Int
res <- forall e a. Buffer e -> (Ptr e -> IO a) -> IO a
withBuffer Buffer Word8
bbuf forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr ->
forall a. RawIO a => a -> Ptr Word8 -> Word64 -> Int -> IO Int
RawIO.read dev
dev (Ptr Word8
ptr forall a b. Ptr a -> Int -> Ptr b
`plusPtr` forall e. Buffer e -> Int
bufR Buffer Word8
bbuf) Word64
offset Int
bytes
let bbuf' :: Buffer Word8
bbuf' = forall e. Int -> Buffer e -> Buffer e
bufferAddOffset Int
res Buffer Word8
bbuf
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
res, Buffer Word8
bbuf'{ bufR :: Int
bufR = forall e. Buffer e -> Int
bufR Buffer Word8
bbuf' forall a. Num a => a -> a -> a
+ Int
res })
readBufNonBlocking :: RawIO dev => dev -> Buffer Word8
-> IO (Maybe Int,
Buffer Word8)
readBufNonBlocking :: forall dev.
RawIO dev =>
dev -> Buffer Word8 -> IO (Maybe Int, Buffer Word8)
readBufNonBlocking dev
dev Buffer Word8
bbuf = do
let bytes :: Int
bytes = forall e. Buffer e -> Int
bufferAvailable Buffer Word8
bbuf
let offset :: Word64
offset = forall e. Buffer e -> Word64
bufferOffset Buffer Word8
bbuf
Maybe Int
res <- forall e a. Buffer e -> (Ptr e -> IO a) -> IO a
withBuffer Buffer Word8
bbuf forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr ->
forall a.
RawIO a =>
a -> Ptr Word8 -> Word64 -> Int -> IO (Maybe Int)
IODevice.readNonBlocking dev
dev (Ptr Word8
ptr forall a b. Ptr a -> Int -> Ptr b
`plusPtr` forall e. Buffer e -> Int
bufR Buffer Word8
bbuf) Word64
offset Int
bytes
case Maybe Int
res of
Maybe Int
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. Maybe a
Nothing, Buffer Word8
bbuf)
Just Int
n -> do let bbuf' :: Buffer Word8
bbuf' = forall e. Int -> Buffer e -> Buffer e
bufferAddOffset Int
n Buffer Word8
bbuf
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just Int
n, Buffer Word8
bbuf'{ bufR :: Int
bufR = forall e. Buffer e -> Int
bufR Buffer Word8
bbuf' forall a. Num a => a -> a -> a
+ Int
n })
writeBuf :: RawIO dev => dev -> Buffer Word8 -> IO (Buffer Word8)
writeBuf :: forall dev. RawIO dev => dev -> Buffer Word8 -> IO (Buffer Word8)
writeBuf dev
dev Buffer Word8
bbuf = do
let bytes :: Int
bytes = forall e. Buffer e -> Int
bufferElems Buffer Word8
bbuf
let offset :: Word64
offset = forall e. Buffer e -> Word64
bufferOffset Buffer Word8
bbuf
forall e a. Buffer e -> (Ptr e -> IO a) -> IO a
withBuffer Buffer Word8
bbuf forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr ->
forall a. RawIO a => a -> Ptr Word8 -> Word64 -> Int -> IO ()
IODevice.write dev
dev (Ptr Word8
ptr forall a b. Ptr a -> Int -> Ptr b
`plusPtr` forall e. Buffer e -> Int
bufL Buffer Word8
bbuf) Word64
offset Int
bytes
let bbuf' :: Buffer Word8
bbuf' = forall e. Int -> Buffer e -> Buffer e
bufferAddOffset Int
bytes Buffer Word8
bbuf
forall (m :: * -> *) a. Monad m => a -> m a
return Buffer Word8
bbuf'{ bufL :: Int
bufL=Int
0, bufR :: Int
bufR=Int
0 }
writeBufNonBlocking :: RawIO dev => dev -> Buffer Word8 -> IO (Int, Buffer Word8)
writeBufNonBlocking :: forall dev.
RawIO dev =>
dev -> Buffer Word8 -> IO (Int, Buffer Word8)
writeBufNonBlocking dev
dev Buffer Word8
bbuf = do
let bytes :: Int
bytes = forall e. Buffer e -> Int
bufferElems Buffer Word8
bbuf
let offset :: Word64
offset = forall e. Buffer e -> Word64
bufferOffset Buffer Word8
bbuf
Int
res <- forall e a. Buffer e -> (Ptr e -> IO a) -> IO a
withBuffer Buffer Word8
bbuf forall a b. (a -> b) -> a -> b
$ \Ptr Word8
ptr ->
forall a. RawIO a => a -> Ptr Word8 -> Word64 -> Int -> IO Int
IODevice.writeNonBlocking dev
dev (Ptr Word8
ptr forall a b. Ptr a -> Int -> Ptr b
`plusPtr` forall e. Buffer e -> Int
bufL Buffer Word8
bbuf) Word64
offset Int
bytes
let bbuf' :: Buffer Word8
bbuf' = forall e. Int -> Buffer e -> Buffer e
bufferAddOffset Int
bytes Buffer Word8
bbuf
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
res, forall e. Int -> Buffer e -> Buffer e
bufferAdjustL (forall e. Buffer e -> Int
bufL Buffer Word8
bbuf forall a. Num a => a -> a -> a
+ Int
res) Buffer Word8
bbuf')