Commit | Line | Data |
---|---|---|
1ec3d69b JA |
1 | #include <stdlib.h> |
2 | #include <stdio.h> | |
3 | #include <string.h> | |
4 | ||
5e746820 TK |
5 | #include "../compiler/compiler.h" |
6 | #include "num2str.h" | |
7 | ||
8 | #define ARRAY_SIZE(x) (sizeof((x)) / (sizeof((x)[0]))) | |
10aa136b | 9 | |
d694a6a7 RE |
10 | /** |
11 | * num2str() - Cheesy number->string conversion, complete with carry rounding error. | |
12 | * @num: quantity (e.g., number of blocks, bytes or bits) | |
13 | * @maxlen: max number of digits in the output string (not counting prefix and units) | |
14 | * @base: multiplier for num (e.g., if num represents Ki, use 1024) | |
15 | * @pow2: select unit prefix - 0=power-of-10 decimal SI, nonzero=power-of-2 binary IEC | |
5e746820 | 16 | * @units: select units - N2S_* macros defined in num2str.h |
d694a6a7 | 17 | * @returns a malloc'd buffer containing "number[<unit prefix>][<units>]" |
1ec3d69b | 18 | */ |
d694a6a7 | 19 | char *num2str(uint64_t num, int maxlen, int base, int pow2, int units) |
1ec3d69b | 20 | { |
d694a6a7 RE |
21 | const char *sistr[] = { "", "k", "M", "G", "T", "P" }; |
22 | const char *iecstr[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" }; | |
23 | const char **unitprefix; | |
24 | const char *unitstr[] = { "", "/s", "B", "bit", "B/s", "bit/s" }; | |
73798eb2 | 25 | const unsigned int thousand[] = { 1000, 1024 }; |
1ec3d69b | 26 | unsigned int modulo, decimals; |
d694a6a7 | 27 | int unit_index = 0, post_index, carry = 0; |
05463816 | 28 | char tmp[32]; |
1ec3d69b JA |
29 | char *buf; |
30 | ||
d694a6a7 RE |
31 | compiletime_assert(sizeof(sistr) == sizeof(iecstr), "unit prefix arrays must be identical sizes"); |
32 | ||
1ec3d69b | 33 | buf = malloc(128); |
d694a6a7 RE |
34 | if (!buf) |
35 | return NULL; | |
36 | ||
37 | if (pow2) | |
38 | unitprefix = iecstr; | |
39 | else | |
40 | unitprefix = sistr; | |
1ec3d69b JA |
41 | |
42 | for (post_index = 0; base > 1; post_index++) | |
43 | base /= thousand[!!pow2]; | |
44 | ||
d694a6a7 RE |
45 | switch (units) { |
46 | case N2S_PERSEC: | |
47 | unit_index = 1; | |
48 | break; | |
49 | case N2S_BYTE: | |
50 | unit_index = 2; | |
51 | break; | |
52 | case N2S_BIT: | |
53 | unit_index = 3; | |
73798eb2 SN |
54 | num *= 8; |
55 | break; | |
d694a6a7 RE |
56 | case N2S_BYTEPERSEC: |
57 | unit_index = 4; | |
58 | break; | |
59 | case N2S_BITPERSEC: | |
60 | unit_index = 5; | |
61 | num *= 8; | |
73798eb2 SN |
62 | break; |
63 | } | |
64 | ||
1ec3d69b | 65 | modulo = -1U; |
d694a6a7 | 66 | while (post_index < sizeof(sistr)) { |
ca09be4b | 67 | sprintf(tmp, "%llu", (unsigned long long) num); |
1ec3d69b JA |
68 | if (strlen(tmp) <= maxlen) |
69 | break; | |
70 | ||
71 | modulo = num % thousand[!!pow2]; | |
72 | num /= thousand[!!pow2]; | |
05463816 | 73 | carry = modulo >= thousand[!!pow2] / 2; |
1ec3d69b JA |
74 | post_index++; |
75 | } | |
76 | ||
77 | if (modulo == -1U) { | |
78 | done: | |
b920b29b | 79 | if (post_index >= ARRAY_SIZE(sistr)) |
b7e147d1 JA |
80 | post_index = 0; |
81 | ||
ca09be4b | 82 | sprintf(buf, "%llu%s%s", (unsigned long long) num, |
d694a6a7 | 83 | unitprefix[post_index], unitstr[unit_index]); |
1ec3d69b JA |
84 | return buf; |
85 | } | |
86 | ||
ca09be4b | 87 | sprintf(tmp, "%llu", (unsigned long long) num); |
1ec3d69b | 88 | decimals = maxlen - strlen(tmp); |
e5a415c8 | 89 | if ((int)decimals <= 1) { |
05463816 JA |
90 | if (carry) |
91 | num++; | |
1ec3d69b | 92 | goto done; |
05463816 JA |
93 | } |
94 | ||
95 | do { | |
96 | sprintf(tmp, "%u", modulo); | |
97 | if (strlen(tmp) <= decimals - 1) | |
98 | break; | |
99 | ||
100 | modulo = (modulo + 9) / 10; | |
101 | } while (1); | |
1ec3d69b | 102 | |
ca09be4b | 103 | sprintf(buf, "%llu.%u%s%s", (unsigned long long) num, modulo, |
d694a6a7 | 104 | unitprefix[post_index], unitstr[unit_index]); |
1ec3d69b JA |
105 | return buf; |
106 | } |