module Ptr.IO
where
import Ptr.Prelude
import qualified Data.ByteString.Internal as A
import qualified Data.ByteString.Short.Internal as B
import qualified Ptr.UncheckedShifting as D
peekStorable :: Storable storable => Ptr Word8 -> IO storable
peekStorable =
peek . castPtr
peekWord8 :: Ptr Word8 -> IO Word8
peekWord8 =
peekStorable
peekBEWord16 :: Ptr Word8 -> IO Word16
#ifdef WORDS_BIGENDIAN
peekBEWord16 =
peekStorable
#else
peekBEWord16 =
fmap byteSwap16 . peekStorable
#endif
peekLEWord16 :: Ptr Word8 -> IO Word16
#ifdef WORDS_BIGENDIAN
peekLEWord16 =
fmap byteSwap16 . peekStorable
#else
peekLEWord16 =
peekStorable
#endif
peekBEWord32 :: Ptr Word8 -> IO Word32
#ifdef WORDS_BIGENDIAN
peekBEWord32 =
peekStorable
#else
peekBEWord32 =
fmap byteSwap32 . peekStorable
#endif
peekLEWord32 :: Ptr Word8 -> IO Word32
#ifdef WORDS_BIGENDIAN
peekLEWord32 =
fmap byteSwap32 . peekStorable
#else
peekLEWord32 =
peekStorable
#endif
peekBEWord64 :: Ptr Word8 -> IO Word64
#ifdef WORDS_BIGENDIAN
peekBEWord64 =
peekStorable
#else
peekBEWord64 =
fmap byteSwap64 . peekStorable
#endif
peekLEWord64 :: Ptr Word8 -> IO Word64
#ifdef WORDS_BIGENDIAN
peekLEWord64 =
fmap byteSwap64 . peekStorable
#else
peekLEWord64 =
peekStorable
#endif
peekBytes :: Ptr Word8 -> Int -> IO ByteString
peekBytes ptr amount =
A.create amount $ \destPtr -> A.memcpy destPtr ptr amount
peekShortByteString :: Ptr Word8 -> Int -> IO ShortByteString
peekShortByteString ptr amount =
B.createFromPtr ptr amount
peekNullTerminatedShortByteString :: Ptr Word8 -> (Int -> IO ShortByteString -> IO a) -> IO a
peekNullTerminatedShortByteString ptr cont =
do
!length <- fromIntegral <$> A.c_strlen (castPtr ptr)
cont length (B.createFromPtr ptr length)
pokeStorable :: Storable a => Ptr Word8 -> a -> IO ()
pokeStorable ptr value =
poke (castPtr ptr) value
pokeWord8 :: Ptr Word8 -> Word8 -> IO ()
pokeWord8 ptr value =
poke ptr value
pokeBEWord16 :: Ptr Word8 -> Word16 -> IO ()
#ifdef WORDS_BIGENDIAN
pokeBEWord16 =
poke
#else
pokeBEWord16 ptr value =
do
pokeStorable ptr (fromIntegral (D.shiftr_w16 value 8) :: Word8)
pokeByteOff ptr 1 (fromIntegral value :: Word8)
#endif
pokeBEWord32 :: Ptr Word8 -> Word32 -> IO ()
#ifdef WORDS_BIGENDIAN
pokeBEWord32 =
pokeStorable
#else
pokeBEWord32 ptr value =
do
pokeStorable ptr (fromIntegral (D.shiftr_w32 value 24) :: Word8)
pokeByteOff ptr 1 (fromIntegral (D.shiftr_w32 value 16) :: Word8)
pokeByteOff ptr 2 (fromIntegral (D.shiftr_w32 value 8) :: Word8)
pokeByteOff ptr 3 (fromIntegral value :: Word8)
#endif
pokeBEWord64 :: Ptr Word8 -> Word64 -> IO ()
#ifdef WORDS_BIGENDIAN
pokeBEWord64 =
pokeStorable
#else
#if WORD_SIZE_IN_BITS < 64
pokeBEWord64 ptr value =
do
pokeBEWord32 ptr (fromIntegral (D.shiftr_w64 value 32))
pokeBEWord32 (plusPtr ptr 4) (fromIntegral value)
#else
pokeBEWord64 ptr value =
do
pokeStorable ptr (fromIntegral (D.shiftr_w64 value 56) :: Word8)
pokeByteOff ptr 1 (fromIntegral (D.shiftr_w64 value 48) :: Word8)
pokeByteOff ptr 2 (fromIntegral (D.shiftr_w64 value 40) :: Word8)
pokeByteOff ptr 3 (fromIntegral (D.shiftr_w64 value 32) :: Word8)
pokeByteOff ptr 4 (fromIntegral (D.shiftr_w64 value 24) :: Word8)
pokeByteOff ptr 5 (fromIntegral (D.shiftr_w64 value 16) :: Word8)
pokeByteOff ptr 6 (fromIntegral (D.shiftr_w64 value 8) :: Word8)
pokeByteOff ptr 7 (fromIntegral value :: Word8)
#endif
#endif
pokeBytesTrimming :: Ptr Word8 -> Int -> ByteString -> IO ()
pokeBytesTrimming ptr maxLength (A.PS fptr offset length) =
withForeignPtr fptr $ \bytesPtr -> A.memcpy ptr (plusPtr bytesPtr offset) (min length maxLength)
pokeBytes :: Ptr Word8 -> ByteString -> IO ()
pokeBytes ptr (A.PS fptr offset length) =
withForeignPtr fptr $ \bytesPtr -> A.memcpy ptr (plusPtr bytesPtr offset) length