top: enabled arbitrarily large numbers in 'scale' guys

While experimenting with a new feature, wherein select
fields display the total upon request, the capacity of
the 'num' passed to some 'scale' guys became an issue.

So this commit will, with the compiler's help, put the
responsibility for converting the integer into a float
within the calling code (instead of the called logic).

Reference(s):
. 03/08/21, newlib branch equivalent commit
commit 62928cf461

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2022-01-13 13:13:13 -06:00 committed by Craig Small
parent e03be322b5
commit 4b54518721
2 changed files with 10 additions and 14 deletions

View File

@ -1641,7 +1641,7 @@ static inline const char *make_str_utf8 (const char *str, int width, int justr,
* Do some scaling then justify stuff. * Do some scaling then justify stuff.
* We'll interpret 'num' as a kibibytes quantity and try to * We'll interpret 'num' as a kibibytes quantity and try to
* format it to reach 'target' while also fitting 'width'. */ * format it to reach 'target' while also fitting 'width'. */
static const char *scale_mem (int target, unsigned long num, int width, int justr) { static const char *scale_mem (int target, float num, int width, int justr) {
// SK_Kb SK_Mb SK_Gb SK_Tb SK_Pb SK_Eb // SK_Kb SK_Mb SK_Gb SK_Tb SK_Pb SK_Eb
#ifdef BOOST_MEMORY #ifdef BOOST_MEMORY
static const char *fmttab[] = { "%.0f", "%#.1f%c", "%#.3f%c", "%#.3f%c", "%#.3f%c", NULL }; static const char *fmttab[] = { "%.0f", "%#.1f%c", "%#.3f%c", "%#.3f%c", "%#.3f%c", NULL };
@ -1649,7 +1649,6 @@ static const char *scale_mem (int target, unsigned long num, int width, int just
static const char *fmttab[] = { "%.0f", "%.1f%c", "%.1f%c", "%.1f%c", "%.1f%c", NULL }; static const char *fmttab[] = { "%.0f", "%.1f%c", "%.1f%c", "%.1f%c", "%.1f%c", NULL };
#endif #endif
static char buf[SMLBUFSIZ]; static char buf[SMLBUFSIZ];
float scaled_num;
char *psfx; char *psfx;
int i; int i;
@ -1657,12 +1656,11 @@ static const char *scale_mem (int target, unsigned long num, int width, int just
if (Rc.zero_suppress && 0 >= num) if (Rc.zero_suppress && 0 >= num)
goto end_justifies; goto end_justifies;
scaled_num = num;
for (i = SK_Kb, psfx = Scaled_sfxtab; i < SK_Eb; psfx++, i++) { for (i = SK_Kb, psfx = Scaled_sfxtab; i < SK_Eb; psfx++, i++) {
if (i >= target if (i >= target
&& (width >= snprintf(buf, sizeof(buf), fmttab[i], scaled_num, *psfx))) && (width >= snprintf(buf, sizeof(buf), fmttab[i], num, *psfx)))
goto end_justifies; goto end_justifies;
scaled_num /= 1024.0; num /= 1024.0;
} }
// well shoot, this outta' fit... // well shoot, this outta' fit...
@ -1674,23 +1672,21 @@ end_justifies:
/* /*
* Do some scaling then justify stuff. */ * Do some scaling then justify stuff. */
static const char *scale_num (unsigned long num, int width, int justr) { static const char *scale_num (float num, int width, int justr) {
static char buf[SMLBUFSIZ]; static char buf[SMLBUFSIZ];
float scaled_num;
char *psfx; char *psfx;
buf[0] = '\0'; buf[0] = '\0';
if (Rc.zero_suppress && 0 >= num) if (Rc.zero_suppress && 0 >= num)
goto end_justifies; goto end_justifies;
if (width >= snprintf(buf, sizeof(buf), "%lu", num)) if (width >= snprintf(buf, sizeof(buf), "%.0f", num))
goto end_justifies; goto end_justifies;
scaled_num = num;
for (psfx = Scaled_sfxtab; 0 < *psfx; psfx++) { for (psfx = Scaled_sfxtab; 0 < *psfx; psfx++) {
scaled_num /= 1024.0; num /= 1024.0;
if (width >= snprintf(buf, sizeof(buf), "%.1f%c", scaled_num, *psfx)) if (width >= snprintf(buf, sizeof(buf), "%.1f%c", num, *psfx))
goto end_justifies; goto end_justifies;
if (width >= snprintf(buf, sizeof(buf), "%.0f%c", scaled_num, *psfx)) if (width >= snprintf(buf, sizeof(buf), "%.0f%c", num, *psfx))
goto end_justifies; goto end_justifies;
} }

View File

@ -736,8 +736,8 @@ typedef struct WIN_t {
//atic inline const char *make_num (long num, int width, int justr, int col, int noz); //atic inline const char *make_num (long num, int width, int justr, int col, int noz);
//atic inline const char *make_str (const char *str, int width, int justr, int col); //atic inline const char *make_str (const char *str, int width, int justr, int col);
//atic inline const char *make_str_utf8 (const char *str, int width, int justr, int col); //atic inline const char *make_str_utf8 (const char *str, int width, int justr, int col);
//atic const char *scale_mem (int target, unsigned long num, int width, int justr); //atic const char *scale_mem (int target, float num, int width, int justr);
//atic const char *scale_num (unsigned long num, int width, int justr); //atic const char *scale_num (float num, int width, int justr);
//atic const char *scale_pcnt (float num, int width, int justr); //atic const char *scale_pcnt (float num, int width, int justr);
//atic const char *scale_tics (TIC_t tics, int width, int justr); //atic const char *scale_tics (TIC_t tics, int width, int justr);
/*------ Fields Management support -------------------------------------*/ /*------ Fields Management support -------------------------------------*/