Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor Settings.c cleanup #1531

Merged
merged 6 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CommandLine.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ int CommandLine_run(int argc, char** argv) {

Machine* host = Machine_new(ut, flags.userId);
ProcessTable* pt = ProcessTable_new(host, flags.pidMatchList);
Settings* settings = Settings_new(host->activeCPUs, dm, dc, ds);
Settings* settings = Settings_new(host, dm, dc, ds);
Machine_populateTablesFromSettings(host, settings, &pt->super);

Header* header = Header_new(host, 2);
Expand Down
118 changes: 40 additions & 78 deletions Settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,89 +31,59 @@ in the source distribution for its full text.
#include "XUtils.h"


/*

static char** readQuotedList(char* line) {
int n = 0;
char** list = xCalloc(sizeof(char*), 1);
int start = 0;
for (;;) {
while (line[start] && line[start] == ' ') {
start++;
}
if (line[start] != '"') {
break;
}
start++;
int close = start;
while (line[close] && line[close] != '"') {
close++;
}
int len = close - start;
char* item = xMalloc(len + 1);
strncpy(item, line + start, len);
item[len] = '\0';
list[n] = item;
n++;
list = xRealloc(list, sizeof(char*) * (n + 1));
start = close + 1;
static void Settings_deleteColumns(Settings* this) {
for (size_t i = 0; i < HeaderLayout_getColumns(this->hLayout); i++) {
String_freeArray(this->hColumns[i].names);
free(this->hColumns[i].modes);
}
list[n] = NULL;
return list;
free(this->hColumns);
}
BenBE marked this conversation as resolved.
Show resolved Hide resolved

static void writeQuotedList(FILE* fp, char** list) {
const char* sep = "";
for (int i = 0; list[i]; i++) {
fprintf(fp, "%s\"%s\"", sep, list[i]);
sep = " ";
static void Settings_deleteScreens(Settings* this) {
if (this->screens) {
for (size_t i = 0; this->screens[i]; i++)
ScreenSettings_delete(this->screens[i]);
free(this->screens);
BenBE marked this conversation as resolved.
Show resolved Hide resolved
}
fprintf(fp, "\n");
}

*/

void Settings_delete(Settings* this) {
free(this->filename);
free(this->initialFilename);
for (unsigned int i = 0; i < HeaderLayout_getColumns(this->hLayout); i++) {
String_freeArray(this->hColumns[i].names);
free(this->hColumns[i].modes);
}
free(this->hColumns);
if (this->screens) {
for (unsigned int i = 0; this->screens[i]; i++) {
ScreenSettings_delete(this->screens[i]);
}
free(this->screens);
}
Settings_deleteColumns(this);
Settings_deleteScreens(this);
free(this);
}

static void Settings_readMeters(Settings* this, const char* line, unsigned int column) {
static char** Settings_splitLineToIDs(const char* line) {
char* trim = String_trim(line);
char** ids = String_split(trim, ' ', NULL);
free(trim);
return ids;
}

static void Settings_readMeters(Settings* this, const char* line, size_t column) {
column = MINIMUM(column, HeaderLayout_getColumns(this->hLayout) - 1);
this->hColumns[column].names = ids;
this->hColumns[column].names = Settings_splitLineToIDs(line);
}

static void Settings_readMeterModes(Settings* this, const char* line, unsigned int column) {
char* trim = String_trim(line);
char** ids = String_split(trim, ' ', NULL);
free(trim);
int len = 0;
for (int i = 0; ids[i]; i++) {
static void Settings_readMeterModes(Settings* this, const char* line, size_t column) {
char** ids = Settings_splitLineToIDs(line);

size_t len = 0;
for (size_t i = 0; ids[i]; i++) {
len++;
}

column = MINIMUM(column, HeaderLayout_getColumns(this->hLayout) - 1);
this->hColumns[column].len = len;
MeterModeId* modes = len ? xCalloc(len, sizeof(MeterModeId)) : NULL;
for (int i = 0; i < len; i++) {
for (size_t i = 0; i < len; i++) {
modes[i] = (MeterModeId) atoi(ids[i]);
}
String_freeArray(ids);
this->hColumns[column].modes = modes;

String_freeArray(ids);
}

static bool Settings_validateMeters(Settings* this) {
Expand Down Expand Up @@ -146,19 +116,16 @@ static bool Settings_validateMeters(Settings* this) {
return anyMeter;
}

static void Settings_defaultMeters(Settings* this, unsigned int initialCpuCount) {
int sizes[] = { 3, 3 };
static void Settings_defaultMeters(Settings* this, const Machine* host) {
unsigned int initialCpuCount = host->activeCPUs;
natoscott marked this conversation as resolved.
Show resolved Hide resolved
size_t sizes[] = { 3, 3 };

if (initialCpuCount > 4 && initialCpuCount <= 128) {
sizes[1]++;
}

// Release any previously allocated memory
for (size_t i = 0; i < HeaderLayout_getColumns(this->hLayout); i++) {
String_freeArray(this->hColumns[i].names);
free(this->hColumns[i].modes);
}
free(this->hColumns);
Settings_deleteColumns(this);

this->hLayout = HF_TWO_50_50;
this->hColumns = xCalloc(HeaderLayout_getColumns(this->hLayout), sizeof(MeterColumnSetting));
Expand Down Expand Up @@ -218,14 +185,9 @@ static const char* toFieldName(Hashtable* columns, int id, bool* enabled) {
}
if (id >= ROW_DYNAMIC_FIELDS) {
const DynamicColumn* column = DynamicColumn_lookup(columns, id);
if (!column) {
if (enabled)
*enabled = false;
return NULL;
}
if (enabled)
*enabled = column->enabled;
return column->name;
*enabled = column ? column->enabled : false;
return column ? column->name : NULL;
}
if (enabled)
*enabled = true;
Expand Down Expand Up @@ -354,7 +316,7 @@ static ScreenSettings* Settings_defaultScreens(Settings* this) {
return this->screens[0];
}

static bool Settings_read(Settings* this, const char* fileName, unsigned int initialCpuCount, bool checkWritability) {
static bool Settings_read(Settings* this, const char* fileName, const Machine* host, bool checkWritability) {
int fd = -1;
const char* fopen_mode = "r+";
if (checkWritability) {
Expand Down Expand Up @@ -592,7 +554,7 @@ static bool Settings_read(Settings* this, const char* fileName, unsigned int ini
}
fclose(fp);
if (!didReadMeters || !Settings_validateMeters(this))
Settings_defaultMeters(this, initialCpuCount);
Settings_defaultMeters(this, host);
if (!this->nScreens)
Settings_defaultScreens(this);
return didReadAny;
Expand Down Expand Up @@ -811,7 +773,7 @@ int Settings_write(const Settings* this, bool onCrash) {
return r;
}

Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* dynamicScreens) {
Settings* Settings_new(const Machine* host, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* dynamicScreens) {
Settings* this = xCalloc(1, sizeof(Settings));

this->writeConfig = true;
Expand Down Expand Up @@ -897,9 +859,9 @@ Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicMeters, H
this->changed = false;
this->delay = DEFAULT_DELAY;

bool ok = Settings_read(this, this->filename, initialCpuCount, /*checkWritability*/true);
bool ok = Settings_read(this, this->filename, host, /*checkWritability*/true);
if (!ok && legacyDotfile) {
ok = Settings_read(this, legacyDotfile, initialCpuCount, this->writeConfig);
ok = Settings_read(this, legacyDotfile, host, this->writeConfig);
if (ok && this->writeConfig) {
// Transition to new location and delete old configuration file
if (Settings_write(this, false) == 0) {
Expand All @@ -911,10 +873,10 @@ Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicMeters, H
this->screenTabs = true;
this->changed = true;

ok = Settings_read(this, SYSCONFDIR "/htoprc", initialCpuCount, /*checkWritability*/false);
ok = Settings_read(this, SYSCONFDIR "/htoprc", host, /*checkWritability*/false);
}
if (!ok) {
Settings_defaultMeters(this, initialCpuCount);
Settings_defaultMeters(this, host);
Settings_defaultScreens(this);
}

Expand Down
3 changes: 2 additions & 1 deletion Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ in the source distribution for its full text.
#define CONFIG_READER_MIN_VERSION 3

struct DynamicScreen_; // IWYU pragma: keep
struct Machine_; // IWYU pragma: keep
struct Table_; // IWYU pragma: keep

typedef struct {
Expand Down Expand Up @@ -128,7 +129,7 @@ void Settings_delete(Settings* this);

int Settings_write(const Settings* this, bool onCrash);

Settings* Settings_new(unsigned int initialCpuCount, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* dynamicScreens);
Settings* Settings_new(const struct Machine_* host, Hashtable* dynamicMeters, Hashtable* dynamicColumns, Hashtable* dynamicScreens);

ScreenSettings* Settings_newScreen(Settings* this, const ScreenDefaults* defaults);

Expand Down
Loading