{-# LINE 1 "src/System/Posix/ByteLevel.hsc" #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module System.Posix.ByteLevel (fdWrite, fdWriteB, writeAllB, writeAllL) where
import Control.Applicative ((<$>))
import Control.Monad (unless)
import qualified Data.ByteString as Strict
import Data.ByteString.Lazy (ByteString, toChunks)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.Function (fix)
import Foreign.C.Error (throwErrnoIfMinus1Retry)
import Foreign.C.String (CString, CStringLen)
import Foreign.C.Types (CInt(..), CSize(..))
import System.Posix.Types (ByteCount, Fd(..))
foreign import ccall "write"
c_write :: CInt -> CString -> CSize -> IO CSize
fdWrite :: Fd -> CStringLen -> IO ByteCount
fdWrite :: Fd -> CStringLen -> IO ByteCount
fdWrite (Fd CInt
fd) (Ptr CChar
cs, Int
l) = forall a. (Eq a, Num a) => String -> IO a -> IO a
throwErrnoIfMinus1Retry String
"write" forall b c a. (b -> c) -> (a -> b) -> a -> c
. CInt -> Ptr CChar -> ByteCount -> IO ByteCount
c_write CInt
fd Ptr CChar
cs forall a b. (a -> b) -> a -> b
$ forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
l
fdWriteB :: Fd -> Strict.ByteString -> IO Int
fdWriteB :: Fd -> ByteString -> IO Int
fdWriteB Fd
fd ByteString
bs = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. ByteString -> (CStringLen -> IO a) -> IO a
unsafeUseAsCStringLen ByteString
bs (Fd -> CStringLen -> IO ByteCount
fdWrite Fd
fd)
writeAllB :: Fd -> Strict.ByteString -> IO ()
writeAllB :: Fd -> ByteString -> IO ()
writeAllB Fd
fd = forall a. (a -> a) -> a
fix forall a b. (a -> b) -> a -> b
$ \ByteString -> IO ()
me ByteString
s -> forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> Bool
Strict.null ByteString
s) forall a b. (a -> b) -> a -> b
$ do
Int
count <- Fd -> ByteString -> IO Int
fdWriteB Fd
fd ByteString
s
ByteString -> IO ()
me forall a b. (a -> b) -> a -> b
$ Int -> ByteString -> ByteString
Strict.drop (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
count) ByteString
s
writeAllL :: Fd -> ByteString -> IO ()
writeAllL :: Fd -> ByteString -> IO ()
writeAllL Fd
fd = forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (Fd -> ByteString -> IO ()
writeAllB Fd
fd) forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
toChunks