module Ptr.ByteString
where
import Ptr.Prelude
import qualified Ptr.Poking as A
import qualified Ptr.Parse as C
import qualified Ptr.Peek as D
import qualified Data.ByteString.Internal as B
poking :: A.Poking -> B.ByteString
poking (A.Poking size population) =
B.unsafeCreate size population
parse :: B.ByteString -> C.Parse result -> (Int -> result) -> (Text -> result) -> result
parse (B.PS fp offset length) (C.Parse parseIO) eoi error =
unsafePerformIO $
withForeignPtr fp $ \ptr ->
parseIO length (plusPtr ptr offset) (return . eoi) (return . error) (\result _ _ -> return result)
peek :: B.ByteString -> D.Peek result -> Maybe result
peek (B.PS fp offset length) (D.Peek amount io) =
if amount <= length
then Just $ unsafePerformIO $ withForeignPtr fp $ \ptr ->
io (plusPtr ptr offset)
else Nothing