Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <stdio.h>
00028 #include <curl/curl.h>
00029
00030 #define STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES 6000
00031 #define MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL 3
00032
00033 struct myprogress {
00034 double lastruntime;
00035 CURL *curl;
00036 };
00037
00038
00039 static int xferinfo(void *p,
00040 curl_off_t dltotal, curl_off_t dlnow,
00041 curl_off_t ultotal, curl_off_t ulnow)
00042 {
00043 struct myprogress *myp = (struct myprogress *)p;
00044 CURL *curl = myp->curl;
00045 double curtime = 0;
00046
00047 curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &curtime);
00048
00049
00050
00051
00052 if((curtime - myp->lastruntime) >= MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL) {
00053 myp->lastruntime = curtime;
00054 fprintf(stderr, "TOTAL TIME: %f \r\n", curtime);
00055 }
00056
00057 fprintf(stderr, "UP: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T
00058 " DOWN: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T
00059 "\r\n",
00060 ulnow, ultotal, dlnow, dltotal);
00061
00062 if(dlnow > STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES)
00063 return 1;
00064 return 0;
00065 }
00066
00067
00068 static int older_progress(void *p,
00069 double dltotal, double dlnow,
00070 double ultotal, double ulnow)
00071 {
00072 return xferinfo(p,
00073 (curl_off_t)dltotal,
00074 (curl_off_t)dlnow,
00075 (curl_off_t)ultotal,
00076 (curl_off_t)ulnow);
00077 }
00078
00079
00080 int main(void)
00081 {
00082 CURL *curl;
00083 CURLcode res = CURLE_OK;
00084 struct myprogress prog;
00085
00086 curl = curl_easy_init();
00087 if(curl) {
00088 prog.lastruntime = 0;
00089 prog.curl = curl;
00090
00091 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
00092
00093 curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, older_progress);
00094
00095 curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &prog);
00096
00097 #if LIBCURL_VERSION_NUM >= 0x072000
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo);
00109
00110
00111 curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &prog);
00112 #endif
00113
00114 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
00115 res = curl_easy_perform(curl);
00116
00117 if(res != CURLE_OK)
00118 fprintf(stderr, "%s\n", curl_easy_strerror(res));
00119
00120
00121 curl_easy_cleanup(curl);
00122 }
00123 return (int)res;
00124 }