module ByteString.TreeBuilder.Poker where
import ByteString.TreeBuilder.Prelude
import Foreign hiding (void)
import qualified Data.ByteString as A
import qualified Data.ByteString.Internal as B
import qualified Foreign as D
import qualified ByteString.TreeBuilder.Tree as E
pokeBytes :: ByteString -> Ptr Word8 -> IO (Ptr Word8)
pokeBytes (B.PS foreignPointer offset length) pointer =
do
withForeignPtr foreignPointer $ \pointer' ->
B.memcpy pointer (plusPtr pointer' offset) length
pure (plusPtr pointer length)
pokeBytesMinus :: ByteString -> Ptr Word8 -> IO (Ptr Word8)
pokeBytesMinus (B.PS foreignPointer offset length) pointer =
do
withForeignPtr foreignPointer $ \pointer' ->
B.memcpy targetPointer (plusPtr pointer' offset) length
pure targetPointer
where
targetPointer =
plusPtr pointer (negate length)
pokeTree :: E.Tree -> D.Ptr Word8 -> IO (D.Ptr Word8)
pokeTree tree ptr =
case tree of
E.Empty -> pure ptr
E.Leaf bytes -> pokeBytes bytes ptr
E.Branch tree1 tree2 -> pokeTree tree1 ptr >>= pokeTree tree2