Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef PROGRESS_H
00012 #define PROGRESS_H
00013
00014
00015 struct progress_s {
00016 void (*callback)(double progress, void *privdata);
00017 void *data;
00018 double min, max;
00019 double epsilon;
00020 double b;
00021 double d_prev;
00022 };
00023 typedef struct progress_s progress_t;
00024
00025
00026
00027
00028 static inline void progress_update(double d, progress_t *prog) {
00029 double d_scaled;
00030
00031 if (prog->callback != NULL) {
00032 d_scaled = prog->min * (1-d) + prog->max * d;
00033 if (d == 1.0 || d_scaled >= prog->d_prev + prog->epsilon) {
00034 prog->callback(prog->min * (1-d) + prog->max * d, prog->data);
00035 prog->d_prev = d_scaled;
00036 }
00037 }
00038 }
00039
00040
00041
00042
00043 static inline void progress_subrange_start(double a, double b, const progress_t *prog, progress_t *sub) {
00044 double min, max;
00045
00046 if (prog->callback == NULL) {
00047 sub->callback = NULL;
00048 return;
00049 }
00050
00051 min = prog->min * (1-a) + prog->max * a;
00052 max = prog->min * (1-b) + prog->max * b;
00053
00054 if (max - min < prog->epsilon) {
00055 sub->callback = NULL;
00056 sub->b = b;
00057 return;
00058 }
00059 sub->callback = prog->callback;
00060 sub->data = prog->data;
00061 sub->epsilon = prog->epsilon;
00062 sub->min = min;
00063 sub->max = max;
00064 sub->d_prev = prog->d_prev;
00065 return;
00066 }
00067
00068 static inline void progress_subrange_end(progress_t *prog, progress_t *sub) {
00069 if (prog->callback != NULL) {
00070 if (sub->callback == NULL) {
00071 progress_update(sub->b, prog);
00072 } else {
00073 prog->d_prev = sub->d_prev;
00074 }
00075 }
00076 }
00077
00078 #endif
00079