ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
util/format.c
Go to the documentation of this file.
1
9
10void format_bytes_pretty(size_t bytes, char *out, size_t out_capacity) {
11 const double KB = 1024.0;
12 const double MB = KB * 1024.0;
13 const double GB = MB * 1024.0;
14 const double TB = GB * 1024.0;
15 const double PB = TB * 1024.0;
16 const double EB = PB * 1024.0;
17
18 const double THRESHOLD = 0.8;
19
20 double byte_val = (double)bytes;
21
22 if (byte_val < THRESHOLD * KB) {
23 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%zu B", bytes));
24 } else if (byte_val < THRESHOLD * MB) {
25 double value = byte_val / KB;
26 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%.2f KB", value));
27 } else if (byte_val < THRESHOLD * GB) {
28 double value = byte_val / MB;
29 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%.2f MB", value));
30 } else if (byte_val < THRESHOLD * TB) {
31 double value = byte_val / GB;
32 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%.2f GB", value));
33 } else if (byte_val < THRESHOLD * PB) {
34 double value = byte_val / TB;
35 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%.2f TB", value));
36 } else if (byte_val < THRESHOLD * EB) {
37 double value = byte_val / PB;
38 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%.2f PB", value));
39 } else {
40 double value = byte_val / EB;
41 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%.2f EB", value));
42 }
43}
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Safe formatted string printing to buffer.
Definition system.c:148
#define SAFE_IGNORE_PRINTF_RESULT(expr)
Definition system.h:525
void format_bytes_pretty(size_t bytes, char *out, size_t out_capacity)
Format byte count into human-readable string.
Definition util/format.c:10
Cross-platform system functions interface for ascii-chat.
📊 String Formatting Utilities