Added an inefficient listing for entries

This commit is contained in:
2024-02-17 12:55:21 +01:00
parent 2d1f228ad0
commit 36a1faf948
4 changed files with 123 additions and 4 deletions

View File

@@ -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);
}