mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-09 23:50:25 +00:00
Codechange: split Write_ValidateSetting to get separate functions for making strings valid and writing strings
This commit is contained in:
parent
ce1c60699a
commit
208952f2ba
@ -2208,7 +2208,7 @@ public:
|
||||
std::string client_name(str);
|
||||
if (!NetworkValidateClientName(client_name)) break;
|
||||
|
||||
SetSettingValue(GetSettingFromName("network.client_name")->AsStringSetting(), client_name.c_str());
|
||||
SetSettingValue(GetSettingFromName("network.client_name")->AsStringSetting(), client_name);
|
||||
this->InvalidateData();
|
||||
break;
|
||||
}
|
||||
|
@ -460,34 +460,31 @@ int32 IntSettingDesc::Read(const void *object) const
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the string value of a setting.
|
||||
* Make the value valid given the limitations of this setting.
|
||||
*
|
||||
* In the case of string settings this is ensuring the string contains only accepted
|
||||
* Utf8 characters and is at most the maximum length defined in this setting.
|
||||
* @param str The string to make valid.
|
||||
*/
|
||||
void StringSettingDesc::MakeValueValid(std::string &str) const
|
||||
{
|
||||
if (this->max_length == 0 || str.size() < this->max_length) return;
|
||||
|
||||
/* In case a maximum length is imposed by the setting, the length
|
||||
* includes the '\0' termination for network transfer purposes.
|
||||
* Also ensure the string is valid after chopping of some bytes. */
|
||||
std::string stdstr(str, this->max_length - 1);
|
||||
str.assign(str_validate(stdstr, SVS_NONE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a string to the actual setting.
|
||||
* @param object The object the setting is to be saved in.
|
||||
* @param str The string to save.
|
||||
*/
|
||||
void StringSettingDesc::Write_ValidateSetting(const void *object, const char *str) const
|
||||
void StringSettingDesc::Write(const void *object, const std::string &str) const
|
||||
{
|
||||
std::string *dst = reinterpret_cast<std::string *>(GetVariableAddress(object, &this->save));
|
||||
|
||||
switch (GetVarMemType(this->save.conv)) {
|
||||
case SLE_VAR_STR:
|
||||
case SLE_VAR_STRQ:
|
||||
if (str != nullptr) {
|
||||
if (this->max_length != 0 && strlen(str) >= this->max_length) {
|
||||
/* In case a maximum length is imposed by the setting, the length
|
||||
* includes the '\0' termination for network transfer purposes.
|
||||
* Also ensure the string is valid after chopping of some bytes. */
|
||||
std::string stdstr(str, this->max_length - 1);
|
||||
dst->assign(str_validate(stdstr, SVS_NONE));
|
||||
} else {
|
||||
dst->assign(str);
|
||||
}
|
||||
} else {
|
||||
dst->clear();
|
||||
}
|
||||
break;
|
||||
|
||||
default: NOT_REACHED();
|
||||
}
|
||||
reinterpret_cast<std::string *>(GetVariableAddress(object, &this->save))->assign(str);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -553,8 +550,9 @@ void IntSettingDesc::ParseValue(const IniItem *item, void *object) const
|
||||
|
||||
void StringSettingDesc::ParseValue(const IniItem *item, void *object) const
|
||||
{
|
||||
const char *str = (item == nullptr) ? this->def : item->value.has_value() ? item->value->c_str() : nullptr;
|
||||
this->Write_ValidateSetting(object, str);
|
||||
std::string str = (item == nullptr) ? this->def : item->value.value_or("");
|
||||
this->MakeValueValid(str);
|
||||
this->Write(object, str);
|
||||
}
|
||||
|
||||
void ListSettingDesc::ParseValue(const IniItem *item, void *object) const
|
||||
@ -2026,12 +2024,12 @@ uint GetCompanySettingIndex(const char *name)
|
||||
* @param force_newgame force the newgame settings
|
||||
* @note Strings WILL NOT be synced over the network
|
||||
*/
|
||||
bool SetSettingValue(const StringSettingDesc *sd, const char *value, bool force_newgame)
|
||||
bool SetSettingValue(const StringSettingDesc *sd, std::string value, bool force_newgame)
|
||||
{
|
||||
assert(sd->save.conv & SLF_NO_NETWORK_SYNC);
|
||||
|
||||
if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ && strcmp(value, "(null)") == 0) {
|
||||
value = nullptr;
|
||||
if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ && value.compare("(null)") == 0) {
|
||||
value.clear();
|
||||
}
|
||||
|
||||
const void *object = (_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game;
|
||||
@ -2045,9 +2043,10 @@ bool SetSettingValue(const StringSettingDesc *sd, const char *value, bool force_
|
||||
* @param object The object the setting is in.
|
||||
* @param newval The new value for the setting.
|
||||
*/
|
||||
void StringSettingDesc::ChangeValue(const void *object, const char *newval) const
|
||||
void StringSettingDesc::ChangeValue(const void *object, std::string &newval) const
|
||||
{
|
||||
this->Write_ValidateSetting(object, newval);
|
||||
this->MakeValueValid(newval);
|
||||
this->Write(object, newval);
|
||||
if (this->proc != nullptr) this->proc(0);
|
||||
|
||||
if (_save_config) SaveToConfig();
|
||||
|
@ -210,21 +210,25 @@ struct ManyOfManySettingDesc : OneOfManySettingDesc {
|
||||
struct StringSettingDesc : SettingDesc {
|
||||
StringSettingDesc(SaveLoad save, const char *name, SettingGuiFlag flags, bool startup, const char *def,
|
||||
uint32 max_length, OnChange proc) :
|
||||
SettingDesc(save, name, flags, startup), def(def), max_length(max_length), proc(proc) {}
|
||||
SettingDesc(save, name, flags, startup), def(def == nullptr ? "" : def), max_length(max_length),
|
||||
proc(proc) {}
|
||||
virtual ~StringSettingDesc() {}
|
||||
|
||||
const char *def; ///< default value given when none is present
|
||||
std::string def; ///< default value given when none is present
|
||||
uint32 max_length; ///< maximum length of the string, 0 means no maximum length
|
||||
OnChange *proc; ///< callback procedure for when the value is changed
|
||||
|
||||
bool IsStringSetting() const override { return true; }
|
||||
void ChangeValue(const void *object, const char *newval) const;
|
||||
void Write_ValidateSetting(const void *object, const char *str) const;
|
||||
void ChangeValue(const void *object, std::string &newval) const;
|
||||
|
||||
void FormatValue(char *buf, const char *last, const void *object) const override;
|
||||
void ParseValue(const IniItem *item, void *object) const override;
|
||||
bool IsSameValue(const IniItem *item, void *object) const override;
|
||||
const std::string &Read(const void *object) const;
|
||||
|
||||
private:
|
||||
void MakeValueValid(std::string &str) const;
|
||||
void Write(const void *object, const std::string &str) const;
|
||||
};
|
||||
|
||||
/** List/array settings. */
|
||||
@ -255,7 +259,7 @@ typedef std::initializer_list<std::unique_ptr<const SettingDesc>> SettingTable;
|
||||
|
||||
const SettingDesc *GetSettingFromName(const char *name);
|
||||
bool SetSettingValue(const IntSettingDesc *sd, int32 value, bool force_newgame = false);
|
||||
bool SetSettingValue(const StringSettingDesc *sd, const char *value, bool force_newgame = false);
|
||||
bool SetSettingValue(const StringSettingDesc *sd, const std::string value, bool force_newgame = false);
|
||||
uint GetSettingIndex(const SettingDesc *sd);
|
||||
|
||||
#endif /* SETTINGS_INTERNAL_H */
|
||||
|
Loading…
Reference in New Issue
Block a user