Codechange: Pass initializer list instead of null-terminated list of group types.

This commit is contained in:
Peter Nelson 2023-10-10 19:25:59 +01:00 committed by Peter Nelson
parent 1fecbeff76
commit 8bd06807e4
5 changed files with 19 additions and 32 deletions

View File

@ -32,9 +32,9 @@
/** /**
* Create a new ini file with given group names. * Create a new ini file with given group names.
* @param list_group_names A \c nullptr terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST * @param list_group_names A list with group names that should be loaded as lists instead of variables. @see IGT_LIST
*/ */
IniFile::IniFile(const char * const *list_group_names) : IniLoadFile(list_group_names) IniFile::IniFile(const IniGroupNameList &list_group_names) : IniLoadFile(list_group_names)
{ {
} }

View File

@ -56,22 +56,8 @@ IniGroup::IniGroup(IniLoadFile *parent, const std::string &name) : next(nullptr)
*parent->last_group = this; *parent->last_group = this;
parent->last_group = &this->next; parent->last_group = &this->next;
if (parent->list_group_names != nullptr) { if (std::find(parent->list_group_names.begin(), parent->list_group_names.end(), name) != parent->list_group_names.end()) this->type = IGT_LIST;
for (uint i = 0; parent->list_group_names[i] != nullptr; i++) { if (std::find(parent->seq_group_names.begin(), parent->seq_group_names.end(), name) != parent->seq_group_names.end()) this->type = IGT_SEQUENCE;
if (this->name == parent->list_group_names[i]) {
this->type = IGT_LIST;
return;
}
}
}
if (parent->seq_group_names != nullptr) {
for (uint i = 0; parent->seq_group_names[i] != nullptr; i++) {
if (this->name == parent->seq_group_names[i]) {
this->type = IGT_SEQUENCE;
return;
}
}
}
} }
/** Free everything we loaded. */ /** Free everything we loaded. */
@ -156,10 +142,10 @@ void IniGroup::Clear()
/** /**
* Construct a new in-memory Ini file representation. * Construct a new in-memory Ini file representation.
* @param list_group_names A \c nullptr terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST * @param list_group_names A list with group names that should be loaded as lists instead of variables. @see IGT_LIST
* @param seq_group_names A \c nullptr terminated list with group names that should be loaded as lists of names. @see IGT_SEQUENCE * @param seq_group_names A list with group names that should be loaded as lists of names. @see IGT_SEQUENCE
*/ */
IniLoadFile::IniLoadFile(const char * const *list_group_names, const char * const *seq_group_names) : IniLoadFile::IniLoadFile(const IniGroupNameList &list_group_names, const IniGroupNameList &seq_group_names) :
group(nullptr), group(nullptr),
list_group_names(list_group_names), list_group_names(list_group_names),
seq_group_names(seq_group_names) seq_group_names(seq_group_names)

View File

@ -53,13 +53,15 @@ struct IniGroup {
/** Ini file that only supports loading. */ /** Ini file that only supports loading. */
struct IniLoadFile { struct IniLoadFile {
using IniGroupNameList = std::initializer_list<std::string_view>;
IniGroup *group; ///< the first group in the ini IniGroup *group; ///< the first group in the ini
IniGroup **last_group; ///< the last group in the ini IniGroup **last_group; ///< the last group in the ini
std::string comment; ///< last comment in file std::string comment; ///< last comment in file
const char * const *list_group_names; ///< nullptr terminated list with group names that are lists const IniGroupNameList list_group_names; ///< list of group names that are lists
const char * const *seq_group_names; ///< nullptr terminated list with group names that are sequences. const IniGroupNameList seq_group_names; ///< list of group names that are sequences.
IniLoadFile(const char * const *list_group_names = nullptr, const char * const *seq_group_names = nullptr); IniLoadFile(const IniGroupNameList &list_group_names = {}, const IniGroupNameList &seq_group_names = {});
virtual ~IniLoadFile(); virtual ~IniLoadFile();
IniGroup *GetGroup(const std::string &name) const; IniGroup *GetGroup(const std::string &name) const;
@ -89,7 +91,7 @@ struct IniLoadFile {
/** Ini file that supports both loading and saving. */ /** Ini file that supports both loading and saving. */
struct IniFile : IniLoadFile { struct IniFile : IniLoadFile {
IniFile(const char * const *list_group_names = nullptr); IniFile(const IniGroupNameList &list_group_names = {});
bool SaveToDisk(const std::string &filename); bool SaveToDisk(const std::string &filename);

View File

@ -131,12 +131,11 @@ static bool IsSignedVarMemType(VarType vt)
*/ */
class ConfigIniFile : public IniFile { class ConfigIniFile : public IniFile {
private: private:
inline static const char * const list_group_names[] = { inline static const IniGroupNameList list_group_names = {
"bans", "bans",
"newgrf", "newgrf",
"servers", "servers",
"server_bind_addresses", "server_bind_addresses",
nullptr,
}; };
public: public:

View File

@ -151,10 +151,10 @@ private:
struct SettingsIniFile : IniLoadFile { struct SettingsIniFile : IniLoadFile {
/** /**
* Construct a new ini loader. * Construct a new ini loader.
* @param list_group_names A \c nullptr terminated list with group names that should be loaded as lists instead of variables. @see IGT_LIST * @param list_group_names A list with group names that should be loaded as lists instead of variables. @see IGT_LIST
* @param seq_group_names A \c nullptr terminated list with group names that should be loaded as lists of names. @see IGT_SEQUENCE * @param seq_group_names A list with group names that should be loaded as lists of names. @see IGT_SEQUENCE
*/ */
SettingsIniFile(const char * const *list_group_names = nullptr, const char * const *seq_group_names = nullptr) : SettingsIniFile(const IniGroupNameList &list_group_names = {}, const IniGroupNameList &seq_group_names = {}) :
IniLoadFile(list_group_names, seq_group_names) IniLoadFile(list_group_names, seq_group_names)
{ {
} }
@ -416,9 +416,9 @@ static const OptionData _opts[] = {
*/ */
static void ProcessIniFile(const char *fname) static void ProcessIniFile(const char *fname)
{ {
static const char * const seq_groups[] = {PREAMBLE_GROUP_NAME, POSTAMBLE_GROUP_NAME, nullptr}; static const IniLoadFile::IniGroupNameList seq_groups = {PREAMBLE_GROUP_NAME, POSTAMBLE_GROUP_NAME};
SettingsIniFile ini{nullptr, seq_groups}; SettingsIniFile ini{{}, seq_groups};
ini.LoadFromDisk(fname, NO_DIRECTORY); ini.LoadFromDisk(fname, NO_DIRECTORY);
DumpGroup(ini, PREAMBLE_GROUP_NAME); DumpGroup(ini, PREAMBLE_GROUP_NAME);