Added an inefficient listing for entries
This commit is contained in:
31
src/utils.c
31
src/utils.c
@@ -342,3 +342,34 @@ Result write_file(const char* path, const char* string) {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Result format_size(uint64_t size, char** out_string) {
|
||||
*out_string = NULL;
|
||||
|
||||
// Determine the unit
|
||||
const char* unit;
|
||||
double value;
|
||||
|
||||
if (size < 1024ULL) {
|
||||
unit = "B";
|
||||
value = size;
|
||||
} else if (size < 1024ULL * 1024) {
|
||||
unit = "KiB";
|
||||
value = (double)size / 1024;
|
||||
} else if (size < 1024ULL * 1024 * 1024) {
|
||||
unit = "MiB";
|
||||
value = (double)size / (1024 * 1024);
|
||||
} else if (size < 1024ULL * 1024 * 1024 * 1024) {
|
||||
unit = "GiB";
|
||||
value = (double)size / (1024 * 1024 * 1024);
|
||||
} else if (size < 1024ULL * 1024 * 1024 * 1024 * 1024) {
|
||||
unit = "TiB";
|
||||
value = (double)size / (1024 * 1024 * 1024 * 1024);
|
||||
} else {
|
||||
unit = "PiB";
|
||||
value = (double)size / (1024 * 1024 * 1024 * 1024 * 1024);
|
||||
}
|
||||
|
||||
// Format the string
|
||||
return format(out_string, "%.2f%s", value, unit);
|
||||
}
|
||||
|
Reference in New Issue
Block a user