Codechange: use std::string as std::map key, instead of stredup string

This commit is contained in:
Rubidium
2023-05-05 11:01:01 +02:00
committed by rubidium42
parent 72082aa7d3
commit bc389a86c9
10 changed files with 54 additions and 111 deletions

View File

@@ -53,7 +53,7 @@ ScriptConfig::ScriptConfig(const ScriptConfig *config)
this->to_load_data.reset();
for (const auto &item : config->settings) {
this->settings[stredup(item.first)] = item.second;
this->settings[item.first] = item.second;
}
/* Virtual functions get called statically in constructors, so make it explicit to remove any confusion. */
@@ -84,9 +84,6 @@ const ScriptConfigItemList *ScriptConfig::GetConfigList()
void ScriptConfig::ClearConfigList()
{
for (const auto &item : this->settings) {
free(item.first);
}
this->settings.clear();
}
@@ -116,19 +113,11 @@ void ScriptConfig::SetSetting(const char *name, int value)
value = Clamp(value, config_item->min_value, config_item->max_value);
const auto it = this->settings.find(name);
if (it != this->settings.end()) {
(*it).second = value;
} else {
this->settings[stredup(name)] = value;
}
this->settings[name] = value;
}
void ScriptConfig::ResetSettings()
{
for (const auto &item : this->settings) {
free(item.first);
}
this->settings.clear();
}
@@ -137,14 +126,13 @@ void ScriptConfig::ResetEditableSettings(bool yet_to_start)
if (this->info == nullptr) return ResetSettings();
for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end();) {
const ScriptConfigItem *config_item = this->info->GetConfigItem(it->first);
const ScriptConfigItem *config_item = this->info->GetConfigItem(it->first.c_str());
assert(config_item != nullptr);
bool editable = yet_to_start || (config_item->flags & SCRIPTCONFIG_INGAME) != 0;
bool visible = _settings_client.gui.ai_developer_tools || (config_item->flags & SCRIPTCONFIG_DEVELOPER) == 0;
if (editable && visible) {
free(it->first);
it = this->settings.erase(it);
} else {
it++;
@@ -209,29 +197,16 @@ void ScriptConfig::StringToSettings(const std::string &value)
std::string ScriptConfig::SettingsToString() const
{
char string[1024];
char *last = lastof(string);
char *s = string;
*s = '\0';
if (this->settings.empty()) return {};
std::string result;
for (const auto &item : this->settings) {
char no[INT32_DIGITS_WITH_SIGN_AND_TERMINATION];
seprintf(no, lastof(no), "%d", item.second);
/* Check if the string would fit in the destination */
size_t needed_size = strlen(item.first) + 1 + strlen(no);
/* If it doesn't fit, skip the next settings */
if (s + needed_size > last) break;
s = strecat(s, item.first, last);
s = strecat(s, "=", last);
s = strecat(s, no, last);
s = strecat(s, ",", last);
fmt::format_to(std::back_inserter(result), "{}={},", item.first, item.second);
}
/* Remove the last ',', but only if at least one setting was saved. */
if (s != string) s[-1] = '\0';
return string;
/* Remove the last ','. */
result.resize(result.size() - 1);
return result;
}
std::optional<std::string> ScriptConfig::GetTextfile(TextfileType type, CompanyID slot) const