progressfunc.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * Use the progress callbacks, old and/or new one depending on available
00024  * libcurl version.
00025  * </DESC>
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 /* this is how the CURLOPT_XFERINFOFUNCTION callback works */
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   /* under certain circumstances it may be desirable for certain functionality
00050      to only run every N seconds, in order to do this the transaction time can
00051      be used */
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 /* for libcurl older than 7.32.0 (CURLOPT_PROGRESSFUNCTION) */
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     /* pass the struct pointer into the progress function */
00095     curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &prog);
00096 
00097 #if LIBCURL_VERSION_NUM >= 0x072000
00098     /* xferinfo was introduced in 7.32.0, no earlier libcurl versions will
00099        compile as they won't have the symbols around.
00100 
00101        If built with a newer libcurl, but running with an older libcurl:
00102        curl_easy_setopt() will fail in run-time trying to set the new
00103        callback, making the older callback get used.
00104 
00105        New libcurls will prefer the new callback and instead use that one even
00106        if both callbacks are set. */
00107 
00108     curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo);
00109     /* pass the struct pointer into the xferinfo function, note that this is
00110        an alias to CURLOPT_PROGRESSDATA */
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     /* always cleanup */
00121     curl_easy_cleanup(curl);
00122   }
00123   return (int)res;
00124 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06