human_readable.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. unsigned frac; /* 0..9 - the fractional digit */
  38. const char *u;
  39. const char *fmt;
  40. if (val == 0)
  41. return "0";
  42. fmt = "%llu";
  43. if (block_size > 1)
  44. val *= block_size;
  45. frac = 0;
  46. u = unit_chars;
  47. if (display_unit) {
  48. val += display_unit/2; /* Deal with rounding */
  49. val /= display_unit; /* Don't combine with the line above! */
  50. /* will just print it as ulonglong (below) */
  51. } else {
  52. while ((val >= 1024)
  53. /* && (u < unit_chars + sizeof(unit_chars) - 1) - always true */
  54. ) {
  55. fmt = "%llu.%u%c";
  56. u++;
  57. frac = (((unsigned)val % 1024) * 10 + 1024/2) / 1024;
  58. val /= 1024;
  59. }
  60. if (frac >= 10) { /* we need to round up here */
  61. ++val;
  62. frac = 0;
  63. }
  64. #if 1
  65. /* If block_size is 0, dont print fractional part */
  66. if (block_size == 0) {
  67. if (frac >= 5) {
  68. ++val;
  69. }
  70. fmt = "%llu%*c";
  71. frac = 1;
  72. }
  73. #endif
  74. }
  75. return auto_string(xasprintf(fmt, val, frac, *u));
  76. }
  77. /* vda's implementations of the similar idea */
  78. /* Convert unsigned long long value into compact 5-char representation.
  79. * String is not terminated (buf[5] is untouched) */
  80. char* FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale)
  81. {
  82. const char *fmt;
  83. char c;
  84. unsigned v, u, idx = 0;
  85. if (ul > 99999) { // do not scale if 99999 or less
  86. ul *= 10;
  87. do {
  88. ul /= 1024;
  89. idx++;
  90. } while (ul >= 100000);
  91. }
  92. v = ul; // ullong divisions are expensive, avoid them
  93. fmt = " 123456789";
  94. u = v / 10;
  95. v = v % 10;
  96. if (!idx) {
  97. // 99999 or less: use "12345" format
  98. // u is value/10, v is last digit
  99. c = buf[0] = " 123456789"[u/1000];
  100. if (c != ' ') fmt = "0123456789";
  101. c = buf[1] = fmt[u/100%10];
  102. if (c != ' ') fmt = "0123456789";
  103. c = buf[2] = fmt[u/10%10];
  104. if (c != ' ') fmt = "0123456789";
  105. buf[3] = fmt[u%10];
  106. buf[4] = "0123456789"[v];
  107. } else {
  108. // value has been scaled into 0..9999.9 range
  109. // u is value, v is 1/10ths (allows for 92.1M format)
  110. if (u >= 100) {
  111. // value is >= 100: use "1234M', " 123M" formats
  112. c = buf[0] = " 123456789"[u/1000];
  113. if (c != ' ') fmt = "0123456789";
  114. c = buf[1] = fmt[u/100%10];
  115. if (c != ' ') fmt = "0123456789";
  116. v = u % 10;
  117. u = u / 10;
  118. buf[2] = fmt[u%10];
  119. } else {
  120. // value is < 100: use "92.1M" format
  121. c = buf[0] = " 123456789"[u/10];
  122. if (c != ' ') fmt = "0123456789";
  123. buf[1] = fmt[u%10];
  124. buf[2] = '.';
  125. }
  126. buf[3] = "0123456789"[v];
  127. buf[4] = scale[idx]; /* typically scale = " kmgt..." */
  128. }
  129. return buf + 5;
  130. }
  131. /* Convert unsigned long long value into compact 4-char
  132. * representation. Examples: "1234", "1.2k", " 27M", "123T"
  133. * String is not terminated (buf[4] is untouched) */
  134. char* FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[4], const char *scale)
  135. {
  136. const char *fmt;
  137. char c;
  138. unsigned v, u, idx = 0;
  139. if (ul > 9999) { // do not scale if 9999 or less
  140. ul *= 10;
  141. do {
  142. ul /= 1024;
  143. idx++;
  144. } while (ul >= 10000);
  145. }
  146. v = ul; // ullong divisions are expensive, avoid them
  147. fmt = " 123456789";
  148. u = v / 10;
  149. v = v % 10;
  150. if (!idx) {
  151. // 9999 or less: use "1234" format
  152. // u is value/10, v is last digit
  153. c = buf[0] = " 123456789"[u/100];
  154. if (c != ' ') fmt = "0123456789";
  155. c = buf[1] = fmt[u/10%10];
  156. if (c != ' ') fmt = "0123456789";
  157. buf[2] = fmt[u%10];
  158. buf[3] = "0123456789"[v];
  159. } else {
  160. // u is value, v is 1/10ths (allows for 9.2M format)
  161. if (u >= 10) {
  162. // value is >= 10: use "123M', " 12M" formats
  163. c = buf[0] = " 123456789"[u/100];
  164. if (c != ' ') fmt = "0123456789";
  165. v = u % 10;
  166. u = u / 10;
  167. buf[1] = fmt[u%10];
  168. } else {
  169. // value is < 10: use "9.2M" format
  170. buf[0] = "0123456789"[u];
  171. buf[1] = '.';
  172. }
  173. buf[2] = "0123456789"[v];
  174. buf[3] = scale[idx]; /* typically scale = " kmgt..." */
  175. }
  176. return buf + 4;
  177. }