cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Progress function trigger time

From: Rob Ward <rob_at_rob-ward.co.uk>
Date: Wed, 30 Nov 2011 13:53:14 +0000

On 30 November 2011 12:24, Saidus Bounderra <bsaidus_at_gmail.com> wrote:

> thanks Rob ... I thinhk it's a good idia
> I will exploite it .. and tell you ..
> but I think that using WinAPI GetTickCount() will be a good alternative ...
>
>
> -------------------------------------------------------------------
> List admin: http://cool.haxx.se/list/listinfo/curl-library
> Etiquette: http://curl.haxx.se/mail/etiquette.html
>

I've just being having another think of a way to make it work on all
platforms in a simple way. As such I've made a few modifcations to the code
below so that it uses the time obtained from the curl transaction for the
counter, this should make it a lot easier, universal accross allplatforms
and also make it so that the same progress function can be used with
multiple handles. Overall I think it is probably more useful to you than
the first version.

I'm wondering it this is a worthwhile piece of code to add as an example
application(needs tidying obviously), does anyone have an opinion on this?

Cheers,

Rob

#include <stdio.h>
#include <curl/curl.h>

#define PROGRESS_INTERVAL 5
double lastrun = 0;

static int progress(void *p,
                    double dltotal, double dlnow,
                    double ultotal, double ulnow)
{
  CURL* curl = (CURL*)p;

  double curtime = 0;
  curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &curtime);

  if( ( curtime - lastrun) >= PROGRESS_INTERVAL)
  {
    lastrun = curtime;

    fprintf(stderr, "UP: %g of %g DOWN: %g of %g\r\n",
          ulnow, ultotal, dlnow, dltotal);
  }
  else
  {
    //printf("\nnot doing anything\n");
  }

  return 0;
}

static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
  int written = fwrite(ptr, size, nmemb, (FILE *)stream);
  return written;
}

int main(void)
{
  CURL *curl;
  CURLcode res=0;
  static const char *bodyfilename = "body.out";
  FILE *bodyfile;

  curl = curl_easy_init();
  if(curl) {

    bodyfile = fopen(bodyfilename,"w");
    if (bodyfile == NULL) {
      curl_easy_cleanup(curl);
      return -1;
    }
    curl_easy_setopt(curl, CURLOPT_URL, "http://rob-ward.net/testfile1.txt
");
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress);
    curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, curl);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, bodyfile);
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
    curl_easy_setopt(curl, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)5000);

    res = curl_easy_perform(curl);

    if(res)
      fprintf(stderr, "%s\n", curl_easy_strerror(res));

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  fclose(bodyfile);

  return (int)res;
}

-- 
------------------------------
Rob Ward
www.rob-ward.co.uk

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2011-11-30