diff --git a/DynamicMeter.c b/DynamicMeter.c index 3b567a53..605bbc3d 100644 --- a/DynamicMeter.c +++ b/DynamicMeter.c @@ -36,20 +36,24 @@ Hashtable* DynamicMeters_new(void) { typedef struct { unsigned int key; const char* name; + bool found; } DynamicIterator; static void DynamicMeter_compare(ht_key_t key, void* value, void* data) { const DynamicMeter* meter = (const DynamicMeter*)value; DynamicIterator* iter = (DynamicIterator*)data; - if (String_eq(iter->name, meter->name)) + if (String_eq(iter->name, meter->name)) { + iter->found = true; iter->key = key; + } } -unsigned int DynamicMeter_search(const ProcessList* pl, const char* name) { - DynamicIterator iter = { .key = 0, .name = name }; +bool DynamicMeter_search(const ProcessList* pl, const char* name, unsigned int* key) { + DynamicIterator iter = { .key = 0, .name = name, .found = false }; if (pl->dynamicMeters) Hashtable_foreach(pl->dynamicMeters, DynamicMeter_compare, &iter); - return iter.key; + *key = iter.key; + return iter.found; } const char* DynamicMeter_lookup(const ProcessList* pl, unsigned int key) { diff --git a/DynamicMeter.h b/DynamicMeter.h index 3d1bc9f2..e723503b 100644 --- a/DynamicMeter.h +++ b/DynamicMeter.h @@ -18,7 +18,7 @@ Hashtable* DynamicMeters_new(void); const char* DynamicMeter_lookup(const ProcessList* pl, unsigned int param); -unsigned int DynamicMeter_search(const ProcessList* pl, const char* name); +bool DynamicMeter_search(const ProcessList* pl, const char* name, unsigned int* key); extern const MeterClass DynamicMeter_class; diff --git a/Header.c b/Header.c index 0fdafea5..3b576a73 100644 --- a/Header.c +++ b/Header.c @@ -46,7 +46,9 @@ void Header_populateFromSettings(Header* this) { Header_forEachColumn(this, col) { const MeterColumnSettings* colSettings = &this->settings->columns[col]; for (int i = 0; i < colSettings->len; i++) { - Header_addMeterByName(this, colSettings->names[i], col); + if (!Header_addMeterByName(this, colSettings->names[i], col)) { + continue; + } if (colSettings->modes[i] != 0) { Header_setMode(this, i, colSettings->modes[i], col); } @@ -86,7 +88,7 @@ void Header_writeBackToSettings(const Header* this) { } } -MeterModeId Header_addMeterByName(Header* this, const char* name, int column) { +bool Header_addMeterByName(Header* this, const char* name, int column) { Vector* meters = this->columns[column]; char* paren = strchr(name, '('); @@ -95,18 +97,20 @@ MeterModeId Header_addMeterByName(Header* this, const char* name, int column) { char* end, dynamic[32] = {0}; int ok = sscanf(paren, "(%10u)", ¶m); // CPUMeter if (!ok) { - ok = sscanf(paren, "(%30s)", dynamic); // DynamicMeter - if (ok && (end = strrchr(dynamic, ')'))) *end = '\0'; - param = ok ? DynamicMeter_search(this->pl, dynamic) : 0; + if (sscanf(paren, "(%30s)", dynamic)) { // DynamicMeter + if ((end = strrchr(dynamic, ')')) == NULL) + return false; // indicate htoprc parse failure + *end = '\0'; + if (!DynamicMeter_search(this->pl, dynamic, ¶m)) + return false; // indicates name lookup failure + } } *paren = '\0'; } - MeterModeId mode = TEXT_METERMODE; for (const MeterClass* const* type = Platform_meterTypes; *type; type++) { if (String_eq(name, (*type)->name)) { Meter* meter = Meter_new(this->pl, param, *type); Vector_add(meters, meter); - mode = meter->mode; break; } } @@ -114,7 +118,7 @@ MeterModeId Header_addMeterByName(Header* this, const char* name, int column) { if (paren) *paren = '('; - return mode; + return true; } void Header_setMode(Header* this, int i, MeterModeId mode, int column) { diff --git a/Header.h b/Header.h index 7f6943c1..22a37074 100644 --- a/Header.h +++ b/Header.h @@ -32,7 +32,7 @@ void Header_populateFromSettings(Header* this); void Header_writeBackToSettings(const Header* this); -MeterModeId Header_addMeterByName(Header* this, const char* name, int column); +bool Header_addMeterByName(Header* this, const char* name, int column); void Header_setMode(Header* this, int i, MeterModeId mode, int column);