From 69e20e79abebbf97899626a3592e8d48efdbef9d Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Wed, 11 Oct 2023 00:38:57 +0100 Subject: [PATCH] Codechange: Add const versions of GetItem/GetGroup, and sprinkle liberally. Non-const version of GetItem is not needed. --- src/base_media_base.h | 6 ++--- src/base_media_func.h | 12 +++++----- src/gfxinit.cpp | 6 ++--- src/hotkeys.cpp | 6 ++--- src/hotkeys.h | 2 +- src/ini_load.cpp | 18 ++++++++++++-- src/ini_type.h | 3 ++- src/music.cpp | 10 ++++---- src/settings.cpp | 42 ++++++++++++++++----------------- src/settingsgen/settingsgen.cpp | 26 ++++++++++---------- 10 files changed, 73 insertions(+), 58 deletions(-) diff --git a/src/base_media_base.h b/src/base_media_base.h index 4fc897acf2..2bd8b58265 100644 --- a/src/base_media_base.h +++ b/src/base_media_base.h @@ -95,7 +95,7 @@ struct BaseSet { return Tnum_files - this->valid_files; } - bool FillSetDetails(IniFile &ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename = true); + bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename = true); /** * Get the description for the given ISO code. @@ -244,7 +244,7 @@ struct GraphicsSet : BaseSet { PaletteType palette; ///< Palette of this graphics set BlitterType blitter; ///< Blitter of this graphics set - bool FillSetDetails(IniFile &ini, const std::string &path, const std::string &full_filename); + bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename); static MD5File::ChecksumResult CheckMD5(const MD5File *file, Subdirectory subdir); }; @@ -301,7 +301,7 @@ struct MusicSet : BaseSet { /** Number of valid songs in set. */ byte num_available; - bool FillSetDetails(IniFile &ini, const std::string &path, const std::string &full_filename); + bool FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename); }; /** All data/functions related with replacing the base music */ diff --git a/src/base_media_func.h b/src/base_media_func.h index 7ecc020df8..56af002cba 100644 --- a/src/base_media_func.h +++ b/src/base_media_func.h @@ -39,15 +39,15 @@ extern void CheckExternalFiles(); * @return true if loading was successful. */ template -bool BaseSet::FillSetDetails(IniFile &ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename) +bool BaseSet::FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename) { - IniGroup *metadata = ini.GetGroup("metadata"); + const IniGroup *metadata = ini.GetGroup("metadata"); if (metadata == nullptr) { Debug(grf, 0, "Base " SET_TYPE "set detail loading: metadata missing."); Debug(grf, 0, " Is {} readable for the user running OpenTTD?", full_filename); return false; } - IniItem *item; + const IniItem *item; fetch_metadata("name"); this->name = *item->value; @@ -74,9 +74,9 @@ bool BaseSet::FillSetDetails(IniFile &ini, const this->fallback = (item != nullptr && item->value && *item->value != "0" && *item->value != "false"); /* For each of the file types we want to find the file, MD5 checksums and warning messages. */ - IniGroup *files = ini.GetGroup("files"); - IniGroup *md5s = ini.GetGroup("md5s"); - IniGroup *origin = ini.GetGroup("origin"); + const IniGroup *files = ini.GetGroup("files"); + const IniGroup *md5s = ini.GetGroup("md5s"); + const IniGroup *origin = ini.GetGroup("origin"); for (uint i = 0; i < Tnum_files; i++) { MD5File *file = &this->files[i]; /* Find the filename first. */ diff --git a/src/gfxinit.cpp b/src/gfxinit.cpp index 8c34bc6986..2f8ac4702c 100644 --- a/src/gfxinit.cpp +++ b/src/gfxinit.cpp @@ -347,13 +347,13 @@ void GfxLoadSprites() UpdateCursorSize(); } -bool GraphicsSet::FillSetDetails(IniFile &ini, const std::string &path, const std::string &full_filename) +bool GraphicsSet::FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename) { bool ret = this->BaseSet::FillSetDetails(ini, path, full_filename, false); if (ret) { - IniGroup *metadata = ini.GetGroup("metadata"); + const IniGroup *metadata = ini.GetGroup("metadata"); assert(metadata != nullptr); /* ret can't be true if metadata isn't present. */ - IniItem *item; + const IniItem *item; fetch_metadata("palette"); this->palette = ((*item->value)[0] == 'D' || (*item->value)[0] == 'd') ? PAL_DOS : PAL_WINDOWS; diff --git a/src/hotkeys.cpp b/src/hotkeys.cpp index 5ea89686a8..2260e456ea 100644 --- a/src/hotkeys.cpp +++ b/src/hotkeys.cpp @@ -274,12 +274,12 @@ HotkeyList::~HotkeyList() * Load HotkeyList from IniFile. * @param ini IniFile to load from. */ -void HotkeyList::Load(IniFile &ini) +void HotkeyList::Load(const IniFile &ini) { - IniGroup *group = ini.GetGroup(this->ini_group); + const IniGroup *group = ini.GetGroup(this->ini_group); if (group == nullptr) return; for (Hotkey &hotkey : this->items) { - IniItem *item = group->GetItem(hotkey.name); + const IniItem *item = group->GetItem(hotkey.name); if (item != nullptr) { hotkey.keycodes.clear(); if (item->value.has_value()) ParseHotkeys(hotkey, item->value->c_str()); diff --git a/src/hotkeys.h b/src/hotkeys.h index 4508500268..88b714590b 100644 --- a/src/hotkeys.h +++ b/src/hotkeys.h @@ -40,7 +40,7 @@ struct HotkeyList { HotkeyList(const std::string &ini_group, const std::vector &items, GlobalHotkeyHandlerFunc global_hotkey_handler = nullptr); ~HotkeyList(); - void Load(IniFile &ini); + void Load(const IniFile &ini); void Save(IniFile &ini) const; int CheckMatch(uint16_t keycode, bool global_only = false) const; diff --git a/src/ini_load.cpp b/src/ini_load.cpp index e6562a1114..c3a8d8103c 100644 --- a/src/ini_load.cpp +++ b/src/ini_load.cpp @@ -49,9 +49,9 @@ IniGroup::IniGroup(const std::string &name, IniGroupType type) : type(type) * @param name name of the item to find. * @return the requested item or nullptr if not found. */ -IniItem *IniGroup::GetItem(const std::string &name) +const IniItem *IniGroup::GetItem(const std::string &name) const { - for (IniItem &item : this->items) { + for (const IniItem &item : this->items) { if (item.name == name) return &item; } @@ -111,6 +111,20 @@ IniLoadFile::IniLoadFile(const IniGroupNameList &list_group_names, const IniGrou { } +/** + * Get the group with the given name. + * @param name name of the group to find. + * @return The requested group or \c nullptr if not found. + */ +const IniGroup *IniLoadFile::GetGroup(const std::string &name) const +{ + for (const IniGroup &group : this->groups) { + if (group.name == name) return &group; + } + + return nullptr; +} + /** * Get the group with the given name. * @param name name of the group to find. diff --git a/src/ini_type.h b/src/ini_type.h index 211209aa39..5ea462ef18 100644 --- a/src/ini_type.h +++ b/src/ini_type.h @@ -39,7 +39,7 @@ struct IniGroup { IniGroup(const std::string &name, IniGroupType type); - IniItem *GetItem(const std::string &name); + const IniItem *GetItem(const std::string &name) const; IniItem &GetOrCreateItem(const std::string &name); IniItem &CreateItem(const std::string &name); void RemoveItem(const std::string &name); @@ -58,6 +58,7 @@ struct IniLoadFile { IniLoadFile(const IniGroupNameList &list_group_names = {}, const IniGroupNameList &seq_group_names = {}); virtual ~IniLoadFile() { } + const IniGroup *GetGroup(const std::string &name) const; IniGroup *GetGroup(const std::string &name); IniGroup &GetOrCreateGroup(const std::string &name); IniGroup &CreateGroup(const std::string &name); diff --git a/src/music.cpp b/src/music.cpp index 1c8d2ca5fa..825ae388b0 100644 --- a/src/music.cpp +++ b/src/music.cpp @@ -116,14 +116,14 @@ template return BaseMedia::used_set != nullptr; } -bool MusicSet::FillSetDetails(IniFile &ini, const std::string &path, const std::string &full_filename) +bool MusicSet::FillSetDetails(const IniFile &ini, const std::string &path, const std::string &full_filename) { bool ret = this->BaseSet::FillSetDetails(ini, path, full_filename); if (ret) { this->num_available = 0; - IniGroup *names = ini.GetGroup("names"); - IniGroup *catindex = ini.GetGroup("catindex"); - IniGroup *timingtrim = ini.GetGroup("timingtrim"); + const IniGroup *names = ini.GetGroup("names"); + const IniGroup *catindex = ini.GetGroup("catindex"); + const IniGroup *timingtrim = ini.GetGroup("timingtrim"); uint tracknr = 1; for (uint i = 0; i < lengthof(this->songinfo); i++) { const std::string &filename = this->files[i].filename; @@ -133,7 +133,7 @@ bool MusicSet::FillSetDetails(IniFile &ini, const std::string &path, const std:: this->songinfo[i].filename = filename; // non-owned pointer - IniItem *item = catindex != nullptr ? catindex->GetItem(_music_file_names[i]) : nullptr; + const IniItem *item = catindex != nullptr ? catindex->GetItem(_music_file_names[i]) : nullptr; if (item != nullptr && item->value.has_value() && !item->value->empty()) { /* Song has a CAT file index, assume it's MPS MIDI format */ this->songinfo[i].filetype = MTT_MPSMIDI; diff --git a/src/settings.cpp b/src/settings.cpp index 179aa414f7..a9d79b53f4 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -586,8 +586,8 @@ const std::string &StringSettingDesc::Read(const void *object) const */ static void IniLoadSettings(IniFile &ini, const SettingTable &settings_table, const char *grpname, void *object, bool only_startup) { - IniGroup *group; - IniGroup *group_def = ini.GetGroup(grpname); + const IniGroup *group; + const IniGroup *group_def = ini.GetGroup(grpname); for (auto &desc : settings_table) { const SettingDesc *sd = GetSettingDesc(desc); @@ -605,7 +605,7 @@ static void IniLoadSettings(IniFile &ini, const SettingTable &settings_table, co group = group_def; } - IniItem *item = nullptr; + const IniItem *item = nullptr; if (group != nullptr) item = group->GetItem(s); if (item == nullptr && group != group_def && group_def != nullptr) { /* For settings.xx.yy load the settings from [settings] yy = ? in case the previous @@ -780,7 +780,7 @@ bool ListSettingDesc::IsDefaultValue(void *) const */ static void IniLoadSettingList(IniFile &ini, const char *grpname, StringList &list) { - IniGroup *group = ini.GetGroup(grpname); + const IniGroup *group = ini.GetGroup(grpname); if (group == nullptr) return; @@ -894,9 +894,9 @@ static void ValidateSettings() } } -static void AILoadConfig(IniFile &ini, const char *grpname) +static void AILoadConfig(const IniFile &ini, const char *grpname) { - IniGroup *group = ini.GetGroup(grpname); + const IniGroup *group = ini.GetGroup(grpname); /* Clean any configured AI */ for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) { @@ -923,9 +923,9 @@ static void AILoadConfig(IniFile &ini, const char *grpname) } } -static void GameLoadConfig(IniFile &ini, const char *grpname) +static void GameLoadConfig(const IniFile &ini, const char *grpname) { - IniGroup *group = ini.GetGroup(grpname); + const IniGroup *group = ini.GetGroup(grpname); /* Clean any configured GameScript */ GameConfig::GetConfig(GameConfig::SSS_FORCE_NEWGAME)->Change(std::nullopt); @@ -933,7 +933,7 @@ static void GameLoadConfig(IniFile &ini, const char *grpname) /* If no group exists, return */ if (group == nullptr || group->items.empty()) return; - IniItem &item = group->items.front(); + const IniItem &item = group->items.front(); GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME); @@ -987,9 +987,9 @@ static bool DecodeHexText(const char *pos, uint8_t *dest, size_t dest_size) * @param grpname Group name containing the configuration of the GRF. * @param is_static GRF is static. */ -static GRFConfig *GRFLoadConfig(IniFile &ini, const char *grpname, bool is_static) +static GRFConfig *GRFLoadConfig(const IniFile &ini, const char *grpname, bool is_static) { - IniGroup *group = ini.GetGroup(grpname); + const IniGroup *group = ini.GetGroup(grpname); GRFConfig *first = nullptr; GRFConfig **curr = &first; @@ -1088,9 +1088,9 @@ static GRFConfig *GRFLoadConfig(IniFile &ini, const char *grpname, bool is_stati return first; } -static IniFileVersion LoadVersionFromConfig(IniFile &ini) +static IniFileVersion LoadVersionFromConfig(const IniFile &ini) { - IniGroup *group = ini.GetGroup("version"); + const IniGroup *group = ini.GetGroup("version"); if (group == nullptr) return IFV_0; auto version_number = group->GetItem("ini_version"); @@ -1247,16 +1247,16 @@ static void RemoveEntriesFromIni(IniFile &ini, const SettingTable &table) * @param[out] old_item The old item to base upgrading on. * @return Whether upgrading should happen; if false, old_item is a nullptr. */ -bool IsConversionNeeded(ConfigIniFile &ini, std::string group, std::string old_var, std::string new_var, IniItem **old_item) +bool IsConversionNeeded(const ConfigIniFile &ini, const std::string &group, const std::string &old_var, const std::string &new_var, const IniItem **old_item) { *old_item = nullptr; - IniGroup *igroup = ini.GetGroup(group); + const IniGroup *igroup = ini.GetGroup(group); /* If the group doesn't exist, there is nothing to convert. */ if (igroup == nullptr) return false; - IniItem *tmp_old_item = igroup->GetItem(old_var); - IniItem *new_item = igroup->GetItem(new_var); + const IniItem *tmp_old_item = igroup->GetItem(old_var); + const IniItem *new_item = igroup->GetItem(new_var); /* If the old item doesn't exist, there is nothing to convert. */ if (tmp_old_item == nullptr) return false; @@ -1301,9 +1301,9 @@ void LoadFromConfig(bool startup) /* Move no_http_content_downloads and use_relay_service from generic_ini to private_ini. */ if (generic_version < IFV_NETWORK_PRIVATE_SETTINGS) { - IniGroup *network = generic_ini.GetGroup("network"); + const IniGroup *network = generic_ini.GetGroup("network"); if (network != nullptr) { - IniItem *no_http_content_downloads = network->GetItem("no_http_content_downloads"); + const IniItem *no_http_content_downloads = network->GetItem("no_http_content_downloads"); if (no_http_content_downloads != nullptr) { if (no_http_content_downloads->value == "true") { _settings_client.network.no_http_content_downloads = true; @@ -1312,7 +1312,7 @@ void LoadFromConfig(bool startup) } } - IniItem *use_relay_service = network->GetItem("use_relay_service"); + const IniItem *use_relay_service = network->GetItem("use_relay_service"); if (use_relay_service != nullptr) { if (use_relay_service->value == "never") { _settings_client.network.use_relay_service = UseRelayService::URS_NEVER; @@ -1325,7 +1325,7 @@ void LoadFromConfig(bool startup) } } - IniItem *old_item; + const IniItem *old_item; if (generic_version < IFV_GAME_TYPE && IsConversionNeeded(generic_ini, "network", "server_advertise", "server_game_type", &old_item)) { auto old_value = BoolSettingDesc::ParseSingleValue(old_item->value->c_str()); diff --git a/src/settingsgen/settingsgen.cpp b/src/settingsgen/settingsgen.cpp index 0d11ed0475..869ec5bb30 100644 --- a/src/settingsgen/settingsgen.cpp +++ b/src/settingsgen/settingsgen.cpp @@ -193,11 +193,11 @@ static const char *DEFAULTS_GROUP_NAME = "defaults"; ///< Name of the group con * @param ifile Loaded INI data. * @param group_name Name of the group to copy. */ -static void DumpGroup(IniLoadFile &ifile, const char * const group_name) +static void DumpGroup(const IniLoadFile &ifile, const char * const group_name) { - IniGroup *grp = ifile.GetGroup(group_name); + const IniGroup *grp = ifile.GetGroup(group_name); if (grp != nullptr && grp->type == IGT_SEQUENCE) { - for (IniItem &item : grp->items) { + for (const IniItem &item : grp->items) { if (!item.name.empty()) { _stored_output.Add(item.name.c_str()); _stored_output.Add("\n", 1); @@ -213,9 +213,9 @@ static void DumpGroup(IniLoadFile &ifile, const char * const group_name) * @param defaults Fallback group to search, \c nullptr skips the search. * @return Text of the item if found, else \c nullptr. */ -static const char *FindItemValue(const char *name, IniGroup *grp, IniGroup *defaults) +static const char *FindItemValue(const char *name, const IniGroup *grp, const IniGroup *defaults) { - IniItem *item = grp->GetItem(name); + const IniItem *item = grp->GetItem(name); if (item == nullptr && defaults != nullptr) item = defaults->GetItem(name); if (item == nullptr || !item->value.has_value()) return nullptr; return item->value->c_str(); @@ -228,7 +228,7 @@ static const char *FindItemValue(const char *name, IniGroup *grp, IniGroup *defa * @param default_grp Default values for items not set in @grp. * @param output Output to use for result. */ -static void DumpLine(IniItem *item, IniGroup *grp, IniGroup *default_grp, OutputStore &output) +static void DumpLine(const IniItem *item, const IniGroup *grp, const IniGroup *default_grp, OutputStore &output) { static const int MAX_VAR_LENGTH = 64; @@ -292,28 +292,28 @@ static void DumpLine(IniItem *item, IniGroup *grp, IniGroup *default_grp, Output * Output all non-special sections through the template / template variable expansion system. * @param ifile Loaded INI data. */ -static void DumpSections(IniLoadFile &ifile) +static void DumpSections(const IniLoadFile &ifile) { static const auto special_group_names = {PREAMBLE_GROUP_NAME, POSTAMBLE_GROUP_NAME, DEFAULTS_GROUP_NAME, TEMPLATES_GROUP_NAME, VALIDATION_GROUP_NAME}; - IniGroup *default_grp = ifile.GetGroup(DEFAULTS_GROUP_NAME); - IniGroup *templates_grp = ifile.GetGroup(TEMPLATES_GROUP_NAME); - IniGroup *validation_grp = ifile.GetGroup(VALIDATION_GROUP_NAME); + const IniGroup *default_grp = ifile.GetGroup(DEFAULTS_GROUP_NAME); + const IniGroup *templates_grp = ifile.GetGroup(TEMPLATES_GROUP_NAME); + const IniGroup *validation_grp = ifile.GetGroup(VALIDATION_GROUP_NAME); if (templates_grp == nullptr) return; /* Output every group, using its name as template name. */ - for (IniGroup &grp : ifile.groups) { + for (const IniGroup &grp : ifile.groups) { /* Exclude special group names. */ if (std::find(std::begin(special_group_names), std::end(special_group_names), grp.name) != std::end(special_group_names)) continue; - IniItem *template_item = templates_grp->GetItem(grp.name); // Find template value. + const IniItem *template_item = templates_grp->GetItem(grp.name); // Find template value. if (template_item == nullptr || !template_item->value.has_value()) { FatalError("Cannot find template {}", grp.name); } DumpLine(template_item, &grp, default_grp, _stored_output); if (validation_grp != nullptr) { - IniItem *validation_item = validation_grp->GetItem(grp.name); // Find template value. + const IniItem *validation_item = validation_grp->GetItem(grp.name); // Find template value. if (validation_item != nullptr && validation_item->value.has_value()) { DumpLine(validation_item, &grp, default_grp, _post_amble_output); }