From 6ce7195ef1b0a6aac9946a4c4c1208f9efed70c4 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 10 Oct 2023 19:25:58 +0100 Subject: [PATCH] Codechange: Split GetGroup into GetGroup/GetOrCreateGroup. This follows the pattern used for GetItem/GetOrCreateItem, and allows use of references where we know the group must exist. --- src/hotkeys.cpp | 4 ++-- src/ini_load.cpp | 15 +++++++++++++++ src/ini_type.h | 1 + src/settings.cpp | 42 ++++++++++++++++++------------------------ 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/hotkeys.cpp b/src/hotkeys.cpp index 8356f0810c..4200460245 100644 --- a/src/hotkeys.cpp +++ b/src/hotkeys.cpp @@ -292,9 +292,9 @@ void HotkeyList::Load(IniFile &ini) */ void HotkeyList::Save(IniFile &ini) const { - IniGroup *group = ini.GetGroup(this->ini_group); + IniGroup &group = ini.GetOrCreateGroup(this->ini_group); for (const Hotkey &hotkey : this->items) { - IniItem &item = group->GetOrCreateItem(hotkey.name); + IniItem &item = group.GetOrCreateItem(hotkey.name); item.SetValue(SaveKeycodes(hotkey)); } } diff --git a/src/ini_load.cpp b/src/ini_load.cpp index 4a48c5a101..fed2e3b8df 100644 --- a/src/ini_load.cpp +++ b/src/ini_load.cpp @@ -193,6 +193,21 @@ IniGroup *IniLoadFile::GetGroup(const std::string &name, bool create_new) return &this->CreateGroup(name); } +/** + * Get the group with the given name, and if it doesn't exist create a new group. + * @param name name of the group to find. + * @return the requested group. + */ +IniGroup &IniLoadFile::GetOrCreateGroup(const std::string &name) +{ + for (IniGroup *group = this->group; group != nullptr; group = group->next) { + if (group->name == name) return *group; + } + + /* Group doesn't exist, make a new one. */ + return this->CreateGroup(name); +} + /** * Create an group with the given name. This does not reuse an existing group of the same name. * @param name name of the group to create. diff --git a/src/ini_type.h b/src/ini_type.h index f390954254..ab14e48d89 100644 --- a/src/ini_type.h +++ b/src/ini_type.h @@ -63,6 +63,7 @@ struct IniLoadFile { virtual ~IniLoadFile(); IniGroup *GetGroup(const std::string &name, bool create_new = true); + IniGroup &GetOrCreateGroup(const std::string &name); IniGroup &CreateGroup(const std::string &name); void RemoveGroup(const std::string &name); diff --git a/src/settings.cpp b/src/settings.cpp index aeacc10ddd..7038cf09fc 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -676,10 +676,10 @@ static void IniSaveSettings(IniFile &ini, const SettingTable &settings_table, co std::string s{ sd->GetName() }; auto sc = s.find('.'); if (sc != std::string::npos) { - group = ini.GetGroup(s.substr(0, sc)); + group = &ini.GetOrCreateGroup(s.substr(0, sc)); s = s.substr(sc + 1); } else { - if (group_def == nullptr) group_def = ini.GetGroup(grpname); + if (group_def == nullptr) group_def = &ini.GetOrCreateGroup(grpname); group = group_def; } @@ -799,13 +799,11 @@ static void IniLoadSettingList(IniFile &ini, const char *grpname, StringList &li */ static void IniSaveSettingList(IniFile &ini, const char *grpname, StringList &list) { - IniGroup *group = ini.GetGroup(grpname); - - if (group == nullptr) return; - group->Clear(); + IniGroup &group = ini.GetOrCreateGroup(grpname); + group.Clear(); for (const auto &iter : list) { - group->GetOrCreateItem(iter).SetValue(""); + group.GetOrCreateItem(iter).SetValue(""); } } @@ -1105,10 +1103,8 @@ static IniFileVersion LoadVersionFromConfig(IniFile &ini) static void AISaveConfig(IniFile &ini, const char *grpname) { - IniGroup *group = ini.GetGroup(grpname); - - if (group == nullptr) return; - group->Clear(); + IniGroup &group = ini.GetOrCreateGroup(grpname); + group.Clear(); for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) { AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME); @@ -1121,16 +1117,14 @@ static void AISaveConfig(IniFile &ini, const char *grpname) name = "none"; } - group->CreateItem(name).SetValue(value); + group.CreateItem(name).SetValue(value); } } static void GameSaveConfig(IniFile &ini, const char *grpname) { - IniGroup *group = ini.GetGroup(grpname); - - if (group == nullptr) return; - group->Clear(); + IniGroup &group = ini.GetOrCreateGroup(grpname); + group.Clear(); GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME); std::string name; @@ -1142,7 +1136,7 @@ static void GameSaveConfig(IniFile &ini, const char *grpname) name = "none"; } - group->CreateItem(name).SetValue(value); + group.CreateItem(name).SetValue(value); } /** @@ -1151,23 +1145,23 @@ static void GameSaveConfig(IniFile &ini, const char *grpname) */ static void SaveVersionInConfig(IniFile &ini) { - IniGroup *group = ini.GetGroup("version"); - group->GetOrCreateItem("version_string").SetValue(_openttd_revision); - group->GetOrCreateItem("version_number").SetValue(fmt::format("{:08X}", _openttd_newgrf_version)); - group->GetOrCreateItem("ini_version").SetValue(std::to_string(INIFILE_VERSION)); + IniGroup &group = ini.GetOrCreateGroup("version"); + group.GetOrCreateItem("version_string").SetValue(_openttd_revision); + group.GetOrCreateItem("version_number").SetValue(fmt::format("{:08X}", _openttd_newgrf_version)); + group.GetOrCreateItem("ini_version").SetValue(std::to_string(INIFILE_VERSION)); } /* Save a GRF configuration to the given group name */ static void GRFSaveConfig(IniFile &ini, const char *grpname, const GRFConfig *list) { - ini.RemoveGroup(grpname); - IniGroup *group = ini.GetGroup(grpname); + IniGroup &group = ini.GetOrCreateGroup(grpname); + group.Clear(); const GRFConfig *c; for (c = list; c != nullptr; c = c->next) { std::string key = fmt::format("{:08X}|{}|{}", BSWAP32(c->ident.grfid), FormatArrayAsHex(c->ident.md5sum), c->filename); - group->GetOrCreateItem(key).SetValue(GRFBuildParamList(c)); + group.GetOrCreateItem(key).SetValue(GRFBuildParamList(c)); } }