mirror of https://github.com/xzeldon/htop.git
Introduce METER_BUFFER_CHECK and METER_BUFFER_APPEND_CHR to cleanup writing to bar buffers
Closes: #294
This commit is contained in:
parent
77ec86aff4
commit
ad764ff972
|
@ -24,12 +24,11 @@ static void MemoryMeter_updateValues(Meter* this, char* buffer, size_t size) {
|
|||
Platform_setMemoryValues(this);
|
||||
|
||||
written = Meter_humanUnit(buffer, this->values[0], size);
|
||||
buffer += written;
|
||||
if ((size -= written) > 0) {
|
||||
*buffer++ = '/';
|
||||
size--;
|
||||
Meter_humanUnit(buffer, this->total, size);
|
||||
}
|
||||
METER_BUFFER_CHECK(buffer, size, written);
|
||||
|
||||
METER_BUFFER_APPEND_CHR(buffer, size, '/');
|
||||
|
||||
Meter_humanUnit(buffer, this->total, size);
|
||||
}
|
||||
|
||||
static void MemoryMeter_display(const Object* cast, RichString* out) {
|
||||
|
|
10
Meter.c
10
Meter.c
|
@ -155,7 +155,7 @@ ListItem* Meter_toListItem(Meter* this, bool moving) {
|
|||
|
||||
static void TextMeterMode_draw(Meter* this, int x, int y, int w) {
|
||||
char buffer[METER_BUFFER_LEN];
|
||||
Meter_updateValues(this, buffer, METER_BUFFER_LEN - 1);
|
||||
Meter_updateValues(this, buffer, sizeof(buffer));
|
||||
(void) w;
|
||||
|
||||
attrset(CRT_colors[METER_TEXT]);
|
||||
|
@ -175,7 +175,7 @@ static const char BarMeterMode_characters[] = "|#*@$%&.";
|
|||
|
||||
static void BarMeterMode_draw(Meter* this, int x, int y, int w) {
|
||||
char buffer[METER_BUFFER_LEN];
|
||||
Meter_updateValues(this, buffer, METER_BUFFER_LEN - 1);
|
||||
Meter_updateValues(this, buffer, sizeof(buffer));
|
||||
|
||||
w -= 2;
|
||||
attrset(CRT_colors[METER_TEXT]);
|
||||
|
@ -309,8 +309,8 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) {
|
|||
for (int i = 0; i < nValues - 1; i++)
|
||||
data->values[i] = data->values[i + 1];
|
||||
|
||||
char buffer[nValues];
|
||||
Meter_updateValues(this, buffer, nValues - 1);
|
||||
char buffer[METER_BUFFER_LEN];
|
||||
Meter_updateValues(this, buffer, sizeof(buffer));
|
||||
|
||||
double value = 0.0;
|
||||
for (uint8_t i = 0; i < this->curItems; i++)
|
||||
|
@ -378,7 +378,7 @@ static void LEDMeterMode_draw(Meter* this, int x, int y, int w) {
|
|||
LEDMeterMode_digits = LEDMeterMode_digitsAscii;
|
||||
|
||||
char buffer[METER_BUFFER_LEN];
|
||||
Meter_updateValues(this, buffer, METER_BUFFER_LEN - 1);
|
||||
Meter_updateValues(this, buffer, sizeof(buffer));
|
||||
|
||||
RichString_begin(out);
|
||||
Meter_displayBuffer(this, buffer, &out);
|
||||
|
|
23
Meter.h
23
Meter.h
|
@ -20,6 +20,29 @@ in the source distribution for its full text.
|
|||
|
||||
#define METER_BUFFER_LEN 256
|
||||
|
||||
#define METER_BUFFER_CHECK(buffer, size, written) \
|
||||
do { \
|
||||
if ((written) < 0 || (size_t)(written) >= (size)) { \
|
||||
return; \
|
||||
} \
|
||||
(buffer) += (written); \
|
||||
(size) -= (size_t)(written); \
|
||||
} while (0)
|
||||
|
||||
#define METER_BUFFER_APPEND_CHR(buffer, size, c) \
|
||||
do { \
|
||||
if ((size) < 2) { \
|
||||
return; \
|
||||
} \
|
||||
*(buffer)++ = c; \
|
||||
*(buffer) = '\0'; \
|
||||
(size)--; \
|
||||
if ((size) == 0) { \
|
||||
return; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
|
||||
struct Meter_;
|
||||
typedef struct Meter_ Meter;
|
||||
|
||||
|
|
11
SwapMeter.c
11
SwapMeter.c
|
@ -22,12 +22,11 @@ static void SwapMeter_updateValues(Meter* this, char* buffer, size_t size) {
|
|||
Platform_setSwapValues(this);
|
||||
|
||||
written = Meter_humanUnit(buffer, this->values[0], size);
|
||||
buffer += written;
|
||||
if ((size -= written) > 0) {
|
||||
*buffer++ = '/';
|
||||
size--;
|
||||
Meter_humanUnit(buffer, this->total, size);
|
||||
}
|
||||
METER_BUFFER_CHECK(buffer, size, written);
|
||||
|
||||
METER_BUFFER_APPEND_CHR(buffer, size, '/');
|
||||
|
||||
Meter_humanUnit(buffer, this->total, size);
|
||||
}
|
||||
|
||||
static void SwapMeter_display(const Object* cast, RichString* out) {
|
||||
|
|
|
@ -20,32 +20,17 @@ static void ZramMeter_updateValues(Meter* this, char* buffer, size_t size) {
|
|||
this->curItems = 1;
|
||||
|
||||
written = Meter_humanUnit(buffer, this->values[0], size);
|
||||
buffer += written;
|
||||
size -= written;
|
||||
if (size <= 0) {
|
||||
return;
|
||||
}
|
||||
*buffer++ = '(';
|
||||
size--;
|
||||
if (size <= 0) {
|
||||
return;
|
||||
}
|
||||
METER_BUFFER_CHECK(buffer, size, written);
|
||||
|
||||
METER_BUFFER_APPEND_CHR(buffer, size, '(');
|
||||
|
||||
written = Meter_humanUnit(buffer, this->values[1], size);
|
||||
buffer += written;
|
||||
size -= written;
|
||||
if (size <= 0) {
|
||||
return;
|
||||
}
|
||||
*buffer++ = ')';
|
||||
size--;
|
||||
if (size <= 0) {
|
||||
return;
|
||||
}
|
||||
*buffer++ = '/';
|
||||
size--;
|
||||
if (size <= 0) {
|
||||
return;
|
||||
}
|
||||
METER_BUFFER_CHECK(buffer, size, written);
|
||||
|
||||
METER_BUFFER_APPEND_CHR(buffer, size, ')');
|
||||
|
||||
METER_BUFFER_APPEND_CHR(buffer, size, '/');
|
||||
|
||||
Meter_humanUnit(buffer, this->total, size);
|
||||
}
|
||||
|
||||
|
|
|
@ -38,12 +38,11 @@ static void ZfsArcMeter_updateValues(Meter* this, char* buffer, size_t size) {
|
|||
Platform_setZfsArcValues(this);
|
||||
|
||||
written = Meter_humanUnit(buffer, this->values[5], size);
|
||||
buffer += written;
|
||||
if ((size -= written) > 0) {
|
||||
*buffer++ = '/';
|
||||
size--;
|
||||
Meter_humanUnit(buffer, this->total, size);
|
||||
}
|
||||
METER_BUFFER_CHECK(buffer, size, written);
|
||||
|
||||
METER_BUFFER_APPEND_CHR(buffer, size, '/');
|
||||
|
||||
Meter_humanUnit(buffer, this->total, size);
|
||||
}
|
||||
|
||||
static void ZfsArcMeter_display(const Object* cast, RichString* out) {
|
||||
|
|
Loading…
Reference in New Issue