From 1624a0f26898b81cd548cb66f6e302afb2bf7bb8 Mon Sep 17 00:00:00 2001
From: Rob Ward <rob@rob-ward.co.uk>
Date: Thu, 1 Dec 2011 09:20:40 +0000
Subject: [PATCH] Example: Edit progress function example - include timed interval

Adds a timer based off of CURLINFO_TOTAL_TIME that is used to perform
certain actions after a minimum amount of time has passed using the
progress function. As a consequence the curl handle is now also passed
into the progress function. Progress example now also includes an
example of how to retreive the TOTAL_TIME and print it out.
---
 docs/examples/progressfunc.c |   23 ++++++++++++++++++++++-
 1 files changed, 22 insertions(+), 1 deletions(-)

diff --git a/docs/examples/progressfunc.c b/docs/examples/progressfunc.c
index 42ed328..cb9fd94 100644
--- a/docs/examples/progressfunc.c
+++ b/docs/examples/progressfunc.c
@@ -22,17 +22,34 @@
 #include <stdio.h>
 #include <curl/curl.h>
 
-#define STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES 6000
+#define STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES         6000
+#define MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL     3
+
+double lastruntime = 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);
+
+  //under certain circumstances it may be desirable for certain functionality
+  //to only fun every N seconds, in order to do this the transaction time can
+  //be used
+  if((curtime - lastruntime) >= MINIMAL_PROGRESS_FUNCTIONALITY_INTERVAL) {
+    lastruntime = curtime;
+    fprintf(stderr, "TOTAL TIME: %f \r\n", curtime);
+  }
+
   fprintf(stderr, "UP: %g of %g  DOWN: %g of %g\r\n",
           ulnow, ultotal, dlnow, dltotal);
 
   if(dlnow > STOP_DOWNLOAD_AFTER_THIS_MANY_BYTES)
     return 1;
+
   return 0;
 }
 
@@ -45,7 +62,11 @@ int main(void)
   if(curl) {
     curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/");
     curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress);
+    curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, curl); //pass the curl
+                                                        //pointer into the
+                                                        //progress function
     curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
+
     res = curl_easy_perform(curl);
 
     if(res)
-- 
1.7.4.1

