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