Add nl_rate2str()

This commit is contained in:
Thomas Graf 2011-06-09 12:29:21 +02:00
parent ed69b2a79b
commit 58e0e1eda9
2 changed files with 27 additions and 0 deletions

View file

@ -38,9 +38,15 @@ extern "C" {
/** @} */
enum {
NL_BYTE_RATE,
NL_BIT_RATE,
};
/* unit pretty-printing */
extern double nl_cancel_down_bytes(unsigned long long, char **);
extern double nl_cancel_down_bits(unsigned long long, char **);
extern int nl_rate2str(unsigned long long, int, char *, size_t);
extern double nl_cancel_down_us(uint32_t, char **);
/* generic unit translations */

View file

@ -151,6 +151,27 @@ double nl_cancel_down_bits(unsigned long long l, char **unit)
}
int nl_rate2str(unsigned long long rate, int type, char *buf, size_t len)
{
char *unit;
double frac;
switch (type) {
case NL_BYTE_RATE:
frac = nl_cancel_down_bytes(rate, &unit);
break;
case NL_BIT_RATE:
frac = nl_cancel_down_bits(rate, &unit);
break;
default:
BUG();
}
return snprintf(buf, len, "%.2f%s/s", frac, unit);
}
/**
* Cancel down a micro second value
* @arg l micro seconds