1
0
Fork 0

Codechange: Add CreateGroup/CreateItem methods for ini files.

This abstracts the internals a bit.
pull/11385/head
Peter Nelson 2023-10-10 19:25:57 +01:00 committed by Peter Nelson
parent 0c85ce29ea
commit d3c5ae2648
3 changed files with 32 additions and 12 deletions

View File

@ -107,6 +107,16 @@ IniItem &IniGroup::GetOrCreateItem(const std::string &name)
} }
/* Item doesn't exist, make a new one. */ /* Item doesn't exist, make a new one. */
return this->CreateItem(name);
}
/**
* Create an item with the given name. This does not reuse an existing item of the same name.
* @param name name of the item to create.
* @return the created item.
*/
IniItem &IniGroup::CreateItem(const std::string &name)
{
return *(new IniItem(this, name)); return *(new IniItem(this, name));
} }
@ -180,9 +190,19 @@ IniGroup *IniLoadFile::GetGroup(const std::string &name, bool create_new)
if (!create_new) return nullptr; if (!create_new) return nullptr;
/* otherwise make a new one */ /* otherwise 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.
*/
IniGroup &IniLoadFile::CreateGroup(const std::string &name)
{
IniGroup *group = new IniGroup(this, name); IniGroup *group = new IniGroup(this, name);
group->comment = "\n"; group->comment = "\n";
return group; return *group;
} }
/** /**
@ -275,7 +295,7 @@ void IniLoadFile::LoadFromDisk(const std::string &filename, Subdirectory subdir)
e--; e--;
} }
s++; // skip [ s++; // skip [
group = new IniGroup(this, std::string(s, e - s)); group = &this->CreateGroup(std::string(s, e - s));
if (comment_size != 0) { if (comment_size != 0) {
group->comment.assign(comment, comment_size); group->comment.assign(comment, comment_size);
comment_size = 0; comment_size = 0;
@ -283,9 +303,9 @@ void IniLoadFile::LoadFromDisk(const std::string &filename, Subdirectory subdir)
} else if (group != nullptr) { } else if (group != nullptr) {
if (group->type == IGT_SEQUENCE) { if (group->type == IGT_SEQUENCE) {
/* A sequence group, use the line as item name without further interpretation. */ /* A sequence group, use the line as item name without further interpretation. */
IniItem *item = new IniItem(group, std::string(buffer, e - buffer)); IniItem &item = group->CreateItem(std::string(buffer, e - buffer));
if (comment_size) { if (comment_size) {
item->comment.assign(comment, comment_size); item.comment.assign(comment, comment_size);
comment_size = 0; comment_size = 0;
} }
continue; continue;
@ -301,9 +321,9 @@ void IniLoadFile::LoadFromDisk(const std::string &filename, Subdirectory subdir)
} }
/* it's an item in an existing group */ /* it's an item in an existing group */
IniItem *item = new IniItem(group, std::string(s, t - s)); IniItem &item = group->CreateItem(std::string(s, t - s));
if (comment_size != 0) { if (comment_size != 0) {
item->comment.assign(comment, comment_size); item.comment.assign(comment, comment_size);
comment_size = 0; comment_size = 0;
} }
@ -320,9 +340,9 @@ void IniLoadFile::LoadFromDisk(const std::string &filename, Subdirectory subdir)
/* If the value was not quoted and empty, it must be nullptr */ /* If the value was not quoted and empty, it must be nullptr */
if (!quoted && e == t) { if (!quoted && e == t) {
item->value.reset(); item.value.reset();
} else { } else {
item->value = StrMakeValid(std::string(t)); item.value = StrMakeValid(std::string(t));
} }
} else { } else {
/* it's an orphan item */ /* it's an orphan item */

View File

@ -46,6 +46,7 @@ struct IniGroup {
IniItem *GetItem(const std::string &name) const; IniItem *GetItem(const std::string &name) const;
IniItem &GetOrCreateItem(const std::string &name); IniItem &GetOrCreateItem(const std::string &name);
IniItem &CreateItem(const std::string &name);
void RemoveItem(const std::string &name); void RemoveItem(const std::string &name);
void Clear(); void Clear();
}; };
@ -62,6 +63,7 @@ struct IniLoadFile {
virtual ~IniLoadFile(); virtual ~IniLoadFile();
IniGroup *GetGroup(const std::string &name, bool create_new = true); IniGroup *GetGroup(const std::string &name, bool create_new = true);
IniGroup &CreateGroup(const std::string &name);
void RemoveGroup(const std::string &name); void RemoveGroup(const std::string &name);
void LoadFromDisk(const std::string &filename, Subdirectory subdir); void LoadFromDisk(const std::string &filename, Subdirectory subdir);

View File

@ -1121,8 +1121,7 @@ static void AISaveConfig(IniFile &ini, const char *grpname)
name = "none"; name = "none";
} }
IniItem *item = new IniItem(group, name); group->CreateItem(name).SetValue(value);
item->SetValue(value);
} }
} }
@ -1143,8 +1142,7 @@ static void GameSaveConfig(IniFile &ini, const char *grpname)
name = "none"; name = "none";
} }
IniItem *item = new IniItem(group, name); group->CreateItem(name).SetValue(value);
item->SetValue(value);
} }
/** /**