summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2023-01-14 12:08:43 +0100
committerSébastien Helleu <flashcode@flashtux.org>2023-01-28 15:13:43 +0100
commit72f4596fb22538ab6691157399c32f151fd98629 (patch)
tree61f4384bd72046161a414c550b2ff7640e67afc0 /src/core
parentd71c3b0f21bdf68534a0f36543e0fcf3454a30da (diff)
downloadweechat-72f4596fb22538ab6691157399c32f151fd98629.zip
core: add function config_file_get_configs_by_priority (issue #1872)
Diffstat (limited to 'src/core')
-rw-r--r--src/core/wee-command.c82
-rw-r--r--src/core/wee-config-file.c58
-rw-r--r--src/core/wee-config-file.h1
3 files changed, 82 insertions, 59 deletions
diff --git a/src/core/wee-command.c b/src/core/wee-command.c
index 725689af0..177e296bd 100644
--- a/src/core/wee-command.c
+++ b/src/core/wee-command.c
@@ -5586,39 +5586,12 @@ command_reload_file (struct t_config_file *config_file)
}
/*
- * Compares two configuration files to sort them by priority (highest priority
- * at beginning of list).
- *
- * Returns:
- * -1: config1 has higher priority than config2
- * 1: config1 has same or lower priority than config2
- */
-
-int
-command_reload_arraylist_cmp_config_cb (void *data,
- struct t_arraylist *arraylist,
- void *pointer1, void *pointer2)
-{
- struct t_config_file *ptr_config1, *ptr_config2;
-
- /* make C compiler happy */
- (void) data;
- (void) arraylist;
-
- ptr_config1 = (struct t_config_file *)pointer1;
- ptr_config2 = (struct t_config_file *)pointer2;
-
- return (ptr_config1->priority > ptr_config2->priority) ? -1 : 1;
-}
-
-
-/*
* Callback for command "/reload": reloads a configuration file.
*/
COMMAND_CALLBACK(reload)
{
- struct t_config_file *ptr_config_file;
+ struct t_config_file *ptr_config;
struct t_arraylist *all_configs;
int i, list_size;
@@ -5632,10 +5605,10 @@ COMMAND_CALLBACK(reload)
{
for (i = 1; i < argc; i++)
{
- ptr_config_file = config_file_search (argv[i]);
- if (ptr_config_file)
+ ptr_config = config_file_search (argv[i]);
+ if (ptr_config)
{
- command_reload_file (ptr_config_file);
+ command_reload_file (ptr_config);
}
else
{
@@ -5647,32 +5620,16 @@ COMMAND_CALLBACK(reload)
}
else
{
- /*
- * build a list of pointers to configs sorted by priority,
- * so that configs with high priority are reloaded first
- */
- all_configs = arraylist_new (
- 32, 1, 1,
- &command_reload_arraylist_cmp_config_cb, NULL,
- NULL, NULL);
+ all_configs = config_file_get_configs_by_priority ();
if (!all_configs)
COMMAND_ERROR;
-
- for (ptr_config_file = config_files; ptr_config_file;
- ptr_config_file = ptr_config_file->next_config)
- {
- arraylist_add (all_configs, ptr_config_file);
- }
-
list_size = arraylist_size (all_configs);
for (i = 0; i < list_size; i++)
{
- ptr_config_file = (struct t_config_file *)arraylist_get (
- all_configs, i);
- if (config_file_valid (ptr_config_file))
- command_reload_file (ptr_config_file);
+ ptr_config = (struct t_config_file *)arraylist_get (all_configs, i);
+ if (config_file_valid (ptr_config))
+ command_reload_file (ptr_config);
}
-
arraylist_free (all_configs);
}
@@ -5825,8 +5782,9 @@ command_save_file (struct t_config_file *config_file)
COMMAND_CALLBACK(save)
{
- struct t_config_file *ptr_config_file;
- int i;
+ struct t_config_file *ptr_config;
+ struct t_arraylist *all_configs;
+ int i, list_size;
/* make C compiler happy */
(void) pointer;
@@ -5839,10 +5797,10 @@ COMMAND_CALLBACK(save)
/* save configuration files asked by user */
for (i = 1; i < argc; i++)
{
- ptr_config_file = config_file_search (argv[i]);
- if (ptr_config_file)
+ ptr_config = config_file_search (argv[i]);
+ if (ptr_config)
{
- command_save_file (ptr_config_file);
+ command_save_file (ptr_config);
}
else
{
@@ -5855,11 +5813,17 @@ COMMAND_CALLBACK(save)
else
{
/* save all configuration files */
- for (ptr_config_file = config_files; ptr_config_file;
- ptr_config_file = ptr_config_file->next_config)
+ all_configs = config_file_get_configs_by_priority ();
+ if (!all_configs)
+ COMMAND_ERROR;
+ list_size = arraylist_size (all_configs);
+ for (i = 0; i < list_size; i++)
{
- command_save_file (ptr_config_file);
+ ptr_config = (struct t_config_file *)arraylist_get (all_configs, i);
+ if (config_file_valid (ptr_config))
+ command_save_file (ptr_config);
}
+ arraylist_free (all_configs);
}
return WEECHAT_RC_OK;
diff --git a/src/core/wee-config-file.c b/src/core/wee-config-file.c
index 3cb12ab9f..0072653e1 100644
--- a/src/core/wee-config-file.c
+++ b/src/core/wee-config-file.c
@@ -35,6 +35,7 @@
#include "weechat.h"
#include "wee-config-file.h"
+#include "wee-arraylist.h"
#include "wee-config.h"
#include "wee-hdata.h"
#include "wee-hook.h"
@@ -251,6 +252,63 @@ config_file_new (struct t_weechat_plugin *plugin, const char *name,
}
/*
+ * Compares two configuration files to sort them by priority (highest priority
+ * at beginning of list).
+ *
+ * Returns:
+ * -1: config1 has higher priority than config2
+ * 1: config1 has same or lower priority than config2
+ */
+
+int
+config_file_arraylist_cmp_config_cb (void *data,
+ struct t_arraylist *arraylist,
+ void *pointer1, void *pointer2)
+{
+ struct t_config_file *ptr_config1, *ptr_config2;
+
+ /* make C compiler happy */
+ (void) data;
+ (void) arraylist;
+
+ ptr_config1 = (struct t_config_file *)pointer1;
+ ptr_config2 = (struct t_config_file *)pointer2;
+
+ return (ptr_config1->priority > ptr_config2->priority) ? -1 : 1;
+}
+
+/*
+ * Returns an arraylist with pointers to configuration files, sorted by
+ * priority (from highest to lowest).
+ */
+
+struct t_arraylist *
+config_file_get_configs_by_priority ()
+{
+ struct t_arraylist *list;
+ struct t_config_file *ptr_config;
+
+ /*
+ * build a list of pointers to configs sorted by priority,
+ * so that configs with high priority are reloaded first
+ */
+ list = arraylist_new (
+ 32, 1, 1,
+ &config_file_arraylist_cmp_config_cb, NULL,
+ NULL, NULL);
+ if (!list)
+ return NULL;
+
+ for (ptr_config = config_files; ptr_config;
+ ptr_config = ptr_config->next_config)
+ {
+ arraylist_add (list, ptr_config);
+ }
+
+ return list;
+}
+
+/*
* Searches for position of section in configuration file (to keep sections
* sorted by name).
*/
diff --git a/src/core/wee-config-file.h b/src/core/wee-config-file.h
index fe337bcf0..771be2aa5 100644
--- a/src/core/wee-config-file.h
+++ b/src/core/wee-config-file.h
@@ -178,6 +178,7 @@ extern struct t_config_file *config_file_new (struct t_weechat_plugin *plugin,
struct t_config_file *config_file),
const void *callback_reload_pointer,
void *callback_reload_data);
+extern struct t_arraylist *config_file_get_configs_by_priority ();
extern struct t_config_section *config_file_new_section (struct t_config_file *config_file,
const char *name,
int user_can_add_options,