Codechange: Use std::string in INI file parsing.

This commit is contained in:
Michael Lutz
2020-05-17 23:32:03 +02:00
parent 8aef14386f
commit 715aa67a9c
11 changed files with 179 additions and 168 deletions

View File

@@ -505,10 +505,6 @@ static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grp
{
IniGroup *group;
IniGroup *group_def = ini->GetGroup(grpname);
IniItem *item;
const void *p;
void *ptr;
const char *s;
for (; sd->save.cmd != SL_END; sd++) {
const SettingDescBase *sdb = &sd->desc;
@@ -517,30 +513,30 @@ static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grp
if (!SlIsObjectCurrentlyValid(sld->version_from, sld->version_to)) continue;
/* For settings.xx.yy load the settings from [xx] yy = ? */
s = strchr(sdb->name, '.');
if (s != nullptr) {
group = ini->GetGroup(sdb->name, s - sdb->name);
s++;
std::string s{ sdb->name };
auto sc = s.find('.');
if (sc != std::string::npos) {
group = ini->GetGroup(s.substr(0, sc));
s = s.substr(sc + 1);
} else {
s = sdb->name;
group = group_def;
}
item = group->GetItem(s, false);
IniItem *item = group->GetItem(s, false);
if (item == nullptr && group != group_def) {
/* For settings.xx.yy load the settings from [settingss] yy = ? in case the previous
/* For settings.xx.yy load the settings from [settings] yy = ? in case the previous
* did not exist (e.g. loading old config files with a [settings] section */
item = group_def->GetItem(s, false);
}
if (item == nullptr) {
/* For settings.xx.zz.yy load the settings from [zz] yy = ? in case the previous
* did not exist (e.g. loading old config files with a [yapf] section */
const char *sc = strchr(s, '.');
if (sc != nullptr) item = ini->GetGroup(s, sc - s)->GetItem(sc + 1, false);
sc = s.find('.');
if (sc != std::string::npos) item = ini->GetGroup(s.substr(0, sc))->GetItem(s.substr(sc + 1), false);
}
p = (item == nullptr) ? sdb->def : StringToVal(sdb, item->value);
ptr = GetVariableAddress(object, sld);
const void *p = (item == nullptr) ? sdb->def : StringToVal(sdb, item->value.has_value() ? item->value->c_str() : nullptr);
void *ptr = GetVariableAddress(object, sld);
switch (sdb->cmd) {
case SDT_BOOLX: // All four are various types of (integer) numbers
@@ -604,7 +600,6 @@ static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grp
IniGroup *group_def = nullptr, *group;
IniItem *item;
char buf[512];
const char *s;
void *ptr;
for (; sd->save.cmd != SL_END; sd++) {
@@ -617,22 +612,22 @@ static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grp
if (sld->conv & SLF_NOT_IN_CONFIG) continue;
/* XXX - wtf is this?? (group override?) */
s = strchr(sdb->name, '.');
if (s != nullptr) {
group = ini->GetGroup(sdb->name, s - sdb->name);
s++;
std::string s{ sdb->name };
auto sc = s.find('.');
if (sc != std::string::npos) {
group = ini->GetGroup(s.substr(0, sc));
s = s.substr(sc + 1);
} else {
if (group_def == nullptr) group_def = ini->GetGroup(grpname);
s = sdb->name;
group = group_def;
}
item = group->GetItem(s, true);
ptr = GetVariableAddress(object, sld);
if (item->value != nullptr) {
if (item->value.has_value()) {
/* check if the value is the same as the old value */
const void *p = StringToVal(sdb, item->value);
const void *p = StringToVal(sdb, item->value->c_str());
/* The main type of a variable/setting is in bytes 8-15
* The subtype (what kind of numbers do we have there) is in 0-7 */
@@ -714,8 +709,7 @@ static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grp
}
/* The value is different, that means we have to write it to the ini */
free(item->value);
item->value = stredup(buf);
item->value.emplace(buf);
}
}
@@ -737,7 +731,7 @@ static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList &li
list.clear();
for (const IniItem *item = group->item; item != nullptr; item = item->next) {
if (item->name != nullptr) list.emplace_back(item->name);
if (!item->name.empty()) list.push_back(item->name);
}
}
@@ -1427,14 +1421,14 @@ static void AILoadConfig(IniFile *ini, const char *grpname)
for (item = group->item; c < MAX_COMPANIES && item != nullptr; c++, item = item->next) {
AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
config->Change(item->name);
config->Change(item->name.c_str());
if (!config->HasScript()) {
if (strcmp(item->name, "none") != 0) {
DEBUG(script, 0, "The AI by the name '%s' was no longer found, and removed from the list.", item->name);
if (item->name != "none") {
DEBUG(script, 0, "The AI by the name '%s' was no longer found, and removed from the list.", item->name.c_str());
continue;
}
}
if (item->value != nullptr) config->StringToSettings(item->value);
if (item->value.has_value()) config->StringToSettings(item->value->c_str());
}
}
@@ -1454,14 +1448,14 @@ static void GameLoadConfig(IniFile *ini, const char *grpname)
GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME);
config->Change(item->name);
config->Change(item->name.c_str());
if (!config->HasScript()) {
if (strcmp(item->name, "none") != 0) {
DEBUG(script, 0, "The GameScript by the name '%s' was no longer found, and removed from the list.", item->name);
if (item->name != "none") {
DEBUG(script, 0, "The GameScript by the name '%s' was no longer found, and removed from the list.", item->name.c_str());
return;
}
}
if (item->value != nullptr) config->StringToSettings(item->value);
if (item->value.has_value()) config->StringToSettings(item->value->c_str());
}
/**
@@ -1485,7 +1479,7 @@ static int DecodeHexNibble(char c)
* @param dest_size Number of bytes in \a dest.
* @return Whether reading was successful.
*/
static bool DecodeHexText(char *pos, uint8 *dest, size_t dest_size)
static bool DecodeHexText(const char *pos, uint8 *dest, size_t dest_size)
{
while (dest_size > 0) {
int hi = DecodeHexNibble(pos[0]);
@@ -1517,7 +1511,7 @@ static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_stati
GRFConfig *c = nullptr;
uint8 grfid_buf[4], md5sum[16];
char *filename = item->name;
const char *filename = item->name.c_str();
bool has_grfid = false;
bool has_md5sum = false;
@@ -1541,8 +1535,8 @@ static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_stati
if (c == nullptr) c = new GRFConfig(filename);
/* Parse parameters */
if (!StrEmpty(item->value)) {
int count = ParseIntList(item->value, c->param, lengthof(c->param));
if (item->value.has_value() && !item->value->empty()) {
int count = ParseIntList(item->value->c_str(), c->param, lengthof(c->param));
if (count < 0) {
SetDParamStr(0, filename);
ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY, WL_CRITICAL);
@@ -1565,7 +1559,7 @@ static GRFConfig *GRFLoadConfig(IniFile *ini, const char *grpname, bool is_stati
SetDParam(1, STR_CONFIG_ERROR_INVALID_GRF_UNKNOWN);
}
SetDParamStr(0, StrEmpty(filename) ? item->name : filename);
SetDParamStr(0, StrEmpty(filename) ? item->name.c_str() : filename);
ShowErrorMessage(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_GRF, WL_CRITICAL);
delete c;
continue;
@@ -1776,8 +1770,8 @@ StringList GetGRFPresetList()
std::unique_ptr<IniFile> ini(IniLoadConfig());
for (IniGroup *group = ini->group; group != nullptr; group = group->next) {
if (strncmp(group->name, "preset-", 7) == 0) {
list.emplace_back(group->name + 7);
if (group->name.compare(0, 7, "preset-") == 0) {
list.push_back(group->name.substr(7));
}
}