Changeset - r23602:f83b6cbc2d6c
[Not reviewed]
master
0 15 0
Michael Lutz - 6 years ago 2019-04-02 19:31:33
michi@icosahedron.de
Codechange: If something is a vector of strings, use a vector of strings instead of an AutoFreeSmallVector.
15 files changed with 94 insertions and 104 deletions:
0 comments (0 inline, 0 general)
src/console_cmds.cpp
Show inline comments
 
@@ -550,25 +550,24 @@ DEF_CONSOLE_CMD(ConUnBan)
 

	
 
	if (argc != 2) return false;
 

	
 
	/* Try by IP. */
 
	uint index;
 
	for (index = 0; index < _network_ban_list.size(); index++) {
 
		if (strcmp(_network_ban_list[index], argv[1]) == 0) break;
 
		if (_network_ban_list[index] == argv[1]) break;
 
	}
 

	
 
	/* Try by index. */
 
	if (index >= _network_ban_list.size()) {
 
		index = atoi(argv[1]) - 1U; // let it wrap
 
	}
 

	
 
	if (index < _network_ban_list.size()) {
 
		char msg[64];
 
		seprintf(msg, lastof(msg), "Unbanned %s", _network_ban_list[index]);
 
		seprintf(msg, lastof(msg), "Unbanned %s", _network_ban_list[index].c_str());
 
		IConsolePrint(CC_DEFAULT, msg);
 
		free(_network_ban_list[index]);
 
		_network_ban_list.erase(_network_ban_list.begin() + index);
 
	} else {
 
		IConsolePrint(CC_DEFAULT, "Invalid list index or IP not in ban-list.");
 
		IConsolePrint(CC_DEFAULT, "For a list of banned IP's, see the command 'banlist'");
 
	}
 

	
 
@@ -582,14 +581,14 @@ DEF_CONSOLE_CMD(ConBanList)
 
		return true;
 
	}
 

	
 
	IConsolePrint(CC_DEFAULT, "Banlist: ");
 

	
 
	uint i = 1;
 
	for (char *entry : _network_ban_list) {
 
		IConsolePrintF(CC_DEFAULT, "  %d) %s", i, entry);
 
	for (const auto &entry : _network_ban_list) {
 
		IConsolePrintF(CC_DEFAULT, "  %d) %s", i, entry.c_str());
 
	}
 

	
 
	return true;
 
}
 

	
 
DEF_CONSOLE_CMD(ConPauseGame)
src/core/smallvec_type.hpp
Show inline comments
 
@@ -96,9 +96,7 @@ public:
 
		}
 

	
 
		std::vector<T>::clear();
 
	}
 
};
 

	
 
typedef AutoFreeSmallVector<char*> StringList; ///< Type for a list of strings.
 

	
 
#endif /* SMALLVEC_TYPE_HPP */
src/game/game_text.cpp
Show inline comments
 
@@ -108,13 +108,13 @@ std::unique_ptr<LanguageStrings> ReadRaw
 

	
 
			/* Remove trailing spaces/newlines from the string. */
 
			size_t i = len;
 
			while (i > 0 && (buffer[i - 1] == '\r' || buffer[i - 1] == '\n' || buffer[i - 1] == ' ')) i--;
 
			buffer[i] = '\0';
 

	
 
			ret->lines.push_back(stredup(buffer, buffer + to_read - 1));
 
			ret->lines.emplace_back(buffer, buffer + to_read - 1);
 

	
 
			if (len > to_read) {
 
				to_read = 0;
 
			} else {
 
				to_read -= len;
 
			}
 
@@ -126,47 +126,47 @@ std::unique_ptr<LanguageStrings> ReadRaw
 
	}
 
}
 

	
 

	
 
/** A reader that simply reads using fopen. */
 
struct StringListReader : StringReader {
 
	const char * const *p;   ///< The current location of the iteration.
 
	const char * const *end; ///< The end of the iteration.
 
	StringList::const_iterator p;   ///< The current location of the iteration.
 
	StringList::const_iterator end; ///< The end of the iteration.
 

	
 
	/**
 
	 * Create the reader.
 
	 * @param data        The data to fill during reading.
 
	 * @param strings     The language strings we are reading.
 
	 * @param master      Are we reading the master file?
 
	 * @param translation Are we reading a translation?
 
	 */
 
	StringListReader(StringData &data, const LanguageStrings &strings, bool master, bool translation) :
 
			StringReader(data, strings.language, master, translation), p(strings.lines.data()), end(p + strings.lines.size())
 
			StringReader(data, strings.language, master, translation), p(strings.lines.begin()), end(strings.lines.end())
 
	{
 
	}
 

	
 
	char *ReadLine(char *buffer, const char *last) override
 
	{
 
		if (this->p == this->end) return NULL;
 

	
 
		strecpy(buffer, *this->p, last);
 
		strecpy(buffer, this->p->c_str(), last);
 
		this->p++;
 

	
 
		return buffer;
 
	}
 
};
 

	
 
/** Class for writing an encoded language. */
 
struct TranslationWriter : LanguageWriter {
 
	StringList *strings; ///< The encoded strings.
 
	StringList &strings; ///< The encoded strings.
 

	
 
	/**
 
	 * Writer for the encoded data.
 
	 * @param strings The string table to add the strings to.
 
	 */
 
	TranslationWriter(StringList *strings) : strings(strings)
 
	TranslationWriter(StringList &strings) : strings(strings)
 
	{
 
	}
 

	
 
	void WriteHeader(const LanguagePackHeader *header)
 
	{
 
		/* We don't use the header. */
 
@@ -181,34 +181,31 @@ struct TranslationWriter : LanguageWrite
 
	{
 
		/* We don't write the length. */
 
	}
 

	
 
	void Write(const byte *buffer, size_t length)
 
	{
 
		char *dest = MallocT<char>(length + 1);
 
		memcpy(dest, buffer, length);
 
		dest[length] = '\0';
 
		this->strings->push_back(dest);
 
		this->strings.emplace_back((const char *)buffer, length);
 
	}
 
};
 

	
 
/** Class for writing the string IDs. */
 
struct StringNameWriter : HeaderWriter {
 
	StringList *strings; ///< The string names.
 
	StringList &strings; ///< The string names.
 

	
 
	/**
 
	 * Writer for the string names.
 
	 * @param strings The string table to add the strings to.
 
	 */
 
	StringNameWriter(StringList *strings) : strings(strings)
 
	StringNameWriter(StringList &strings) : strings(strings)
 
	{
 
	}
 

	
 
	void WriteStringID(const char *name, int stringid)
 
	{
 
		if (stringid == (int)this->strings->size()) this->strings->push_back(stredup(name));
 
		if (stringid == (int)this->strings.size()) this->strings.emplace_back(name);
 
	}
 

	
 
	void Finalise(const StringData &data)
 
	{
 
		/* Nothing to do. */
 
	}
 
@@ -311,23 +308,23 @@ void GameStrings::Compile()
 
	StringListReader master_reader(data, *this->raw_strings[0], true, false);
 
	master_reader.ParseFile();
 
	if (_errors != 0) throw std::exception();
 

	
 
	this->version = data.Version();
 

	
 
	StringNameWriter id_writer(&this->string_names);
 
	StringNameWriter id_writer(this->string_names);
 
	id_writer.WriteHeader(data);
 

	
 
	for (const auto &p : this->raw_strings) {
 
		data.FreeTranslation();
 
		StringListReader translation_reader(data, *p, false, strcmp(p->language, "english") != 0);
 
		translation_reader.ParseFile();
 
		if (_errors != 0) throw std::exception();
 

	
 
		this->compiled_strings.emplace_back(new LanguageStrings(p->language));
 
		TranslationWriter writer(&this->compiled_strings.back()->lines);
 
		TranslationWriter writer(this->compiled_strings.back()->lines);
 
		writer.WriteLang(data);
 
	}
 
}
 

	
 
/** The currently loaded game strings. */
 
GameStrings *_current_data = NULL;
 
@@ -337,13 +334,13 @@ GameStrings *_current_data = NULL;
 
 * @param id The ID of the game string.
 
 * @return The encoded string.
 
 */
 
const char *GetGameStringPtr(uint id)
 
{
 
	if (id >= _current_data->cur_language->lines.size()) return GetStringPtr(STR_UNDEFINED);
 
	return _current_data->cur_language->lines[id];
 
	return _current_data->cur_language->lines[id].c_str();
 
}
 

	
 
/**
 
 * Register the current translation to the Squirrel engine.
 
 * @param engine The engine to update/
 
 */
 
@@ -356,14 +353,14 @@ void RegisterGameTranslation(Squirrel *e
 
	HSQUIRRELVM vm = engine->GetVM();
 
	sq_pushroottable(vm);
 
	sq_pushstring(vm, "GSText", -1);
 
	if (SQ_FAILED(sq_get(vm, -2))) return;
 

	
 
	int idx = 0;
 
	for (const char * const p : _current_data->string_names) {
 
		sq_pushstring(vm, p, -1);
 
	for (const auto &p : _current_data->string_names) {
 
		sq_pushstring(vm, p.c_str(), -1);
 
		sq_pushinteger(vm, idx);
 
		sq_rawset(vm, -3);
 
		idx++;
 
	}
 

	
 
	sq_pop(vm, 2);
src/network/core/address.cpp
Show inline comments
 
@@ -153,33 +153,32 @@ bool NetworkAddress::IsFamily(int family
 
/**
 
 * Checks whether this IP address is contained by the given netmask.
 
 * @param netmask the netmask in CIDR notation to test against.
 
 * @note netmask without /n assumes all bits need to match.
 
 * @return true if this IP is within the netmask.
 
 */
 
bool NetworkAddress::IsInNetmask(char *netmask)
 
bool NetworkAddress::IsInNetmask(const char *netmask)
 
{
 
	/* Resolve it if we didn't do it already */
 
	if (!this->IsResolved()) this->GetAddress();
 

	
 
	int cidr = this->address.ss_family == AF_INET ? 32 : 128;
 

	
 
	NetworkAddress mask_address;
 

	
 
	/* Check for CIDR separator */
 
	char *chr_cidr = strchr(netmask, '/');
 
	const char *chr_cidr = strchr(netmask, '/');
 
	if (chr_cidr != NULL) {
 
		int tmp_cidr = atoi(chr_cidr + 1);
 

	
 
		/* Invalid CIDR, treat as single host */
 
		if (tmp_cidr > 0 || tmp_cidr < cidr) cidr = tmp_cidr;
 

	
 
		/* Remove and then replace the / so that NetworkAddress works on the IP portion */
 
		*chr_cidr = '\0';
 
		mask_address = NetworkAddress(netmask, 0, this->address.ss_family);
 
		*chr_cidr = '/';
 
		/* Remove the / so that NetworkAddress works on the IP portion */
 
		std::string ip_str(netmask, chr_cidr - netmask);
 
		mask_address = NetworkAddress(ip_str.c_str(), 0, this->address.ss_family);
 
	} else {
 
		mask_address = NetworkAddress(netmask, 0, this->address.ss_family);
 
	}
 

	
 
	if (mask_address.GetAddressLength() == 0) return false;
 

	
src/network/core/address.h
Show inline comments
 
@@ -126,13 +126,13 @@ public:
 
	bool IsResolved() const
 
	{
 
		return this->resolved;
 
	}
 

	
 
	bool IsFamily(int family);
 
	bool IsInNetmask(char *netmask);
 
	bool IsInNetmask(const char *netmask);
 

	
 
	/**
 
	 * Compare the address of this class with the address of another.
 
	 * @param address the other address.
 
	 * @return < 0 if address is less, 0 if equal and > 0 if address is more
 
	 */
src/network/core/tcp_listen.h
Show inline comments
 
@@ -51,19 +51,19 @@ public:
 
			DEBUG(net, 1, "[%s] Client connected from %s on frame %d", Tsocket::GetName(), address.GetHostname(), _frame_counter);
 

	
 
			SetNoDelay(s); // XXX error handling?
 

	
 
			/* Check if the client is banned */
 
			bool banned = false;
 
			for (char *entry : _network_ban_list) {
 
				banned = address.IsInNetmask(entry);
 
			for (const auto &entry : _network_ban_list) {
 
				banned = address.IsInNetmask(entry.c_str());
 
				if (banned) {
 
					Packet p(Tban_packet);
 
					p.PrepareToSend();
 

	
 
					DEBUG(net, 1, "[%s] Banned ip tried to join (%s), refused", Tsocket::GetName(), entry);
 
					DEBUG(net, 1, "[%s] Banned ip tried to join (%s), refused", Tsocket::GetName(), entry.c_str());
 

	
 
					if (send(s, (const char*)p.buffer, p.size, 0) < 0) {
 
						DEBUG(net, 0, "send failed with error %d", GET_LAST_ERROR());
 
					}
 
					closesocket(s);
 
					break;
src/network/network.cpp
Show inline comments
 
@@ -629,14 +629,14 @@ void NetworkAddServer(const char *b)
 
 * Get the addresses to bind to.
 
 * @param addresses the list to write to.
 
 * @param port the port to bind to.
 
 */
 
void GetBindAddresses(NetworkAddressList *addresses, uint16 port)
 
{
 
	for (char *iter : _network_bind_list) {
 
		addresses->emplace_back(iter, port);
 
	for (const auto &iter : _network_bind_list) {
 
		addresses->emplace_back(iter.c_str(), port);
 
	}
 

	
 
	/* No address, so bind to everything. */
 
	if (addresses->size() == 0) {
 
		addresses->emplace_back("", port);
 
	}
 
@@ -644,16 +644,16 @@ void GetBindAddresses(NetworkAddressList
 

	
 
/* Generates the list of manually added hosts from NetworkGameList and
 
 * dumps them into the array _network_host_list. This array is needed
 
 * by the function that generates the config file. */
 
void NetworkRebuildHostList()
 
{
 
	_network_host_list.Clear();
 
	_network_host_list.clear();
 

	
 
	for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
 
		if (item->manually) _network_host_list.push_back(stredup(item->address.GetAddressAsString(false)));
 
		if (item->manually) _network_host_list.emplace_back(item->address.GetAddressAsString(false));
 
	}
 
}
 

	
 
/** Non blocking connection create to actually connect to servers */
 
class TCPClientConnecter : TCPConnecter {
 
public:
src/network/network_gui.cpp
Show inline comments
 
@@ -1042,14 +1042,14 @@ void ShowNetworkGameWindow()
 
	DeleteWindowById(WC_NETWORK_WINDOW, WN_NETWORK_WINDOW_START);
 

	
 
	/* Only show once */
 
	if (first) {
 
		first = false;
 
		/* Add all servers from the config file to our list. */
 
		for (char *iter : _network_host_list) {
 
			NetworkAddServer(iter);
 
		for (const auto &iter : _network_host_list) {
 
			NetworkAddServer(iter.c_str());
 
		}
 
	}
 

	
 
	new NetworkGameWindow(&_network_game_window_desc);
 
}
 

	
src/network/network_server.cpp
Show inline comments
 
@@ -2080,28 +2080,28 @@ uint NetworkServerKickOrBanIP(ClientID c
 
 */
 
uint NetworkServerKickOrBanIP(const char *ip, bool ban)
 
{
 
	/* Add address to ban-list */
 
	if (ban) {
 
		bool contains = false;
 
		for (char *iter : _network_ban_list) {
 
			if (strcmp(iter, ip) == 0) {
 
		for (const auto &iter : _network_ban_list) {
 
			if (iter == ip) {
 
				contains = true;
 
				break;
 
			}
 
		}
 
		if (!contains) _network_ban_list.push_back(stredup(ip));
 
		if (!contains) _network_ban_list.emplace_back(ip);
 
	}
 

	
 
	uint n = 0;
 

	
 
	/* There can be multiple clients with the same IP, kick them all */
 
	NetworkClientSocket *cs;
 
	FOR_ALL_CLIENT_SOCKETS(cs) {
 
		if (cs->client_id == CLIENT_ID_SERVER) continue;
 
		if (cs->client_address.IsInNetmask(const_cast<char *>(ip))) {
 
		if (cs->client_address.IsInNetmask(ip)) {
 
			NetworkServerKickClient(cs->client_id);
 
			n++;
 
		}
 
	}
 

	
 
	return n;
src/newgrf_gui.cpp
Show inline comments
 
@@ -562,14 +562,12 @@ struct NewGRFTextfileWindow : public Tex
 
void ShowNewGRFTextfileWindow(TextfileType file_type, const GRFConfig *c)
 
{
 
	DeleteWindowById(WC_TEXTFILE, file_type);
 
	new NewGRFTextfileWindow(file_type, c);
 
}
 

	
 
static GRFPresetList _grf_preset_list; ///< List of known NewGRF presets. @see GetGRFPresetList
 

	
 
typedef std::map<uint32, const GRFConfig *> GrfIdMap; ///< Map of grfid to the grf config.
 

	
 
/**
 
 * Add all grf configs from \a c into the map.
 
 * @param c Grf list to add.
 
 * @param grfid_map Map to add them to.
 
@@ -602,12 +600,14 @@ struct NewGRFWindow : public Window, New
 
	GUIGRFConfigList avails;    ///< Available (non-active) grfs.
 
	const GRFConfig *avail_sel; ///< Currently selected available grf. \c NULL is none is selected.
 
	int avail_pos;              ///< Index of #avail_sel if existing, else \c -1.
 
	StringFilter string_filter; ///< Filter for available grf.
 
	QueryString filter_editbox; ///< Filter editbox;
 

	
 
	StringList grf_presets;     ///< List of known NewGRF presets.
 

	
 
	GRFConfig *actives;         ///< Temporary active grf list to which changes are made.
 
	GRFConfig *active_sel;      ///< Selected active grf item.
 

	
 
	GRFConfig **orig_list;      ///< List active grfs in the game. Used as initial value, may be updated by the window.
 
	bool editable;              ///< Is the window editable?
 
	bool show_params;           ///< Are the grf-parameters shown in the info-panel?
 
@@ -629,13 +629,13 @@ struct NewGRFWindow : public Window, New
 
		this->execute     = execute;
 
		this->show_params = show_params;
 
		this->preset      = -1;
 
		this->active_over = -1;
 

	
 
		CopyGRFConfigList(&this->actives, *orig_list, false);
 
		GetGRFPresetList(&_grf_preset_list);
 
		this->grf_presets = GetGRFPresetList();
 

	
 
		this->CreateNestedTree();
 
		this->vscroll = this->GetScrollbar(WID_NS_SCROLLBAR);
 
		this->vscroll2 = this->GetScrollbar(WID_NS_SCROLL2BAR);
 

	
 
		this->GetWidget<NWidgetStacked>(WID_NS_SHOW_REMOVE)->SetDisplayedPlane(this->editable ? 0 : 1);
 
@@ -670,13 +670,12 @@ struct NewGRFWindow : public Window, New
 
			ResetGRFConfig(false);
 
			ReloadNewGRFData();
 
		}
 

	
 
		/* Remove the temporary copy of grf-list used in window */
 
		ClearGRFConfigList(&this->actives);
 
		_grf_preset_list.Clear();
 
	}
 

	
 
	/**
 
	 * Test whether the currently active set of NewGRFs can be upgraded with the available NewGRFs.
 
	 * @return Whether an upgrade is possible.
 
	 */
 
@@ -744,17 +743,15 @@ struct NewGRFWindow : public Window, New
 
			case WID_NS_NEWGRF_INFO:
 
				size->height = max(size->height, WD_FRAMERECT_TOP + 10 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + padding.height + 2);
 
				break;
 

	
 
			case WID_NS_PRESET_LIST: {
 
				Dimension d = GetStringBoundingBox(STR_NUM_CUSTOM);
 
				for (uint i = 0; i < _grf_preset_list.size(); i++) {
 
					if (_grf_preset_list[i] != NULL) {
 
						SetDParamStr(0, _grf_preset_list[i]);
 
						d = maxdim(d, GetStringBoundingBox(STR_JUST_RAW_STRING));
 
					}
 
				for (const auto &i : this->grf_presets) {
 
					SetDParamStr(0, i.c_str());
 
					d = maxdim(d, GetStringBoundingBox(STR_JUST_RAW_STRING));
 
				}
 
				d.width += padding.width;
 
				*size = maxdim(d, *size);
 
				break;
 
			}
 

	
 
@@ -780,13 +777,13 @@ struct NewGRFWindow : public Window, New
 
		switch (widget) {
 
			case WID_NS_PRESET_LIST:
 
				if (this->preset == -1) {
 
					SetDParam(0, STR_NUM_CUSTOM);
 
				} else {
 
					SetDParam(0, STR_JUST_RAW_STRING);
 
					SetDParamStr(1, _grf_preset_list[this->preset]);
 
					SetDParamStr(1, this->grf_presets[this->preset].c_str());
 
				}
 
				break;
 
		}
 
	}
 

	
 
	/**
 
@@ -926,16 +923,14 @@ struct NewGRFWindow : public Window, New
 
			case WID_NS_PRESET_LIST: {
 
				DropDownList list;
 

	
 
				/* Add 'None' option for clearing list */
 
				list.emplace_back(new DropDownListStringItem(STR_NONE, -1, false));
 

	
 
				for (uint i = 0; i < _grf_preset_list.size(); i++) {
 
					if (_grf_preset_list[i] != NULL) {
 
						list.emplace_back(new DropDownListCharStringItem(_grf_preset_list[i], i, false));
 
					}
 
				for (uint i = 0; i < this->grf_presets.size(); i++) {
 
					list.emplace_back(new DropDownListCharStringItem(this->grf_presets[i].c_str(), i, false));
 
				}
 

	
 
				this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
 
				ShowDropDownList(this, std::move(list), this->preset, WID_NS_PRESET_LIST);
 
				break;
 
			}
 
@@ -946,20 +941,20 @@ struct NewGRFWindow : public Window, New
 
				extern void OpenBrowser(const char *url);
 
				OpenBrowser(c->GetURL());
 
				break;
 
			}
 

	
 
			case WID_NS_PRESET_SAVE:
 
				ShowSavePresetWindow((this->preset == -1) ? NULL : _grf_preset_list[this->preset]);
 
				ShowSavePresetWindow((this->preset == -1) ? NULL : this->grf_presets[this->preset].c_str());
 
				break;
 

	
 
			case WID_NS_PRESET_DELETE:
 
				if (this->preset == -1) return;
 

	
 
				DeleteGRFPresetFromConfig(_grf_preset_list[this->preset]);
 
				GetGRFPresetList(&_grf_preset_list);
 
				DeleteGRFPresetFromConfig(this->grf_presets[this->preset].c_str());
 
				this->grf_presets = GetGRFPresetList();
 
				this->preset = -1;
 
				this->InvalidateData();
 
				this->DeleteChildWindows(WC_QUERY_STRING); // Remove the parameter query window
 
				break;
 

	
 
			case WID_NS_MOVE_UP: { // Move GRF up
 
@@ -1153,13 +1148,13 @@ struct NewGRFWindow : public Window, New
 
		if (!this->editable) return;
 

	
 
		ClearGRFConfigList(&this->actives);
 
		this->preset = index;
 

	
 
		if (index != -1) {
 
			this->actives = LoadGRFPresetFromConfig(_grf_preset_list[index]);
 
			this->actives = LoadGRFPresetFromConfig(this->grf_presets[index].c_str());
 
		}
 
		this->avails.ForceRebuild();
 

	
 
		ResetObjectToPlace();
 
		DeleteWindowByClass(WC_GRF_PARAMETERS);
 
		DeleteWindowByClass(WC_TEXTFILE);
 
@@ -1169,17 +1164,17 @@ struct NewGRFWindow : public Window, New
 

	
 
	void OnQueryTextFinished(char *str) override
 
	{
 
		if (str == NULL) return;
 

	
 
		SaveGRFPresetToConfig(str, this->actives);
 
		GetGRFPresetList(&_grf_preset_list);
 
		this->grf_presets = GetGRFPresetList();
 

	
 
		/* Switch to this preset */
 
		for (uint i = 0; i < _grf_preset_list.size(); i++) {
 
			if (_grf_preset_list[i] != NULL && strcmp(_grf_preset_list[i], str) == 0) {
 
		for (uint i = 0; i < this->grf_presets.size(); i++) {
 
			if (this->grf_presets[i] == str) {
 
				this->preset = i;
 
				break;
 
			}
 
		}
 

	
 
		this->InvalidateData();
 
@@ -2035,27 +2030,27 @@ static WindowDesc _save_preset_desc(
 
	_nested_save_preset_widgets, lengthof(_nested_save_preset_widgets)
 
);
 

	
 
/** Class for the save preset window. */
 
struct SavePresetWindow : public Window {
 
	QueryString presetname_editbox; ///< Edit box of the save preset.
 
	GRFPresetList presets; ///< Available presets.
 
	StringList presets; ///< Available presets.
 
	Scrollbar *vscroll; ///< Pointer to the scrollbar widget.
 
	int selected; ///< Selected entry in the preset list, or \c -1 if none selected.
 

	
 
	/**
 
	 * Constructor of the save preset window.
 
	 * @param initial_text Initial text to display in the edit box, or \c NULL.
 
	 */
 
	SavePresetWindow(const char *initial_text) : Window(&_save_preset_desc), presetname_editbox(32)
 
	{
 
		GetGRFPresetList(&this->presets);
 
		this->presets = GetGRFPresetList();
 
		this->selected = -1;
 
		if (initial_text != NULL) {
 
			for (uint i = 0; i < this->presets.size(); i++) {
 
				if (!strcmp(initial_text, this->presets[i])) {
 
				if (this->presets[i] == initial_text) {
 
					this->selected = i;
 
					break;
 
				}
 
			}
 
		}
 

	
 
@@ -2080,13 +2075,13 @@ struct SavePresetWindow : public Window 
 
	{
 
		switch (widget) {
 
			case WID_SVP_PRESET_LIST: {
 
				resize->height = FONT_HEIGHT_NORMAL + 2U;
 
				size->height = 0;
 
				for (uint i = 0; i < this->presets.size(); i++) {
 
					Dimension d = GetStringBoundingBox(this->presets[i]);
 
					Dimension d = GetStringBoundingBox(this->presets[i].c_str());
 
					size->width = max(size->width, d.width + WD_FRAMETEXT_LEFT + WD_FRAMETEXT_RIGHT);
 
					resize->height = max(resize->height, d.height);
 
				}
 
				size->height = ClampU((uint)this->presets.size(), 5, 20) * resize->height + 1;
 
				break;
 
			}
 
@@ -2105,13 +2100,13 @@ struct SavePresetWindow : public Window 
 
				uint min_index = this->vscroll->GetPosition();
 
				uint max_index = min(min_index + this->vscroll->GetCapacity(), (uint)this->presets.size());
 

	
 
				for (uint i = min_index; i < max_index; i++) {
 
					if ((int)i == this->selected) GfxFillRect(r.left + 1, y, r.right - 1, y + step_height - 2, PC_DARK_BLUE);
 

	
 
					const char *text = this->presets[i];
 
					const char *text = this->presets[i].c_str();
 
					DrawString(r.left + WD_FRAMERECT_LEFT, r.right, y + offset_y, text, ((int)i == this->selected) ? TC_WHITE : TC_SILVER);
 
					y += step_height;
 
				}
 
				break;
 
			}
 
		}
 
@@ -2121,13 +2116,13 @@ struct SavePresetWindow : public Window 
 
	{
 
		switch (widget) {
 
			case WID_SVP_PRESET_LIST: {
 
				uint row = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SVP_PRESET_LIST);
 
				if (row < this->presets.size()) {
 
					this->selected = row;
 
					this->presetname_editbox.text.Assign(this->presets[row]);
 
					this->presetname_editbox.text.Assign(this->presets[row].c_str());
 
					this->SetWidgetDirty(WID_SVP_PRESET_LIST);
 
					this->SetWidgetDirty(WID_SVP_EDITBOX);
 
				}
 
				break;
 
			}
 

	
src/openttd.cpp
Show inline comments
 
@@ -442,14 +442,14 @@ struct AfterNewGRFScan : NewGRFScanCallb
 
		MusicDriver::GetInstance()->SetVolume(_settings_client.music.music_vol);
 

	
 
		if (startyear != INVALID_YEAR) _settings_newgame.game_creation.starting_year = startyear;
 
		if (generation_seed != GENERATE_NEW_SEED) _settings_newgame.game_creation.generation_seed = generation_seed;
 

	
 
		if (dedicated_host != NULL) {
 
			_network_bind_list.Clear();
 
			_network_bind_list.push_back(stredup(dedicated_host));
 
			_network_bind_list.clear();
 
			_network_bind_list.emplace_back(dedicated_host);
 
		}
 
		if (dedicated_port != 0) _settings_client.network.server_port = dedicated_port;
 

	
 
		/* initialize the ingame console */
 
		IConsoleInit();
 
		InitializeGUI();
src/saveload/game_sl.cpp
Show inline comments
 
@@ -126,20 +126,20 @@ static const SaveLoad _game_language_hea
 

	
 
static const SaveLoad _game_language_string[] = {
 
	SLEG_STR(_game_saveload_string, SLE_STR | SLF_ALLOW_CONTROL),
 
	 SLE_END()
 
};
 

	
 
static void SaveReal_GSTR(LanguageStrings *ls)
 
static void SaveReal_GSTR(const LanguageStrings *ls)
 
{
 
	_game_saveload_string  = ls->language;
 
	_game_saveload_strings = (uint)ls->lines.size();
 

	
 
	SlObject(NULL, _game_language_header);
 
	for (uint i = 0; i < _game_saveload_strings; i++) {
 
		_game_saveload_string = ls->lines[i];
 
	for (const auto &i : ls->lines) {
 
		_game_saveload_string = i.c_str();
 
		SlObject(NULL, _game_language_string);
 
	}
 
}
 

	
 
static void Load_GSTR()
 
{
 
@@ -150,13 +150,13 @@ static void Load_GSTR()
 
		_game_saveload_string = NULL;
 
		SlObject(NULL, _game_language_header);
 

	
 
		std::unique_ptr<LanguageStrings> ls(new LanguageStrings(_game_saveload_string != NULL ? _game_saveload_string : ""));
 
		for (uint i = 0; i < _game_saveload_strings; i++) {
 
			SlObject(NULL, _game_language_string);
 
			ls->lines.push_back(stredup(_game_saveload_string != NULL ? _game_saveload_string : ""));
 
			ls->lines.emplace_back(_game_saveload_string != NULL ? _game_saveload_string : "");
 
		}
 

	
 
		_current_data->raw_strings.push_back(std::move(ls));
 
	}
 

	
 
	/* If there were no strings in the savegame, set GameStrings to NULL */
src/settings.cpp
Show inline comments
 
@@ -81,13 +81,13 @@ char *_config_file; ///< Configuration f
 

	
 
typedef std::list<ErrorMessageData> ErrorList;
 
static ErrorList _settings_error_list; ///< Errors while loading minimal settings.
 

	
 

	
 
typedef void SettingDescProc(IniFile *ini, const SettingDesc *desc, const char *grpname, void *object);
 
typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList *list);
 
typedef void SettingDescProcList(IniFile *ini, const char *grpname, StringList &list);
 

	
 
static bool IsSignedVarMemType(VarType vt);
 

	
 
/**
 
 * Groups in openttd.cfg that are actually lists.
 
 */
 
@@ -715,43 +715,43 @@ static void IniSaveSettings(IniFile *ini
 
 * saved and a callback function should be defined that will take over the
 
 * list-handling and store the data itself somewhere.
 
 * @param ini IniFile handle to the ini file with the source data
 
 * @param grpname character string identifying the section-header of the ini file that will be parsed
 
 * @param list new list with entries of the given section
 
 */
 
static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList *list)
 
static void IniLoadSettingList(IniFile *ini, const char *grpname, StringList &list)
 
{
 
	IniGroup *group = ini->GetGroup(grpname);
 

	
 
	if (group == NULL || list == NULL) return;
 
	if (group == NULL) return;
 

	
 
	list->Clear();
 
	list.clear();
 

	
 
	for (const IniItem *item = group->item; item != NULL; item = item->next) {
 
		if (item->name != NULL) list->push_back(stredup(item->name));
 
		if (item->name != NULL) list.emplace_back(item->name);
 
	}
 
}
 

	
 
/**
 
 * Saves all items from a list into the 'grpname' section
 
 * The list parameter can be a NULL pointer, in this case a callback function
 
 * should be defined that will provide the source data to be saved.
 
 * @param ini IniFile handle to the ini file where the destination data is saved
 
 * @param grpname character string identifying the section-header of the ini file
 
 * @param list pointer to an string(pointer) array that will be used as the
 
 *             source to be saved into the relevant ini section
 
 */
 
static void IniSaveSettingList(IniFile *ini, const char *grpname, StringList *list)
 
static void IniSaveSettingList(IniFile *ini, const char *grpname, StringList &list)
 
{
 
	IniGroup *group = ini->GetGroup(grpname);
 

	
 
	if (group == NULL || list == NULL) return;
 
	if (group == NULL) return;
 
	group->Clear();
 

	
 
	for (char *iter : *list) {
 
		group->GetItem(iter, true)->SetValue("");
 
	for (const auto &iter : list) {
 
		group->GetItem(iter.c_str(), true)->SetValue("");
 
	}
 
}
 

	
 
/**
 
 * Load a WindowDesc from config.
 
 * @param ini IniFile handle to the ini file with the source data
 
@@ -1696,15 +1696,15 @@ static void HandleSettingDescs(IniFile *
 

	
 
	if (other_settings) {
 
		proc(ini, _settings,         "patches",  &_settings_newgame);
 
		proc(ini, _currency_settings,"currency", &_custom_currency);
 
		proc(ini, _company_settings, "company",  &_settings_client.company);
 

	
 
		proc_list(ini, "server_bind_addresses", &_network_bind_list);
 
		proc_list(ini, "servers", &_network_host_list);
 
		proc_list(ini, "bans",    &_network_ban_list);
 
		proc_list(ini, "server_bind_addresses", _network_bind_list);
 
		proc_list(ini, "servers", _network_host_list);
 
		proc_list(ini, "bans",    _network_ban_list);
 
	}
 
}
 

	
 
static IniFile *IniLoadConfig()
 
{
 
	IniFile *ini = new IniFile(_list_group_names);
 
@@ -1764,27 +1764,26 @@ void SaveToConfig()
 
	ini->SaveToDisk(_config_file);
 
	delete ini;
 
}
 

	
 
/**
 
 * Get the list of known NewGrf presets.
 
 * @param[in,out] list Pointer to list for storing the preset names.
 
 * @returns List of preset names.
 
 */
 
void GetGRFPresetList(GRFPresetList *list)
 
StringList GetGRFPresetList()
 
{
 
	list->Clear();
 
	StringList list;
 

	
 
	IniFile *ini = IniLoadConfig();
 
	IniGroup *group;
 
	for (group = ini->group; group != NULL; group = group->next) {
 
	std::unique_ptr<IniFile> ini(IniLoadConfig());
 
	for (IniGroup *group = ini->group; group != NULL; group = group->next) {
 
		if (strncmp(group->name, "preset-", 7) == 0) {
 
			list->push_back(stredup(group->name + 7));
 
			list.emplace_back(group->name + 7);
 
		}
 
	}
 

	
 
	delete ini;
 
	return list;
 
}
 

	
 
/**
 
 * Load a NewGRF configuration by preset-name.
 
 * @param config_name Name of the preset.
 
 * @return NewGRF configuration.
src/settings_func.h
Show inline comments
 
@@ -11,12 +11,13 @@
 

	
 
#ifndef SETTINGS_FUNC_H
 
#define SETTINGS_FUNC_H
 

	
 
#include "core/smallvec_type.hpp"
 
#include "company_type.h"
 
#include "string_type.h"
 

	
 
struct IniFile;
 

	
 
void IConsoleSetSetting(const char *name, const char *value, bool force_newgame = false);
 
void IConsoleSetSetting(const char *name, int32 value);
 
void IConsoleGetSetting(const char *name, bool force_newgame = false);
 
@@ -25,17 +26,13 @@ void IConsoleListSettings(const char *pr
 
void LoadFromConfig(bool minimal = false);
 
void SaveToConfig();
 

	
 
void IniLoadWindowSettings(IniFile *ini, const char *grpname, void *desc);
 
void IniSaveWindowSettings(IniFile *ini, const char *grpname, void *desc);
 

	
 
/* Functions to load and save NewGRF settings to a separate
 
 * configuration file, used for presets. */
 
typedef AutoFreeSmallVector<char *> GRFPresetList;
 

	
 
void GetGRFPresetList(GRFPresetList *list);
 
StringList GetGRFPresetList();
 
struct GRFConfig *LoadGRFPresetFromConfig(const char *config_name);
 
void SaveGRFPresetToConfig(const char *config_name, struct GRFConfig *config);
 
void DeleteGRFPresetFromConfig(const char *config_name);
 

	
 
uint GetCompanySettingIndex(const char *name);
 
void SetDefaultCompanySettings(CompanyID cid);
src/string_type.h
Show inline comments
 
@@ -10,12 +10,14 @@
 
/** @file string_type.h Types for strings. */
 

	
 
#ifndef STRING_TYPE_H
 
#define STRING_TYPE_H
 

	
 
#include "core/enum_type.hpp"
 
#include <vector>
 
#include <string>
 

	
 
/** A non-breaking space. */
 
#define NBSP "\xC2\xA0"
 

	
 
/** A left-to-right marker, marks the next character as left-to-right. */
 
#define LRM "\xE2\x80\x8E"
 
@@ -50,7 +52,11 @@ enum StringValidationSettings {
 
	SVS_REPLACE_WITH_QUESTION_MARK = 1 << 0, ///< Replace the unknown/bad bits with question marks.
 
	SVS_ALLOW_NEWLINE              = 1 << 1, ///< Allow newlines.
 
	SVS_ALLOW_CONTROL_CODE         = 1 << 2, ///< Allow the special control codes.
 
};
 
DECLARE_ENUM_AS_BIT_SET(StringValidationSettings)
 

	
 

	
 
/** Type for a list of strings. */
 
typedef std::vector<std::string> StringList;
 

	
 
#endif /* STRING_TYPE_H */
0 comments (0 inline, 0 general)