/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 1985-2011 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Glenn Fowler <[email protected]> *17* David Korn <[email protected]> *18* Phong Vo <[email protected]> *19* *20***********************************************************************/21#pragma prototyped22/*23* Glenn Fowler24* AT&T Research25*26* return number n scaled to metric multiples of k { 1000 1024 }27* return string length is at most 5 chars + terminating nul28*/2930#include <ast.h>31#include <lclib.h>3233char*34fmtscale(register Sfulong_t n, int k)35{36register Sfulong_t m;37int r;38int z;39const char* u;40char suf[3];41char* s;42char* buf;43Lc_numeric_t* p = (Lc_numeric_t*)LCINFO(AST_LC_NUMERIC)->data;4445static const char scale[] = "bkMGTPE";4647u = scale;48if (n < 1000)49r = 0;50else51{52m = 0;53while (n >= k && *(u + 1))54{55m = n;56n /= k;57u++;58}59if ((r = (10 * (m % k) + (k / 2)) / k) > 9)60{61r = 0;62n++;63}64if (k == 1024 && n >= 1000)65{66n = 1;67r = 0;68u++;69}70}71buf = fmtbuf(z = 8);72s = suf;73if (u > scale)74{75if (k == 1024)76{77*s++ = *u == 'k' ? 'K' : *u;78*s++ = 'i';79}80else81*s++ = *u;82}83*s = 0;84if (n > 0 && n < 10)85sfsprintf(buf, z, "%I*u%c%d%s", sizeof(n), n, p->decimal >= 0 ? p->decimal : '.', r, suf);86else87{88if (r >= 5)89n++;90sfsprintf(buf, z, "%I*u%s", sizeof(n), n, suf);91}92return buf;93}949596