Line |
Branch |
Exec |
Source |
1 |
|
|
#ifndef UTIL_NUMTOSTR_H |
2 |
|
|
#define UTIL_NUMTOSTR_H |
3 |
|
|
|
4 |
|
|
#include <stddef.h> |
5 |
|
|
#include <stdint.h> |
6 |
|
|
#include <sys/types.h> |
7 |
|
|
#include "macros.h" |
8 |
|
|
|
9 |
|
|
enum { |
10 |
|
|
HRSIZE_MAX = DECIMAL_STR_MAX(uintmax_t) + STRLEN(".99 GiB"), |
11 |
|
|
FILESIZE_STR_MAX = HRSIZE_MAX + DECIMAL_STR_MAX(uintmax_t) + STRLEN(" ()"), |
12 |
|
|
}; |
13 |
|
|
|
14 |
|
|
extern const char hextab_lower[16]; |
15 |
|
|
extern const char hextab_upper[16]; |
16 |
|
|
|
17 |
|
|
// Encodes a byte of data as 2 hexadecimal digits |
18 |
|
45 |
static inline size_t hex_encode_byte(char out[2], uint8_t byte) |
19 |
|
|
{ |
20 |
|
45 |
out[0] = hextab_lower[byte >> 4]; |
21 |
|
45 |
out[1] = hextab_lower[byte & 0xF]; |
22 |
|
45 |
return 2; |
23 |
|
|
} |
24 |
|
|
|
25 |
|
|
size_t buf_umax_to_str(uintmax_t x, char *buf) NONNULL_ARGS; |
26 |
|
|
size_t buf_umax_to_hex_str(uintmax_t x, char *buf, size_t min_digits) NONNULL_ARGS; |
27 |
|
|
size_t buf_uint_to_str(unsigned int x, char *buf) NONNULL_ARGS; |
28 |
|
|
size_t buf_u8_to_str(uint8_t x, char *buf) NONNULL_ARGS; |
29 |
|
|
const char *umax_to_str(uintmax_t x) RETURNS_NONNULL; |
30 |
|
|
const char *uint_to_str(unsigned int x) RETURNS_NONNULL; |
31 |
|
|
const char *ulong_to_str(unsigned long x) RETURNS_NONNULL; |
32 |
|
|
char *file_permissions_to_str(mode_t mode, char buf[10]) NONNULL_ARGS_AND_RETURN; |
33 |
|
|
char *human_readable_size(uintmax_t bytes, char buf[HRSIZE_MAX]) NONNULL_ARGS_AND_RETURN; |
34 |
|
|
char *filesize_to_str(uintmax_t bytes, char buf[FILESIZE_STR_MAX]) NONNULL_ARGS_AND_RETURN; |
35 |
|
|
|
36 |
|
|
#endif |
37 |
|
|
|