Changeset - r25649:96782a72248d
[Not reviewed]
master
0 2 0
rubidium42 - 3 years ago 2021-05-30 09:30:03
rubidium@openttd.org
Codechange: use StrStartsWith/StrEndsWith when finding settings
2 files changed with 14 insertions and 18 deletions:
0 comments (0 inline, 0 general)
src/settings.cpp
Show inline comments
 
@@ -1232,13 +1232,13 @@ static void HandleOldDiffCustom(bool sav
 
	/* Iterate over all the old difficulty settings, and convert the list-value to the new setting. */
 
	uint i = 0;
 
	for (const auto &name : _old_diff_settings) {
 
		if (has_no_town_council_tolerance && name == "town_council_tolerance") continue;
 

	
 
		std::string fullname = "difficulty." + name;
 
		const SettingDesc *sd = GetSettingFromName(fullname.c_str());
 
		const SettingDesc *sd = GetSettingFromName(fullname);
 

	
 
		/* Some settings are no longer in use; skip reading those. */
 
		if (sd == nullptr) {
 
			i++;
 
			continue;
 
		}
 
@@ -1702,67 +1702,63 @@ void IntSettingDesc::ChangeValue(const v
 
 * Given a name of setting, return a setting description from the table.
 
 * @param name Name of the setting to return a setting description of.
 
 * @param settings Table to look in for the setting.
 
 * @return Pointer to the setting description of setting \a name if it can be found,
 
 *         \c nullptr indicates failure to obtain the description.
 
 */
 
static const SettingDesc *GetSettingFromName(const char *name, const SettingTable &settings)
 
static const SettingDesc *GetSettingFromName(const std::string_view name, const SettingTable &settings)
 
{
 
	/* First check all full names */
 
	for (auto &sd : settings) {
 
		if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
 
		if (strcmp(sd->name, name) == 0) return sd.get();
 
		if (sd->name == name) return sd.get();
 
	}
 

	
 
	/* Then check the shortcut variant of the name. */
 
	std::string short_name_suffix = std::string{ "." }.append(name);
 
	for (auto &sd : settings) {
 
		if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
 
		const char *short_name = strchr(sd->name, '.');
 
		if (short_name != nullptr) {
 
			short_name++;
 
			if (strcmp(short_name, name) == 0) return sd.get();
 
		}
 
		if (StrEndsWith(sd->name, short_name_suffix)) return sd.get();
 
	}
 

	
 
	return nullptr;
 
}
 

	
 
/**
 
 * Get the SaveLoad from all settings matching the prefix.
 
 * @param prefix The prefix to look for.
 
 * @param saveloads A vector to store the result in.
 
 */
 
void GetSettingSaveLoadByPrefix(const char *prefix, std::vector<SaveLoad> &saveloads)
 
void GetSettingSaveLoadByPrefix(std::string_view prefix, std::vector<SaveLoad> &saveloads)
 
{
 
	size_t prefixlen = strlen(prefix);
 

	
 
	for (auto &sd : _settings) {
 
		if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
 
		if (strncmp(sd->name, prefix, prefixlen) == 0) saveloads.push_back(sd->save);
 
		if (StrStartsWith(sd->name, prefix)) saveloads.push_back(sd->save);
 
	}
 
}
 

	
 
/**
 
 * Given a name of setting, return a company setting description of it.
 
 * @param name  Name of the company setting to return a setting description of.
 
 * @return Pointer to the setting description of setting \a name if it can be found,
 
 *         \c nullptr indicates failure to obtain the description.
 
 */
 
static const SettingDesc *GetCompanySettingFromName(const char *name)
 
static const SettingDesc *GetCompanySettingFromName(std::string_view name)
 
{
 
	if (strncmp(name, "company.", 8) == 0) name += 8;
 
	static const std::string_view company_prefix = "company.";
 
	if (StrStartsWith(name, company_prefix)) name.remove_prefix(company_prefix.size());
 
	return GetSettingFromName(name, _company_settings);
 
}
 

	
 
/**
 
 * Given a name of any setting, return any setting description of it.
 
 * @param name  Name of the setting to return a setting description of.
 
 * @return Pointer to the setting description of setting \a name if it can be found,
 
 *         \c nullptr indicates failure to obtain the description.
 
 */
 
const SettingDesc *GetSettingFromName(const char *name)
 
const SettingDesc *GetSettingFromName(const std::string_view name)
 
{
 
	auto sd = GetSettingFromName(name, _settings);
 
	if (sd != nullptr) return sd;
 

	
 
	return GetCompanySettingFromName(name);
 
}
 
@@ -1778,13 +1774,13 @@ const SettingDesc *GetSettingFromName(co
 
 * @return the cost of this operation or an error
 
 * @see _settings
 
 */
 
CommandCost CmdChangeSetting(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const std::string &text)
 
{
 
	if (text.empty()) return CMD_ERROR;
 
	const SettingDesc *sd = GetSettingFromName(text.c_str());
 
	const SettingDesc *sd = GetSettingFromName(text);
 

	
 
	if (sd == nullptr) return CMD_ERROR;
 
	if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) return CMD_ERROR;
 
	if (!sd->IsIntSetting()) return CMD_ERROR;
 

	
 
	if (!sd->IsEditable(true)) return CMD_ERROR;
src/settings_internal.h
Show inline comments
 
@@ -298,12 +298,12 @@ struct NullSettingDesc : SettingDesc {
 
	void ParseValue(const IniItem *item, void *object) const override { NOT_REACHED(); }
 
	bool IsSameValue(const IniItem *item, void *object) const override { NOT_REACHED(); }
 
};
 

	
 
typedef std::initializer_list<std::unique_ptr<const SettingDesc>> SettingTable;
 

	
 
const SettingDesc *GetSettingFromName(const char *name);
 
void GetSettingSaveLoadByPrefix(const char *prefix, std::vector<SaveLoad> &saveloads);
 
const SettingDesc *GetSettingFromName(const std::string_view name);
 
void GetSettingSaveLoadByPrefix(const std::string_view prefix, std::vector<SaveLoad> &saveloads);
 
bool SetSettingValue(const IntSettingDesc *sd, int32 value, bool force_newgame = false);
 
bool SetSettingValue(const StringSettingDesc *sd, const std::string value, bool force_newgame = false);
 

	
 
#endif /* SETTINGS_INTERNAL_H */
0 comments (0 inline, 0 general)