Codechange: use std::string for formatting settings

This commit is contained in:
Rubidium 2023-05-24 21:35:11 +02:00 committed by rubidium42
parent 1412ea48ed
commit f4b0ac2bd4
3 changed files with 46 additions and 54 deletions

View File

@ -106,16 +106,13 @@ static auto &GenericSettingTables()
*/
static void SurveySettingsTable(nlohmann::json &survey, const SettingTable &table, void *object)
{
char buf[512];
for (auto &desc : table) {
const SettingDesc *sd = GetSettingDesc(desc);
/* Skip any old settings we no longer save/load. */
if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
auto name = sd->GetName();
sd->FormatValue(buf, lastof(buf), object);
survey[name] = buf;
survey[name] = sd->FormatValue(object);
}
}

View File

@ -318,12 +318,13 @@ static bool LoadIntList(const char *str, void *array, int nelems, VarType type)
* @param nelems the number of elements the array holds.
* @param type the type of elements the array holds (eg INT8, UINT16, etc.)
*/
void ListSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
std::string ListSettingDesc::FormatValue(const void *object) const
{
const byte *p = static_cast<const byte *>(GetVariableAddress(object, this->save));
int i, v = 0;
for (i = 0; i != this->save.length; i++) {
std::string result;
for (size_t i = 0; i != this->save.length; i++) {
int64_t v;
switch (GetVarMemType(this->save.conv)) {
case SLE_VAR_BL:
case SLE_VAR_I8: v = *(const int8 *)p; p += 1; break;
@ -334,41 +335,39 @@ void ListSettingDesc::FormatValue(char *buf, const char *last, const void *objec
case SLE_VAR_U32: v = *(const uint32 *)p; p += 4; break;
default: NOT_REACHED();
}
if (IsSignedVarMemType(this->save.conv)) {
buf += seprintf(buf, last, (i == 0) ? "%d" : ",%d", v);
} else {
buf += seprintf(buf, last, (i == 0) ? "%u" : ",%u", v);
}
if (i != 0) result += ',';
result += std::to_string(v);
}
return result;
}
char *OneOfManySettingDesc::FormatSingleValue(char *buf, const char *last, uint id) const
std::string OneOfManySettingDesc::FormatSingleValue(uint id) const
{
if (id >= this->many.size()) {
return buf + seprintf(buf, last, "%d", id);
return std::to_string(id);
}
return strecpy(buf, this->many[id].c_str(), last);
return this->many[id];
}
void OneOfManySettingDesc::FormatValue(char *buf, const char *last, const void *object) const
std::string OneOfManySettingDesc::FormatValue(const void *object) const
{
uint id = (uint)this->Read(object);
this->FormatSingleValue(buf, last, id);
return this->FormatSingleValue(id);
}
void ManyOfManySettingDesc::FormatValue(char *buf, const char *last, const void *object) const
std::string ManyOfManySettingDesc::FormatValue(const void *object) const
{
uint bitmask = (uint)this->Read(object);
if (bitmask == 0) {
buf[0] = '\0';
return;
return {};
}
bool first = true;
std::string result;
for (uint id : SetBitIterator(bitmask)) {
if (!first) buf = strecpy(buf, "|", last);
buf = this->FormatSingleValue(buf, last, id);
first = false;
if (!result.empty()) result += '|';
result += this->FormatSingleValue(id);
}
return result;
}
/**
@ -651,7 +650,6 @@ static void IniSaveSettings(IniFile &ini, const SettingTable &settings_table, co
{
IniGroup *group_def = nullptr, *group;
IniItem *item;
char buf[512];
for (auto &desc : settings_table) {
const SettingDesc *sd = GetSettingDesc(desc);
@ -674,25 +672,27 @@ static void IniSaveSettings(IniFile &ini, const SettingTable &settings_table, co
item = group->GetItem(s, true);
if (!item->value.has_value() || !sd->IsSameValue(item, object)) {
/* Value has changed, get the new value and put it into a buffer */
sd->FormatValue(buf, lastof(buf), object);
/* The value is different, that means we have to write it to the ini */
item->value.emplace(buf);
item->value.emplace(sd->FormatValue(object));
}
}
}
void IntSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
std::string IntSettingDesc::FormatValue(const void *object) const
{
uint32 i = (uint32)this->Read(object);
seprintf(buf, last, IsSignedVarMemType(this->save.conv) ? "%d" : "%u", i);
int64_t i;
if (IsSignedVarMemType(this->save.conv)) {
i = this->Read(object);
} else {
i = (uint32_t)this->Read(object);
}
return std::to_string(i);
}
void BoolSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
std::string BoolSettingDesc::FormatValue(const void *object) const
{
bool val = this->Read(object) != 0;
strecpy(buf, val ? "true" : "false", last);
return val ? "true" : "false";
}
bool IntSettingDesc::IsSameValue(const IniItem *item, void *object) const
@ -702,19 +702,17 @@ bool IntSettingDesc::IsSameValue(const IniItem *item, void *object) const
return item_value == object_value;
}
void StringSettingDesc::FormatValue(char *buf, const char *last, const void *object) const
std::string StringSettingDesc::FormatValue(const void *object) const
{
const std::string &str = this->Read(object);
switch (GetVarMemType(this->save.conv)) {
case SLE_VAR_STR: strecpy(buf, str.c_str(), last); break;
case SLE_VAR_STR: return str;
case SLE_VAR_STRQ:
if (str.empty()) {
buf[0] = '\0';
} else {
seprintf(buf, last, "\"%s\"", str.c_str());
return str;
}
break;
return fmt::format("\"{}\"", str);
default: NOT_REACHED();
}
@ -1736,8 +1734,7 @@ void IConsoleGetSetting(const char *name, bool force_newgame)
if (sd->IsStringSetting()) {
IConsolePrint(CC_INFO, "Current value for '{}' is '{}'.", sd->GetName(), sd->AsStringSetting()->Read(object));
} else if (sd->IsIntSetting()) {
char value[20];
sd->FormatValue(value, lastof(value), object);
std::string value = sd->FormatValue(object);
const IntSettingDesc *int_setting = sd->AsIntSetting();
IConsolePrint(CC_INFO, "Current value for '{}' is '{}' (min: {}{}, max: {}).",
sd->GetName(), value, (sd->flags & SF_GUI_0_IS_SPECIAL) ? "(0) " : "", int_setting->min, int_setting->max);
@ -1750,9 +1747,7 @@ static void IConsoleListSettingsTable(const SettingTable &table, const char *pre
const SettingDesc *sd = GetSettingDesc(desc);
if (!SlIsObjectCurrentlyValid(sd->save.version_from, sd->save.version_to)) continue;
if (prefilter != nullptr && sd->GetName().find(prefilter) == std::string::npos) continue;
char value[80];
sd->FormatValue(value, lastof(value), &GetGameSettings());
IConsolePrint(CC_DEFAULT, "{} = {}", sd->GetName(), value);
IConsolePrint(CC_DEFAULT, "{} = {}", sd->GetName(), sd->FormatValue(&GetGameSettings()));
}
}

View File

@ -111,7 +111,7 @@ struct SettingDesc {
* @param last The end of the buffer to format into.
* @param object The object the setting is in.
*/
virtual void FormatValue(char *buf, const char *last, const void *object) const = 0;
virtual std::string FormatValue(const void *object) const = 0;
/**
* Parse/read the value from the Ini item into the setting associated with this object.
@ -178,7 +178,7 @@ struct IntSettingDesc : SettingDesc {
void MakeValueValidAndWrite(const void *object, int32 value) const;
virtual size_t ParseValue(const char *str) const;
void FormatValue(char *buf, const char *last, const void *object) const override;
std::string FormatValue(const void *object) const override;
void ParseValue(const IniItem *item, void *object) const override;
bool IsSameValue(const IniItem *item, void *object) const override;
int32 Read(const void *object) const;
@ -198,7 +198,7 @@ struct BoolSettingDesc : IntSettingDesc {
bool IsBoolSetting() const override { return true; }
size_t ParseValue(const char *str) const override;
void FormatValue(char *buf, const char *last, const void *object) const override;
std::string FormatValue(const void *object) const override;
};
/** One of many setting. */
@ -219,10 +219,10 @@ struct OneOfManySettingDesc : IntSettingDesc {
OnConvert *many_cnvt; ///< callback procedure when loading value mechanism fails
static size_t ParseSingleValue(const char *str, size_t len, const std::vector<std::string> &many);
char *FormatSingleValue(char *buf, const char *last, uint id) const;
std::string FormatSingleValue(uint id) const;
size_t ParseValue(const char *str) const override;
void FormatValue(char *buf, const char *last, const void *object) const override;
std::string FormatValue(const void *object) const override;
};
/** Many of many setting. */
@ -235,7 +235,7 @@ struct ManyOfManySettingDesc : OneOfManySettingDesc {
str_val, cat, pre_check, post_callback, many, many_cnvt) {}
size_t ParseValue(const char *str) const override;
void FormatValue(char *buf, const char *last, const void *object) const override;
std::string FormatValue(const void *object) const override;
};
/** String settings. */
@ -268,7 +268,7 @@ struct StringSettingDesc : SettingDesc {
bool IsStringSetting() const override { return true; }
void ChangeValue(const void *object, std::string &newval) const;
void FormatValue(char *buf, const char *last, const void *object) const override;
std::string FormatValue(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;
@ -285,7 +285,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;
std::string FormatValue(const void *object) const override;
void ParseValue(const IniItem *item, void *object) const override;
bool IsSameValue(const IniItem *item, void *object) const override;
};
@ -295,7 +295,7 @@ struct NullSettingDesc : SettingDesc {
NullSettingDesc(const SaveLoad &save) :
SettingDesc(save, SF_NOT_IN_CONFIG, false) {}
void FormatValue(char *buf, const char *last, const void *object) const override { NOT_REACHED(); }
std::string FormatValue(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(); }
};