progressfunc.c 3.9 KB

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