Return end of string pointer in SettingDesc formatters

This commit is contained in:
Jonathan G Rennison 2024-01-10 23:19:36 +00:00
parent b42beecb0d
commit 2ec4b2a8ca
2 changed files with 27 additions and 22 deletions

View File

@ -393,8 +393,9 @@ static bool LoadIntList(const char *str, void *array, int nelems, VarType type)
* @param array pointer to the integer-arrays that is read from
* @param nelems the number of elements the array holds.
* @param type the type of elements the array holds (eg INT8, UINT16, etc.)
* @return The pointer to the terminating null-character in the destination buffer
*/
void ListSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
char *ListSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
{
const byte *p = static_cast<const byte *>(GetVariableAddress(object, this->save));
int i, v = 0;
@ -416,6 +417,7 @@ void ListSettingDesc::FormatValue(char *buf, const char *last, const void *objec
buf += seprintf(buf, last, (i == 0) ? "%u" : ",%u", v);
}
}
return buf;
}
char *OneOfManySettingDesc::FormatSingleValue(char *buf, const char *last, uint id) const
@ -426,17 +428,17 @@ char *OneOfManySettingDesc::FormatSingleValue(char *buf, const char *last, uint
return strecpy(buf, this->many[id].c_str(), last);
}
void OneOfManySettingDesc::FormatIntValue(char *buf, const char *last, uint32_t value) const
char *OneOfManySettingDesc::FormatIntValue(char *buf, const char *last, uint32_t value) const
{
this->FormatSingleValue(buf, last, value);
return this->FormatSingleValue(buf, last, value);
}
void ManyOfManySettingDesc::FormatIntValue(char *buf, const char *last, uint32_t value) const
char *ManyOfManySettingDesc::FormatIntValue(char *buf, const char *last, uint32_t value) const
{
uint bitmask = (uint)value;
if (bitmask == 0) {
buf[0] = '\0';
return;
return buf;
}
bool first = true;
for (uint id : SetBitIterator(bitmask)) {
@ -444,6 +446,7 @@ void ManyOfManySettingDesc::FormatIntValue(char *buf, const char *last, uint32_t
buf = this->FormatSingleValue(buf, last, id);
first = false;
}
return buf;
}
/**
@ -808,20 +811,20 @@ static void IniSaveSettings(IniFile &ini, const SettingTable &settings_table, co
}
}
void IntSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
char *IntSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
{
uint32_t i = (uint32_t)this->Read(object);
this->FormatIntValue(buf, last, i);
return this->FormatIntValue(buf, last, i);
}
void IntSettingDesc::FormatIntValue(char *buf, const char *last, uint32_t value) const
char *IntSettingDesc::FormatIntValue(char *buf, const char *last, uint32_t value) const
{
seprintf(buf, last, IsSignedVarMemType(this->save.conv) ? "%d" : "%u", value);
return buf + seprintf(buf, last, IsSignedVarMemType(this->save.conv) ? "%d" : "%u", value);
}
void BoolSettingDesc::FormatIntValue(char *buf, const char *last, uint32_t value) const
char *BoolSettingDesc::FormatIntValue(char *buf, const char *last, uint32_t value) const
{
strecpy(buf, (value != 0) ? "true" : "false", last);
return strecpy(buf, (value != 0) ? "true" : "false", last);
}
bool IntSettingDesc::IsSameValue(const IniItem *item, void *object) const
@ -837,7 +840,7 @@ bool IntSettingDesc::IsDefaultValue(void *object) const
return this->def == object_value;
}
void StringSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
char *StringSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
{
const std::string &str = this->Read(object);
switch (GetVarMemType(this->save.conv)) {
@ -847,12 +850,13 @@ void StringSettingDesc::FormatValue(char *buf, const char *last, const void *obj
if (str.empty()) {
buf[0] = '\0';
} else {
seprintf(buf, last, "\"%s\"", str.c_str());
buf += seprintf(buf, last, "\"%s\"", str.c_str());
}
break;
default: NOT_REACHED();
}
return buf;
}
bool StringSettingDesc::IsSameValue(const IniItem *item, void *object) const

View File

@ -145,8 +145,9 @@ struct SettingDesc {
* @param buf The before of the buffer to format into.
* @param last The end of the buffer to format into.
* @param object The object the setting is in.
* @return The pointer to the terminating null-character in the destination buffer
*/
virtual void FormatValue(char *buf, const char *last, const void *object) const = 0;
virtual char *FormatValue(char *buf, const char *last, const void *object) const = 0;
/**
* Parse/read the value from the Ini item into the setting associated with this object.
@ -223,8 +224,8 @@ struct IntSettingDesc : SettingDesc {
void MakeValueValidAndWrite(const void *object, int32_t value) const;
virtual size_t ParseValue(const char *str) const;
void FormatValue(char *buf, const char *last, const void *object) const override;
virtual void FormatIntValue(char *buf, const char *last, uint32_t value) const;
char *FormatValue(char *buf, const char *last, const void *object) const override;
virtual char *FormatIntValue(char *buf, const char *last, uint32_t value) const;
void ParseValue(const IniItem *item, void *object) const override;
bool IsSameValue(const IniItem *item, void *object) const override;
bool IsDefaultValue(void *object) const override;
@ -246,7 +247,7 @@ struct BoolSettingDesc : IntSettingDesc {
bool IsBoolSetting() const override { return true; }
size_t ParseValue(const char *str) const override;
void FormatIntValue(char *buf, const char *last, uint32_t value) const override;
char *FormatIntValue(char *buf, const char *last, uint32_t value) const override;
};
/** One of many setting. */
@ -269,7 +270,7 @@ struct OneOfManySettingDesc : IntSettingDesc {
char *FormatSingleValue(char *buf, const char *last, uint id) const;
size_t ParseValue(const char *str) const override;
void FormatIntValue(char *buf, const char *last, uint32_t value) const override;
char *FormatIntValue(char *buf, const char *last, uint32_t value) const override;
};
/** Many of many setting. */
@ -282,7 +283,7 @@ struct ManyOfManySettingDesc : OneOfManySettingDesc {
str_val, cat, pre_check, post_callback, many, many_cnvt) {}
size_t ParseValue(const char *str) const override;
void FormatIntValue(char *buf, const char *last, uint32_t value) const override;
char *FormatIntValue(char *buf, const char *last, uint32_t value) const override;
};
/** String settings. */
@ -315,7 +316,7 @@ struct StringSettingDesc : SettingDesc {
bool IsStringSetting() const override { return true; }
void ChangeValue(const void *object, std::string &newval, SaveToConfigFlags ini_save_flags) const;
void FormatValue(char *buf, const char *last, const void *object) const override;
char *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;
bool IsDefaultValue(void *object) const override;
@ -333,7 +334,7 @@ struct ListSettingDesc : SettingDesc {
const char *def; ///< default value given when none is present
void FormatValue(char *buf, const char *last, const void *object) const override;
char *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;
bool IsDefaultValue(void *object) const override;
@ -346,7 +347,7 @@ struct NullSettingDesc : SettingDesc {
NullSettingDesc(const SaveLoad &save, const char *name, const char *patx_name) :
SettingDesc(save, name, SF_NOT_IN_CONFIG, nullptr, false, patx_name) {}
void FormatValue(char *buf, const char *last, const void *object) const override { NOT_REACHED(); }
char *FormatValue(char *buf, const char *last, const void *object) const override { NOT_REACHED(); }
void ParseValue(const IniItem *item, void *object) const override { NOT_REACHED(); }
bool IsSameValue(const IniItem *item, void *object) const override { NOT_REACHED(); }
bool IsDefaultValue(void *object) const override { NOT_REACHED(); }