Changeset - r28010:6864facde095
[Not reviewed]
master
0 4 0
Peter Nelson - 7 months ago 2023-10-10 18:25:58
peter1138@openttd.org
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.
4 files changed with 36 insertions and 26 deletions:
0 comments (0 inline, 0 general)
src/hotkeys.cpp
Show inline comments
 
@@ -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));
 
	}
 
}
src/ini_load.cpp
Show inline comments
 
@@ -194,6 +194,21 @@ IniGroup *IniLoadFile::GetGroup(const st
 
}
 

	
 
/**
 
 * 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.
 
 * @return the created group.
src/ini_type.h
Show inline comments
 
@@ -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);
 

	
src/settings.cpp
Show inline comments
 
@@ -676,10 +676,10 @@ static void IniSaveSettings(IniFile &ini
 
		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 &
 
 */
 
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 LoadVersionFromCon
 

	
 
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, c
 
			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,
 
		name = "none";
 
	}
 

	
 
	group->CreateItem(name).SetValue(value);
 
	group.CreateItem(name).SetValue(value);
 
}
 

	
 
/**
 
@@ -1151,23 +1145,23 @@ static void GameSaveConfig(IniFile &ini,
 
 */
 
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));
 
	}
 
}
 

	
0 comments (0 inline, 0 general)