summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Graf <tgraf@suug.ch>2011-06-09 10:29:21 (GMT)
committerThomas Graf <tgraf@suug.ch>2011-06-09 10:29:21 (GMT)
commit58e0e1eda9cbb09a56ffdc8ac6934dc5f5f884b2 (patch)
treeff7a7d6b4d3f9d0d7b38fc93bec80e603b883862
parented69b2a79b8e0b71c230b5e75626aa620a0d5ff9 (diff)
downloadlibnl-58e0e1eda9cbb09a56ffdc8ac6934dc5f5f884b2.zip
libnl-58e0e1eda9cbb09a56ffdc8ac6934dc5f5f884b2.tar.gz
libnl-58e0e1eda9cbb09a56ffdc8ac6934dc5f5f884b2.tar.bz2
Add nl_rate2str()
-rw-r--r--include/netlink/utils.h6
-rw-r--r--lib/utils.c21
2 files changed, 27 insertions, 0 deletions
diff --git a/include/netlink/utils.h b/include/netlink/utils.h
index 309e02f..a1ef82e 100644
--- a/include/netlink/utils.h
+++ b/include/netlink/utils.h
@@ -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 */
diff --git a/lib/utils.c b/lib/utils.c
index b507608..c7d3abd 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -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