diff options
author | Коренберг Марк <mark@ideco.ru> | 2012-08-28 12:39:14 (GMT) |
---|---|---|
committer | Коренберг Марк <mark@ideco.ru> | 2012-08-28 12:59:59 (GMT) |
commit | 25d640da4a132f36ba28791e96b856cdcb899528 (patch) | |
tree | 273a1077502049fd1cc305d87c70180bea6b7f51 /lib/utils.c | |
parent | a0f1c0e281ee78ab8ee874bbb6c2140c12101284 (diff) | |
download | libnl-25d640da4a132f36ba28791e96b856cdcb899528.zip libnl-25d640da4a132f36ba28791e96b856cdcb899528.tar.gz libnl-25d640da4a132f36ba28791e96b856cdcb899528.tar.bz2 |
lib/utils.c: One kilobit now is a 1000bits (instead of 1024)
http://en.wikipedia.org/wiki/Kilobit
Also, convert "char*" to "const char*" in output value,
as returned values can not be modified.
Diffstat (limited to 'lib/utils.c')
-rw-r--r-- | lib/utils.c | 46 |
1 files changed, 28 insertions, 18 deletions
diff --git a/lib/utils.c b/lib/utils.c index 467fd7f..0f6ff14 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -121,10 +121,11 @@ int __nl_read_num_str_file(const char *path, int (*cb)(long, const char *)) * * Cancels down a byte counter until it reaches a reasonable * unit. The chosen unit is assigned to \a unit. + * This function assume 1024 bytes in one kilobyte * * @return The cancelled down byte counter in the new unit. */ -double nl_cancel_down_bytes(unsigned long long l, char **unit) +double nl_cancel_down_bytes(unsigned long long l, const char **unit) { if (l >= 1099511627776LL) { *unit = "TiB"; @@ -149,30 +150,36 @@ double nl_cancel_down_bytes(unsigned long long l, char **unit) * @arg l bit counter * @arg unit destination unit pointer * - * Cancels downa bit counter until it reaches a reasonable + * Cancels down bit counter until it reaches a reasonable * unit. The chosen unit is assigned to \a unit. + * This function assume 1000 bits in one kilobit * * @return The cancelled down bit counter in the new unit. */ -double nl_cancel_down_bits(unsigned long long l, char **unit) +double nl_cancel_down_bits(unsigned long long l, const char **unit) { - if (l >= 1099511627776ULL) { + if (l >= 1000000000000ULL) { *unit = "Tbit"; - return ((double) l) / 1099511627776ULL; - } else if (l >= 1073741824) { + return ((double) l) / 1000000000000ULL; + } + + if (l >= 1000000000) { *unit = "Gbit"; - return ((double) l) / 1073741824; - } else if (l >= 1048576) { + return ((double) l) / 1000000000; + } + + if (l >= 1000000) { *unit = "Mbit"; - return ((double) l) / 1048576; - } else if (l >= 1024) { + return ((double) l) / 1000000; + } + + if (l >= 1000) { *unit = "Kbit"; - return ((double) l) / 1024; - } else { - *unit = "bit"; - return (double) l; + return ((double) l) / 1000; } - + + *unit = "bit"; + return (double) l; } int nl_rate2str(unsigned long long rate, int type, char *buf, size_t len) @@ -238,6 +245,9 @@ double nl_cancel_down_us(uint32_t l, char **unit) * - b,kb/k,m/mb,gb/g for bytes * - bit,kbit/mbit/gbit * + * This function assume 1000 bits in one kilobit and + * 1024 bytes in one kilobyte + * * @return The number of bytes or -1 if the string is unparseable */ long nl_size2int(const char *str) @@ -253,13 +263,13 @@ long nl_size2int(const char *str) else if (!strcasecmp(p, "gb") || !strcasecmp(p, "g")) l *= 1024*1024*1024; else if (!strcasecmp(p, "gbit")) - l *= 1024*1024*1024/8; + l *= 1000000000L/8; else if (!strcasecmp(p, "mb") || !strcasecmp(p, "m")) l *= 1024*1024; else if (!strcasecmp(p, "mbit")) - l *= 1024*1024/8; + l *= 1000000/8; else if (!strcasecmp(p, "kbit")) - l *= 1024/8; + l *= 1000/8; else if (!strcasecmp(p, "bit")) l /= 8; else if (strcasecmp(p, "b") != 0) |