Codechange: Pass ini file by reference and prefer automatic storage.

This avoids new/delete operations, and (not) checking for nullptr.
This commit is contained in:
Peter Nelson 2023-10-10 19:25:56 +01:00 committed by Peter Nelson
parent 3961318974
commit 0c85ce29ea
7 changed files with 40 additions and 53 deletions

View File

@ -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, MAX_GFT, true> {
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_SONGS_AVAILABLE, false> {
/** 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 */

View File

@ -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_tars>::FillSetDetails(IniFile *ini, const
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 std::string &filename, size_t basepath_
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 std::string &filename, size_t basepath_
delete set;
}
delete ini;
return ret;
}

View File

@ -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");

View File

@ -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 keycode, bool global_only) const
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);
}

View File

@ -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;

View File

@ -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;

View File

@ -188,28 +188,14 @@ static const char *TEMPLATES_GROUP_NAME = "templates"; ///< Name of the group co
static const char *VALIDATION_GROUP_NAME = "validation"; ///< Name of the group containing the validation statements.
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, IniGroup *grp, IniGroup *default_grp, Output
* 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);
}
/**