[m-users.] Memory allocation from C back into Mercury

Sean Charles (emacstheviking) objitsu at gmail.com
Tue Aug 30 03:05:33 AEST 2022


Hi,

I now have a working binding for libcurl that can do a GET and I am stuck trying to find a good example of how to allocate a mercury string to hand back into Mercury land.
I have the same code from here: https://curl.se/libcurl/c/getinmemory.html <https://curl.se/libcurl/c/getinmemory.html>

After a call succeeds, here is how I am currently handling to curl response:

        if (CURLE_OK == res) {
            printf(""* %lu bytes retrieved\\n"", (unsigned long)chunk.size);
            long HttpStatus;
            curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &HttpStatus);
            Response = makeResponse((int)HttpStatus);
        }
        else {
            Response = makeError(1, curl_easy_strerror(res));
        }

The line in bold, here is where I need to be able to create a string with the data from chunk.memory, for the number of bytes received, chunk.size, this is where I am unsure how to continue. The makeResponse() code is:


:- func make_response(int::in)
    = (io.res(curl_response)::out) is det.

make_response(HttpStatus)
    = ok(curl_response(HttpStatus, "Data!", [])).

:- pragma foreign_export("C", make_response(in)
    = (out), "makeResponse").

with:

:- type curl_header ---> curl_header(string, string).
:- type curl_response
    --->    curl_response(
                status  :: int,
               content :: string,
                headers :: list(curl_header)
            ).


I am also unsure of how to map a C "long"; the curl call `curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &longVal);` expects a pointer to a long, which I am currently then casting to an integer given the known range of HTTP status codes, but in general, if I wanted to pass that into a mercury function, what type do I use given I am on M1 arm64, the C specs only state 'minimum width' of 32, so int32 springs to mind but is this the correct assumption ?

The final intention is to be able to have enough HTTP support available so that I can use the average API out there, GET/POST/PUT/FETCH/OPTIONS, with a view to implementing support layer for using my SOLID POD from a Mercury command line application.

Thanks,

Sean.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20220829/65903225/attachment.html>


More information about the users mailing list