aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-11-05 00:44:39 +0000
committervda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277>2006-11-05 00:44:39 +0000
commit04353387ec2a1ee4a28e44be9062c2a6967f397d (patch)
tree95ef467494c3178ffb308b95f8743886d9c9a5ae
parent48b1ebd12ec4ef182b4ea423f2309705ae141e8e (diff)
downloadbusybox-w32-04353387ec2a1ee4a28e44be9062c2a6967f397d.tar.gz
busybox-w32-04353387ec2a1ee4a28e44be9062c2a6967f397d.tar.bz2
busybox-w32-04353387ec2a1ee4a28e44be9062c2a6967f397d.zip
smart_ulltoa5: make available in libbb
git-svn-id: svn://busybox.net/trunk/busybox@16510 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r--include/libbb.h1
-rw-r--r--libbb/xfuncs.c60
-rw-r--r--miscutils/nmeter.c36
3 files changed, 56 insertions, 41 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 607433118..8c1d78434 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -278,6 +278,7 @@ extern FILE *fopen_or_warn(const char *filename, const char *mode);
278extern FILE *fopen_or_warn_stdin(const char *filename); 278extern FILE *fopen_or_warn_stdin(const char *filename);
279 279
280 280
281extern void smart_ulltoa5(unsigned long long ul, char buf[5]);
281extern void utoa_to_buf(unsigned n, char *buf, unsigned buflen); 282extern void utoa_to_buf(unsigned n, char *buf, unsigned buflen);
282extern char *utoa(unsigned n); 283extern char *utoa(unsigned n);
283extern void itoa_to_buf(int n, char *buf, unsigned buflen); 284extern void itoa_to_buf(int n, char *buf, unsigned buflen);
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index c72265003..352515af4 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -201,23 +201,71 @@ void xsetenv(const char *key, const char *value)
201 bb_error_msg_and_die(bb_msg_memory_exhausted); 201 bb_error_msg_and_die(bb_msg_memory_exhausted);
202} 202}
203 203
204
205// Converts unsigned long long value into compact 4-char
206// representation. Examples: "1234", "1.2k", " 27M", "123T"
207// Fifth char is always '\0'
208void smart_ulltoa5(unsigned long long ul, char buf[5])
209{
210 char *fmt;
211 char c;
212 unsigned v,idx = 0;
213 ul *= 10;
214 if (ul > 9999*10) { // do not scale if 9999 or less
215 while (ul >= 10000) {
216 ul /= 1024;
217 idx++;
218 }
219 }
220 v = ul; // ullong divisions are expensive, avoid them
221
222 fmt = " 123456789";
223 if (!idx) { // 9999 or less: use 1234 format
224 c = buf[0] = " 123456789"[v/10000];
225 if (c!=' ') fmt = "0123456789";
226 c = buf[1] = fmt[v/1000%10];
227 if (c!=' ') fmt = "0123456789";
228 buf[2] = fmt[v/100%10];
229 buf[3] = "0123456789"[v/10%10];
230 } else {
231 if (v>=10*10) { // scaled value is >=10: use 123M format
232 c = buf[0] = " 123456789"[v/1000];
233 if (c!=' ') fmt = "0123456789";
234 buf[1] = fmt[v/100%10];
235 buf[2] = "0123456789"[v/10%10];
236 } else { // scaled value is <10: use 1.2M format
237 buf[0] = "0123456789"[v/10];
238 buf[1] = '.';
239 buf[2] = "0123456789"[v%10];
240 }
241 // see http://en.wikipedia.org/wiki/Tera
242 buf[3] = " kMGTPEZY"[idx];
243 }
244 buf[4] = '\0';
245}
246
247
204// Convert unsigned integer to ascii, writing into supplied buffer. A 248// Convert unsigned integer to ascii, writing into supplied buffer. A
205// truncated result is always null terminated (unless buflen is 0), and 249// truncated result is always null terminated (unless buflen is 0), and
206// contains the first few digits of the result ala strncpy. 250// contains the first few digits of the result ala strncpy.
251void BUG_sizeof_unsigned_not_4(void);
207void utoa_to_buf(unsigned n, char *buf, unsigned buflen) 252void utoa_to_buf(unsigned n, char *buf, unsigned buflen)
208{ 253{
209 int i, out = 0; 254 unsigned i, out, res;
255 if (sizeof(unsigned) != 4)
256 BUG_sizeof_unsigned_not_4();
210 if (buflen) { 257 if (buflen) {
211 for (i=1000000000; i; i/=10) { 258 out = 0;
212 int res = n/i; 259 for (i = 1000000000; i; i /= 10) {
213 260 res = n / i;
214 if ((res || out || i == 1) && --buflen>0) { 261 if (res || out || i == 1) {
262 if (!--buflen) break;
215 out++; 263 out++;
216 n -= res*i; 264 n -= res*i;
217 *buf++ = '0' + res; 265 *buf++ = '0' + res;
218 } 266 }
219 } 267 }
220 *buf = 0; 268 *buf = '\0';
221 } 269 }
222} 270}
223 271
diff --git a/miscutils/nmeter.c b/miscutils/nmeter.c
index d71bd6add..326d7b85f 100644
--- a/miscutils/nmeter.c
+++ b/miscutils/nmeter.c
@@ -214,42 +214,8 @@ static int rdval_diskstats(const char* p, ullong *vec)
214 214
215static void scale(ullong ul) 215static void scale(ullong ul)
216{ 216{
217 char *fmt;
218 char buf[5]; 217 char buf[5];
219 char c; 218 smart_ulltoa5(ul, buf);
220 unsigned v,idx = 0;
221 ul *= 10;
222 if (ul > 9999*10) { // do not scale if 9999 or less
223 while (ul >= 10000) {
224 ul /= 1024;
225 idx++;
226 }
227 }
228 v = ul; // ullong divisions are expensive, avoid them
229
230 fmt = " 123456789";
231 if (!idx) { // 9999 or less: use 1234 format
232 c = buf[0] = " 123456789"[v/10000];
233 if (c!=' ') fmt = "0123456789";
234 c = buf[1] = fmt[v/1000%10];
235 if (c!=' ') fmt = "0123456789";
236 buf[2] = fmt[v/100%10];
237 buf[3] = "0123456789"[v/10%10];
238 } else {
239 if (v>=10*10) { // scaled value is >=10: use 123M format
240 c = buf[0] = " 123456789"[v/1000];
241 if (c!=' ') fmt = "0123456789";
242 buf[1] = fmt[v/100%10];
243 buf[2] = "0123456789"[v/10%10];
244 } else { // scaled value is <10: use 1.2M format
245 buf[0] = "0123456789"[v/10];
246 buf[1] = '.';
247 buf[2] = "0123456789"[v%10];
248 }
249 // see http://en.wikipedia.org/wiki/Tera
250 buf[3] = " kMGTPEZY"[idx];
251 }
252 buf[4] = '\0';
253 put(buf); 219 put(buf);
254} 220}
255 221