tizen 2.3.1 release
[external/curl.git] / docs / examples / progressfunc.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2013, 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 http://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 #include <stdio.h>
23 #include <curl/curl.h>
24
25 #define STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES         6000
26 #define MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL     3
27
28 struct myprogress {
29   double lastruntime;
30   CURL *curl;
31 };
32
33 /* this is how the CURLOPT_XFERINFOFUNCTION callback works */
34 static int xferinfo(void *p,
35                     curl_off_t dltotal, curl_off_t dlnow,
36                     curl_off_t ultotal, curl_off_t ulnow)
37 {
38   struct myprogress *myp = (struct myprogress *)p;
39   CURL *curl = myp->curl;
40   double curtime = 0;
41
42   curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &curtime);
43
44   /* under certain circumstances it may be desirable for certain functionality
45      to only run every N seconds, in order to do this the transaction time can
46      be used */
47   if((curtime - myp->lastruntime) >= MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL) {
48     myp->lastruntime = curtime;
49     fprintf(stderr, "TOTAL TIME: %f \r\n", curtime);
50   }
51
52   fprintf(stderr, "UP: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T
53           "  DOWN: %" CURL_FORMAT_CURL_OFF_T " of %" CURL_FORMAT_CURL_OFF_T
54           "\r\n",
55           ulnow, ultotal, dlnow, dltotal);
56
57   if(dlnow > STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES)
58     return 1;
59   return 0;
60 }
61
62 /* for libcurl older than 7.32.0 (CURLOPT_PROGRESSFUNCTION) */
63 static int older_progress(void *p,
64                           double dltotal, double dlnow,
65                           double ultotal, double ulnow)
66 {
67   return xferinfo(p,
68                   (curl_off_t)dltotal,
69                   (curl_off_t)dlnow,
70                   (curl_off_t)ultotal,
71                   (curl_off_t)ulnow);
72 }
73
74
75 int main(void)
76 {
77   CURL *curl;
78   CURLcode res = CURLE_OK;
79   struct myprogress prog;
80
81   curl = curl_easy_init();
82   if(curl) {
83     prog.lastruntime = 0;
84     prog.curl = curl;
85
86     curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
87
88     curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, older_progress);
89     /* pass the struct pointer into the progress function */
90     curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, &prog);
91
92 #if LIBCURL_VERSION_NUM >= 0x072000
93     /* xferinfo was introduced in 7.32.0, no earlier libcurl versions will
94        compile as they won't have the symbols around.
95
96        If built with a newer libcurl, but running with an older libcurl:
97        curl_easy_setopt() will fail in run-time trying to set the new
98        callback, making the older callback get used.
99
100        New libcurls will prefer the new callback and instead use that one even
101        if both callbacks are set. */
102
103     curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, xferinfo);
104     /* pass the struct pointer into the xferinfo function, note that this is
105        an alias to CURLOPT_PROGRESSDATA */
106     curl_easy_setopt(curl, CURLOPT_XFERINFODATA, &prog);
107 #endif
108
109     curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
110     res = curl_easy_perform(curl);
111
112     if(res != CURLE_OK)
113       fprintf(stderr, "%s\n", curl_easy_strerror(res));
114
115     /* always cleanup */
116     curl_easy_cleanup(curl);
117   }
118   return (int)res;
119 }