1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334 | //-------------------------------------------------------------------------
// Filename : TtyProgressTool.cpp
//
// Purpose : Progress bar for use in text terminals (ttys)
//
// Special Notes :
//
// Creator : Jason Kraftcheck
//
// Creation Date : 06/16/03
//-------------------------------------------------------------------------
#ifndef _WIN32
# include <unistd.h>
#endif
#include <cstring>
#include "AppUtil.hpp"
#include "TtyProgressTool.hpp"
#include "CubitUtil.hpp"
// Enable/disable display of progress bar and/or numeric percent.
const bool DISPLAY_PROGRESS_BAR = true;
const bool DISPLAY_NUM_PERCENT = true;
// Min size for progress bar. Will not be shown if available
// space is less than this value.
const int MIN_PROGRESS_BAR_WIDTH = 10;
// Maximum size for progress bar. If 0, limited only by available
// space.
const int MAX_PROGRESS_BAR_WIDTH = 0;
// Characters composing progress bar.
const char PROGRESS_BAR_START = '|';
const char PROGRESS_BAR_END = '|';
const char PROGRESS_BAR_FILLED = '=';
const char PROGRESS_BAR_CURRENT = '>';
const char PROGRESS_BAR_EMPTY = ' ';
/* buffer layout:
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|\r|m |e |s |s |a |g |e |: | || |= |= |= |= |= |> | | || |9 |9 |9 |% |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
^ ^ ^
| | |
+--- lineBuffer +--- barStart bufferEnd ---+
*/
TtyProgressTool::~TtyProgressTool()
{
clear_all();
}
void TtyProgressTool::clear_all()
{
// free allocated buffers
// (messages were created with strdup (CubitUtil::util_strdup),
// which uses malloc so use free() (CubitUtil::util_strdup_free())).
delete [] lineBuffer;
if (firstMessage)
CubitUtil::util_strdup_free(firstMessage);
if (secondMessage)
CubitUtil::util_strdup_free(secondMessage);
// zero data so calls to step or percent fail.
myRange = 0;
prevWidth = 0;
lineBuffer = barStart = bufferEnd = 0;
firstMessage = secondMessage = 0;
}
bool TtyProgressTool::update()
{
// Minimum/maximum space for progress bar. (Add 2 to constants
// for the leading and trailing '|' character.)
const int MIN_PROGRESS_WIDTH = MIN_PROGRESS_BAR_WIDTH + 2;
const int MAX_PROGRESS_WIDTH = MAX_PROGRESS_BAR_WIDTH + 2;
// Get terminal width. Assume 80 if query fails.
int width, height;
if ( !AppUtil::instance()->get_terminal_size(height, width) )
width = 80;
#ifdef _WIN32
// Windows cmd.exe wraps the line when the last character
// of the line is output, rather than the first character
// after the end of the line. Stop one short so to avoid
// newlines.
width--;
#endif
// If terminal width hasn't changed then no update is
// required. return.
if ( width && width == prevWidth )
return true;
prevWidth = width;
// If width isn't at least 6 don't try to display anything.
// Clear data and return.
if ( width < 4 )
{
delete [] lineBuffer;
lineBuffer = barStart = bufferEnd = 0;
return false;
}
// Re-create lineBuffer for the new terminal width if necessary.
if ( !lineBuffer || (bufferEnd - lineBuffer - 1 < width) )
{
delete [] lineBuffer;
#ifdef _WIN32
lineBuffer = new char [width+2];
lineBuffer[width+1] = '\r';
#else
lineBuffer = new char [width+1];
#endif
if ( !lineBuffer )
return false;
}
bufferEnd = lineBuffer + width + 1; // one past end of buffer
barStart = 0; // start of progress bar (null if no progress bar)
char* bar_end = 0; // end of progress bar
// Current start and end (fill lineBuffer until start == end)
char* start = lineBuffer;
char* end = bufferEnd;
// Use \r to move cursor to beginning of line without advancing a line.
*(start++) = '\r'; // leading \r
// If displaying numeric percent on end of line, allocate 4 spaces
// at end of line and place '%' char in the fourth one.
if (DISPLAY_NUM_PERCENT && (end - start > 3))
{
end[-1] = '%';
end -= 4;
}
// Copy first message into line
if (firstMessage && (end - start > 2))
{
int len = strlen(firstMessage);
// need to truncate message?
if (len + 2 > end - start)
len = end - start - 2;
memcpy(start, firstMessage, len);
start += len;
// add trailing ": "
*(start++) = ':';
*(start++) = ' ';
}
// Allocate space for minimum progress bar if sufficient space
if (DISPLAY_PROGRESS_BAR && (end - start > MIN_PROGRESS_WIDTH))
{
bar_end = end;
end -= MIN_PROGRESS_WIDTH;
}
// Copy second message into line buffer
if (secondMessage && (end - start > 2))
{
int len = strlen(secondMessage);
// need to truncate message?
if (len + 2 > end - start)
len = end - start - 2;
memcpy(start, secondMessage, len);
start += len;
// add trailing ": "
*(start++) = ':';
*(start++) = ' ';
}
// Finish progress bar setup (if progress bar is displayed at all)
if (bar_end)
{
// Grow progress bar up to MAX_PROGRESS_WIDTH (or all the
// remaining space if MAX_PROGRESS_BAR_WIDTH is unset)
int diff = bar_end - start - MAX_PROGRESS_WIDTH;
end = start;
if (MAX_PROGRESS_BAR_WIDTH && diff > 0)
end += diff;
// Put in bounding '|' chars for progress bar and
// initialize barStart to point *after* the leading '|'
barStart = end;
barStart[0] = PROGRESS_BAR_START;
bar_end[-1] = PROGRESS_BAR_END;
barStart++;
}
// fill any remaining space with spaces
if (start < end)
memset( start, ' ', end - start );
return true;
}
void TtyProgressTool::start( int lower, int upper,
const char* s1, const char* s2,
CubitBoolean, CubitBoolean )
{
// get rid of any old state if someone forgot to call end().
clear_all();
// store passed range
myRange = upper - lower;
currentVal = currPercent = 0;
prevWidth = 0;
// copy passed messages
const char* empty = "";<--- The scope of the variable 'empty' can be reduced. [+]The scope of the variable 'empty' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
if (s1)
firstMessage = CubitUtil::util_strdup((char*)s1);
else
firstMessage = CubitUtil::util_strdup((char*)empty);
if (s2)
secondMessage = CubitUtil::util_strdup((char*)s2);
// generate line buffer
display(0,0);
}
void TtyProgressTool::step()
{
int new_count = currentVal + 1;
if( myRange > 0 )
display( new_count, 100 * new_count / myRange );
}
void TtyProgressTool::percent( double p )
{
display( (int)(p * myRange), (int)(100 * p) );
}
void TtyProgressTool::end()
{
// clear terminal line
if (lineBuffer)
{
memset( lineBuffer + 1, ' ', bufferEnd - lineBuffer - 1 );
fwrite( lineBuffer, bufferEnd - lineBuffer, 1, stdout );
putchar('\r');
fflush( stdout );
}
// clear internal data
clear_all();
}
void TtyProgressTool::check_interrupt()
{
}
void TtyProgressTool::display( int new_count, int new_percent )
{
int prev_width = prevWidth;
bool do_output = false;
// check for change in terminal width and update if necessary.
if (!update())
return;
// if tty size changed or percent changed, need to update
if (prev_width != prevWidth || new_percent != currPercent)
{
currPercent = new_percent;
do_output = true;
}
// calculate start of numeric percent in lineBuffer
char* pctStart = bufferEnd;
if (DISPLAY_NUM_PERCENT)
pctStart -= 4; // start of numeric percent
// check if the progress bar output is changing, and if
// so set do_output to true
size_t bar_width = 0, bar_drawn = 0, old_drawn;<--- The scope of the variable 'old_drawn' can be reduced. [+]The scope of the variable 'old_drawn' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
if (barStart && myRange > 0)
{
bar_width = pctStart - barStart - 1;
bar_drawn = new_count >= myRange ? bar_width :
new_count <= 0 ? 0 :
bar_width * new_count / myRange;
old_drawn = currentVal >= myRange ? bar_width :
currentVal <= 0 ? 0 :
bar_width * currentVal / myRange;
if (bar_drawn != old_drawn)
do_output = true;
}
currentVal = new_count;
if (DISPLAY_NUM_PERCENT && do_output)
{
// normalize numeric percent to values that can be displayed
int pct = currPercent;
if (pct < 0)
pct = 0;
else if(pct > 999)
pct = 999;
// display numeric percent in last three chars of line
pctStart[2] = (char)('0' + pct % 10);
pctStart[1] = pctStart[0] = ' ';
for ( int i = 1; i >= 0 && pct > 9; i-- )
{
pct /= 10;
pctStart[i] = (char)('0' + pct % 10);
}
}
// if progress bar is to be displayed, update it.
if (barStart && do_output)
{
// write '=' for filled section of bar
if (bar_drawn > 1)
memset(barStart, PROGRESS_BAR_FILLED, bar_drawn - 1);
// write '>' between filled and unfilled sections of bar
if (bar_drawn > 0)
barStart[bar_drawn - 1] = PROGRESS_BAR_CURRENT;
// write spaces in unfilled part of bar
if (bar_drawn < bar_width)
memset(barStart + bar_drawn, PROGRESS_BAR_EMPTY, bar_width - bar_drawn);
}
// write the buffer to the terminal
if (do_output)
{
fwrite( lineBuffer, bufferEnd - lineBuffer, 1, stdout );
fflush( stdout );
}
}
|