1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-17 02:25:09 +00:00

simplify human_readable

The only change of behaviour occurs when adjusting the unit of positive
values. For some reason 1000LL was used, which has now been replaced by
1024LL. Usage of abs() and MAX() might be OS dependent, but it should
not be too hard to implement them by hand if they're missing somewhere.
This commit is contained in:
Phil Sutter 2008-12-16 03:18:35 +01:00
parent d744f9bfbb
commit d321573c2d

View File

@ -1112,17 +1112,25 @@ static void human_readable(long long num, char *buf, int size)
float fnum;
int precision, len;
static const int WIDTH = 10, SHORT_WIDTH = 8;
int width;
const char *format, *format2;
if (num < 1024LL && num > -1024LL) {
if (short_units) {
spaced_print(buf, size, "%lld%c", SHORT_WIDTH, num, **suffix);
} else {
spaced_print(buf, size, "%lld%s", WIDTH, num, *suffix);
}
if (short_units) {
width = SHORT_WIDTH;
format = "%lld%1s";
format2 = "%.*f%1s";
} else {
width = WIDTH;
format = "%lld%s";
format2 = "%.*f%s";
}
if (llabs(num) < 1024LL) {
spaced_print(buf, size, format, width, num, *suffix);
return;
}
while ((num / 1024 >= 1000LL || num / 1024 <= -1024LL) && **(suffix + 2)) {
while (llabs(num / 1024) >= 1024LL && **(suffix + 2)) {
num /= 1024;
suffix++;
}
@ -1136,14 +1144,9 @@ static void human_readable(long long num, char *buf, int size)
if (precision < 0) {
break;
}
if (short_units) {
len = spaced_print(buf, size, "%.*f%c", SHORT_WIDTH,
precision, fnum, **suffix);
} else {
len = spaced_print(buf, size, "%.*f%s", WIDTH, precision,
fnum, *suffix);
}
} while (len >= (short_units ? SHORT_WIDTH : WIDTH) || len >= size);
len = spaced_print(buf, size, format2, width,
precision, fnum, *suffix);
} while (len >= MAX(width, size));
}
/* global object list root element */