feat: filesystem and Files app support for >64 file entries, 64-bit sizes, async ops, GUI toolkit improvements to devexplorer app
This commit is contained in:
@@ -133,25 +133,28 @@ inline void format_mac(char* buf, const uint8_t* mac) {
|
||||
// File size formatting
|
||||
// ============================================================================
|
||||
|
||||
inline void format_size(char* buf, int size) {
|
||||
inline void format_size(char* buf, uint64_t size) {
|
||||
// Pick the largest unit that keeps the integer part below 1024, then show
|
||||
// one decimal place for small values. 64-bit throughout: no 2 GiB overflow.
|
||||
static const char* const units[] = { "B", "KB", "MB", "GB", "TB", "PB" };
|
||||
if (size < 1024) {
|
||||
snprintf(buf, 16, "%d B", size);
|
||||
} else if (size < 1024 * 1024) {
|
||||
int kb = size / 1024;
|
||||
int frac = ((size % 1024) * 10) / 1024;
|
||||
if (kb < 10) {
|
||||
snprintf(buf, 16, "%d.%d KB", kb, frac);
|
||||
} else {
|
||||
snprintf(buf, 16, "%d KB", kb);
|
||||
}
|
||||
snprintf(buf, 16, "%d B", (int)size);
|
||||
return;
|
||||
}
|
||||
int unit = 0;
|
||||
uint64_t scaled = size;
|
||||
uint64_t rem = 0;
|
||||
while (scaled >= 1024 && unit < 5) {
|
||||
rem = scaled % 1024;
|
||||
scaled /= 1024;
|
||||
unit++;
|
||||
}
|
||||
int whole = (int)scaled;
|
||||
int frac = (int)((rem * 10) / 1024);
|
||||
if (whole < 10) {
|
||||
snprintf(buf, 16, "%d.%d %s", whole, frac, units[unit]);
|
||||
} else {
|
||||
int mb = size / (1024 * 1024);
|
||||
int frac = ((size % (1024 * 1024)) * 10) / (1024 * 1024);
|
||||
if (mb < 10) {
|
||||
snprintf(buf, 16, "%d.%d MB", mb, frac);
|
||||
} else {
|
||||
snprintf(buf, 16, "%d MB", mb);
|
||||
}
|
||||
snprintf(buf, 16, "%d %s", whole, units[unit]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user