ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
util/format.c
Go to the documentation of this file.
1
7#include <ascii-chat/util/format.h>
8#include <ascii-chat/platform/system.h>
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
16 if ((double)bytes < KB) {
17 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%zu B", bytes));
18 } else if ((double)bytes < MB) {
19 double value = (double)bytes / KB;
20 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%.2f KB", value));
21 } else if ((double)bytes < GB) {
22 double value = (double)bytes / MB;
23 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%.2f MB", value));
24 } else if ((double)bytes < TB) {
25 double value = (double)bytes / GB;
26 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%.2f GB", value));
27 } else {
28 double value = (double)bytes / TB;
29 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%.2f TB", value));
30 }
31}
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Safe formatted string printing to buffer.
Definition system.c:456
void format_bytes_pretty(size_t bytes, char *out, size_t out_capacity)
Definition util/format.c:10