ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
format.c
Go to the documentation of this file.
1
7#include "format.h"
8#include "platform/system.h"
9
10void format_bytes_pretty(size_t bytes, char *out, size_t out_capacity) {
11 const double MB = 1024.0 * 1024.0;
12 const double GB = MB * 1024.0;
13 const double TB = GB * 1024.0;
14
15 if ((double)bytes < MB) {
16 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%zu B", bytes));
17 } else if ((double)bytes < GB) {
18 double value = (double)bytes / MB;
19 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%.2f MB", value));
20 } else if ((double)bytes < TB) {
21 double value = (double)bytes / GB;
22 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%.2f GB", value));
23 } else {
24 double value = (double)bytes / TB;
25 SAFE_IGNORE_PRINTF_RESULT(safe_snprintf(out, out_capacity, "%.2f TB", value));
26 }
27}
📊 String Formatting Utilities
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Safe version of snprintf that ensures null termination.
#define SAFE_IGNORE_PRINTF_RESULT(expr)
Definition system.h:427
void format_bytes_pretty(size_t bytes, char *out, size_t out_capacity)
Format byte count into human-readable string.
Definition format.c:10
Cross-platform system functions interface for ascii-chat.