Changeset - r25470:c9e16b1dc2f4
[Not reviewed]
master
0 6 0
rubidium42 - 4 years ago 2021-04-28 15:38:02
rubidium@openttd.org
Codechange: clean up C-string support from settings
6 files changed with 5 insertions and 79 deletions:
0 comments (0 inline, 0 general)
src/saveload/saveload.cpp
Show inline comments
 
@@ -585,7 +585,6 @@ static inline uint SlCalcConvMemLen(VarT
 

	
 
	switch (length << 4) {
 
		case SLE_VAR_STRB:
 
		case SLE_VAR_STRBQ:
 
		case SLE_VAR_STR:
 
		case SLE_VAR_STRQ:
 
			return SlReadArrayLength();
 
@@ -881,7 +880,6 @@ static inline size_t SlCalcStringLen(con
 
			len = SIZE_MAX;
 
			break;
 
		case SLE_VAR_STRB:
 
		case SLE_VAR_STRBQ:
 
			str = (const char *)ptr;
 
			len = length;
 
			break;
 
@@ -920,7 +918,6 @@ static void SlString(void *ptr, size_t l
 
			switch (GetVarMemType(conv)) {
 
				default: NOT_REACHED();
 
				case SLE_VAR_STRB:
 
				case SLE_VAR_STRBQ:
 
					len = SlCalcNetStringLen((char *)ptr, length);
 
					break;
 
				case SLE_VAR_STR:
 
@@ -941,7 +938,6 @@ static void SlString(void *ptr, size_t l
 
			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);
src/saveload/saveload.h
Show inline comments
 
@@ -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
 
@@ -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,
 
@@ -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,
 

	
src/script/api/script_gamesettings.cpp
Show inline comments
 
@@ -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)
src/settings.cpp
Show inline comments
 
@@ -415,8 +415,7 @@ static const void *StringToVal(const Set
 
			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;
 
	}
 
@@ -497,35 +496,6 @@ static void Write_ValidateSetting(void *
 

	
 
/**
 
 * 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.
 
 * @param sd  Pointer to the information for the conversions and limitations to apply.
 
 * @param p   The string to save.
 
@@ -611,10 +581,6 @@ static void IniLoadSettings(IniFile *ini
 
				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;
 
@@ -736,24 +702,6 @@ static void IniSaveSettings(IniFile *ini
 
				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;
 
@@ -2123,11 +2071,7 @@ bool SetSettingValue(uint index, const c
 
	}
 

	
 
	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();
 
@@ -2184,7 +2128,7 @@ void IConsoleSetSetting(const char *name
 
	}
 

	
 
	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;
 
@@ -2235,9 +2179,7 @@ void IConsoleGetSetting(const char *name
 

	
 
	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) {
 
@@ -2268,8 +2210,6 @@ void IConsoleListSettings(const char *pr
 

	
 
		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 {
src/settings_internal.h
Show inline comments
 
@@ -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 */
src/table/settings.h.preamble
Show inline comments
 
@@ -73,9 +73,6 @@ static size_t ConvertLandscape(const cha
 
#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)
 

	
 
@@ -104,9 +101,6 @@ static size_t ConvertLandscape(const cha
 
#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)
 

	
0 comments (0 inline, 0 general)