human_readable.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * June 30, 2001 Manuel Novoa III
  4. *
  5. * All-integer version (hey, not everyone has floating point) of
  6. * make_human_readable_str, modified from similar code I had written
  7. * for busybox several months ago.
  8. *
  9. * Notes:
  10. * 1) I'm using an unsigned long long to hold the product size * block_size,
  11. * as df (which calls this routine) could request a representation of a
  12. * partition size in bytes > max of unsigned long. If long longs aren't
  13. * available, it would be possible to do what's needed using polynomial
  14. * representations (say, powers of 1024) and manipulating coefficients.
  15. * The base ten "bytes" output could be handled similarly.
  16. *
  17. * 2) This routine always outputs a decimal point and a tenths digit when
  18. * display_unit != 0. Hence, it isn't uncommon for the returned string
  19. * to have a length of 5 or 6.
  20. *
  21. * It might be nice to add a flag to indicate no decimal digits in
  22. * that case. This could be either an additional parameter, or a
  23. * special value of display_unit. Such a flag would also be nice for du.
  24. *
  25. * Some code to omit the decimal point and tenths digit is sketched out
  26. * and "#if 0"'d below.
  27. *
  28. * Licensed under GPLv2, see file LICENSE in this source tree.
  29. */
  30. #include "libbb.h"
  31. const char* FAST_FUNC make_human_readable_str(unsigned long long val,
  32. unsigned long block_size, unsigned long display_unit)
  33. {
  34. static const char unit_chars[] ALIGN1 = {
  35. '\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'
  36. };
  37. static char *str;
  38. unsigned frac; /* 0..9 - the fractional digit */
  39. const char *u;
  40. const char *fmt;
  41. if (val == 0)
  42. return "0";
  43. fmt = "%llu";
  44. if (block_size > 1)
  45. val *= block_size;
  46. frac = 0;
  47. u = unit_chars;
  48. if (display_unit) {
  49. val += display_unit/2; /* Deal with rounding */
  50. val /= display_unit; /* Don't combine with the line above! */
  51. /* will just print it as ulonglong (below) */
  52. } else {
  53. while ((val >= 1024)
  54. /* && (u < unit_chars + sizeof(unit_chars) - 1) - always true */
  55. ) {
  56. fmt = "%llu.%u%c";
  57. u++;
  58. frac = (((unsigned)val % 1024) * 10 + 1024/2) / 1024;
  59. val /= 1024;
  60. }
  61. if (frac >= 10) { /* we need to round up here */
  62. ++val;
  63. frac = 0;
  64. }
  65. #if 1
  66. /* If block_size is 0, dont print fractional part */
  67. if (block_size == 0) {
  68. if (frac >= 5) {
  69. ++val;
  70. }
  71. fmt = "%llu%*c";
  72. frac = 1;
  73. }
  74. #endif
  75. }
  76. if (!str) {
  77. /* sufficient for any width of val */
  78. str = xmalloc(sizeof(val)*3 + 2 + 3);
  79. }
  80. sprintf(str, fmt, val, frac, *u);
  81. return str;
  82. }
  83. /* vda's implementations of the similar idea */
  84. /* Convert unsigned long long value into compact 5-char representation.
  85. * String is not terminated (buf[5] is untouched) */
  86. char* FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale)
  87. {
  88. const char *fmt;
  89. char c;
  90. unsigned v, u, idx = 0;
  91. if (ul > 99999) { // do not scale if 99999 or less
  92. ul *= 10;
  93. do {
  94. ul /= 1024;
  95. idx++;
  96. } while (ul >= 100000);
  97. }
  98. v = ul; // ullong divisions are expensive, avoid them
  99. fmt = " 123456789";
  100. u = v / 10;
  101. v = v % 10;
  102. if (!idx) {
  103. // 99999 or less: use "12345" format
  104. // u is value/10, v is last digit
  105. c = buf[0] = " 123456789"[u/1000];
  106. if (c != ' ') fmt = "0123456789";
  107. c = buf[1] = fmt[u/100%10];
  108. if (c != ' ') fmt = "0123456789";
  109. c = buf[2] = fmt[u/10%10];
  110. if (c != ' ') fmt = "0123456789";
  111. buf[3] = fmt[u%10];
  112. buf[4] = "0123456789"[v];
  113. } else {
  114. // value has been scaled into 0..9999.9 range
  115. // u is value, v is 1/10ths (allows for 92.1M format)
  116. if (u >= 100) {
  117. // value is >= 100: use "1234M', " 123M" formats
  118. c = buf[0] = " 123456789"[u/1000];
  119. if (c != ' ') fmt = "0123456789";
  120. c = buf[1] = fmt[u/100%10];
  121. if (c != ' ') fmt = "0123456789";
  122. v = u % 10;
  123. u = u / 10;
  124. buf[2] = fmt[u%10];
  125. } else {
  126. // value is < 100: use "92.1M" format
  127. c = buf[0] = " 123456789"[u/10];
  128. if (c != ' ') fmt = "0123456789";
  129. buf[1] = fmt[u%10];
  130. buf[2] = '.';
  131. }
  132. buf[3] = "0123456789"[v];
  133. buf[4] = scale[idx]; /* typically scale = " kmgt..." */
  134. }
  135. return buf + 5;
  136. }
  137. /* Convert unsigned long long value into compact 4-char
  138. * representation. Examples: "1234", "1.2k", " 27M", "123T"
  139. * String is not terminated (buf[4] is untouched) */
  140. char* FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[4], const char *scale)
  141. {
  142. const char *fmt;
  143. char c;
  144. unsigned v, u, idx = 0;
  145. if (ul > 9999) { // do not scale if 9999 or less
  146. ul *= 10;
  147. do {
  148. ul /= 1024;
  149. idx++;
  150. } while (ul >= 10000);
  151. }
  152. v = ul; // ullong divisions are expensive, avoid them
  153. fmt = " 123456789";
  154. u = v / 10;
  155. v = v % 10;
  156. if (!idx) {
  157. // 9999 or less: use "1234" format
  158. // u is value/10, v is last digit
  159. c = buf[0] = " 123456789"[u/100];
  160. if (c != ' ') fmt = "0123456789";
  161. c = buf[1] = fmt[u/10%10];
  162. if (c != ' ') fmt = "0123456789";
  163. buf[2] = fmt[u%10];
  164. buf[3] = "0123456789"[v];
  165. } else {
  166. // u is value, v is 1/10ths (allows for 9.2M format)
  167. if (u >= 10) {
  168. // value is >= 10: use "123M', " 12M" formats
  169. c = buf[0] = " 123456789"[u/100];
  170. if (c != ' ') fmt = "0123456789";
  171. v = u % 10;
  172. u = u / 10;
  173. buf[1] = fmt[u%10];
  174. } else {
  175. // value is < 10: use "9.2M" format
  176. buf[0] = "0123456789"[u];
  177. buf[1] = '.';
  178. }
  179. buf[2] = "0123456789"[v];
  180. buf[3] = scale[idx]; /* typically scale = " kmgt..." */
  181. }
  182. return buf + 4;
  183. }