2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, version 3 of the License ONLY.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #define PROGRESS_WINDOW (10)
42 TIMESTAMP stamps
[PROGRESS_WINDOW
];
46 static double get_time_delta (void) {
47 static double res
= 10.0;
48 static int checked
= 0;
50 LIST
*var
= var_get("K8JAM-PROGRESS-TIME-DELTA"); /*FIXME: a perfectly idiotic name*/
52 TARGET
*t
= bindtarget(var
->string
);
53 pushsettings(t
->settings
);
54 t
->boundname
= search(t
->name
, &t
->time
);
55 popsettings(t
->settings
);
57 res
= atof(t
->boundname
);
58 if (res
< 0.0) res
= 0.0;
67 #define LAST_STAMP(p) ((p)->stamps[(p)->stampcount-1])
68 #define FIRST_STAMP(p) ((p)->stamps[0])
71 PROGRESS
*progress_start (int total
) {
72 PROGRESS
*p
= malloc(sizeof(PROGRESS
));
75 progress_update(p
, 0);
80 static double progress_estimate (PROGRESS
*progress
) {
83 if (progress
->stampcount
< 2) return 0.0;
84 count
= LAST_STAMP(progress
).completed
-FIRST_STAMP(progress
).completed
;
85 left
= progress
->total
-LAST_STAMP(progress
).completed
;
86 elapsed
= (double)(LAST_STAMP(progress
).when
-FIRST_STAMP(progress
).when
);
87 if (elapsed
<= 0.1) elapsed
= 0.1;
93 double progress_update (PROGRESS
*progress
, int completed
) {
95 double dd
= get_time_delta();
97 /* only return progress every 10 seconds */
98 if (progress
->stampcount
> 0 && (dd
<= 0.00001 || difftime(now
, LAST_STAMP(progress
).when
) < dd
)) return 0.0;
99 if (progress
->stampcount
== PROGRESS_WINDOW
) {
100 memmove(progress
->stamps
, progress
->stamps
+1, sizeof(progress
->stamps
[0])*(PROGRESS_WINDOW
-1));
101 --progress
->stampcount
;
103 ++progress
->stampcount
;
104 LAST_STAMP(progress
).completed
= completed
;
105 LAST_STAMP(progress
).when
= now
;
106 return progress_estimate(progress
);