summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2014-08-24 19:18:09 +0200
committerSébastien Helleu <flashcode@flashtux.org>2014-08-24 19:18:09 +0200
commit56f099bec647ef79542e3e65e847e24d1bdcaa61 (patch)
tree274f7d92173b21cf45c717bc98f432f662e92161 /src/core
parentfa4436b2623d4774adcfab48af891abfcb0e712f (diff)
downloadweechat-56f099bec647ef79542e3e65e847e24d1bdcaa61.zip
core: sort linked lists with configuration files and sections by name
Diffstat (limited to 'src/core')
-rw-r--r--src/core/wee-config-file.c157
1 files changed, 143 insertions, 14 deletions
diff --git a/src/core/wee-config-file.c b/src/core/wee-config-file.c
index 00bc6dbf3..bb06fa59a 100644
--- a/src/core/wee-config-file.c
+++ b/src/core/wee-config-file.c
@@ -82,6 +82,76 @@ config_file_search (const char *name)
}
/*
+ * 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.
*
* Returns pointer to new configuration file, NULL if error.
@@ -136,19 +206,84 @@ 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.
*
* Returns pointer to new section, NULL if error.
@@ -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;