Skip to content

Commit

Permalink
Codechange: clean up C-string support from settings
Browse files Browse the repository at this point in the history
  • Loading branch information
rubidium42 authored and pull[bot] committed Feb 2, 2024
1 parent d3546f8 commit 1008128
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 79 deletions.
4 changes: 0 additions & 4 deletions src/saveload/saveload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,6 @@ static inline uint SlCalcConvMemLen(VarType conv)

switch (length << 4) {
case SLE_VAR_STRB:
case SLE_VAR_STRBQ:
case SLE_VAR_STR:
case SLE_VAR_STRQ:
return SlReadArrayLength();
Expand Down Expand Up @@ -881,7 +880,6 @@ static inline size_t SlCalcStringLen(const void *ptr, size_t length, VarType con
len = SIZE_MAX;
break;
case SLE_VAR_STRB:
case SLE_VAR_STRBQ:
str = (const char *)ptr;
len = length;
break;
Expand Down Expand Up @@ -920,7 +918,6 @@ static void SlString(void *ptr, size_t length, VarType conv)
switch (GetVarMemType(conv)) {
default: NOT_REACHED();
case SLE_VAR_STRB:
case SLE_VAR_STRBQ:
len = SlCalcNetStringLen((char *)ptr, length);
break;
case SLE_VAR_STR:
Expand All @@ -941,7 +938,6 @@ static void SlString(void *ptr, size_t length, VarType conv)
switch (GetVarMemType(conv)) {
default: NOT_REACHED();
case SLE_VAR_STRB:
case SLE_VAR_STRBQ:
if (len >= length) {
DEBUG(sl, 1, "String length in savegame is bigger than buffer, truncating");
SlCopyBytes(ptr, length);
Expand Down
3 changes: 0 additions & 3 deletions src/saveload/saveload.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,6 @@ enum VarTypes {
SLE_VAR_U64 = 8 << 4,
SLE_VAR_NULL = 9 << 4, ///< useful to write zeros in savegame.
SLE_VAR_STRB = 10 << 4, ///< string (with pre-allocated buffer)
SLE_VAR_STRBQ = 11 << 4, ///< string enclosed in quotes (with pre-allocated buffer)
SLE_VAR_STR = 12 << 4, ///< string pointer
SLE_VAR_STRQ = 13 << 4, ///< string pointer enclosed in quotes
SLE_VAR_NAME = 14 << 4, ///< old custom name to be converted to a char pointer
Expand All @@ -470,7 +469,6 @@ enum VarTypes {
SLE_CHAR = SLE_FILE_I8 | SLE_VAR_CHAR,
SLE_STRINGID = SLE_FILE_STRINGID | SLE_VAR_U32,
SLE_STRINGBUF = SLE_FILE_STRING | SLE_VAR_STRB,
SLE_STRINGBQUOTE = SLE_FILE_STRING | SLE_VAR_STRBQ,
SLE_STRING = SLE_FILE_STRING | SLE_VAR_STR,
SLE_STRINGQUOTE = SLE_FILE_STRING | SLE_VAR_STRQ,
SLE_NAME = SLE_FILE_STRINGID | SLE_VAR_NAME,
Expand All @@ -479,7 +477,6 @@ enum VarTypes {
SLE_UINT = SLE_UINT32,
SLE_INT = SLE_INT32,
SLE_STRB = SLE_STRINGBUF,
SLE_STRBQ = SLE_STRINGBQUOTE,
SLE_STR = SLE_STRING,
SLE_STRQ = SLE_STRINGQUOTE,

Expand Down
2 changes: 1 addition & 1 deletion src/script/api/script_gamesettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{
uint i;
const SettingDesc *sd = GetSettingFromName(setting, &i);
return sd != nullptr && sd->desc.cmd != SDT_STRING;
return sd != nullptr && sd->desc.cmd != SDT_STDSTRING;
}

/* static */ int32 ScriptGameSettings::GetValue(const char *setting)
Expand Down
68 changes: 4 additions & 64 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,7 @@ static const void *StringToVal(const SettingDescBase *desc, const char *orig_str
return desc->def;
}

case SDT_STDSTRING:
case SDT_STRING: return orig_str;
case SDT_STDSTRING: return orig_str;
case SDT_INTLIST: return str;
default: break;
}
Expand Down Expand Up @@ -495,35 +494,6 @@ static void Write_ValidateSetting(void *ptr, const SettingDesc *sd, int32 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) {
char *begin = (char*)ptr;
char *end = begin + sld->length - 1;
strecpy(begin, p, end);
str_validate(begin, end, SVS_NONE);
}
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.
Expand Down Expand Up @@ -611,10 +581,6 @@ static void IniLoadSettings(IniFile *ini, const SettingDesc *sd, const char *grp
Write_ValidateSetting(ptr, sd, (int32)(size_t)p);
break;

case SDT_STRING:
Write_ValidateString(ptr, sld, (const char *)p);
break;

case SDT_STDSTRING:
Write_ValidateStdString(ptr, sd, (const char *)p);
break;
Expand Down Expand Up @@ -736,24 +702,6 @@ static void IniSaveSettings(IniFile *ini, const SettingDesc *sd, const char *grp
break;
}

case SDT_STRING:
switch (GetVarMemType(sld->conv)) {
case SLE_VAR_STRB: strecpy(buf, (char*)ptr, lastof(buf)); break;
case SLE_VAR_STRBQ:seprintf(buf, lastof(buf), "\"%s\"", (char*)ptr); break;
case SLE_VAR_STR: strecpy(buf, *(char**)ptr, lastof(buf)); break;

case SLE_VAR_STRQ:
if (*(char**)ptr == nullptr) {
buf[0] = '\0';
} else {
seprintf(buf, lastof(buf), "\"%s\"", *(char**)ptr);
}
break;

default: NOT_REACHED();
}
break;

case SDT_STDSTRING:
switch (GetVarMemType(sld->conv)) {
case SLE_VAR_STR: strecpy(buf, reinterpret_cast<std::string *>(ptr)->c_str(), lastof(buf)); break;
Expand Down Expand Up @@ -2123,11 +2071,7 @@ bool SetSettingValue(uint index, const char *value, bool force_newgame)
}

void *ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
if (sd->desc.cmd == SDT_STRING) {
Write_ValidateString(ptr, &sd->save, value);
} else {
Write_ValidateStdString(ptr, sd, value);
}
Write_ValidateStdString(ptr, sd, value);
if (sd->desc.proc != nullptr) sd->desc.proc(0);

if (_save_config) SaveToConfig();
Expand Down Expand Up @@ -2184,7 +2128,7 @@ void IConsoleSetSetting(const char *name, const char *value, bool force_newgame)
}

bool success;
if (sd->desc.cmd == SDT_STRING || sd->desc.cmd == SDT_STDSTRING) {
if (sd->desc.cmd == SDT_STDSTRING) {
success = SetSettingValue(index, value, force_newgame);
} else {
uint32 val;
Expand Down Expand Up @@ -2235,9 +2179,7 @@ void IConsoleGetSetting(const char *name, bool force_newgame)

ptr = GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);

if (sd->desc.cmd == SDT_STRING) {
IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s'", name, (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char * const *)ptr : (const char *)ptr);
} else if (sd->desc.cmd == SDT_STDSTRING) {
if (sd->desc.cmd == SDT_STDSTRING) {
IConsolePrintF(CC_WARNING, "Current value for '%s' is: '%s'", name, reinterpret_cast<const std::string *>(ptr)->c_str());
} else {
if (sd->desc.cmd == SDT_BOOLX) {
Expand Down Expand Up @@ -2268,8 +2210,6 @@ void IConsoleListSettings(const char *prefilter)

if (sd->desc.cmd == SDT_BOOLX) {
seprintf(value, lastof(value), (*(const bool *)ptr != 0) ? "on" : "off");
} else if (sd->desc.cmd == SDT_STRING) {
seprintf(value, lastof(value), "%s", (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) ? *(const char * const *)ptr : (const char *)ptr);
} else if (sd->desc.cmd == SDT_STDSTRING) {
seprintf(value, lastof(value), "%s", reinterpret_cast<const std::string *>(ptr)->c_str());
} else {
Expand Down
1 change: 0 additions & 1 deletion src/settings_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ enum SettingDescType : byte {
SDT_ONEOFMANY = 2, ///< bitmasked number where only ONE bit may be set
SDT_MANYOFMANY = 3, ///< bitmasked number where MULTIPLE bits may be set
SDT_INTLIST = 4, ///< list of integers separated by a comma ','
SDT_STRING = 5, ///< string with a pre-allocated buffer
SDT_STDSTRING = 6, ///< \c std::string
SDT_END,
/* 9 more possible primitives */
Expand Down
6 changes: 0 additions & 6 deletions src/table/settings.h.preamble
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ static size_t ConvertLandscape(const char *value);
#define SDTG_LIST(name, type, length, flags, guiflags, var, def, str, strhelp, strval, proc, from, to, cat, extra, startup)\
SDTG_GENERAL(name, SDT_INTLIST, SL_ARR, type, flags, guiflags, var, length, def, 0, 0, 0, nullptr, str, strhelp, strval, proc, from, to, cat, extra, startup)

#define SDTG_STR(name, type, flags, guiflags, var, def, str, strhelp, strval, proc, from, to, cat, extra, startup)\
SDTG_GENERAL(name, SDT_STRING, SL_STR, type, flags, guiflags, var, sizeof(var), def, 0, 0, 0, nullptr, str, strhelp, strval, proc, from, to, cat, extra, startup)

#define SDTG_SSTR(name, type, flags, guiflags, var, def, str, strhelp, strval, proc, from, to, cat, extra, startup)\
SDTG_GENERAL(name, SDT_STDSTRING, SL_STDSTR, type, flags, guiflags, var, sizeof(var), def, 0, 0, 0, nullptr, str, strhelp, strval, proc, from, to, cat, extra, startup)

Expand Down Expand Up @@ -104,9 +101,6 @@ static size_t ConvertLandscape(const char *value);
#define SDT_LIST(base, var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extra, startup)\
SDT_GENERAL(#var, SDT_INTLIST, SL_ARR, type, flags, guiflags, base, var, lengthof(((base*)8)->var), def, 0, 0, 0, nullptr, str, strhelp, strval, proc, nullptr, from, to, cat, extra, startup)

#define SDT_STR(base, var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extra, startup)\
SDT_GENERAL(#var, SDT_STRING, SL_STR, type, flags, guiflags, base, var, sizeof(((base*)8)->var), def, 0, 0, 0, nullptr, str, strhelp, strval, proc, nullptr, from, to, cat, extra, startup)

#define SDT_SSTR(base, var, type, flags, guiflags, def, str, strhelp, strval, proc, from, to, cat, extra, startup)\
SDT_GENERAL(#var, SDT_STDSTRING, SL_STDSTR, type, flags, guiflags, base, var, sizeof(((base*)8)->var), def, 0, 0, 0, nullptr, str, strhelp, strval, proc, nullptr, from, to, cat, extra, startup)

Expand Down

0 comments on commit 1008128

Please sign in to comment.