I have created a very small libcurl binding that should be useful for those wanting to access online resources from BennuGD.
Right now it only supports one download at a time but that limitation will be lifted.
Here you can see a small video of the library downloading the main BennuGD forum code:
http://www.youtube.com/watch?v=2pm_sPxkqsc (http://www.youtube.com/watch?v=2pm_sPxkqsc)
- OSX Binary (http://code.google.com/p/bennugd-monolithic/downloads/detail?name=mod_curl_osx-20120124.tar.bz2) (Compiled in Lion).
- win32 Binary (http://code.google.com/p/bennugd-monolithic/downloads/detail?name=mod_curl_win32-20120124.7z) (Compiled in WinXP PRO).
- Ubuntu in my PPA (https://launchpad.net/~josebagar/+archive/ppa).
The API is very simple right now:
int curl_get(string srcURL, string dstFILE);
Downloads the data located at scrURL (which must be a proper URL-formatted string). It supports downloads over many different protocols, including HTTP, HTTPS and FTP.
The function returns 0, but when more than one simultaneous downloads are supported, it'll return the transfer ID, useful for retrieving info about the transfer status.int curl_info(int curl_id, int info_type)
Retrieves info about the transfer with ID curl_id. The second argument is the kind of info you want to retrieve. Possible values are:
* 0: Returns 1 if transfer is in progress. 0 otherwise.
* 1: Returns 1 if transfer finished, 0 otherwise.
* 2: Returns 1 if transfer failed or the output file couldn't be written
Right now the first argument is ignored, but left there so that the when multiple simultaneous transfers are supported, your code won't break.
[Edit] I've changed the API to closely mimic curl's easy API. I'll update the function description here ASAP. In the meanwhile, please use the provided example.
List of possible errors after doing curl_perform():
http://code.google.com/p/bennugd-monolithic/source/browse/trunk/3rdparty/curl/include/curl/curl.h#393
Hope it's useful. I'll upload binaries for other systems as I compile them.
[Edit] Obviously the idea is to get this into the iOS and Android ports, too.
win32 binaries added to the first post. Hope they're useful!
OSX binary updated. Also, one no longer needs to use curl_fetch to download to a string, you just use:
curl_setopt(curl, CURLOPT_WRITEDATA, &output);
Where output is the string where the download will be saved to, obviously.
Updated the win32 and OSX binaries and re-linked them, this time from Google Code hoping that the bad guys won't remove the files again.
The new version now changes the value of status (when used as curl_perform(curl, &status); ) in case of error to reflect the internal error emitted by libcurl. Possible errors are listed here:
http://code.google.com/p/bennugd-monolithic/source/browse/trunk/3rdparty/curl/include/curl/curl.h#393
Some example code for you to use:
import "mod_video"
import "mod_curl"
import "mod_say"
import "mod_mouse"
import "mod_text"
import "mod_map"
import "mod_file"
Global
int width = 1024;
int height = 768;
int quit=0;
end;
Process bouncer()
Private
int vx=3, vy=3;
Begin
graph = load_png("Icon.png");
// Position the graphic onscreen
x = 10+graphic_info(0, GRAPH, G_WIDTH);
y = 10+graphic_info(0, GRAPH, G_HEIGHT);
while(quit == 0)
if(x + vx >= width || x+vx < 0)
vx = -vx;
end
if(y+vy >= height || y+vy < 0)
vy = -vy;
end
x += vx; y += vy;
FRAME;
End;
End;
Process main()
Private
int i=0, status=0, curl=0;
int tostring=0; // Switch to 1 to download to a string
string output;
Begin
set_mode(width, height, 16);
// Remove Google logo, if it exists already
if(file_exists("classicplus.png"))
fremove("classicplus.png");
say("Removed existing logo");
end;
bouncer();
say("Starting download");
// Start libcurl, set options, perform transfer
curl = curl_init();
if(curl == -1)
say("Curl initialisation failed, quitting");
quit = 1;
return;
end;
curl_setopt(curl, CURLOPT_NOPROGRESS, 1);
if(tostring == 0)
// Use this to write to a file
curl_setopt(curl, CURLOPT_WRITEDATA, "classicplus.png");
curl_setopt(curl, CURLOPT_URL, "http://www.google.es/logos/classicplus.png");
else
// Use this to download to a string
curl_setopt(curl, CURLOPT_WRITEDATA, &output);
curl_setopt(curl, CURLOPT_URL, "http://www.google.es/");
end
curl_perform(curl, &status);
// Wait for the transfer to finish
while(status < 0)
FRAME;
end;
if(status > 0)
say("Transfer failed with status "+status+", quitting");
quit=1;
return;
end
if(tostring == 0)
// Replace the bouncer image by the google logo we just downloaded
unload_map(0, son.graph);
son.graph = load_png("classicplus.png");
else
write(0, 0, 0, 0, output);
end
curl_cleanup(curl);
say("Download done!");
while(! mouse.right)
FRAME;
end;
quit=1;
End;