ΟΚ, here's the code, nothing special
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <dlfcn.h>
CURL *curl_handle = 0;
FILE * datafile;
FILE * errfile;
char oz_module_name[] = "Mozart-libcurl";
#define _USING_LINUX_DLL
#ifdef _USING_LINUX_DLL
void *dll_handle = 0;
CURL * (*p_curl_easy_init)();
CURLcode (*p_curl_easy_setopt)(CURL*, CURLoption,...);
CURLcode (*p_curl_easy_perform)(CURL*);
void (*p_curl_easy_cleanup)(CURL *);
int setup_dll() {
dll_handle = dlopen("/usr/lib/libcurl.so", RTLD_LAZY);
if (!dll_handle) return FALSE;
p_curl_easy_init = (CURL* (*)())dlsym(dll_handle,"curl_easy_init");
p_curl_easy_cleanup = (void (*)(CURL*))dlsym(dll_handle,"curl_easy_cleanup");
p_curl_easy_setopt = (CURLcode(*)(CURL*,CURLoption,...))dlsym(dll_handle,"curl_easy_setopt");
p_curl_easy_perform = (CURLcode(*)(CURL*))dlsym(dll_handle,"curl_easy_perform");
return TRUE;
}
void cleanup_dll() {
dlclose(dll_handle);
}
#else
CURL * (*p_curl_easy_init)() = curl_easy_init;
CURLcode (*p_curl_easy_setopt)(CURL*, CURLoption,...) = curl_easy_setopt;
CURLcode (*p_curl_easy_perform)(CURL*) = curl_easy_perform;
void (*p_curl_easy_cleanup)(CURL*) = curl_easy_cleanup;
#endif
static long n;
OZ_BI_define(counter_next,0,1){
OZ_RETURN_INT(n++);
}
OZ_BI_end
OZ_BI_define(easy_entry_init,0,1) {
#ifdef _USING_LINUX_DLL
if (!setup_dll()) return OZ_FAILED;
#endif
curl_handle = p_curl_easy_init();
if (!curl_handle) return OZ_FAILED;
OZ_RETURN_INT((int)curl_handle);
}
OZ_BI_end
OZ_BI_define(easy_setopt,3,1){
CURLcode rc;
OZ_declareAtom(1,curlOptionStr);
CURLoption curlOpt =
(!strcmp(curlOptionStr,"CURLOPT_URL")) ? CURLOPT_URL :
(!strcmp(curlOptionStr,"CURLOPT_WRITEDATA")) ? CURLOPT_WRITEDATA :
(!strcmp(curlOptionStr,"CURLOPT_POSTFIELDS")) ? CURLOPT_POSTFIELDS :
(!strcmp(curlOptionStr,"CURLOPT_POST")) ? CURLOPT_POST :
(!strcmp(curlOptionStr,"CURLOPT_STDERR")) ? CURLOPT_STDERR :
(!strcmp(curlOptionStr,"CURLOPT_USERAGENT")) ? CURLOPT_USERAGENT :
(CURLoption)0 ;
if(curlOpt==0) OZ_RETURN_INT(1);
OZ_declareAtom(2,curlParameter);
switch(curlOpt) {
case CURLOPT_WRITEDATA :
if(!(datafile=fopen(curlParameter,"w"))) OZ_RETURN_INT(100);
rc = p_curl_easy_setopt(curl_handle, curlOpt, datafile);
break;
case CURLOPT_STDERR :
if(!(errfile=fopen(curlParameter,"w"))) OZ_RETURN_INT(100);
rc = p_curl_easy_setopt(curl_handle, curlOpt, errfile);
break;
default:
rc = p_curl_easy_setopt(curl_handle, curlOpt, curlParameter);
}
OZ_RETURN_INT(rc);
}
OZ_BI_end
OZ_BI_define(easy_perform_exec,1,1){
CURLcode rc = p_curl_easy_perform(curl_handle);
OZ_RETURN_INT(rc);
}
OZ_BI_end
OZ_BI_define(easy_cleanup_exit,1,1){
CURLcode rc=(CURLcode)0;
if (datafile!=stdout) fclose(datafile);
if (errfile!=stderr) fclose(errfile);
p_curl_easy_cleanup(curl_handle);
#ifdef _USING_LINUX_DLL
if(dlclose(dll_handle)) rc=(CURLcode)20;
#endif
OZ_RETURN_INT(rc);
}
OZ_BI_end
OZ_C_proc_interface table[] = {
{"init",0,1,easy_entry_init},
{"setopt",3,1,easy_setopt},
{"exec",1,1,easy_perform_exec},
{"close",1,1,easy_cleanup_exit},
{"next",0,1,counter_next},
{0,0,0,0}
} ;
OZ_C_proc_interface * oz_init_module(void){
n=1;
return table;
}
int main(int argc, char *argv[]) {
int rc;
n=0;
datafile = stdout;
errfile = stderr;
if(!setup_dll()) return 10;;
curl_handle = p_curl_easy_init();
if (!curl_handle) {
printf("whoops!\n");
}
else {
char url[] = "http://www.mozart-oz.org";
rc = p_curl_easy_setopt(curl_handle, CURLOPT_URL, url);
printf("setopt-url = %d\n",rc);
rc = p_curl_easy_perform(curl_handle);
printf("\nperform = %d\n", rc);
p_curl_easy_cleanup(curl_handle);
}
cleanup_dll();
return 0;
}
declare [Curl] = {Module.link ['/home/alex/Code/c/ozlibcurl.so{native}']}
H = {Curl.init}
R1 = {Curl.setopt 123 'CURLOPT_URL' 'http://www.mozart-oz.org'}
R2 = {Curl.setopt 123 'CURLOPT_WRITEDATA' './somedata.txt'}
R3 = {Curl.setopt 123 'CURLOPT_STDERR' './some-err.txt'}
RC = {Curl.exec 123}
J = {Curl.close 123}
{Browse {Curl.next }}
{Browse H}
{Browse R1}
{Browse R2}
{Browse R3}
{Browse RC}
{Browse J}