Merge pull request #25 from trtom/master
[platform/upstream/curl.git] / src / tool_cb_prg.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, 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 "setup.h"
23
24 #include <curl/curl.h>
25
26 #define ENABLE_CURLX_PRINTF
27 /* use our own printf() functions */
28 #include "curlx.h"
29
30 #include "tool_cfgable.h"
31 #include "tool_cb_prg.h"
32
33 #include "memdebug.h" /* keep this as LAST include */
34
35 /*
36 ** callback for CURLOPT_PROGRESSFUNCTION
37 */
38
39 int tool_progress_cb(void *clientp,
40                      double dltotal, double dlnow,
41                      double ultotal, double ulnow)
42 {
43   /* The original progress-bar source code was written for curl by Lars Aas,
44      and this new edition inherits some of his concepts. */
45
46   char line[256];
47   char outline[256];
48   char format[40];
49   double frac;
50   double percent;
51   int barwidth;
52   int num;
53   int i;
54
55   struct ProgressData *bar = (struct ProgressData *)clientp;
56
57   /* expected transfer size */
58   curl_off_t total = (curl_off_t)dltotal + (curl_off_t)ultotal +
59     bar->initial_size;
60
61   /* we've come this far */
62   curl_off_t point = (curl_off_t)dlnow + (curl_off_t)ulnow +
63     bar->initial_size;
64
65   if(point > total)
66     /* we have got more than the expected total! */
67     total = point;
68
69   /* simply count invokes */
70   bar->calls++;
71
72   if(total < 1) {
73     curl_off_t prevblock = bar->prev / 1024;
74     curl_off_t thisblock = point / 1024;
75     while(thisblock > prevblock) {
76       fprintf(bar->out, "#");
77       prevblock++;
78     }
79   }
80   else {
81     frac = (double)point / (double)total;
82     percent = frac * 100.0f;
83     barwidth = bar->width - 7;
84     num = (int) (((double)barwidth) * frac);
85     for(i = 0; i < num; i++)
86       line[i] = '#';
87     line[i] = '\0';
88     snprintf(format, sizeof(format), "%%-%ds %%5.1f%%%%", barwidth);
89     snprintf(outline, sizeof(outline), format, line, percent);
90     fprintf(bar->out, "\r%s", outline);
91   }
92   fflush(bar->out);
93   bar->prev = point;
94
95   return 0;
96 }
97
98 void progressbarinit(struct ProgressData *bar,
99                      struct Configurable *config)
100 {
101 #ifdef __EMX__
102   /* 20000318 mgs */
103   int scr_size[2];
104 #endif
105   char *colp;
106
107   memset(bar, 0, sizeof(struct ProgressData));
108
109   /* pass this through to progress function so
110    * it can display progress towards total file
111    * not just the part that's left. (21-may-03, dbyron) */
112   if(config->use_resume)
113     bar->initial_size = config->resume_from;
114
115 /* TODO: get terminal width through ansi escapes or something similar.
116    try to update width when xterm is resized... - 19990617 larsa */
117 #ifndef __EMX__
118   /* 20000318 mgs
119    * OS/2 users most likely won't have this env var set, and besides that
120    * we're using our own way to determine screen width */
121   colp = curlx_getenv("COLUMNS");
122   if(colp) {
123     char *endptr;
124     long num = strtol(colp, &endptr, 10);
125     if((endptr != colp) && (endptr == colp + strlen(colp)) && (num > 0))
126       bar->width = (int)num;
127     else
128       bar->width = 79;
129     curl_free(colp);
130   }
131   else
132     bar->width = 79;
133 #else
134   /* 20000318 mgs
135    * We use this emx library call to get the screen width, and subtract
136    * one from what we got in order to avoid a problem with the cursor
137    * advancing to the next line if we print a string that is as long as
138    * the screen is wide. */
139
140   _scrsize(scr_size);
141   bar->width = scr_size[0] - 1;
142 #endif
143
144   bar->out = config->errors;
145 }
146