Changeset - r28017:b8af78becb18
[Not reviewed]
master
0 10 0
Peter Nelson - 14 months ago 2023-10-10 23:38:57
peter1138@openttd.org
Codechange: Add const versions of GetItem/GetGroup, and sprinkle liberally.

Non-const version of GetItem is not needed.
10 files changed with 73 insertions and 58 deletions:
0 comments (0 inline, 0 general)
src/base_media_base.h
Show inline comments
 
@@ -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<GraphicsSet
 
	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<MusicSet, NUM_
 
	/** 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 */
src/base_media_func.h
Show inline comments
 
@@ -39,15 +39,15 @@ extern void CheckExternalFiles();
 
 * @return true if loading was successful.
 
 */
 
template <class T, size_t Tnum_files, bool Tsearch_in_tars>
 
bool BaseSet<T, Tnum_files, Tsearch_in_tars>::FillSetDetails(IniFile &ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename)
 
bool BaseSet<T, Tnum_files, Tsearch_in_tars>::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<T, Tnum_files, Tsearch_in_t
 
	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. */
src/gfxinit.cpp
Show inline comments
 
@@ -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<GraphicsSet, MAX_GFT, true>::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;
src/hotkeys.cpp
Show inline comments
 
@@ -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());
src/hotkeys.h
Show inline comments
 
@@ -40,7 +40,7 @@ struct HotkeyList {
 
	HotkeyList(const std::string &ini_group, const std::vector<Hotkey> &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;
src/ini_load.cpp
Show inline comments
 
@@ -49,9 +49,9 @@ IniGroup::IniGroup(const std::string &na
 
 * @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;
 
	}
 

	
 
@@ -116,6 +116,20 @@ IniLoadFile::IniLoadFile(const IniGroupN
 
 * @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.
 
 * @return The requested group or \c nullptr if not found.
 
 */
 
IniGroup *IniLoadFile::GetGroup(const std::string &name)
 
{
 
	for (IniGroup &group : this->groups) {
src/ini_type.h
Show inline comments
 
@@ -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);
src/music.cpp
Show inline comments
 
@@ -116,14 +116,14 @@ template <class Tbase_set>
 
	return BaseMedia<Tbase_set>::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<MusicSet, NUM_SONGS_AVAILABLE, false>::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 &i
 

	
 
			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;
src/settings.cpp
Show inline comments
 
@@ -586,8 +586,8 @@ const std::string &StringSettingDesc::Re
 
 */
 
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
 
			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(voi
 
 */
 
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, c
 
	}
 
}
 

	
 
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,
 
	/* 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 *po
 
 * @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 
 
	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
 
 * @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());
src/settingsgen/settingsgen.cpp
Show inline comments
 
@@ -193,11 +193,11 @@ static const char *DEFAULTS_GROUP_NAME  
 
 * @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
 
 * @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 c
 
 * @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, IniG
 
 * 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);
 
			}
0 comments (0 inline, 0 general)