diff options
| author | sumuel <samuel@yakubos.org> | 2026-08-17 20:44:55 +0000 |
|---|---|---|
| committer | sumuel <samuel@yakubos.org> | 2026-08-17 20:44:55 +0000 |
| commit | 76424950e373d3b04ac3dd13019151bfba3e8423 (patch) | |
| tree | 4c3cfdbda039e592b9186be3e28f8d7cfd439e8a /src/common/impl/size.c | |
Add the files
Diffstat (limited to 'src/common/impl/size.c')
| -rw-r--r-- | src/common/impl/size.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/common/impl/size.c b/src/common/impl/size.c new file mode 100644 index 0000000..4e8d1e3 --- /dev/null +++ b/src/common/impl/size.c @@ -0,0 +1,42 @@ +#include "common/size.h" + +#include <inttypes.h> + +static void appendNum(FFstrbuf* result, uint64_t bytes, uint32_t base, const char** prefixes) { + const FFOptionsDisplay* options = &instance.config.display; + double size = (double) bytes; + uint8_t counter = 0; + + while (size >= base && counter < options->sizeMaxPrefix && prefixes[counter + 1]) { + size /= base; + counter++; + } + + if (counter == 0) { + ffStrbufAppendUInt(result, bytes); + } else { + ffStrbufAppendDouble(result, size, (int8_t) options->sizeNdigits, true); + } + if (options->sizeSpaceBeforeUnit != FF_SPACE_BEFORE_UNIT_NEVER) { + ffStrbufAppendC(result, ' '); + } + ffStrbufAppendS(result, prefixes[counter]); +} + +void ffSizeAppendNum(uint64_t bytes, FFstrbuf* result) { + const FFOptionsDisplay* options = &instance.config.display; + switch (options->sizeBinaryPrefix) { + case FF_SIZE_BINARY_PREFIX_TYPE_IEC: + appendNum(result, bytes, 1024, (const char*[]) { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", NULL }); + break; + case FF_SIZE_BINARY_PREFIX_TYPE_SI: + appendNum(result, bytes, 1000, (const char*[]) { "B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB", NULL }); + break; + case FF_SIZE_BINARY_PREFIX_TYPE_JEDEC: + appendNum(result, bytes, 1024, (const char*[]) { "B", "KB", "MB", "GB", "TB", NULL }); + break; + default: + appendNum(result, bytes, 1024, (const char*[]) { "B", NULL }); + break; + } +} |