-- GENERATED by C->Haskell Compiler, version 0.28.8 Switcheroo, 25 November 2017 (Haskell)
-- Edit the ORIGNAL .chs file instead!


{-# LINE 1 "src/OpenDHT/Internal/Blob.chs" #-}

{-|
  Module      : OpenDHT.Internal.Blob
  Description : Internal definitions for blobs.
  Copyright   : (c) Simon Désaulniers, 2025
  License     : GPL-3

  Maintainer  : sim.desaulniers@gmail.com
-}

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 synonym for C-bindings. Not meant to be used by the library user.
-}
type CBlobPtr = Ptr ()

{-| Internal intermediary type used to retrieve values under Blobs.
-}
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 ()

{-| Get the string of bytes under the Blob type.
-}
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 ()

{-| Delete a blob for which OpenDHT holds the pointer to.
-}
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

--  vim: set sts=2 ts=2 sw=2 tw=120 et :