Floating point error. Bug #64394
[ntk/apt.git] / apt-pkg / contrib / progress.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: progress.cc,v 1.9 2000/06/05 04:22:25 jgg Exp $
4 /* ######################################################################
5
6 OpProgress - Operation Progress
7
8 ##################################################################### */
9 /*}}}*/
10 // Include Files /*{{{*/
11 #ifdef __GNUG__
12 #pragma implementation "apt-pkg/progress.h"
13 #endif
14 #include <apt-pkg/progress.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/configuration.h>
17 #include <stdio.h>
18 /*}}}*/
19
20 // OpProgress::OpProgress - Constructor /*{{{*/
21 // ---------------------------------------------------------------------
22 /* */
23 OpProgress::OpProgress() : Current(0), Total(0), Size(0), SubTotal(1),
24 LastPercent(0), Percent(0)
25 {
26 memset(&LastTime,0,sizeof(LastTime));
27 }
28 /*}}}*/
29 // OpProgress::Progress - Sub progress with no state change /*{{{*/
30 // ---------------------------------------------------------------------
31 /* Current is the Base Overall progress in units of Total. Cur is the sub
32 progress in units of SubTotal. Size is a scaling factor that says what
33 percent of Total SubTotal is. */
34 void OpProgress::Progress(unsigned long Cur)
35 {
36 if (Total == 0 || Size == 0 || SubTotal == 0)
37 Percent = 0;
38 else
39 Percent = (Current + Cur/((float)SubTotal)*Size)*100.0/Total;
40 Update();
41 }
42 /*}}}*/
43 // OpProgress::OverallProgress - Set the overall progress /*{{{*/
44 // ---------------------------------------------------------------------
45 /* */
46 void OpProgress::OverallProgress(unsigned long Current, unsigned long Total,
47 unsigned long Size,string Op)
48 {
49 this->Current = Current;
50 this->Total = Total;
51 this->Size = Size;
52 this->Op = Op;
53 SubOp = string();
54 if (Total == 0)
55 Percent = 0;
56 else
57 Percent = Current*100.0/Total;
58 Update();
59 }
60 /*}}}*/
61 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
62 // ---------------------------------------------------------------------
63 /* */
64 void OpProgress::SubProgress(unsigned long SubTotal,string Op)
65 {
66 this->SubTotal = SubTotal;
67 SubOp = Op;
68 if (Total == 0)
69 Percent = 0;
70 else
71 Percent = Current*100.0/Total;
72 Update();
73 }
74 /*}}}*/
75 // OpProgress::SubProgress - Set the sub progress state /*{{{*/
76 // ---------------------------------------------------------------------
77 /* */
78 void OpProgress::SubProgress(unsigned long SubTotal)
79 {
80 this->SubTotal = SubTotal;
81 if (Total == 0)
82 Percent = 0;
83 else
84 Percent = Current*100.0/Total;
85 Update();
86 }
87 /*}}}*/
88 // OpProgress::CheckChange - See if the display should be updated /*{{{*/
89 // ---------------------------------------------------------------------
90 /* Progress calls are made so frequently that if every one resulted in
91 an update the display would be swamped and the system much slower.
92 This provides an upper bound on the update rate. */
93 bool OpProgress::CheckChange(float Interval)
94 {
95 // New major progress indication
96 if (Op != LastOp)
97 {
98 MajorChange = true;
99 LastOp = Op;
100 return true;
101 }
102 MajorChange = false;
103
104 if (SubOp != LastSubOp)
105 {
106 LastSubOp = SubOp;
107 return true;
108 }
109
110 if ((int)LastPercent == (int)Percent)
111 return false;
112
113 // Check time delta
114 struct timeval Now;
115 gettimeofday(&Now,0);
116 double Diff = Now.tv_sec - LastTime.tv_sec + (Now.tv_usec - LastTime.tv_usec)/1000000.0;
117 if (Diff < Interval)
118 return false;
119 LastTime = Now;
120 LastPercent = Percent;
121 return true;
122 }
123 /*}}}*/
124 // OpTextProgress::OpTextProgress - Constructor /*{{{*/
125 // ---------------------------------------------------------------------
126 /* */
127 OpTextProgress::OpTextProgress(Configuration &Config) :
128 NoUpdate(false), NoDisplay(false), LastLen(0)
129 {
130 if (Config.FindI("quiet",0) >= 1)
131 NoUpdate = true;
132 if (Config.FindI("quiet",0) >= 2)
133 NoDisplay = true;
134 };
135 /*}}}*/
136 // OpTextProgress::Done - Clean up the display /*{{{*/
137 // ---------------------------------------------------------------------
138 /* */
139 void OpTextProgress::Done()
140 {
141 if (NoUpdate == false && OldOp.empty() == false)
142 {
143 char S[300];
144 if (_error->PendingError() == true)
145 snprintf(S,sizeof(S),"\r%s... Error!",OldOp.c_str());
146 else
147 snprintf(S,sizeof(S),"\r%s... Done",OldOp.c_str());
148 Write(S);
149 cout << endl;
150 OldOp = string();
151 }
152
153 if (NoUpdate == true && NoDisplay == false && OldOp.empty() == false)
154 {
155 OldOp = string();
156 cout << endl;
157 }
158 }
159 /*}}}*/
160 // OpTextProgress::Update - Simple text spinner /*{{{*/
161 // ---------------------------------------------------------------------
162 /* */
163 void OpTextProgress::Update()
164 {
165 if (CheckChange() == false)
166 return;
167
168 // No percent spinner
169 if (NoUpdate == true)
170 {
171 if (MajorChange == false)
172 return;
173 if (NoDisplay == false)
174 {
175 if (OldOp.empty() == false)
176 cout << endl;
177 OldOp = "a";
178 cout << Op << "..." << flush;
179 }
180
181 return;
182 }
183
184 // Erase the old text and 'log' the event
185 char S[300];
186 if (MajorChange == true && OldOp.empty() == false)
187 {
188 snprintf(S,sizeof(S),"\r%s",OldOp.c_str());
189 Write(S);
190 cout << endl;
191 }
192
193 // Print the spinner
194 snprintf(S,sizeof(S),"\r%s... %u%%",Op.c_str(),(unsigned int)Percent);
195 Write(S);
196
197 OldOp = Op;
198 }
199 /*}}}*/
200 // OpTextProgress::Write - Write the progress string /*{{{*/
201 // ---------------------------------------------------------------------
202 /* This space fills the end to overwrite the previous text */
203 void OpTextProgress::Write(const char *S)
204 {
205 cout << S;
206 for (unsigned int I = strlen(S); I < LastLen; I++)
207 cout << ' ';
208 cout << '\r' << flush;
209 LastLen = strlen(S);
210 }
211 /*}}}*/