Added a utility function to convert an arbitrary byte size into a human readable string.

E.g. 849484 becomes "829.57 KB".
This commit is contained in:
Jan Vidar Krey
2012-10-02 23:01:57 +02:00
parent 0a7cb86014
commit 10d8157477
2 changed files with 30 additions and 0 deletions

View File

@@ -305,6 +305,23 @@ int is_number(const char* value, int* num)
}
const char* format_size(size_t bytes, char* buf, size_t bufsize)
{
static const char* quant[] = { "B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
size_t b = bytes;
size_t factor = 0;
size_t divisor = 1;
while (b > 1024)
{
factor++;
b = (b >> 10);
divisor = (divisor << 10);
}
snprintf(buf, bufsize, "%.2f %s", (double) bytes / (double) divisor, quant[factor]);
return buf;
}
const char* uhub_itoa(int val)
{
static char buf[22];