Skip to content

Commit

Permalink
Refactor column delete into a shared Settings_deleteColumns
Browse files Browse the repository at this point in the history
Rather than open-coding it twice, use a shared routine for
deleting columns from the Settings structure.  In order to
improve code readability refactor the screen array deletion
similarly and call both routines from Settings_delete.
  • Loading branch information
natoscott authored and BenBE committed Sep 4, 2024
1 parent 179182d commit f3c3ac7
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions Settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,27 @@ static void writeQuotedList(FILE* fp, char** list) {
*/

void Settings_delete(Settings* this) {
free(this->filename);
free(this->initialFilename);
for (unsigned int i = 0; i < HeaderLayout_getColumns(this->hLayout); i++) {
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);
}
free(this->hColumns);
}

static void Settings_deleteScreens(Settings* this) {
if (this->screens) {
for (unsigned int i = 0; this->screens[i]; i++) {
for (size_t i = 0; this->screens[i]; i++)
ScreenSettings_delete(this->screens[i]);
}
free(this->screens);
}
}

void Settings_delete(Settings* this) {
free(this->filename);
free(this->initialFilename);
Settings_deleteColumns(this);
Settings_deleteScreens(this);
free(this);
}

Expand Down Expand Up @@ -154,11 +161,7 @@ static void Settings_defaultMeters(Settings* this, unsigned int initialCpuCount)
}

// 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

0 comments on commit f3c3ac7

Please sign in to comment.