Changeset - r25284:75b6f86a0de0
[Not reviewed]
master
0 1 0
rubidium42 - 3 years ago 2021-04-27 16:24:33
rubidium@openttd.org
Codechange: writing and string validation to its own functions
1 file changed with 57 insertions and 34 deletions:
0 comments (0 inline, 0 general)
src/settings.cpp
Show inline comments
 
@@ -493,12 +493,60 @@ static void Write_ValidateSetting(void *
 
	}
 

	
 
	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
 
 * @param ini pointer to IniFile structure that holds administrative information
 
 * @param sd pointer to SettingDesc structure whose internally pointed variables will
 
 *        be given values
 
 * @param grpname the group of the IniFile to search in for the new values
 
 * @param object pointer to the object been loaded
 
@@ -548,42 +596,17 @@ static void IniLoadSettings(IniFile *ini
 
			case SDT_ONEOFMANY:
 
			case SDT_MANYOFMANY:
 
				Write_ValidateSetting(ptr, sd, (int32)(size_t)p);
 
				break;
 

	
 
			case SDT_STRING:
 
				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((const char*)p);
 
						break;
 

	
 
					default: NOT_REACHED();
 
				}
 
				Write_ValidateString(ptr, sld, (const char *)p);
 
				break;
 

	
 
			case SDT_STDSTRING:
 
				switch (GetVarMemType(sld->conv)) {
 
					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();
 
				}
 

	
 
				Write_ValidateStdString(ptr, sld, (const char *)p);
 
				break;
 

	
 
			case SDT_INTLIST: {
 
				if (!LoadIntList((const char*)p, ptr, sld->length, GetVarMemType(sld->conv))) {
 
					ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_ARRAY);
 
					msg.SetDParamStr(0, sdb->name);
 
@@ -2079,19 +2102,19 @@ uint GetCompanySettingIndex(const char *
 
 */
 
bool SetSettingValue(uint index, const char *value, bool force_newgame)
 
{
 
	const SettingDesc *sd = &_settings[index];
 
	assert(sd->save.conv & SLF_NO_NETWORK_SYNC);
 

	
 
	if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ) {
 
		char **var = (char**)GetVariableAddress((_game_mode == GM_MENU || force_newgame) ? &_settings_newgame : &_settings_game, &sd->save);
 
		free(*var);
 
		*var = strcmp(value, "(null)") == 0 ? nullptr : stredup(value);
 
	} else {
 
		char *var = (char*)GetVariableAddress(nullptr, &sd->save);
 
		strecpy(var, value, &var[sd->save.length - 1]);
 
	if (GetVarMemType(sd->save.conv) == SLE_VAR_STRQ && strcmp(value, "(null)") == 0) {
 
		value = nullptr;
 
	}
 

	
 
	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);
 
	}
 
	if (sd->desc.proc != nullptr) sd->desc.proc(0);
 

	
 
	if (_save_config) SaveToConfig();
 
	return true;
 
}
0 comments (0 inline, 0 general)