Changeset - r28006:7835d82043ea
[Not reviewed]
master
0 2 0
Peter Nelson - 12 months ago 2023-10-11 11:31:12
peter1138@openttd.org
Codechange: Accept std::string in RemoveGroup().
2 files changed with 3 insertions and 3 deletions:
0 comments (0 inline, 0 general)
src/ini_load.cpp
Show inline comments
 
@@ -168,51 +168,51 @@ IniLoadFile::~IniLoadFile()
 
 * and \a create_new is \c true create a new group.
 
 * @param name name of the group to find.
 
 * @param create_new Allow creation of group if it does not exist.
 
 * @return The requested group if it exists or was created, else \c nullptr.
 
 */
 
IniGroup *IniLoadFile::GetGroup(const std::string &name, bool create_new)
 
{
 
	/* does it exist already? */
 
	for (IniGroup *group = this->group; group != nullptr; group = group->next) {
 
		if (group->name == name) return group;
 
	}
 

	
 
	if (!create_new) return nullptr;
 

	
 
	/* otherwise make a new one */
 
	IniGroup *group = new IniGroup(this, name);
 
	group->comment = "\n";
 
	return group;
 
}
 

	
 
/**
 
 * Remove the group with the given name.
 
 * @param name name of the group to remove.
 
 */
 
void IniLoadFile::RemoveGroup(const char *name)
 
void IniLoadFile::RemoveGroup(const std::string &name)
 
{
 
	size_t len = strlen(name);
 
	size_t len = name.length();
 
	IniGroup *prev = nullptr;
 
	IniGroup *group;
 

	
 
	/* does it exist already? */
 
	for (group = this->group; group != nullptr; prev = group, group = group->next) {
 
		if (group->name.compare(0, len, name) == 0) {
 
			break;
 
		}
 
	}
 

	
 
	if (group == nullptr) return;
 

	
 
	if (prev != nullptr) {
 
		prev->next = prev->next->next;
 
		if (this->last_group == &group->next) this->last_group = &prev->next;
 
	} else {
 
		this->group = this->group->next;
 
		if (this->last_group == &group->next) this->last_group = &this->group;
 
	}
 

	
 
	group->next = nullptr;
 
	delete group;
 
}
 

	
src/ini_type.h
Show inline comments
 
@@ -41,49 +41,49 @@ struct IniGroup {
 
	std::string name;    ///< name of group
 
	std::string comment; ///< comment for group
 

	
 
	IniGroup(struct IniLoadFile *parent, const std::string &name);
 
	~IniGroup();
 

	
 
	IniItem *GetItem(const std::string &name) const;
 
	IniItem &GetOrCreateItem(const std::string &name);
 
	void RemoveItem(const std::string &name);
 
	void Clear();
 
};
 

	
 
/** Ini file that only supports loading. */
 
struct IniLoadFile {
 
	IniGroup *group;                      ///< the first group in the ini
 
	IniGroup **last_group;                ///< the last group in the ini
 
	std::string comment;                  ///< last comment in file
 
	const char * const *list_group_names; ///< nullptr terminated list with group names that are lists
 
	const char * const *seq_group_names;  ///< nullptr terminated list with group names that are sequences.
 

	
 
	IniLoadFile(const char * const *list_group_names = nullptr, const char * const *seq_group_names = nullptr);
 
	virtual ~IniLoadFile();
 

	
 
	IniGroup *GetGroup(const std::string &name, bool create_new = true);
 
	void RemoveGroup(const char *name);
 
	void RemoveGroup(const std::string &name);
 

	
 
	void LoadFromDisk(const std::string &filename, Subdirectory subdir);
 

	
 
	/**
 
	 * Open the INI file.
 
	 * @param filename Name of the INI file.
 
	 * @param subdir The subdir to load the file from.
 
	 * @param[out] size Size of the opened file.
 
	 * @return File handle of the opened file, or \c nullptr.
 
	 */
 
	virtual FILE *OpenFile(const std::string &filename, Subdirectory subdir, size_t *size) = 0;
 

	
 
	/**
 
	 * Report an error about the file contents.
 
	 * @param pre    Prefix text of the \a buffer part.
 
	 * @param buffer Part of the file with the error.
 
	 * @param post   Suffix text of the \a buffer part.
 
	 */
 
	virtual void ReportFileError(const char * const pre, const char * const buffer, const char * const post) = 0;
 
};
 

	
 
/** Ini file that supports both loading and saving. */
 
struct IniFile : IniLoadFile {
 
	IniFile(const char * const *list_group_names = nullptr);
0 comments (0 inline, 0 general)