From 624fbed1fd22b3aa9888d27f2b9731e49f9e5388 Mon Sep 17 00:00:00 2001 From: NightlyFox Date: Thu, 27 Sep 2018 23:06:01 -0500 Subject: [PATCH] added check for empty themePath, added check for existing settings config, changed ptr to char array and used strncpy --- common/theme.c | 40 ++++++++++++++++++++++++++-------------- common/theme.h | 2 +- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/common/theme.c b/common/theme.c index 40b1efd..6fc60bf 100644 --- a/common/theme.c +++ b/common/theme.c @@ -54,11 +54,8 @@ void themeStartup(ThemePreset preset) { .hbmenuLogoImage = hbmenu_logo_dark_bin }; - const char* themePath = ""; - config_t themeCfg = {0}; - config_setting_t *setting; - config_init(&themeCfg); - GetThemePathFromConfig(themeCfg, setting, &themePath); + char themePath[PATH_MAX] = {0}; + GetThemePathFromConfig(themePath); theme_t *themeDefault; config_t cfg = {0}; @@ -67,8 +64,10 @@ void themeStartup(ThemePreset preset) { color_t text, frontWave, middleWave, backWave, background, highlight, separator, borderColor, borderTextColor; int waveBlending; const char *AText, *BText; - bool good_cfg = config_read_file(&cfg, themePath); - config_destroy(&themeCfg); + bool good_cfg = false; + + if(themePath[0]!=0) + good_cfg = config_read_file(&cfg, themePath); switch (preset) { case THEME_PRESET_LIGHT: @@ -137,7 +136,10 @@ void themeStartup(ThemePreset preset) { config_destroy(&cfg); } -void GetThemePathFromConfig(config_t cfg, config_setting_t *setting, const char** themePath) { +void GetThemePathFromConfig(char* themePath) { + const char* tmpThemePath = ""; + config_t cfg = {0}; + config_setting_t *setting; char tmp_path[PATH_MAX] = {0}; #ifdef __SWITCH__ @@ -150,9 +152,12 @@ void GetThemePathFromConfig(config_t cfg, config_setting_t *setting, const char* if(good_cfg) { setting = config_lookup(&cfg, "hbmenuConfig"); if(setting != NULL){ - config_setting_lookup_string(setting, "themePath", themePath); + if(config_setting_lookup_string(setting, "themePath", &tmpThemePath)) + strncpy(themePath, tmpThemePath, PATH_MAX-1); } } + + config_destroy(&cfg); } void SetThemePathToConfig(const char* themePath) { @@ -167,11 +172,18 @@ void SetThemePathToConfig(const char* themePath) { #endif strncat(settingPath, "config/nx-hbmenu/settings.cfg", sizeof(settingPath)-2); - - root = config_root_setting(&cfg); - group = config_setting_add(root, "hbmenuConfig", CONFIG_TYPE_GROUP); - setting = config_setting_add(group, "themePath", CONFIG_TYPE_STRING); - config_setting_set_string(setting, themePath); + bool good_cfg = config_read_file(&cfg, settingPath); + + if(good_cfg) { + group = config_lookup(&cfg, "hbmenuConfig"); + setting = config_setting_lookup(group, "themePath"); + config_setting_set_string(setting, themePath); + } else { + root = config_root_setting(&cfg); + group = config_setting_add(root, "hbmenuConfig", CONFIG_TYPE_GROUP); + setting = config_setting_add(group, "themePath", CONFIG_TYPE_STRING); + config_setting_set_string(setting, themePath); + } if(!config_write_file(&cfg, settingPath)) { menuCreateMsgBox(780, 300, "Something went wrong, and the theme could not be applied!"); diff --git a/common/theme.h b/common/theme.h index 956f6e2..59c924e 100644 --- a/common/theme.h +++ b/common/theme.h @@ -30,7 +30,7 @@ typedef enum bool colorFromSetting(config_setting_t *rgba, color_t *col); void themeStartup(ThemePreset preset); -void GetThemePathFromConfig(config_t cfg, config_setting_t *setting, const char** themePath); +void GetThemePathFromConfig(char* themePath); void SetThemePathToConfig(const char* themePath); extern theme_t themeCurrent;