1
0
Fork 0

Codechange: writing and string validation to its own functions

pull/9112/head
rubidium42 2021-04-27 18:24:33 +02:00 committed by rubidium42
parent b54d8a49fb
commit 0e449f20dc
1 changed files with 57 additions and 34 deletions

View File

@ -495,6 +495,54 @@ static void Write_ValidateSetting(void *ptr, const SettingDesc *sd, int32 val)
WriteValue(ptr, sd->save.conv, (int64)val); WriteValue(ptr, sd->save.conv, (int64)val);
} }
/**
* Set the string value of a setting.
* @param ptr Pointer to the storage location (might be a pointer to a pointer).
* @param sld Pointer to the information for the conversions and limitations to apply.
* @param p The string to save.
*/
static void Write_ValidateString(void *ptr, const SaveLoad *sld, const char *p)
{
switch (GetVarMemType(sld->conv)) {
case SLE_VAR_STRB:
case SLE_VAR_STRBQ:
if (p != nullptr) strecpy((char*)ptr, (const char*)p, (char*)ptr + sld->length - 1);
break;
case SLE_VAR_STR:
case SLE_VAR_STRQ:
free(*(char**)ptr);
*(char**)ptr = p == nullptr ? nullptr : stredup(p);
break;
default: NOT_REACHED();
}
}
/**
* Set the string value of a setting.
* @param ptr Pointer to the std::string.
* @param sld Pointer to the information for the conversions and limitations to apply.
* @param p The string to save.
*/
static void Write_ValidateStdString(void *ptr, const SaveLoad *sld, const char *p)
{
std::string *dst = reinterpret_cast<std::string *>(ptr);
switch (GetVarMemType(sld->conv)) {
case SLE_VAR_STR:
case SLE_VAR_STRQ:
if (p != nullptr) {
dst->assign(p);
} else {
dst->clear();
}
break;
default: NOT_REACHED();
}
}
/** /**
* Load values from a group of an IniFile structure into the internal representation * Load values from a group of an IniFile structure into the internal representation
* @param ini pointer to IniFile structure that holds administrative information * @param ini pointer to IniFile structure that holds administrative information
@ -551,36 +599,11 @@ static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grp
break; break;
case SDT_STRING: case SDT_STRING:
switch (GetVarMemType(sld->conv)) { Write_ValidateString(ptr, sld, (const char *)p);
case SLE_VAR_STRB:
case SLE_VAR_STRBQ:
if (p != nullptr) strecpy((char*)ptr, (const char*)p, (char*)ptr + sld->length - 1);
break;
case SLE_VAR_STR:
case SLE_VAR_STRQ:
free(*(char**)ptr);
*(char**)ptr = p == nullptr ? nullptr : stredup((const char*)p);
break;
default: NOT_REACHED();
}
break; break;
case SDT_STDSTRING: case SDT_STDSTRING:
switch (GetVarMemType(sld->conv)) { Write_ValidateStdString(ptr, sld, (const char *)p);
case SLE_VAR_STR:
case SLE_VAR_STRQ:
if (p != nullptr) {
reinterpret_cast<std::string *>(ptr)->assign((const char *)p);
} else {
reinterpret_cast<std::string *>(ptr)->clear();
}
break;
default: NOT_REACHED();
}
break; break;
case SDT_INTLIST: { case SDT_INTLIST: {
@ -2082,13 +2105,13 @@ bool SetSettingValue(uint index, const char *value, bool force_newgame)
const SettingDesc *sd = &_settings[index]; const SettingDesc *sd = &_settings[index];
assert(sd->save.conv & SLF_NO_NETWORK_SYNC); assert(sd->save.conv & SLF_NO_NETWORK_SYNC);
if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) { if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ && strcmp(value, "(null)") == 0) {
char **var = (char**)GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save); value = nullptr;
free(*var); }
*var = strcmp(value, "(null)") == 0 ? nullptr : stredup(value);
} else { void *ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
char *var = (char*)GetVariableAddress(nullptr, &sd->save); if (sd->desc.cmd == SDT_STRING) {
strecpy(var, value, &var[sd->save.length - 1]); Write_ValidateString(ptr, &sd->save, value);
} }
if (sd->desc.proc != nullptr) sd->desc.proc(0); if (sd->desc.proc != nullptr) sd->desc.proc(0);