Last active
November 8, 2016 16:16
-
-
Save psibi/e01ae8e9fe760488685253916b27efbb to your computer and use it in GitHub Desktop.
Sample Haskell FFI read
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foreign import ccall unsafe "read" c_read :: CInt -> Ptr () -> CSize -> IO CSsize | |
read :: Int -- ^ file descriptor | |
-> Int64 -- ^ Bytes to allocate | |
-> Word64 -- ^ Read upto this many bytes | |
-> IO (ByteString, Int64) | |
read fd bytes len = do | |
(ptr :: Ptr ()) <- mallocBytes (fromIntegral bytes) | |
size <- c_read (fromIntegral fd) ptr (fromIntegral len) | |
bstring <- packCString (castPtr ptr) | |
free ptr | |
return (bstring, (fromIntegral size)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment