Changeset - r25460:8baaaba1929c
[Not reviewed]
master
0 2 0
rubidium42 - 4 years ago 2021-04-27 18:32:35
rubidium@openttd.org
Change: further support for std::string in settings
2 files changed with 21 insertions and 7 deletions:
0 comments (0 inline, 0 general)
src/network/network_gui.cpp
Show inline comments
 
@@ -645,13 +645,13 @@ public:
 
			y += FONT_HEIGHT_NORMAL;
 

	
 
			SetDParamStr(0, sel->info.server_revision);
 
			DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_VERSION); // server version
 
			y += FONT_HEIGHT_NORMAL;
 

	
 
			SetDParamStr(0, sel->connection_string.c_str());
 
			SetDParamStr(0, sel->connection_string);
 
			DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_SERVER_ADDRESS); // server address
 
			y += FONT_HEIGHT_NORMAL;
 

	
 
			SetDParam(0, sel->info.start_date);
 
			DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_NETWORK_SERVER_LIST_START_DATE); // start date
 
			y += FONT_HEIGHT_NORMAL;
src/settings.cpp
Show inline comments
 
@@ -524,24 +524,32 @@ static void Write_ValidateString(void *p
 
	}
 
}
 

	
 
/**
 
 * 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 sd  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)
 
static void Write_ValidateStdString(void *ptr, const SettingDesc *sd, const char *p)
 
{
 
	std::string *dst = reinterpret_cast<std::string *>(ptr);
 

	
 
	switch (GetVarMemType(sld->conv)) {
 
	switch (GetVarMemType(sd->save.conv)) {
 
		case SLE_VAR_STR:
 
		case SLE_VAR_STRQ:
 
			if (p != nullptr) {
 
				dst->assign(p);
 
				if (sd->desc.max != 0 && strlen(p) >= sd->desc.max) {
 
					/* In case a maximum length is imposed by the setting, the length
 
					 * includes the '\0' termination for network transfer purposes.
 
					 * Also ensure the string is valid after chopping of some bytes. */
 
					std::string str(p, sd->desc.max - 1);
 
					dst->assign(str_validate(str, SVS_NONE));
 
				} else {
 
					dst->assign(p);
 
				}
 
			} else {
 
				dst->clear();
 
			}
 
			break;
 

	
 
		default: NOT_REACHED();
 
@@ -605,13 +613,13 @@ static void IniLoadSettings(IniFile *ini
 

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

	
 
			case SDT_STDSTRING:
 
				Write_ValidateStdString(ptr, sld, (const char *)p);
 
				Write_ValidateStdString(ptr, sd, (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);
 
@@ -2116,12 +2124,14 @@ bool SetSettingValue(uint index, const c
 
		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);
 
	} else {
 
		Write_ValidateStdString(ptr, sd, value);
 
	}
 
	if (sd->desc.proc != nullptr) sd->desc.proc(0);
 

	
 
	if (_save_config) SaveToConfig();
 
	return true;
 
}
 
@@ -2173,13 +2183,13 @@ void IConsoleSetSetting(const char *name
 
	if (sd == nullptr) {
 
		IConsolePrintF(CC_WARNING, "'%s' is an unknown setting.", name);
 
		return;
 
	}
 

	
 
	bool success;
 
	if (sd->desc.cmd == SDT_STRING) {
 
	if (sd->desc.cmd == SDT_STRING || sd->desc.cmd == SDT_STDSTRING) {
 
		success = SetSettingValue(index, value, force_newgame);
 
	} else {
 
		uint32 val;
 
		extern bool GetArgumentInteger(uint32 *value, const char *arg);
 
		success = GetArgumentInteger(&val, value);
 
		if (!success) {
 
@@ -2226,12 +2236,14 @@ 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) {
 
		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) {
 
			seprintf(value, lastof(value), (*(const bool*)ptr != 0) ? "on" : "off");
 
		} else {
 
			seprintf(value, lastof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
 
		}
 
@@ -2257,12 +2269,14 @@ void IConsoleListSettings(const char *pr
 
		const void *ptr = GetVariableAddress(&GetGameSettings(), &sd->save);
 

	
 
		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 {
 
			seprintf(value, lastof(value), sd->desc.min < 0 ? "%d" : "%u", (int32)ReadValue(ptr, sd->save.conv));
 
		}
 
		IConsolePrintF(CC_DEFAULT, "%s = %s", sd->desc.name, value);
 
	}
 

	
0 comments (0 inline, 0 general)