ascii-chat 0.8.38
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
format.c File Reference

📊 Byte size formatting utilities for human-readable output (B, KB, MB, GB, TB) More...

Go to the source code of this file.

Functions

void format_bytes_pretty (size_t bytes, char *out, size_t out_capacity)
 

Detailed Description

📊 Byte size formatting utilities for human-readable output (B, KB, MB, GB, TB)

Definition in file util/format.c.

Function Documentation

◆ format_bytes_pretty()

void format_bytes_pretty ( size_t  bytes,
char *  out,
size_t  out_capacity 
)

Definition at line 10 of file util/format.c.

10 {
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

References safe_snprintf().

Referenced by buffer_pool_create(), buffer_pool_log_stats(), client_video_render_thread(), frame_check_size_overflow(), frame_validate_new(), handle_image_frame_packet(), packet_decode_frame_data_malloc(), and packet_validate_frame_dimensions().