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) | |
9e55f2c7 | 13 | * @maxlen: max number of digits in the output string (not counting prefix and units, but counting .) |
d694a6a7 RE |
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 }; |
9e55f2c7 | 26 | unsigned int modulo; |
d694a6a7 | 27 | int unit_index = 0, post_index, carry = 0; |
9e55f2c7 | 28 | char tmp[32], fmt[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 | ||
9e55f2c7 TK |
65 | /* |
66 | * Divide by K/Ki until string length of num <= maxlen. | |
67 | */ | |
1ec3d69b | 68 | modulo = -1U; |
d694a6a7 | 69 | while (post_index < sizeof(sistr)) { |
ca09be4b | 70 | sprintf(tmp, "%llu", (unsigned long long) num); |
1ec3d69b JA |
71 | if (strlen(tmp) <= maxlen) |
72 | break; | |
73 | ||
74 | modulo = num % thousand[!!pow2]; | |
75 | num /= thousand[!!pow2]; | |
05463816 | 76 | carry = modulo >= thousand[!!pow2] / 2; |
1ec3d69b JA |
77 | post_index++; |
78 | } | |
79 | ||
9e55f2c7 TK |
80 | /* |
81 | * If no modulo, then we're done. | |
82 | */ | |
1ec3d69b JA |
83 | if (modulo == -1U) { |
84 | done: | |
b920b29b | 85 | if (post_index >= ARRAY_SIZE(sistr)) |
b7e147d1 JA |
86 | post_index = 0; |
87 | ||
ca09be4b | 88 | sprintf(buf, "%llu%s%s", (unsigned long long) num, |
d694a6a7 | 89 | unitprefix[post_index], unitstr[unit_index]); |
1ec3d69b JA |
90 | return buf; |
91 | } | |
92 | ||
9e55f2c7 TK |
93 | /* |
94 | * If no room for decimals, then we're done. | |
95 | */ | |
ca09be4b | 96 | sprintf(tmp, "%llu", (unsigned long long) num); |
9e55f2c7 | 97 | if ((int)(maxlen - strlen(tmp)) <= 1) { |
05463816 JA |
98 | if (carry) |
99 | num++; | |
1ec3d69b | 100 | goto done; |
05463816 JA |
101 | } |
102 | ||
9e55f2c7 TK |
103 | /* |
104 | * Fill in everything and return the result. | |
105 | */ | |
106 | assert(maxlen - strlen(tmp) - 1 > 0); | |
107 | assert(modulo < thousand[!!pow2]); | |
108 | sprintf(fmt, "%%.%df", (int)(maxlen - strlen(tmp) - 1)); | |
109 | sprintf(tmp, fmt, (double)modulo / (double)thousand[!!pow2]); | |
1ec3d69b | 110 | |
9e55f2c7 | 111 | sprintf(buf, "%llu.%s%s%s", (unsigned long long) num, &tmp[2], |
d694a6a7 | 112 | unitprefix[post_index], unitstr[unit_index]); |
1ec3d69b JA |
113 | return buf; |
114 | } |