Changeset - r28007:aba174f6a1f7
[Not reviewed]
master
0 7 0
Peter Nelson - 14 months ago 2023-10-10 18:25:56
peter1138@openttd.org
Codechange: Pass ini file by reference and prefer automatic storage.

This avoids new/delete operations, and (not) checking for nullptr.
7 files changed with 40 insertions and 53 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(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(struct IniFile *ini, const std::string &path, const std::string &full_filename);
 
	bool FillSetDetails(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(struct IniFile *ini, const std::string &path, const std::string &full_filename);
 
	bool FillSetDetails(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,9 +39,9 @@ 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(IniFile &ini, const std::string &path, const std::string &full_filename, bool allow_empty_filename)
 
{
 
	IniGroup *metadata = ini->GetGroup("metadata");
 
	IniGroup *metadata = ini.GetGroup("metadata");
 
	IniItem *item;
 

	
 
	fetch_metadata("name");
 
@@ -69,9 +69,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");
 
	IniGroup *files  = ini.GetGroup("files");
 
	IniGroup *md5s   = ini.GetGroup("md5s");
 
	IniGroup *origin = ini.GetGroup("origin");
 
	for (uint i = 0; i < Tnum_files; i++) {
 
		MD5File *file = &this->files[i];
 
		/* Find the filename first. */
 
@@ -159,9 +159,9 @@ bool BaseMedia<Tbase_set>::AddFile(const
 
	Debug(grf, 1, "Checking {} for base " SET_TYPE " set", filename);
 

	
 
	Tbase_set *set = new Tbase_set();
 
	IniFile *ini = new IniFile();
 
	IniFile ini{};
 
	std::string path{ filename, basepath_length };
 
	ini->LoadFromDisk(path, BASESET_DIR);
 
	ini.LoadFromDisk(path, BASESET_DIR);
 

	
 
	auto psep = path.rfind(PATHSEPCHAR);
 
	if (psep != std::string::npos) {
 
@@ -218,7 +218,6 @@ bool BaseMedia<Tbase_set>::AddFile(const
 
		delete set;
 
	}
 

	
 
	delete ini;
 
	return ret;
 
}
 

	
src/gfxinit.cpp
Show inline comments
 
@@ -347,11 +347,11 @@ void GfxLoadSprites()
 
	UpdateCursorSize();
 
}
 

	
 
bool GraphicsSet::FillSetDetails(IniFile *ini, const std::string &path, const std::string &full_filename)
 
bool GraphicsSet::FillSetDetails(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");
 
		IniGroup *metadata = ini.GetGroup("metadata");
 
		IniItem *item;
 

	
 
		fetch_metadata("palette");
src/hotkeys.cpp
Show inline comments
 
@@ -274,9 +274,9 @@ HotkeyList::~HotkeyList()
 
 * Load HotkeyList from IniFile.
 
 * @param ini IniFile to load from.
 
 */
 
void HotkeyList::Load(IniFile *ini)
 
void HotkeyList::Load(IniFile &ini)
 
{
 
	IniGroup *group = ini->GetGroup(this->ini_group);
 
	IniGroup *group = ini.GetGroup(this->ini_group);
 
	for (Hotkey &hotkey : this->items) {
 
		IniItem *item = group->GetItem(hotkey.name);
 
		if (item != nullptr) {
 
@@ -290,9 +290,9 @@ void HotkeyList::Load(IniFile *ini)
 
 * Save HotkeyList to IniFile.
 
 * @param ini IniFile to save to.
 
 */
 
void HotkeyList::Save(IniFile *ini) const
 
void HotkeyList::Save(IniFile &ini) const
 
{
 
	IniGroup *group = ini->GetGroup(this->ini_group);
 
	IniGroup *group = ini.GetGroup(this->ini_group);
 
	for (const Hotkey &hotkey : this->items) {
 
		IniItem &item = group->GetOrCreateItem(hotkey.name);
 
		item.SetValue(SaveKeycodes(hotkey));
 
@@ -320,8 +320,8 @@ int HotkeyList::CheckMatch(uint16_t keyc
 

	
 
static void SaveLoadHotkeys(bool save)
 
{
 
	IniFile *ini = new IniFile();
 
	ini->LoadFromDisk(_hotkeys_file, NO_DIRECTORY);
 
	IniFile ini{};
 
	ini.LoadFromDisk(_hotkeys_file, NO_DIRECTORY);
 

	
 
	for (HotkeyList *list : *_hotkey_lists) {
 
		if (save) {
 
@@ -331,8 +331,7 @@ static void SaveLoadHotkeys(bool save)
 
		}
 
	}
 

	
 
	if (save) ini->SaveToDisk(_hotkeys_file);
 
	delete ini;
 
	if (save) ini.SaveToDisk(_hotkeys_file);
 
}
 

	
 

	
src/hotkeys.h
Show inline comments
 
@@ -40,8 +40,8 @@ struct HotkeyList {
 
	HotkeyList(const std::string &ini_group, const std::vector<Hotkey> &items, GlobalHotkeyHandlerFunc global_hotkey_handler = nullptr);
 
	~HotkeyList();
 

	
 
	void Load(IniFile *ini);
 
	void Save(IniFile *ini) const;
 
	void Load(IniFile &ini);
 
	void Save(IniFile &ini) const;
 

	
 
	int CheckMatch(uint16_t keycode, bool global_only = false) const;
 

	
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(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");
 
		IniGroup *names = ini.GetGroup("names");
 
		IniGroup *catindex = ini.GetGroup("catindex");
 
		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;
src/settingsgen/settingsgen.cpp
Show inline comments
 
@@ -189,27 +189,13 @@ static const char *VALIDATION_GROUP_NAME
 
static const char *DEFAULTS_GROUP_NAME  = "defaults"; ///< Name of the group containing default values for the template variables.
 

	
 
/**
 
 * Load the INI file.
 
 * @param filename Name of the file to load.
 
 * @return         Loaded INI data.
 
 */
 
static IniLoadFile *LoadIniFile(const char *filename)
 
{
 
	static const char * const seq_groups[] = {PREAMBLE_GROUP_NAME, POSTAMBLE_GROUP_NAME, nullptr};
 

	
 
	IniLoadFile *ini = new SettingsIniFile(nullptr, seq_groups);
 
	ini->LoadFromDisk(filename, NO_DIRECTORY);
 
	return ini;
 
}
 

	
 
/**
 
 * Dump a #IGT_SEQUENCE group into #_stored_output.
 
 * @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(IniLoadFile &ifile, const char * const group_name)
 
{
 
	IniGroup *grp = ifile->GetGroup(group_name, false);
 
	IniGroup *grp = ifile.GetGroup(group_name, false);
 
	if (grp != nullptr && grp->type == IGT_SEQUENCE) {
 
		for (IniItem *item = grp->item; item != nullptr; item = item->next) {
 
			if (!item->name.empty()) {
 
@@ -306,17 +292,17 @@ 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(IniLoadFile &ifile)
 
{
 
	static const char * const special_group_names[] = {PREAMBLE_GROUP_NAME, POSTAMBLE_GROUP_NAME, DEFAULTS_GROUP_NAME, TEMPLATES_GROUP_NAME, VALIDATION_GROUP_NAME, nullptr};
 

	
 
	IniGroup *default_grp = ifile->GetGroup(DEFAULTS_GROUP_NAME, false);
 
	IniGroup *templates_grp  = ifile->GetGroup(TEMPLATES_GROUP_NAME, false);
 
	IniGroup *validation_grp  = ifile->GetGroup(VALIDATION_GROUP_NAME, false);
 
	IniGroup *default_grp = ifile.GetGroup(DEFAULTS_GROUP_NAME, false);
 
	IniGroup *templates_grp = ifile.GetGroup(TEMPLATES_GROUP_NAME, false);
 
	IniGroup *validation_grp = ifile.GetGroup(VALIDATION_GROUP_NAME, false);
 
	if (templates_grp == nullptr) return;
 

	
 
	/* Output every group, using its name as template name. */
 
	for (IniGroup *grp = ifile->group; grp != nullptr; grp = grp->next) {
 
	for (IniGroup *grp = ifile.group; grp != nullptr; grp = grp->next) {
 
		const char * const *sgn;
 
		for (sgn = special_group_names; *sgn != nullptr; sgn++) if (grp->name == *sgn) break;
 
		if (*sgn != nullptr) continue;
 
@@ -430,11 +416,14 @@ static const OptionData _opts[] = {
 
 */
 
static void ProcessIniFile(const char *fname)
 
{
 
	IniLoadFile *ini_data = LoadIniFile(fname);
 
	DumpGroup(ini_data, PREAMBLE_GROUP_NAME);
 
	DumpSections(ini_data);
 
	DumpGroup(ini_data, POSTAMBLE_GROUP_NAME);
 
	delete ini_data;
 
	static const char * const seq_groups[] = {PREAMBLE_GROUP_NAME, POSTAMBLE_GROUP_NAME, nullptr};
 

	
 
	SettingsIniFile ini{nullptr, seq_groups};
 
	ini.LoadFromDisk(fname, NO_DIRECTORY);
 

	
 
	DumpGroup(ini, PREAMBLE_GROUP_NAME);
 
	DumpSections(ini);
 
	DumpGroup(ini, POSTAMBLE_GROUP_NAME);
 
}
 

	
 
/**
0 comments (0 inline, 0 general)