mirror of
https://github.com/weechat/weechat.git
synced 2026-06-12 14:14:48 +02:00
core: move function hook_get_priority_and_name to wee-string.c (issue #1872)
This commit is contained in:
@@ -78,7 +78,8 @@ hook_command_run (struct t_weechat_plugin *plugin,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hook_get_priority_and_name (command, &priority, &ptr_command);
|
||||
string_get_priority_and_name (command, &priority, &ptr_command,
|
||||
HOOK_PRIORITY_DEFAULT);
|
||||
hook_init_data (new_hook, plugin, HOOK_TYPE_COMMAND_RUN, priority,
|
||||
callback_pointer, callback_data);
|
||||
|
||||
|
||||
@@ -308,7 +308,8 @@ hook_command (struct t_weechat_plugin *plugin, const char *command,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hook_get_priority_and_name (command, &priority, &ptr_command);
|
||||
string_get_priority_and_name (command, &priority, &ptr_command,
|
||||
HOOK_PRIORITY_DEFAULT);
|
||||
hook_init_data (new_hook, plugin, HOOK_TYPE_COMMAND, priority,
|
||||
callback_pointer, callback_data);
|
||||
|
||||
|
||||
@@ -78,7 +78,9 @@ hook_completion (struct t_weechat_plugin *plugin, const char *completion_item,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hook_get_priority_and_name (completion_item, &priority, &ptr_completion_item);
|
||||
string_get_priority_and_name (completion_item,
|
||||
&priority, &ptr_completion_item,
|
||||
HOOK_PRIORITY_DEFAULT);
|
||||
hook_init_data (new_hook, plugin, HOOK_TYPE_COMPLETION, priority,
|
||||
callback_pointer, callback_data);
|
||||
|
||||
|
||||
@@ -75,7 +75,8 @@ hook_config (struct t_weechat_plugin *plugin, const char *option,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hook_get_priority_and_name (option, &priority, &ptr_option);
|
||||
string_get_priority_and_name (option, &priority, &ptr_option,
|
||||
HOOK_PRIORITY_DEFAULT);
|
||||
hook_init_data (new_hook, plugin, HOOK_TYPE_CONFIG, priority,
|
||||
callback_pointer, callback_data);
|
||||
|
||||
|
||||
@@ -78,7 +78,8 @@ hook_focus (struct t_weechat_plugin *plugin,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hook_get_priority_and_name (area, &priority, &ptr_area);
|
||||
string_get_priority_and_name (area, &priority, &ptr_area,
|
||||
HOOK_PRIORITY_DEFAULT);
|
||||
hook_init_data (new_hook, plugin, HOOK_TYPE_FOCUS, priority,
|
||||
callback_pointer, callback_data);
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "../wee-hook.h"
|
||||
#include "../wee-infolist.h"
|
||||
#include "../wee-log.h"
|
||||
#include "../wee-string.h"
|
||||
|
||||
|
||||
/*
|
||||
@@ -77,7 +78,8 @@ hook_hdata (struct t_weechat_plugin *plugin, const char *hdata_name,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hook_get_priority_and_name (hdata_name, &priority, &ptr_hdata_name);
|
||||
string_get_priority_and_name (hdata_name, &priority, &ptr_hdata_name,
|
||||
HOOK_PRIORITY_DEFAULT);
|
||||
hook_init_data (new_hook, plugin, HOOK_TYPE_HDATA, priority,
|
||||
callback_pointer, callback_data);
|
||||
|
||||
|
||||
@@ -77,7 +77,8 @@ hook_hsignal (struct t_weechat_plugin *plugin, const char *signal,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hook_get_priority_and_name (signal, &priority, &ptr_signal);
|
||||
string_get_priority_and_name (signal, &priority, &ptr_signal,
|
||||
HOOK_PRIORITY_DEFAULT);
|
||||
hook_init_data (new_hook, plugin, HOOK_TYPE_HSIGNAL, priority,
|
||||
callback_pointer, callback_data);
|
||||
|
||||
|
||||
@@ -77,7 +77,8 @@ hook_info_hashtable (struct t_weechat_plugin *plugin, const char *info_name,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hook_get_priority_and_name (info_name, &priority, &ptr_info_name);
|
||||
string_get_priority_and_name (info_name, &priority, &ptr_info_name,
|
||||
HOOK_PRIORITY_DEFAULT);
|
||||
hook_init_data (new_hook, plugin, HOOK_TYPE_INFO_HASHTABLE, priority,
|
||||
callback_pointer, callback_data);
|
||||
|
||||
|
||||
@@ -76,7 +76,8 @@ hook_info (struct t_weechat_plugin *plugin, const char *info_name,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hook_get_priority_and_name (info_name, &priority, &ptr_info_name);
|
||||
string_get_priority_and_name (info_name, &priority, &ptr_info_name,
|
||||
HOOK_PRIORITY_DEFAULT);
|
||||
hook_init_data (new_hook, plugin, HOOK_TYPE_INFO, priority,
|
||||
callback_pointer, callback_data);
|
||||
|
||||
|
||||
@@ -77,7 +77,8 @@ hook_infolist (struct t_weechat_plugin *plugin, const char *infolist_name,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hook_get_priority_and_name (infolist_name, &priority, &ptr_infolist_name);
|
||||
string_get_priority_and_name (infolist_name, &priority, &ptr_infolist_name,
|
||||
HOOK_PRIORITY_DEFAULT);
|
||||
hook_init_data (new_hook, plugin, HOOK_TYPE_INFOLIST, priority,
|
||||
callback_pointer, callback_data);
|
||||
|
||||
|
||||
@@ -87,7 +87,8 @@ hook_line (struct t_weechat_plugin *plugin, const char *buffer_type,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hook_get_priority_and_name (buffer_type, &priority, &ptr_buffer_type);
|
||||
string_get_priority_and_name (buffer_type, &priority, &ptr_buffer_type,
|
||||
HOOK_PRIORITY_DEFAULT);
|
||||
hook_init_data (new_hook, plugin, HOOK_TYPE_LINE, priority,
|
||||
callback_pointer, callback_data);
|
||||
|
||||
|
||||
@@ -75,7 +75,8 @@ hook_modifier (struct t_weechat_plugin *plugin, const char *modifier,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hook_get_priority_and_name (modifier, &priority, &ptr_modifier);
|
||||
string_get_priority_and_name (modifier, &priority, &ptr_modifier,
|
||||
HOOK_PRIORITY_DEFAULT);
|
||||
hook_init_data (new_hook, plugin, HOOK_TYPE_MODIFIER, priority,
|
||||
callback_pointer, callback_data);
|
||||
|
||||
|
||||
@@ -77,7 +77,8 @@ hook_signal (struct t_weechat_plugin *plugin, const char *signal,
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hook_get_priority_and_name (signal, &priority, &ptr_signal);
|
||||
string_get_priority_and_name (signal, &priority, &ptr_signal,
|
||||
HOOK_PRIORITY_DEFAULT);
|
||||
hook_init_data (new_hook, plugin, HOOK_TYPE_SIGNAL, priority,
|
||||
callback_pointer, callback_data);
|
||||
|
||||
|
||||
@@ -340,51 +340,6 @@ hook_remove_deleted ()
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Extracts priority and name from a string.
|
||||
*
|
||||
* String can be:
|
||||
* - a simple name like "test":
|
||||
* => priority = 1000 (default), name = "test"
|
||||
* - a priority + "|" + name, like "500|test":
|
||||
* => priority = 500, name = "test"
|
||||
*/
|
||||
|
||||
void
|
||||
hook_get_priority_and_name (const char *string,
|
||||
int *priority, const char **name)
|
||||
{
|
||||
char *pos, *str_priority, *error;
|
||||
long number;
|
||||
|
||||
if (priority)
|
||||
*priority = HOOK_PRIORITY_DEFAULT;
|
||||
if (name)
|
||||
*name = string;
|
||||
|
||||
if (!string)
|
||||
return;
|
||||
|
||||
pos = strchr (string, '|');
|
||||
if (pos)
|
||||
{
|
||||
str_priority = string_strndup (string, pos - string);
|
||||
if (str_priority)
|
||||
{
|
||||
error = NULL;
|
||||
number = strtol (str_priority, &error, 10);
|
||||
if (error && !error[0])
|
||||
{
|
||||
if (priority)
|
||||
*priority = number;
|
||||
if (name)
|
||||
*name = pos + 1;
|
||||
}
|
||||
free (str_priority);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Initializes a new hook with default values.
|
||||
*/
|
||||
|
||||
@@ -123,8 +123,6 @@ extern int hook_socketpair_ok;
|
||||
|
||||
extern void hook_init ();
|
||||
extern void hook_add_to_list (struct t_hook *new_hook);
|
||||
extern void hook_get_priority_and_name (const char *string, int *priority,
|
||||
const char **name);
|
||||
extern void hook_init_data (struct t_hook *hook,
|
||||
struct t_weechat_plugin *plugin,
|
||||
int type, int priority,
|
||||
|
||||
@@ -4129,6 +4129,52 @@ string_replace_with_callback (const char *string,
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* Extracts priority and name from a string.
|
||||
*
|
||||
* String can be:
|
||||
* - a simple name like "test":
|
||||
* => priority = default_priority, name = "test"
|
||||
* - a priority + "|" + name, like "500|test":
|
||||
* => priority = 500, name = "test"
|
||||
*/
|
||||
|
||||
void
|
||||
string_get_priority_and_name (const char *string,
|
||||
int *priority, const char **name,
|
||||
int default_priority)
|
||||
{
|
||||
char *pos, *str_priority, *error;
|
||||
long number;
|
||||
|
||||
if (priority)
|
||||
*priority = default_priority;
|
||||
if (name)
|
||||
*name = string;
|
||||
|
||||
if (!string)
|
||||
return;
|
||||
|
||||
pos = strchr (string, '|');
|
||||
if (pos)
|
||||
{
|
||||
str_priority = string_strndup (string, pos - string);
|
||||
if (str_priority)
|
||||
{
|
||||
error = NULL;
|
||||
number = strtol (str_priority, &error, 10);
|
||||
if (error && !error[0])
|
||||
{
|
||||
if (priority)
|
||||
*priority = number;
|
||||
if (name)
|
||||
*name = pos + 1;
|
||||
}
|
||||
free (str_priority);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Hashes a shared string.
|
||||
* The string starts after the reference count, which is skipped.
|
||||
|
||||
@@ -141,6 +141,10 @@ extern char *string_replace_with_callback (const char *string,
|
||||
char *(*callback)(void *data, const char *text),
|
||||
void *callback_data,
|
||||
int *errors);
|
||||
extern void string_get_priority_and_name (const char *string,
|
||||
int *priority,
|
||||
const char **name,
|
||||
int default_priority);
|
||||
extern const char *string_shared_get (const char *string);
|
||||
extern void string_shared_free (const char *string);
|
||||
extern char **string_dyn_alloc (int size_alloc);
|
||||
|
||||
@@ -2540,6 +2540,67 @@ TEST(CoreString, InputForBuffer)
|
||||
config_file_option_reset (config_look_command_chars, 1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests functions:
|
||||
* string_get_priority_and_name
|
||||
*/
|
||||
|
||||
TEST(CoreString, GetPriorityAndName)
|
||||
{
|
||||
const char *empty = "";
|
||||
const char *delimiter = "|";
|
||||
const char *name = "test";
|
||||
const char *name_prio_empty = "|test";
|
||||
const char *name_prio = "1234|test";
|
||||
const char *ptr_name;
|
||||
int priority;
|
||||
|
||||
string_get_priority_and_name (NULL, NULL, NULL, 0);
|
||||
string_get_priority_and_name ("test", NULL, NULL, 0);
|
||||
|
||||
/* NULL => (default_priority, NULL) */
|
||||
priority = -1;
|
||||
ptr_name = NULL;
|
||||
string_get_priority_and_name (NULL, &priority, &ptr_name, 500);
|
||||
LONGS_EQUAL(500, priority);
|
||||
POINTERS_EQUAL(NULL, ptr_name);
|
||||
|
||||
/* "" => (default_priority, "") */
|
||||
priority = -1;
|
||||
ptr_name = NULL;
|
||||
string_get_priority_and_name (empty, &priority, &ptr_name, 500);
|
||||
LONGS_EQUAL(500, priority);
|
||||
STRCMP_EQUAL("", ptr_name);
|
||||
|
||||
/* "|" => (0, "") */
|
||||
priority = -1;
|
||||
ptr_name = NULL;
|
||||
string_get_priority_and_name (delimiter, &priority, &ptr_name, 500);
|
||||
LONGS_EQUAL(0, priority);
|
||||
STRCMP_EQUAL("", ptr_name);
|
||||
|
||||
/* "test" => (default_priority, "test") */
|
||||
priority = -1;
|
||||
ptr_name = NULL;
|
||||
string_get_priority_and_name (name, &priority, &ptr_name, 500);
|
||||
LONGS_EQUAL(500, priority);
|
||||
STRCMP_EQUAL("test", ptr_name);
|
||||
|
||||
/* "|test" => (0, "test") */
|
||||
priority = -1;
|
||||
ptr_name = NULL;
|
||||
string_get_priority_and_name (name_prio_empty, &priority, &ptr_name, 500);
|
||||
LONGS_EQUAL(0, priority);
|
||||
STRCMP_EQUAL("test", ptr_name);
|
||||
|
||||
/* "1234|test" => (1234, "test") */
|
||||
priority = -1;
|
||||
ptr_name = NULL;
|
||||
string_get_priority_and_name (name_prio, &priority, &ptr_name, 500);
|
||||
LONGS_EQUAL(1234, priority);
|
||||
STRCMP_EQUAL("test", ptr_name);
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests functions:
|
||||
* string_shared_get
|
||||
|
||||
Reference in New Issue
Block a user