progressfunc.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 /* <DESC>
23  * Use the progress callbacks, old and/or new one depending on available
24  * libcurl version.
25  * </DESC>
26  */
27 #include <stdio.h>
28 #include <curl/curl.h>
29 
30 #define STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES 6000
31 #define MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL 3
32 
33 struct myprogress {
34  double lastruntime;
36 };
37 
38 /* this is how the CURLOPT_XFERINFOFUNCTION callback works */
39 static int xferinfo(void *p,
40  curl_off_t dltotal, curl_off_t dlnow,
41  curl_off_t ultotal, curl_off_t ulnow)
42 {
43  struct myprogress *myp = (struct myprogress *)p;
44  CURL *curl = myp->curl;
45  double curtime = 0;
46 
47  curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &curtime);
48 
49  /* under certain circumstances it may be desirable for certain functionality
50  to only run every N seconds, in order to do this the transaction time can
51  be used */
53  myp->lastruntime = curtime;
54  fprintf(stderr, "TOTAL TIME: %f \r\n", curtime);
55  }
56 
59  "\r\n",
60  ulnow, ultotal, dlnow, dltotal);
61 
63  return 1;
64  return 0;
65 }
66 
67 /* for libcurl older than 7.32.0 (CURLOPT_PROGRESSFUNCTION) */
68 static int older_progress(void *p,
69  double dltotal, double dlnow,
70  double ultotal, double ulnow)
71 {
72  return xferinfo(p,
73  (curl_off_t)dltotal,
74  (curl_off_t)dlnow,
75  (curl_off_t)ultotal,
76  (curl_off_t)ulnow);
77 }
78 
79 
80 int main(void)
81 {
82  CURL *curl;
84  struct myprogress prog;
85 
86  curl = curl_easy_init();
87  if(curl) {
88  prog.lastruntime = 0;
89  prog.curl = curl;
90 
91  curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
92 
93  curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, older_progress);
94  /* pass the struct pointer into the progress function */
95  curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &prog);
96 
97 #if LIBCURL_VERSION_NUM >= 0x072000
98  /* xferinfo was introduced in 7.32.0, no earlier libcurl versions will
99  compile as they won't have the symbols around.
100 
101  If built with a newer libcurl, but running with an older libcurl:
102  curl_easy_setopt() will fail in run-time trying to set the new
103  callback, making the older callback get used.
104 
105  New libcurls will prefer the new callback and instead use that one even
106  if both callbacks are set. */
107 
108  curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo);
109  /* pass the struct pointer into the xferinfo function, note that this is
110  an alias to CURLOPT_PROGRESSDATA */
112 #endif
113 
114  curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
115  res = curl_easy_perform(curl);
116 
117  if(res != CURLE_OK)
118  fprintf(stderr, "%s\n", curl_easy_strerror(res));
119 
120  /* always cleanup */
121  curl_easy_cleanup(curl);
122  }
123  return (int)res;
124 }
static int xferinfo(void *p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
Definition: progressfunc.c:39
#define STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES
Definition: progressfunc.c:30
CURLcode
Definition: curl.h:454
static int res
#define CURLOPT_XFERINFODATA
Definition: curl.h:1086
#define curl_easy_setopt(handle, option, value)
Definition: typecheck-gcc.h:41
const char ** p
Definition: unit1394.c:76
#define curl_easy_getinfo(handle, info, arg)
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
static int older_progress(void *p, double dltotal, double dlnow, double ultotal, double ulnow)
Definition: progressfunc.c:68
CURL_TYPEOF_CURL_OFF_T curl_off_t
Definition: system.h:420
Definition: curl.h:455
int main(void)
Definition: progressfunc.c:80
void CURL
Definition: curl.h:102
CURL * curl
Definition: progressfunc.c:35
#define fprintf
Definition: curl_printf.h:41
#define MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL
Definition: progressfunc.c:31
double lastruntime
Definition: progressfunc.c:34
CURL_EXTERN const char * curl_easy_strerror(CURLcode)
Definition: strerror.c:57
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl)
#define CURL_FORMAT_CURL_OFF_T
Definition: system.h:373


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16