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.
This commit is contained in:
parent
a0f1c0e281
commit
25d640da4a
2 changed files with 33 additions and 23 deletions
|
@ -44,8 +44,8 @@ enum {
|
|||
};
|
||||
|
||||
/* unit pretty-printing */
|
||||
extern double nl_cancel_down_bytes(unsigned long long, char **);
|
||||
extern double nl_cancel_down_bits(unsigned long long, char **);
|
||||
extern double nl_cancel_down_bytes(unsigned long long, const char **);
|
||||
extern double nl_cancel_down_bits(unsigned long long, const char **);
|
||||
extern int nl_rate2str(unsigned long long, int, char *, size_t);
|
||||
extern double nl_cancel_down_us(uint32_t, char **);
|
||||
|
||||
|
|
52
lib/utils.c
52
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) {
|
||||
*unit = "Gbit";
|
||||
return ((double) l) / 1073741824;
|
||||
} else if (l >= 1048576) {
|
||||
*unit = "Mbit";
|
||||
return ((double) l) / 1048576;
|
||||
} else if (l >= 1024) {
|
||||
*unit = "Kbit";
|
||||
return ((double) l) / 1024;
|
||||
} else {
|
||||
*unit = "bit";
|
||||
return (double) l;
|
||||
return ((double) l) / 1000000000000ULL;
|
||||
}
|
||||
|
||||
|
||||
if (l >= 1000000000) {
|
||||
*unit = "Gbit";
|
||||
return ((double) l) / 1000000000;
|
||||
}
|
||||
|
||||
if (l >= 1000000) {
|
||||
*unit = "Mbit";
|
||||
return ((double) l) / 1000000;
|
||||
}
|
||||
|
||||
if (l >= 1000) {
|
||||
*unit = "Kbit";
|
||||
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)
|
||||
|
|
Loading…
Add table
Reference in a new issue