diff --git a/src/include/pv.h b/src/include/pv.h index 74971d2..37ad063 100644 --- a/src/include/pv.h +++ b/src/include/pv.h @@ -78,6 +78,13 @@ extern unsigned int pv_getnum_count(const char *, bool); */ extern bool pv_getnum_check(const char *, pv_numtype); +/* + * Return an integer representing the first amount as a percentage of the + * second total, i.e. 100*amount/total. If the second value, the total, is + * zero or less, return 0. + */ +extern int pv_percentage(off_t, const off_t); + /* * String handling wrappers. */ diff --git a/src/pv/display.c b/src/pv/display.c index 320b542..3b41c34 100644 --- a/src/pv/display.c +++ b/src/pv/display.c @@ -151,21 +151,6 @@ void pv_screensize(unsigned int *width, unsigned int *height) } -/* - * Calculate the percentage transferred so far and return it. - */ -static int pv__calc_percentage(off_t so_far, const off_t total) -{ - if (total < 1) - return 0; - - so_far *= 100; - so_far /= total; - - return (int) so_far; -} - - /* * Given how many bytes have been transferred, the total byte count to * transfer, and the current average transfer rate, return the estimated @@ -751,7 +736,7 @@ void pv_calculate_transfer_rate(pvstate_t state, bool final) if (state->calc.percentage > 199) state->calc.percentage = 0; } else { - state->calc.percentage = pv__calc_percentage(state->transfer.total_written, state->control.size); + state->calc.percentage = pv_percentage(state->transfer.total_written, state->control.size); } /* Ensure the percentage is never negative or huge. */ @@ -1128,7 +1113,7 @@ bool pv_format(pvstate_t state, bool final) case PV_COMPONENT_BUFPERCENT: /* Transfer buffer percentage utilisation. */ if (state->transfer.buffer_size > 0) { - int pct_used = pv__calc_percentage((off_t) + int pct_used = pv_percentage((off_t) (state->transfer.read_position - state->transfer.write_position), (off_t) diff --git a/src/pv/number.c b/src/pv/number.c index edbf97d..2202be6 100644 --- a/src/pv/number.c +++ b/src/pv/number.c @@ -280,4 +280,20 @@ bool pv_getnum_check(const char *str, pv_numtype type) return true; } + +/* + * Return an integer representing "amount" as a percentage of "total", i.e. + * 100*amount/total. If "total" is zero or less, return 0. + */ +int pv_percentage(off_t amount, const off_t total) +{ + if (total < 1) + return 0; + + amount *= 100; + amount /= total; + + return (int) amount; +} + /* EOF */