1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-30 23:06:38 +02:00

Update of plugin API list functions

This commit is contained in:
Sebastien Helleu
2007-11-04 12:16:22 +01:00
parent 2e18be982f
commit 24bcc4de4b
7 changed files with 352 additions and 97 deletions
+107
View File
@@ -38,6 +38,108 @@
static struct t_weechat_plugin *weechat_plugin = NULL;
/*
* demo_print_list: display a list
*/
static void
demo_print_list (void *list, char *item_name)
{
char *fields, **argv;
int i, j, argc;
i = 1;
while (weechat_list_next (list))
{
weechat_printf (NULL, "--- %s #%d ---", item_name, i);
fields = weechat_list_fields (list);
if (fields)
{
argv = weechat_string_explode (fields, ",", 0, &argc);
if (argv && (argc > 0))
{
for (j = 0; j < argc; j++)
{
switch (argv[j][0])
{
case 'i':
weechat_printf (NULL, " %s: %d",
argv[j] + 2,
weechat_list_int (list,
argv[j] + 2));
break;
case 's':
weechat_printf (NULL, " %s: %s",
argv[j] + 2,
weechat_list_string (list,
argv[j] + 2));
break;
case 'p':
weechat_printf (NULL, " %s: %X",
argv[j] + 2,
weechat_list_pointer (list,
argv[j] + 2));
break;
case 't':
weechat_printf (NULL, " %s: %ld",
argv[j] + 2,
weechat_list_time (list,
argv[j] + 2));
break;
}
}
}
if (argv)
weechat_string_free_exploded (argv);
}
i++;
}
}
/*
* demo_buffer_infos: display buffer infos
*/
static void
demo_buffer_infos ()
{
struct t_plugin_list *list;
list = weechat_list_get ("buffer", NULL);
if (list)
{
demo_print_list (list, "buffer");
weechat_list_free (list);
}
}
/*
* demo_command: demo command
*/
static int
demo_command (void *data, char *args)
{
/* make C compiler happy */
(void) data;
if (args)
{
if (weechat_strcasecmp (args, "buffer") == 0)
{
demo_buffer_infos ();
return PLUGIN_RC_SUCCESS;
}
}
weechat_printf (NULL,
"Demo: missing argument for /demo command "
"(try /help demo)");
return PLUGIN_RC_SUCCESS;
}
/*
* weechat_plugin_init: init demo plugin
*/
@@ -46,6 +148,11 @@ int
weechat_plugin_init (struct t_weechat_plugin *plugin)
{
weechat_plugin = plugin;
weechat_hook_command ("demo", "demo command", "[action]",
"action: one of following actions:\n"
" buffer display infos about buffers",
"buffer", demo_command, NULL);
return PLUGIN_RC_SUCCESS;
}
+87 -56
View File
@@ -155,11 +155,11 @@ plugin_api_strncasecmp (struct t_weechat_plugin *plugin,
}
/*
* plugin_api_explode_string: explode a string
* plugin_api_string_explode: explode a string
*/
char **
plugin_api_explode_string (struct t_weechat_plugin *plugin, char *string,
plugin_api_string_explode (struct t_weechat_plugin *plugin, char *string,
char *separators, int num_items_max,
int *num_items)
{
@@ -174,11 +174,11 @@ plugin_api_explode_string (struct t_weechat_plugin *plugin, char *string,
}
/*
* plugin_api_free_exploded_string: free exploded string
* plugin_api_string_free_exploded: free exploded string
*/
void
plugin_api_free_exploded_string (struct t_weechat_plugin *plugin,
plugin_api_string_free_exploded (struct t_weechat_plugin *plugin,
char **exploded_string)
{
/* make C compiler happy */
@@ -932,93 +932,124 @@ plugin_api_list_get (struct t_weechat_plugin *plugin, char *name,
}
/*
* plugin_api_list_next: get next item in a list
* if current item pointer is NULL,
* then return first item of list
*/
struct t_plugin_list_item *
plugin_api_list_next (struct t_weechat_plugin *plugin, void *list)
{
if (!plugin || !list)
return NULL;
return plugin_list_next_item ((struct t_plugin_list *)list);
}
/*
* plugin_api_list_prev: get previousi item in a list
* if current item pointer is NULL,
* then return last item of list
*/
struct t_plugin_list_item *
plugin_api_list_prev (struct t_weechat_plugin *plugin, void *list)
{
if (!plugin || !list)
return NULL;
return plugin_list_prev_item ((struct t_plugin_list *)list);
}
/*
* plugin_api_list_int: get an integer variable value in an item
* plugin_api_list_next: move item pointer to next item in a list
* return 1 if pointer is still ok
* 0 if end of list was reached
*/
int
plugin_api_list_int (struct t_weechat_plugin *plugin, void *list_item,
char *var)
plugin_api_list_next (struct t_weechat_plugin *plugin, void *list)
{
if (!plugin || !list_item)
if (!plugin || !list
|| !plugin_list_valid ((struct t_plugin_list *)list))
return 0;
return plugin_list_get_int ((struct t_plugin_list_item *)list_item,
var);
return (plugin_list_next_item ((struct t_plugin_list *)list)) ? 1 : 0;
}
/*
* plugin_api_list_string: get a string variable value in an item
* plugin_api_list_prev: move item pointer to previous item in a list
* return 1 if pointer is still ok
* 0 if beginning of list was reached
*/
int
plugin_api_list_prev (struct t_weechat_plugin *plugin, void *list)
{
if (!plugin || !list
|| !plugin_list_valid ((struct t_plugin_list *)list))
return 0;
return (plugin_list_prev_item ((struct t_plugin_list *)list)) ? 1 : 0;
}
/*
* plugin_api_list_fields: get list of fields for current list item
*/
char *
plugin_api_list_string (struct t_weechat_plugin *plugin, void *list_item,
char *var)
plugin_api_list_fields (struct t_weechat_plugin *plugin, void *list)
{
if (!plugin || !list_item)
if (!plugin || !list
|| !plugin_list_valid ((struct t_plugin_list *)list))
return NULL;
return plugin_list_get_string ((struct t_plugin_list_item *)list_item,
var);
return plugin_list_get_fields ((struct t_plugin_list *)list);
}
/*
* plugin_api_list_pointer: get a pointer variable value in an item
* plugin_api_list_int: get an integer variable value in current list item
*/
int
plugin_api_list_int (struct t_weechat_plugin *plugin, void *list,
char *var)
{
if (!plugin || !list
|| !plugin_list_valid ((struct t_plugin_list *)list)
|| !((struct t_plugin_list *)list)->ptr_item)
return 0;
return plugin_list_get_int ((struct t_plugin_list *)list, var);
}
/*
* plugin_api_list_string: get a string variable value in current list item
*/
char *
plugin_api_list_string (struct t_weechat_plugin *plugin, void *list,
char *var)
{
if (!plugin || !list
|| !plugin_list_valid ((struct t_plugin_list *)list)
|| !((struct t_plugin_list *)list)->ptr_item)
return NULL;
return plugin_list_get_string ((struct t_plugin_list *)list, var);
}
/*
* plugin_api_list_pointer: get a pointer variable value in current list item
*/
void *
plugin_api_list_pointer (struct t_weechat_plugin *plugin, void *list_item,
plugin_api_list_pointer (struct t_weechat_plugin *plugin, void *list,
char *var)
{
if (!plugin || !list_item)
if (!plugin || !list
|| !plugin_list_valid ((struct t_plugin_list *)list)
|| !((struct t_plugin_list *)list)->ptr_item)
return NULL;
return plugin_list_get_pointer ((struct t_plugin_list_item *)list_item,
var);
return plugin_list_get_pointer ((struct t_plugin_list *)list, var);
}
/*
* plugin_api_list_time: get a time variable value in an item
* plugin_api_list_time: get a time variable value in current list item
*/
time_t
plugin_api_list_time (struct t_weechat_plugin *plugin, void *list_item,
plugin_api_list_time (struct t_weechat_plugin *plugin, void *list,
char *var)
{
if (!plugin || !list_item)
if (!plugin || !list
|| !plugin_list_valid ((struct t_plugin_list *)list)
|| !((struct t_plugin_list *)list)->ptr_item)
return 0;
return plugin_list_get_time ((struct t_plugin_list_item *)list_item,
var);
return plugin_list_get_time ((struct t_plugin_list *)list, var);
}
/*
* plugin_api_list_free: free a list
*/
void
plugin_api_list_free (struct t_weechat_plugin *plugin, void *list)
{
if (plugin && list && plugin_list_valid ((struct t_plugin_list *)list))
plugin_list_free ((struct t_plugin_list *)list);
}
/*
+14 -13
View File
@@ -32,9 +32,9 @@ extern char *plugin_api_ngettext (struct t_weechat_plugin *, char *, char *,
extern int plugin_api_strcasecmp (struct t_weechat_plugin *,char *, char *);
extern int plugin_api_strncasecmp (struct t_weechat_plugin *,char *, char *,
int);
extern char **plugin_api_explode_string (struct t_weechat_plugin *, char *,
extern char **plugin_api_string_explode (struct t_weechat_plugin *, char *,
char *, int, int *);
extern void plugin_api_free_exploded_string (struct t_weechat_plugin *,
extern void plugin_api_string_free_exploded (struct t_weechat_plugin *,
char **);
/* directories */
@@ -91,17 +91,18 @@ extern void plugin_api_command (struct t_weechat_plugin *, void *, char *);
extern char *plugin_api_info_get (struct t_weechat_plugin *, char *);
/* lists */
extern struct t_plugin_list *(*list_get) (struct t_weechat_plugin *, char *,
void *);
extern struct t_plugin_list_item *list_next (struct t_weechat_plugin *,
void *);
extern struct t_plugin_list_item *list_prev (struct t_weechat_plugin *,
void *);
extern int *list_int (struct t_weechat_plugin *, void *, char *);
extern char *list_str (struct t_weechat_plugin *, void *, char *);
extern void *list_pointer (struct t_weechat_plugin *, void *, char *);
extern time_t list_time (struct t_weechat_plugin *, void *, char *);
extern void list_free (struct t_weechat_plugin *, void *);
extern struct t_plugin_list *plugin_api_list_get (struct t_weechat_plugin *,
char *, void *);
extern int plugin_api_list_next (struct t_weechat_plugin *,
void *);
extern int plugin_api_list_prev (struct t_weechat_plugin *,
void *);
extern char *plugin_api_list_fields (struct t_weechat_plugin *, void *);
extern int plugin_api_list_int (struct t_weechat_plugin *, void *, char *);
extern char *plugin_api_list_string (struct t_weechat_plugin *, void *, char *);
extern void *plugin_api_list_pointer (struct t_weechat_plugin *, void *, char *);
extern time_t plugin_api_list_time (struct t_weechat_plugin *, void *, char *);
extern void plugin_api_list_free (struct t_weechat_plugin *, void *);
/* config */
extern char *plugin_api_config_get (struct t_weechat_plugin *, char *);
+112 -18
View File
@@ -78,6 +78,7 @@ plugin_list_new_item (struct t_plugin_list *list)
{
new_item->vars = NULL;
new_item->last_var = NULL;
new_item->fields = NULL;
new_item->prev_item = list->last_item;
new_item->next_item = NULL;
@@ -100,11 +101,15 @@ plugin_list_new_var_int (struct t_plugin_list_item *item,
char *name, int value)
{
struct t_plugin_list_var *new_var;
if (!item || !name || !name[0])
return NULL;
new_var = (struct t_plugin_list_var *)malloc (sizeof (struct t_plugin_list_var));
if (new_var)
{
new_var->name = strdup (name);
new_var->type = PLUGIN_LIST_VAR_INTEGER;
new_var->value_int = value;
new_var->value_string = NULL;
new_var->value_pointer = NULL;
@@ -132,12 +137,16 @@ plugin_list_new_var_string (struct t_plugin_list_item *item,
{
struct t_plugin_list_var *new_var;
if (!item || !name || !name[0])
return NULL;
new_var = (struct t_plugin_list_var *)malloc (sizeof (struct t_plugin_list_var));
if (new_var)
{
new_var->name = strdup (name);
new_var->type = PLUGIN_LIST_VAR_STRING;
new_var->value_int = 0;
new_var->value_string = strdup (value);
new_var->value_string = (value) ? strdup (value) : NULL;
new_var->value_time = 0;
new_var->prev_var = item->last_var;
@@ -162,10 +171,14 @@ plugin_list_new_var_pointer (struct t_plugin_list_item *item,
{
struct t_plugin_list_var *new_var;
if (!item || !name || !name[0])
return NULL;
new_var = (struct t_plugin_list_var *)malloc (sizeof (struct t_plugin_list_var));
if (new_var)
{
new_var->name = strdup (name);
new_var->type = PLUGIN_LIST_VAR_POINTER;
new_var->value_int = 0;
new_var->value_string = NULL;
new_var->value_pointer = pointer;
@@ -193,10 +206,14 @@ plugin_list_new_var_time (struct t_plugin_list_item *item,
{
struct t_plugin_list_var *new_var;
if (!item || !name || !name[0])
return NULL;
new_var = (struct t_plugin_list_var *)malloc (sizeof (struct t_plugin_list_var));
if (new_var)
{
new_var->name = strdup (name);
new_var->type = PLUGIN_LIST_VAR_TIME;
new_var->value_int = 0;
new_var->value_string = NULL;
new_var->value_pointer = NULL;
@@ -214,6 +231,28 @@ plugin_list_new_var_time (struct t_plugin_list_item *item,
return new_var;
}
/*
* plugin_list_valid: check if a list pointer exists
* return 1 if list exists
* 0 if list is not found
*/
int
plugin_list_valid (struct t_plugin_list *list)
{
struct t_plugin_list *ptr_list;
for (ptr_list = plugin_lists; ptr_list;
ptr_list = ptr_list->next_list)
{
if (ptr_list == list)
return 1;
}
/* list not found */
return 0;
}
/*
* plugin_list_next_item: return next item for a list
* if current item pointer is NULL,
@@ -251,18 +290,71 @@ plugin_list_prev_item (struct t_plugin_list *list)
}
/*
* plugin_list_get_int: get an integer variable value in an item
* plugin_list_get_fields: get list of fields for current list item
*/
char *
plugin_list_get_fields (struct t_plugin_list *list)
{
struct t_plugin_list_var *ptr_var;
int length;
if (!list || !list->ptr_item)
return NULL;
/* list of fields already asked ? if yes, just return string */
if (list->ptr_item->fields)
return list->ptr_item->fields;
length = 0;
for (ptr_var = list->ptr_item->vars; ptr_var; ptr_var = ptr_var->next_var)
{
length += strlen (ptr_var->name) + 3;
}
list->ptr_item->fields = (char *)malloc (length + 1);
if (!list->ptr_item->fields)
return NULL;
list->ptr_item->fields[0] = '\0';
for (ptr_var = list->ptr_item->vars; ptr_var; ptr_var = ptr_var->next_var)
{
switch (ptr_var->type)
{
case PLUGIN_LIST_VAR_INTEGER:
strcat (list->ptr_item->fields, "i:");
break;
case PLUGIN_LIST_VAR_STRING:
strcat (list->ptr_item->fields, "s:");
break;
case PLUGIN_LIST_VAR_POINTER:
strcat (list->ptr_item->fields, "p:");
break;
case PLUGIN_LIST_VAR_TIME:
strcat (list->ptr_item->fields, "t:");
break;
}
strcat (list->ptr_item->fields, ptr_var->name);
if (ptr_var->next_var)
strcat (list->ptr_item->fields, ",");
}
return list->ptr_item->fields;
}
/*
* plugin_list_get_int: get an integer variable value in current list item
*/
int
plugin_list_get_int (struct t_plugin_list_item *item, char *var)
plugin_list_get_int (struct t_plugin_list *list, char *var)
{
struct t_plugin_list_var *ptr_var;
if (!item || !var || !var[0])
if (!list || !list->ptr_item || !var || !var[0])
return 0;
for (ptr_var = item->vars; ptr_var; ptr_var = ptr_var->next_var)
for (ptr_var = list->ptr_item->vars; ptr_var; ptr_var = ptr_var->next_var)
{
if (string_strcasecmp (ptr_var->name, var) == 0)
{
@@ -278,18 +370,18 @@ plugin_list_get_int (struct t_plugin_list_item *item, char *var)
}
/*
* plugin_list_get_string: get a string variable value in an item
* plugin_list_get_string: get a string variable value in current list item
*/
char *
plugin_list_get_string (struct t_plugin_list_item *item, char *var)
plugin_list_get_string (struct t_plugin_list *list, char *var)
{
struct t_plugin_list_var *ptr_var;
if (!item || !var || !var[0])
if (!list || !list->ptr_item || !var || !var[0])
return NULL;
for (ptr_var = item->vars; ptr_var; ptr_var = ptr_var->next_var)
for (ptr_var = list->ptr_item->vars; ptr_var; ptr_var = ptr_var->next_var)
{
if (string_strcasecmp (ptr_var->name, var) == 0)
{
@@ -305,18 +397,18 @@ plugin_list_get_string (struct t_plugin_list_item *item, char *var)
}
/*
* plugin_list_get_pointer: get a pointer variable value in an item
* plugin_list_get_pointer: get a pointer variable value in current list item
*/
void *
plugin_list_get_pointer (struct t_plugin_list_item *item, char *var)
plugin_list_get_pointer (struct t_plugin_list *list, char *var)
{
struct t_plugin_list_var *ptr_var;
if (!item || !var || !var[0])
if (!list || !list->ptr_item || !var || !var[0])
return NULL;
for (ptr_var = item->vars; ptr_var; ptr_var = ptr_var->next_var)
for (ptr_var = list->ptr_item->vars; ptr_var; ptr_var = ptr_var->next_var)
{
if (string_strcasecmp (ptr_var->name, var) == 0)
{
@@ -332,18 +424,18 @@ plugin_list_get_pointer (struct t_plugin_list_item *item, char *var)
}
/*
* plugin_list_get_time: get a time variable value in an item
* plugin_list_get_time: get a time variable value in current list item
*/
time_t
plugin_list_get_time (struct t_plugin_list_item *item, char *var)
plugin_list_get_time (struct t_plugin_list *list, char *var)
{
struct t_plugin_list_var *ptr_var;
if (!item || !var || !var[0])
if (!list || !list->ptr_item || !var || !var[0])
return 0;
for (ptr_var = item->vars; ptr_var; ptr_var = ptr_var->next_var)
for (ptr_var = list->ptr_item->vars; ptr_var; ptr_var = ptr_var->next_var)
{
if (string_strcasecmp (ptr_var->name, var) == 0)
{
@@ -420,6 +512,8 @@ plugin_list_item_free (struct t_plugin_list *list,
{
plugin_list_var_free (item, item->vars);
}
if (item->fields)
free (item->fields);
list->items = new_items;
}
+8 -4
View File
@@ -46,6 +46,8 @@ struct t_plugin_list_item
{
struct t_plugin_list_var *vars; /* item variables */
struct t_plugin_list_var *last_var; /* last variable */
char *fields; /* fields list (NULL if never */
/* asked) */
struct t_plugin_list_item *prev_item; /* link to previous item */
struct t_plugin_list_item *next_item; /* link to next item */
};
@@ -76,12 +78,14 @@ extern struct t_plugin_list_var *plugin_list_new_var_pointer (struct t_plugin_li
char *, void *);
extern struct t_plugin_list_var *plugin_list_new_var_time (struct t_plugin_list_item *,
char *, time_t);
extern int plugin_list_valid (struct t_plugin_list *);
extern struct t_plugin_list_item *plugin_list_next_item (struct t_plugin_list *);
extern struct t_plugin_list_item *plugin_list_prev_item (struct t_plugin_list *);
extern int plugin_list_get_int (struct t_plugin_list_item *, char *);
extern char *plugin_list_get_string (struct t_plugin_list_item *, char *);
extern void *plugin_list_get_pointer (struct t_plugin_list_item *, char *);
extern time_t plugin_list_get_time (struct t_plugin_list_item *, char *);
extern char *plugin_list_get_fields (struct t_plugin_list *);
extern int plugin_list_get_int (struct t_plugin_list *, char *);
extern char *plugin_list_get_string (struct t_plugin_list *, char *);
extern void *plugin_list_get_pointer (struct t_plugin_list *, char *);
extern time_t plugin_list_get_time (struct t_plugin_list *, char *);
extern void plugin_list_free (struct t_plugin_list *);
extern void plugin_list_print_log ();
+12 -2
View File
@@ -231,8 +231,8 @@ plugin_load (char *filename)
new_plugin->ngettext = &plugin_api_ngettext;
new_plugin->strcasecmp = &plugin_api_strcasecmp;
new_plugin->strncasecmp = &plugin_api_strncasecmp;
new_plugin->explode_string = &plugin_api_explode_string;
new_plugin->free_exploded_string = &plugin_api_free_exploded_string;
new_plugin->string_explode = &plugin_api_string_explode;
new_plugin->string_free_exploded = &plugin_api_string_free_exploded;
new_plugin->mkdir_home = &plugin_api_mkdir_home;
new_plugin->exec_on_files = &plugin_api_exec_on_files;
@@ -262,6 +262,16 @@ plugin_load (char *filename)
new_plugin->info_get = &plugin_api_info_get;
new_plugin->list_get = &plugin_api_list_get;
new_plugin->list_next = &plugin_api_list_next;
new_plugin->list_prev = &plugin_api_list_prev;
new_plugin->list_fields = &plugin_api_list_fields;
new_plugin->list_int = &plugin_api_list_int;
new_plugin->list_string = &plugin_api_list_string;
new_plugin->list_pointer = &plugin_api_list_pointer;
new_plugin->list_time = &plugin_api_list_time;
new_plugin->list_free = &plugin_api_list_free;
new_plugin->config_get = &plugin_api_config_get;
new_plugin->config_set = &plugin_api_config_set;
new_plugin->plugin_config_get = &plugin_api_plugin_config_get;
+12 -4
View File
@@ -63,9 +63,9 @@ struct t_weechat_plugin
char *(*ngettext) (struct t_weechat_plugin *, char *, char *, int);
int (*strcasecmp) (struct t_weechat_plugin *, char *, char *);
int (*strncasecmp) (struct t_weechat_plugin *, char *, char *, int);
char **(*explode_string) (struct t_weechat_plugin *, char *, char *, int,
char **(*string_explode) (struct t_weechat_plugin *, char *, char *, int,
int *);
void (*free_exploded_string) (struct t_weechat_plugin *, char **);
void (*string_free_exploded) (struct t_weechat_plugin *, char **);
/* directories */
int (*mkdir_home) (struct t_weechat_plugin *, char *);
@@ -115,8 +115,9 @@ struct t_weechat_plugin
/* lists */
struct t_plugin_list *(*list_get) (struct t_weechat_plugin *, char *,
void *);
struct t_plugin_list *(*list_next) (struct t_weechat_plugin *, void *);
struct t_plugin_list *(*list_prev) (struct t_weechat_plugin *, void *);
int (*list_next) (struct t_weechat_plugin *, void *);
int (*list_prev) (struct t_weechat_plugin *, void *);
char *(*list_fields) (struct t_weechat_plugin *, void *);
int (*list_int) (struct t_weechat_plugin *, void *, char *);
char *(*list_string) (struct t_weechat_plugin *, void *, char *);
void *(*list_pointer) (struct t_weechat_plugin *, void *, char *);
@@ -147,6 +148,11 @@ struct t_weechat_plugin
weechat_plugin->strcasecmp(weechat_plugin, string1, string2)
#define weechat_strncasecmp(string1, string2, max) \
weechat_plugin->strncasecmp(weechat_plugin, string1, string2, max)
#define weechat_string_explode(string1, separator, max, num_items) \
weechat_plugin->string_explode(weechat_plugin, string1, separator, \
max, num_items)
#define weechat_string_free_exploded(array_str) \
weechat_plugin->string_free_exploded(weechat_plugin, array_str)
#define weechat_printf(buffer, argz...) \
weechat_plugin->printf(weechat_plugin, buffer, ##argz)
@@ -199,6 +205,8 @@ struct t_weechat_plugin
weechat_plugin->list_next(weechat_plugin, ptrlist)
#define weechat_list_prev(ptrlist) \
weechat_plugin->list_prev(weechat_plugin, ptrlist)
#define weechat_list_fields(ptrlist) \
weechat_plugin->list_fields(weechat_plugin, ptrlist)
#define weechat_list_int(ptritem, var) \
weechat_plugin->list_int(weechat_plugin, ptritem, var)
#define weechat_list_string(ptritem, var) \