add and use pretty num
This commit is contained in:
16
src/utils.c
16
src/utils.c
@@ -10,7 +10,10 @@
|
||||
#include "log.h"
|
||||
#include "utils.h"
|
||||
|
||||
#define MKPATH(path, dir, file) do {char *nt = stpcpy(path, dir); *nt++ = '/'; stpcpy(nt, file); } while (0)
|
||||
#define MKPATH(path, dir, file) do { \
|
||||
char *nt = stpcpy(path, dir); \
|
||||
*nt++ = '/'; stpcpy(nt, file); \
|
||||
} while (0)
|
||||
|
||||
inline void timespec_add_ms(struct timespec *ts, const uint16_t ms) {
|
||||
ts->tv_nsec += ms * 1000000L;
|
||||
@@ -26,7 +29,13 @@ inline uint64_t timespec_diff_us(const struct timespec *a, const struct timespec
|
||||
return sec * 1000000LL + nsec / 1000LL;
|
||||
}
|
||||
|
||||
char *pretty_bytes(const uint64_t bytes, int8_t start_suffix, char *buf, const size_t bufsz) {
|
||||
inline char *pretty_num(const double num, char *buf, const size_t bufsz) {
|
||||
const uint8_t decimals = (num < 10) ? 2 : (num < 100) ? 1 : 0;
|
||||
snprintf(buf, bufsz, "%.*f", decimals, num);
|
||||
return buf;
|
||||
}
|
||||
|
||||
inline char *pretty_bytes(const uint64_t bytes, int8_t start_suffix, char *buf, const size_t bufsz) {
|
||||
constexpr char suffix_list[] = {'K', 'M', 'G', 'T', 'P', 'E', 'Z'};
|
||||
start_suffix *= -1;
|
||||
double bytes_d = (double)bytes;
|
||||
@@ -34,8 +43,7 @@ char *pretty_bytes(const uint64_t bytes, int8_t start_suffix, char *buf, const s
|
||||
bytes_d /= 1024.0;
|
||||
start_suffix++;
|
||||
}
|
||||
const uint8_t decimals = (bytes_d < 10) ? 2 : (bytes_d < 100) ? 1 : 0;
|
||||
snprintf(buf, bufsz, "%.*f%c", decimals, bytes_d, suffix_list[MAX(0, start_suffix)]);
|
||||
snprintf(buf, bufsz, "%s%c", P_NUM(bytes_d), suffix_list[MAX(0, start_suffix)]);
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user