Changeset - r10005:7dd15e6609a8
[Not reviewed]
master
0 3 0
rubidium - 16 years ago 2008-08-25 06:35:28
rubidium@openttd.org
(svn r14164) -Codechange: simplify and unify the addition of ini items with value when not loading an ini file.
-Fix: wrong insertion management causing leaks.
3 files changed with 34 insertions and 35 deletions:
0 comments (0 inline, 0 general)
src/ini.cpp
Show inline comments
 
@@ -16,12 +16,20 @@ IniItem::IniItem(IniGroup *parent, const
 

	
 
	this->name = strndup(name, len);
 
	*parent->last_item = this;
 
	parent->last_item = &this->next;
 
}
 

	
 
IniItem::IniItem(IniGroup *parent, const char *name, const char *value) : next(NULL), comment(NULL)
 
{
 
	this->name  = strdup(name);
 
	this->value = strdup(value);
 
	*parent->last_item = this;
 
	parent->last_item = &this->next;
 
}
 

	
 
IniItem::~IniItem()
 
{
 
	free(this->name);
 
	free(this->value);
 
	free(this->comment);
 

	
src/ini_type.h
Show inline comments
 
@@ -15,14 +15,36 @@ enum IniGroupType {
 
struct IniItem {
 
	IniItem *next; ///< The next item in this group
 
	char *name;    ///< The name of this item
 
	char *value;   ///< The value of this item
 
	char *comment; ///< The comment associated with this item
 

	
 
	/**
 
	 * Construct a new in-memory item of an Ini file.
 
	 * @param parent the group we belong to
 
	 * @param name   the name of the item
 
	 * @param len    the length of the name of the item
 
	 */
 
	IniItem(struct IniGroup *parent, const char *name, size_t len = 0);
 

	
 
	/**
 
	 * Construct a new in-memory item of an Ini file.
 
	 * @param parent the group we belong to
 
	 * @param name   the name of the item
 
	 * @param value  the value to immediatelly assign
 
	 */
 
	IniItem(IniGroup *parent, const char *name, const char *value);
 

	
 
	/** Free everything we loaded. */
 
	~IniItem();
 

	
 
	/**
 
	 * Replace the current value with another value.
 
	 * @param value the value to replace with.
 
	 */
 
	void SetValue(const char *value);
 
};
 

	
 
/** A group within an ini file. */
 
struct IniGroup {
 
	IniGroup *next;      ///< the next group within this file
 
	IniGroupType type;   ///< type of group
src/settings.cpp
Show inline comments
 
@@ -625,36 +625,25 @@ static void ini_load_setting_list(IniFil
 
 *             source to be saved into the relevant ini section
 
 * @param len the maximum number of items available for the above list
 
 * @param proc callback function that can will provide the source data if defined */
 
static void ini_save_setting_list(IniFile *ini, const char *grpname, char **list, uint len, SettingListCallbackProc proc)
 
{
 
	IniGroup *group = ini->GetGroup(grpname);
 
	IniItem *item = NULL;
 
	const char *entry;
 
	uint i;
 
	bool first = true;
 

	
 
	if (proc == NULL && list == NULL) return;
 
	if (group == NULL) return;
 
	group->item = NULL;
 

	
 
	for (i = 0; i != len; i++) {
 
		entry = (proc != NULL) ? proc(NULL, i) : list[i];
 

	
 
		if (entry == NULL || *entry == '\0') continue;
 

	
 
		if (first) { // add first item to the head of the group
 
			item = new IniItem(group, entry);
 
			item->value = strdup("");
 
			group->item = item;
 
			first = false;
 
		} else { // all other items are attached to the previous one
 
			item->next = new IniItem(group, entry);
 
			item = item->next;
 
			item->value = strdup("");
 
		}
 
		new IniItem(group, entry, "");
 
	}
 
}
 

	
 
//***************************
 
// OTTD specific INI stuff
 
//***************************
 
@@ -1688,75 +1677,55 @@ static GRFConfig *GRFLoadConfig(IniFile 
 
	return first;
 
}
 

	
 
static void NewsDisplaySaveConfig(IniFile *ini, const char *grpname)
 
{
 
	IniGroup *group = ini->GetGroup(grpname);
 
	IniItem **item;
 

	
 
	if (group == NULL) return;
 
	group->item = NULL;
 
	item = &group->item;
 

	
 
	for (int i = 0; i < NT_END; i++) {
 
		const char *value;
 
		int v = _news_type_data[i].display;
 

	
 
		value = (v == ND_OFF ? "off" : (v == ND_SUMMARY ? "summarized" : "full"));
 

	
 
		*item = new IniItem(group, _news_type_data[i].name);
 
		(*item)->value = strdup(value);
 
		item = &(*item)->next;
 
		new IniItem(group, _news_type_data[i].name, value);
 
	}
 
}
 

	
 
/**
 
 * Save the version of OpenTTD to the ini file.
 
 * @param ini the ini to write to
 
 */
 
static void SaveVersionInConfig(IniFile *ini)
 
{
 
	IniGroup *group = ini->GetGroup("version");
 

	
 
	if (group == NULL) return;
 
	group->item = NULL;
 
	IniItem **item = &group->item;
 

	
 
	char version[9];
 
	snprintf(version, lengthof(version), "%08X", _openttd_newgrf_version);
 

	
 
	const char *versions[][2] = {
 
		{ "version_string", _openttd_revision },
 
		{ "version_number", version }
 
	};
 

	
 
	for (uint i = 0; i < lengthof(versions); i++) {
 
		*item = new IniItem(group, versions[i][0]);
 
		(*item)->value = strdup(versions[i][1]);
 
		item = &(*item)->next;
 
		new IniItem(group, versions[i][0], versions[i][1]);
 
	}
 
}
 

	
 
/* Save a GRF configuration to the given group name */
 
static void GRFSaveConfig(IniFile *ini, const char *grpname, const GRFConfig *list)
 
{
 
	IniGroup *group = ini->GetGroup(grpname);
 
	IniItem **item;
 
	const GRFConfig *c;
 

	
 
	if (group == NULL) return;
 
	group->item = NULL;
 
	item = &group->item;
 

	
 
	for (c = list; c != NULL; c = c->next) {
 
		char params[512];
 
		GRFBuildParamList(params, c, lastof(params));
 

	
 
		*item = new IniItem(group, c->filename);
 
		(*item)->value = strdup(params);
 
		item = &(*item)->next;
 
		new IniItem(group, c->filename, params);
 
	}
 
}
 

	
 
/* Common handler for saving/loading variables to the configuration file */
 
static void HandleSettingDescs(IniFile *ini, SettingDescProc *proc, SettingDescProcList *proc_list)
 
{
0 comments (0 inline, 0 general)