{-# LINE 1 "src/OpenDHT/Internal/Blob.chs" #-}
module OpenDHT.Internal.Blob where
import qualified Foreign.C.Types as C2HSImp
import qualified Foreign.Ptr as C2HSImp
import qualified Foreign.Storable as C2HSImp
import qualified Data.ByteString as BS
import Control.Monad.IO.Class
import Foreign.Marshal.Array
import Foreign.Marshal.Alloc
import Foreign.Ptr
import Foreign.C.Types
import Foreign.Storable
import OpenDHT.Types
type CBlobPtr = Ptr ()
data DataView = DataView { DataView -> Ptr CUChar
_data :: Ptr CUChar
, _dataSize :: CULong
}
instance Storable DataView where
sizeOf _ = 16
{-# LINE 38 "src/OpenDHT/Internal/Blob.chs" #-}
alignment _ = 8
{-# LINE 39 "src/OpenDHT/Internal/Blob.chs" #-}
poke p (DataView d s) = (\ptr val -> do {C2HSImp.pokeByteOff ptr 0 (val :: (C2HSImp.Ptr C2HSImp.CUChar))}) p d
>> (\ptr val -> do {C2HSImp.pokeByteOff ptr 8 (val :: C2HSImp.CULong)}) p s
peek p = DataView <$> (\ptr -> do {C2HSImp.peekByteOff ptr 0 :: IO (C2HSImp.Ptr C2HSImp.CUChar)}) p
<*> (\ptr -> do {C2HSImp.peekByteOff ptr 8 :: IO C2HSImp.CULong}) p
bytesFromDataView :: DataView -> IO BS.ByteString
bytesFromDataView :: DataView -> IO ByteString
bytesFromDataView DataView
dv = do
[CUChar]
cuchars <- Int -> Ptr CUChar -> IO [CUChar]
forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray (CULong -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CULong -> Int) -> CULong -> Int
forall a b. (a -> b) -> a -> b
$ DataView -> CULong
_dataSize DataView
dv) (DataView -> Ptr CUChar
_data DataView
dv)
ByteString -> IO ByteString
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ByteString -> IO ByteString) -> ByteString -> IO ByteString
forall a b. (a -> b) -> a -> b
$ [Word8] -> ByteString
BS.pack ([Word8] -> ByteString) -> [Word8] -> ByteString
forall a b. (a -> b) -> a -> b
$ (CUChar -> Word8) -> [CUChar] -> [Word8]
forall a b. (a -> b) -> [a] -> [b]
map (\ (CUChar Word8
w) -> Word8
w) [CUChar]
cuchars
foreign import ccall "wr_dht_blob_get_data" dhtBlobGetDataViewC :: Ptr DataView -> CBlobPtr -> IO ()
viewBlob :: CBlobPtr -> Dht BS.ByteString
viewBlob :: CBlobPtr -> Dht ByteString
viewBlob CBlobPtr
b = IO ByteString -> Dht ByteString
forall a. IO a -> Dht a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ByteString -> Dht ByteString)
-> IO ByteString -> Dht ByteString
forall a b. (a -> b) -> a -> b
$ do
Ptr DataView
dvPtr <- IO (Ptr DataView)
forall a. Storable a => IO (Ptr a)
malloc
Ptr DataView -> CBlobPtr -> IO ()
dhtBlobGetDataViewC Ptr DataView
dvPtr CBlobPtr
b
DataView
dv <- Ptr DataView -> IO DataView
forall a. Storable a => Ptr a -> IO a
peek Ptr DataView
dvPtr
Ptr DataView -> IO ()
forall a. Ptr a -> IO ()
free Ptr DataView
dvPtr
DataView -> IO ByteString
bytesFromDataView DataView
dv
foreign import ccall "dht_blob_delete" dhtBlobDeleteC :: CBlobPtr -> IO ()
deleteBlob :: CBlobPtr -> Dht ()
deleteBlob :: CBlobPtr -> Dht ()
deleteBlob = IO () -> Dht ()
forall a. IO a -> Dht a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Dht ()) -> (CBlobPtr -> IO ()) -> CBlobPtr -> Dht ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CBlobPtr -> IO ()
dhtBlobDeleteC