tool_cb_prg.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "tool_setup.h"
00023 
00024 #define ENABLE_CURLX_PRINTF
00025 /* use our own printf() functions */
00026 #include "curlx.h"
00027 
00028 #include "tool_cfgable.h"
00029 #include "tool_cb_prg.h"
00030 #include "tool_util.h"
00031 
00032 #include "memdebug.h" /* keep this as LAST include */
00033 
00034 /*
00035 ** callback for CURLOPT_XFERINFOFUNCTION
00036 */
00037 
00038 #define MAX_BARLENGTH 256
00039 
00040 int tool_progress_cb(void *clientp,
00041                      curl_off_t dltotal, curl_off_t dlnow,
00042                      curl_off_t ultotal, curl_off_t ulnow)
00043 {
00044   /* The original progress-bar source code was written for curl by Lars Aas,
00045      and this new edition inherits some of his concepts. */
00046 
00047   char line[MAX_BARLENGTH+1];
00048   char format[40];
00049   double frac;
00050   double percent;
00051   int barwidth;
00052   int num;
00053   struct timeval now = tvnow();
00054   struct ProgressData *bar = (struct ProgressData *)clientp;
00055   curl_off_t total;
00056   curl_off_t point;
00057 
00058   /* expected transfer size */
00059   total = dltotal + ultotal + bar->initial_size;
00060 
00061   /* we've come this far */
00062   point = dlnow + ulnow + bar->initial_size;
00063 
00064   if(bar->calls && (tvdiff(now, bar->prevtime) < 100L) && point < total)
00065     /* after first call, limit progress-bar updating to 10 Hz */
00066     /* update when we're at 100% even if last update is less than 200ms ago */
00067     return 0;
00068 
00069   if(point > total)
00070     /* we have got more than the expected total! */
00071     total = point;
00072 
00073   /* simply count invokes */
00074   bar->calls++;
00075 
00076   if(total < 1) {
00077     curl_off_t prevblock = bar->prev / 1024;
00078     curl_off_t thisblock = point / 1024;
00079     while(thisblock > prevblock) {
00080       fprintf(bar->out, "#");
00081       prevblock++;
00082     }
00083   }
00084   else if(point != bar->prev) {
00085     frac = (double)point / (double)total;
00086     percent = frac * 100.0f;
00087     barwidth = bar->width - 7;
00088     num = (int) (((double)barwidth) * frac);
00089     if(num > MAX_BARLENGTH)
00090       num = MAX_BARLENGTH;
00091     memset(line, '#', num);
00092     line[num] = '\0';
00093     snprintf(format, sizeof(format), "\r%%-%ds %%5.1f%%%%", barwidth);
00094     fprintf(bar->out, format, line, percent);
00095   }
00096   fflush(bar->out);
00097   bar->prev = point;
00098   bar->prevtime = now;
00099 
00100   return 0;
00101 }
00102 
00103 void progressbarinit(struct ProgressData *bar,
00104                      struct OperationConfig *config)
00105 {
00106 #ifdef __EMX__
00107   /* 20000318 mgs */
00108   int scr_size[2];
00109 #endif
00110   char *colp;
00111 
00112   memset(bar, 0, sizeof(struct ProgressData));
00113 
00114   /* pass this through to progress function so
00115    * it can display progress towards total file
00116    * not just the part that's left. (21-may-03, dbyron) */
00117   if(config->use_resume)
00118     bar->initial_size = config->resume_from;
00119 
00120 /* TODO: get terminal width through ansi escapes or something similar.
00121    try to update width when xterm is resized... - 19990617 larsa */
00122 #ifndef __EMX__
00123   /* 20000318 mgs
00124    * OS/2 users most likely won't have this env var set, and besides that
00125    * we're using our own way to determine screen width */
00126   colp = curlx_getenv("COLUMNS");
00127   if(colp) {
00128     char *endptr;
00129     long num = strtol(colp, &endptr, 10);
00130     if((endptr != colp) && (endptr == colp + strlen(colp)) && (num > 0))
00131       bar->width = (int)num;
00132     else
00133       bar->width = 79;
00134     curl_free(colp);
00135   }
00136   else
00137     bar->width = 79;
00138 #else
00139   /* 20000318 mgs
00140    * We use this emx library call to get the screen width, and subtract
00141    * one from what we got in order to avoid a problem with the cursor
00142    * advancing to the next line if we print a string that is as long as
00143    * the screen is wide. */
00144 
00145   _scrsize(scr_size);
00146   bar->width = scr_size[0] - 1;
00147 #endif
00148 
00149   bar->out = config->global->errors;
00150 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06