On the Bacon Website, Peter has an
example of using libcurl to download a file over http, using the
CURL module by Doyle Wisenant.
It works, but there is no progress indicator so you don't know how far in the download process you actually are.
I decided to add a progressbar to Peter's code; here is the code with a screenshot showing how it will look...
'
' Download a binary file example over HTTP using CURL - PvE december 2010
'
' Added progressbar callback, redirect support, and save filename - AIR october 2018
'
' ADAPTED FROM: https://stackoverflow.com/questions/1637587/c-libcurl-console-progress-bar
FUNCTION progress_callback(void* clientp, double dltotal, double dlnow, double ultotal, double ulnow )
LOCAL totaldotz, dotz, ii TYPE int
LOCAL fractiondownloaded TYPE double
IF dltotal <= 0.0 THEN RETURN 0
totaldotz = 40
fractiondownloaded = dlnow/dltotal
dotz = ROUND(fractiondownloaded * totaldotz)
PRINT fractiondownloaded * 100 FORMAT "%3.0f%% [";
WHILE ii < dotz DO
PRINT "=";
INCR ii
WEND
WHILE ii < totaldotz DO
PRINT " ";
INCR ii
WEND
PRINT (STRING)clientp FORMAT "] %s\r";
RETURN 0
END FUNCTION
' Include the CURL context
INCLUDE "curl.bac"
CONST filename$ = "documentation.pdf"
CONST savefilename$ = "Bacon.pdf"
CLEAR
PRINT "Downloading Bacon Documentation in PDF format...\n"
' File where we store the download
OPEN savefilename$ FOR WRITING AS download
' Create the handle
curl = curl_easy_init()
' Set the options
curl_easy_setopt(curl, CURLOPT_URL, CONCAT$("http://www.basic-converter.org/", filename$))
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1)
curl_easy_setopt(curl, CURLOPT_WRITEDATA, download)
curl_easy_setopt(curl, CURLOPT_NOPROGRESS,0)
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION,progress_callback)
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, ADDRESS(savefilename$))
' Perform the GET
success = curl_easy_perform(curl)
' Cleanup
curl_easy_cleanup(curl)
' Close filehandle
CLOSE FILE download
PRINT "\n\nDownload Complete.\n"
This was pretty easy to implement because Bacon is a phenomenal language!
Tested on both macOS and Linux (64bit Ubuntu Server and Raspbian on a RasPi).
For macOS, you'll need to change the name of the library in the curl.bac file from:Just change the $lib= section towards the top of the curl.bac file to
IF INSTR(OS$, "Darwin") THEN
lib$ = "libcurl.dylib"
ELSE
lib$ = "libcurl.so"
END IF
AIR.