Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "tool_setup.h"
00023
00024 #define ENABLE_CURLX_PRINTF
00025
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"
00033
00034
00035
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
00045
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
00059 total = dltotal + ultotal + bar->initial_size;
00060
00061
00062 point = dlnow + ulnow + bar->initial_size;
00063
00064 if(bar->calls && (tvdiff(now, bar->prevtime) < 100L) && point < total)
00065
00066
00067 return 0;
00068
00069 if(point > total)
00070
00071 total = point;
00072
00073
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
00108 int scr_size[2];
00109 #endif
00110 char *colp;
00111
00112 memset(bar, 0, sizeof(struct ProgressData));
00113
00114
00115
00116
00117 if(config->use_resume)
00118 bar->initial_size = config->resume_from;
00119
00120
00121
00122 #ifndef __EMX__
00123
00124
00125
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
00140
00141
00142
00143
00144
00145 _scrsize(scr_size);
00146 bar->width = scr_size[0] - 1;
00147 #endif
00148
00149 bar->out = config->global->errors;
00150 }