Created
September 9, 2020 04:37
-
-
Save MarcelineVQ/3a3e48e609478a7f634111c557d8c9f4 to your computer and use it in GitHub Desktop.
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
import Data.Buffer | |
-- Redefining this here to modify it | |
%foreign "scheme:blodwen-new-buffer" | |
prim_newBuffer' : Int -> PrimIO Buffer | |
export | |
newBuffer' : HasIO io => (bytes : Int) -> io Buffer | |
newBuffer' size = primIO $ prim_newBuffer' size | |
-- No Maybe, if we can't allocate memory then die. | |
-- does nothing, Buffer is managed by scheme gc atm | |
free : HasIO io => Buffer -> io () | |
free _ = pure () | |
export | |
withAlloc : HasIO io => (bytes : Int) -> (Buffer -> io a) -> io a | |
withAlloc size act = do buf <- newBuffer' size | |
res <- act buf | |
free buf | |
pure res | |
{- | |
Given a signature like | |
char *curl_easy_unescape( CURL *curl, const char *url , int inlength, int *outlength ); | |
we can replace occurences like int *outlength with Buffer in our foreign signature. | |
We can do that because pointers to c types in chez's ffi use bytevectors for arguments | |
on the scheme side and Buffer is a bytevector. | |
-} | |
%foreign "C:curl_easy_unescape,libcurl,curl/curl.h" | |
prim_curl_easy_unescape : Ptr HandlePtr -> (url : String) -> (url_len : Int) | |
-> Buffer -> PrimIO (Ptr String) | |
curl_easy_unescape : HasIO io => CurlHandle ty -> String -> io String | |
curl_easy_unescape (MkH h) str = do | |
b <- newBuffer' 8 -- 64 bits allocated, don't really need them all | |
strptr <- primIO $ prim_curl_easy_unescape h str 0 b | |
res <- getInt32 b 0 -- Our c wrote an int, so take out an Int32 | |
printLn res -- this will print what curl_easy_unescape wrote into b | |
... | |
other stuff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment