1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
/*
* Copyright (C) 2020 - Vito Caputo - <vcaputo@pengaru.com>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include "humane.h"
/* Print bytes as a "human-readable" string in Si storage units into humane->buf and return it. */
char * humane_bytes(humane_t *humane, uint64_t bytes)
{
double z = bytes;
static const char * units[] = {
"B",
"KiB",
"MiB",
"GiB",
"TiB",
"PiB",
"EiB",
};
int order = 0;
while (z >= 1024) {
order++;
z /= 1024;
}
/* FIXME: isn't there a format specifier for adaptive precision? where %.2 means
* use a maximum of two digits but only extend a non-zero fraction up to that limit,
* i.e. don't produce outputs like 1.00. but produce 1.1 or 1.01, but 1.00 should be 1.
* I can't remember if there's a double format to do that, and can't waste more time
* reading the printf(3) man page.
*/
snprintf(humane->buf, sizeof(humane->buf), "%.2f %s", z, units[order]);
return humane->buf;
}
#if 0
/* TODO: when/if unit tests become a thing in this tree, turn this into one of them and assert the
* stringified "humane" outputs match expectations.
*/
#define U64(x) UINT64_C(x)
int main(int argc, char *argv[])
{
uint64_t nums[] = {
0,
U64(1),
U64(512),
U64(1024),
U64(1024) + U64(512),
U64(1024) * U64(1024),
U64(1024) * U64(1024) * U64(1024),
U64(1024) * U64(1024) * U64(1024) * U64(1024),
U64(1024) * U64(1024) * U64(1024) * U64(1024) * U64(1024),
U64(1024) * U64(1024) * U64(1024) * U64(1024) * U64(1024) * U64(1024),
UINT64_MAX,
};
humane_t humane = {};
for (int i = 0; i < sizeof(nums) / sizeof(*nums); i++)
printf("%"PRIu64" humane: %s\n", nums[i], humane_bytes(&humane, nums[i]));
}
#endif
|