mirror of
https://github.com/weechat/weechat.git
synced 2026-06-26 04:46:37 +02:00
core: sort linked lists with configuration files and sections by name
This commit is contained in:
+143
-14
@@ -81,6 +81,76 @@ config_file_search (const char *name)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Searches for position of configuration file (to keep configuration files
|
||||
* sorted by name).
|
||||
*/
|
||||
|
||||
struct t_config_file *
|
||||
config_file_config_find_pos (const char *name)
|
||||
{
|
||||
struct t_config_file *ptr_config;
|
||||
|
||||
if (name)
|
||||
{
|
||||
for (ptr_config = config_files; ptr_config;
|
||||
ptr_config = ptr_config->next_config)
|
||||
{
|
||||
if (string_strcasecmp (name, ptr_config->name) < 0)
|
||||
return ptr_config;
|
||||
}
|
||||
}
|
||||
|
||||
/* position not found (we will add to the end of list) */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Inserts a configuration file in list (keeping configuration files sorted by
|
||||
* name).
|
||||
*/
|
||||
|
||||
void
|
||||
config_file_config_insert (struct t_config_file *config_file)
|
||||
{
|
||||
struct t_config_file *pos_config;
|
||||
|
||||
if (!config_file)
|
||||
return;
|
||||
|
||||
if (config_files)
|
||||
{
|
||||
pos_config = config_file_config_find_pos (config_file->name);
|
||||
if (pos_config)
|
||||
{
|
||||
/* insert configuration file into the list (before config found) */
|
||||
config_file->prev_config = pos_config->prev_config;
|
||||
config_file->next_config = pos_config;
|
||||
if (pos_config->prev_config)
|
||||
(pos_config->prev_config)->next_config = config_file;
|
||||
else
|
||||
config_files = config_file;
|
||||
pos_config->prev_config = config_file;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* add configuration file to the end of list */
|
||||
config_file->prev_config = last_config_file;
|
||||
config_file->next_config = NULL;
|
||||
last_config_file->next_config = config_file;
|
||||
last_config_file = config_file;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* first configuration file */
|
||||
config_file->prev_config = NULL;
|
||||
config_file->next_config = NULL;
|
||||
config_files = config_file;
|
||||
last_config_file = config_file;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Creates a new configuration file.
|
||||
*
|
||||
@@ -136,18 +206,83 @@ config_file_new (struct t_weechat_plugin *plugin, const char *name,
|
||||
new_config_file->sections = NULL;
|
||||
new_config_file->last_section = NULL;
|
||||
|
||||
new_config_file->prev_config = last_config_file;
|
||||
new_config_file->next_config = NULL;
|
||||
if (config_files)
|
||||
last_config_file->next_config = new_config_file;
|
||||
else
|
||||
config_files = new_config_file;
|
||||
last_config_file = new_config_file;
|
||||
config_file_config_insert (new_config_file);
|
||||
}
|
||||
|
||||
return new_config_file;
|
||||
}
|
||||
|
||||
/*
|
||||
* Searches for position of section in configuration file (to keep sections
|
||||
* sorted by name).
|
||||
*/
|
||||
|
||||
struct t_config_section *
|
||||
config_file_section_find_pos (struct t_config_file *config_file,
|
||||
const char *name)
|
||||
{
|
||||
struct t_config_section *ptr_section;
|
||||
|
||||
if (config_file && name)
|
||||
{
|
||||
for (ptr_section = config_file->sections; ptr_section;
|
||||
ptr_section = ptr_section->next_section)
|
||||
{
|
||||
if (string_strcasecmp (name, ptr_section->name) < 0)
|
||||
return ptr_section;
|
||||
}
|
||||
}
|
||||
|
||||
/* position not found (we will add to the end of list) */
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Inserts a section in configuration file (keeping sections sorted by name).
|
||||
*/
|
||||
|
||||
void
|
||||
config_file_section_insert_in_config (struct t_config_section *section)
|
||||
{
|
||||
struct t_config_section *pos_section;
|
||||
|
||||
if (!section || !section->config_file)
|
||||
return;
|
||||
|
||||
if (section->config_file->sections)
|
||||
{
|
||||
pos_section = config_file_section_find_pos (section->config_file,
|
||||
section->name);
|
||||
if (pos_section)
|
||||
{
|
||||
/* insert section into the list (before section found) */
|
||||
section->prev_section = pos_section->prev_section;
|
||||
section->next_section = pos_section;
|
||||
if (pos_section->prev_section)
|
||||
(pos_section->prev_section)->next_section = section;
|
||||
else
|
||||
(section->config_file)->sections = section;
|
||||
pos_section->prev_section = section;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* add section to end of sections */
|
||||
section->prev_section = (section->config_file)->last_section;
|
||||
section->next_section = NULL;
|
||||
(section->config_file)->last_section->next_section = section;
|
||||
(section->config_file)->last_section = section;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* first section of file */
|
||||
section->prev_section = NULL;
|
||||
section->next_section = NULL;
|
||||
(section->config_file)->sections = section;
|
||||
(section->config_file)->last_section = section;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Creates a new section in a configuration file.
|
||||
*
|
||||
@@ -216,13 +351,7 @@ config_file_new_section (struct t_config_file *config_file, const char *name,
|
||||
new_section->options = NULL;
|
||||
new_section->last_option = NULL;
|
||||
|
||||
new_section->prev_section = config_file->last_section;
|
||||
new_section->next_section = NULL;
|
||||
if (config_file->sections)
|
||||
config_file->last_section->next_section = new_section;
|
||||
else
|
||||
config_file->sections = new_section;
|
||||
config_file->last_section = new_section;
|
||||
config_file_section_insert_in_config (new_section);
|
||||
}
|
||||
|
||||
return new_section;
|
||||
|
||||
Reference in New Issue
Block a user