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