summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2018-08-07 21:50:04 +0200
committerSébastien Helleu <flashcode@flashtux.org>2018-08-12 20:30:13 +0200
commit12a6f74ec01d9daa2e23dce5ab15b1ee3ce09006 (patch)
treeacf40a6fdbe99a8192c29408f130bdf900f12d2e /src/core
parentd699ae89aa0ff818b6264ad17f62377f1753999d (diff)
downloadweechat-12a6f74ec01d9daa2e23dce5ab15b1ee3ce09006.zip
core: fix check of tags in lines
All changes: - fix check of tags in lines: check lines without tags, fix check of tags with negation ("!tag") - add string functions string_split_tags and string_free_split_tags - add tests on function gui_line_match_tags
Diffstat (limited to 'src/core')
-rw-r--r--src/core/wee-config.c36
-rw-r--r--src/core/wee-hook.c45
-rw-r--r--src/core/wee-string.c67
-rw-r--r--src/core/wee-string.h2
4 files changed, 90 insertions, 60 deletions
diff --git a/src/core/wee-config.c b/src/core/wee-config.c
index c2ae20bc7..c2bb09d30 100644
--- a/src/core/wee-config.c
+++ b/src/core/wee-config.c
@@ -885,9 +885,6 @@ void
config_change_highlight_tags (const void *pointer, void *data,
struct t_config_option *option)
{
- int i;
- char **tags_array;
-
/* make C compiler happy */
(void) pointer;
(void) data;
@@ -895,11 +892,7 @@ config_change_highlight_tags (const void *pointer, void *data,
if (config_highlight_tags)
{
- for (i = 0; i < config_num_highlight_tags; i++)
- {
- string_free_split (config_highlight_tags[i]);
- }
- free (config_highlight_tags);
+ string_free_split_tags (config_highlight_tags);
config_highlight_tags = NULL;
}
config_num_highlight_tags = 0;
@@ -907,22 +900,9 @@ config_change_highlight_tags (const void *pointer, void *data,
if (CONFIG_STRING(config_look_highlight_tags)
&& CONFIG_STRING(config_look_highlight_tags)[0])
{
- tags_array = string_split (CONFIG_STRING(config_look_highlight_tags),
- ",", 0, 0, &config_num_highlight_tags);
- if (tags_array)
- {
- config_highlight_tags = malloc (config_num_highlight_tags *
- sizeof (*config_highlight_tags));
- if (config_highlight_tags)
- {
- for (i = 0; i < config_num_highlight_tags; i++)
- {
- config_highlight_tags[i] = string_split (tags_array[i],
- "+", 0, 0, NULL);
- }
- }
- string_free_split (tags_array);
- }
+ config_highlight_tags = string_split_tags (
+ CONFIG_STRING(config_look_highlight_tags),
+ &config_num_highlight_tags);
}
}
@@ -4618,8 +4598,6 @@ config_weechat_write ()
void
config_weechat_free ()
{
- int i;
-
config_file_free (weechat_config_file);
if (config_highlight_regex)
@@ -4631,11 +4609,7 @@ config_weechat_free ()
if (config_highlight_tags)
{
- for (i = 0; i < config_num_highlight_tags; i++)
- {
- string_free_split (config_highlight_tags[i]);
- }
- free (config_highlight_tags);
+ string_free_split_tags (config_highlight_tags);
config_highlight_tags = NULL;
}
config_num_highlight_tags = 0;
diff --git a/src/core/wee-hook.c b/src/core/wee-hook.c
index 8e83c0a8e..66be9451a 100644
--- a/src/core/wee-hook.c
+++ b/src/core/wee-hook.c
@@ -2419,8 +2419,6 @@ hook_print (struct t_weechat_plugin *plugin, struct t_gui_buffer *buffer,
{
struct t_hook *new_hook;
struct t_hook_print *new_hook_print;
- char **tags_array;
- int i;
if (!callback)
return NULL;
@@ -2441,28 +2439,8 @@ hook_print (struct t_weechat_plugin *plugin, struct t_gui_buffer *buffer,
new_hook->hook_data = new_hook_print;
new_hook_print->callback = callback;
new_hook_print->buffer = buffer;
- new_hook_print->tags_count = 0;
- new_hook_print->tags_array = NULL;
- if (tags)
- {
- tags_array = string_split (tags, ",", 0, 0,
- &new_hook_print->tags_count);
- if (tags_array)
- {
- new_hook_print->tags_array = malloc (new_hook_print->tags_count *
- sizeof (*new_hook_print->tags_array));
- if (new_hook_print->tags_array)
- {
- for (i = 0; i < new_hook_print->tags_count; i++)
- {
- new_hook_print->tags_array[i] = string_split (tags_array[i],
- "+", 0, 0,
- NULL);
- }
- }
- string_free_split (tags_array);
- }
- }
+ new_hook_print->tags_array = string_split_tags (tags,
+ &new_hook_print->tags_count);
new_hook_print->message = (message) ? strdup (message) : NULL;
new_hook_print->strip_colors = strip_colors;
@@ -4016,11 +3994,7 @@ unhook (struct t_hook *hook)
case HOOK_TYPE_PRINT:
if (HOOK_PRINT(hook, tags_array))
{
- for (i = 0; i < HOOK_PRINT(hook, tags_count); i++)
- {
- string_free_split (HOOK_PRINT(hook, tags_array)[i]);
- }
- free (HOOK_PRINT(hook, tags_array));
+ string_free_split_tags (HOOK_PRINT(hook, tags_array));
HOOK_PRINT(hook, tags_array) = NULL;
}
if (HOOK_PRINT(hook, message))
@@ -4897,6 +4871,19 @@ hook_print_log ()
log_printf (" buffer. . . . . . . . : 0x%lx", HOOK_PRINT(ptr_hook, buffer));
log_printf (" tags_count. . . . . . : %d", HOOK_PRINT(ptr_hook, tags_count));
log_printf (" tags_array. . . . . . : 0x%lx", HOOK_PRINT(ptr_hook, tags_array));
+ if (HOOK_PRINT(ptr_hook, tags_array))
+ {
+ for (i = 0; i < HOOK_PRINT(ptr_hook, tags_count); i++)
+ {
+ for (j = 0; HOOK_PRINT(ptr_hook, tags_array)[i][j]; j++)
+ {
+ log_printf (" tags_array[%03d][%03d]: '%s'",
+ i,
+ j,
+ HOOK_PRINT(ptr_hook, tags_array)[i][j]);
+ }
+ }
+ }
log_printf (" message . . . . . . . : '%s'", HOOK_PRINT(ptr_hook, message));
log_printf (" strip_colors. . . . . : %d", HOOK_PRINT(ptr_hook, strip_colors));
break;
diff --git a/src/core/wee-string.c b/src/core/wee-string.c
index 6a2939da9..c79a59f43 100644
--- a/src/core/wee-string.c
+++ b/src/core/wee-string.c
@@ -2307,6 +2307,73 @@ string_free_split_command (char **split_command)
}
/*
+ * Splits tags in an array of tags.
+ *
+ * The format of tags is a list of tags separated by commas (logical OR),
+ * and for each item, multiple tags can be separated by "+" (logical AND).
+ *
+ * For example:
+ * irc_join
+ * irc_join,irc_quit
+ * irc_join+nick_toto,irc_quit
+ */
+
+char ***
+string_split_tags (const char *tags, int *num_tags)
+{
+ char ***tags_array, **tags_array_temp;
+ int i, tags_count;
+
+ tags_array = NULL;
+ tags_count = 0;
+
+ if (tags)
+ {
+ tags_array_temp = string_split (tags, ",", 0, 0, &tags_count);
+ if (tags_array_temp && (tags_count > 0))
+ {
+ tags_array = malloc ((tags_count + 1) * sizeof (*tags_array));
+ if (tags_array)
+ {
+ for (i = 0; i < tags_count; i++)
+ {
+ tags_array[i] = string_split_shared (tags_array_temp[i],
+ "+", 0, 0,
+ NULL);
+ }
+ tags_array[tags_count] = NULL;
+ }
+ }
+ if (tags_array_temp)
+ string_free_split (tags_array_temp);
+ }
+
+ if (num_tags)
+ *num_tags = tags_count;
+
+ return tags_array;
+}
+
+/*
+ * Frees tags split.
+ */
+
+void
+string_free_split_tags (char ***split_tags)
+{
+ int i;
+
+ if (split_tags)
+ {
+ for (i = 0; split_tags[i]; i++)
+ {
+ string_free_split_shared (split_tags[i]);
+ }
+ free (split_tags);
+ }
+}
+
+/*
* Converts a string to another charset.
*
* Note: result must be freed after use.
diff --git a/src/core/wee-string.h b/src/core/wee-string.h
index 1679a33ad..d891b17dd 100644
--- a/src/core/wee-string.h
+++ b/src/core/wee-string.h
@@ -95,6 +95,8 @@ extern char *string_build_with_split_string (const char **split_string,
const char *separator);
extern char **string_split_command (const char *command, char separator);
extern void string_free_split_command (char **split_command);
+extern char ***string_split_tags (const char *tags, int *num_tags);
+extern void string_free_split_tags (char ***split_tags);
extern char *string_iconv (int from_utf8, const char *from_code,
const char *to_code, const char *string);
extern char *string_iconv_to_internal (const char *charset, const char *string);