From 44e16c05110a19fd8c622d3aeb46179d05d8ec69 Mon Sep 17 00:00:00 2001 From: Sebastien Helleu Date: Sun, 2 Jan 2011 16:32:54 +0100 Subject: Add function "hashtable_set_pointer" in plugin API --- src/core/wee-hashtable.c | 64 +++++++++++++++++++++++++++++++++++++++----- src/core/wee-hashtable.h | 12 ++++++--- src/plugins/plugin.c | 1 + src/plugins/weechat-plugin.h | 9 ++++++- 4 files changed, 75 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/core/wee-hashtable.c b/src/core/wee-hashtable.c index 2080426d3..99210cbc7 100644 --- a/src/core/wee-hashtable.c +++ b/src/core/wee-hashtable.c @@ -149,6 +149,8 @@ hashtable_new (int size, new_hashtable->callback_keycmp = &hashtable_keycmp_string_cb; else new_hashtable->callback_keycmp = callback_keycmp; + + new_hashtable->callback_free_value = NULL; } return new_hashtable; } @@ -211,19 +213,20 @@ hashtable_alloc_type (enum t_hashtable_type type, } /* - * hashtable_free_type: free space used by a key or value + * hashtable_free_key: free space used by a key */ void -hashtable_free_type (enum t_hashtable_type type, void *value) +hashtable_free_key (struct t_hashtable *hashtable, + struct t_hashtable_item *item) { - switch (type) + switch (hashtable->type_keys) { case HASHTABLE_INTEGER: case HASHTABLE_STRING: case HASHTABLE_BUFFER: case HASHTABLE_TIME: - free (value); + free (item->key); break; case HASHTABLE_POINTER: break; @@ -232,6 +235,38 @@ hashtable_free_type (enum t_hashtable_type type, void *value) } } +/* + * hashtable_free_value: free space used by a value + */ + +void +hashtable_free_value (struct t_hashtable *hashtable, + struct t_hashtable_item *item) +{ + if (hashtable->callback_free_value) + { + (void) (hashtable->callback_free_value) (hashtable, + item->key, + item->value); + } + else + { + switch (hashtable->type_values) + { + case HASHTABLE_INTEGER: + case HASHTABLE_STRING: + case HASHTABLE_BUFFER: + case HASHTABLE_TIME: + free (item->value); + break; + case HASHTABLE_POINTER: + break; + case HASHTABLE_NUM_TYPES: + break; + } + } +} + /* * hashtable_set_with_size: set value for item in hash table * argument size is used only for type "buffer" @@ -267,7 +302,7 @@ hashtable_set_with_size (struct t_hashtable *hashtable, /* replace value if item is already in hash table */ if (ptr_item && (hashtable->callback_keycmp (hashtable, key, ptr_item->key) == 0)) { - hashtable_free_type (hashtable->type_values, ptr_item->value); + hashtable_free_value (hashtable, ptr_item); hashtable_alloc_type (hashtable->type_values, value, value_size, &ptr_item->value, &ptr_item->value_size); @@ -732,6 +767,21 @@ hashtable_get_string (struct t_hashtable *hashtable, const char *property) return NULL; } +/* + * hashtable_set_pointer: set a hashtable property (pointer) + */ + +void +hashtable_set_pointer (struct t_hashtable *hashtable, const char *property, + void *pointer) +{ + if (hashtable && property) + { + if (string_strcasecmp (property, "callback_free_value") == 0) + hashtable->callback_free_value = pointer; + } +} + /* * hashtable_add_to_infolist: add hashtable keys and values to infolist * return 1 if ok, 0 if error @@ -813,8 +863,8 @@ hashtable_remove_item (struct t_hashtable *hashtable, return; /* free key and value */ - hashtable_free_type (hashtable->type_keys, item->key); - hashtable_free_type (hashtable->type_values, item->value); + hashtable_free_value (hashtable, item); + hashtable_free_key (hashtable, item); /* remove item from list */ if (item->prev_item) diff --git a/src/core/wee-hashtable.h b/src/core/wee-hashtable.h index 4a76c6f21..b11a83429 100644 --- a/src/core/wee-hashtable.h +++ b/src/core/wee-hashtable.h @@ -27,6 +27,8 @@ typedef unsigned int (t_hashtable_hash_key)(struct t_hashtable *hashtable, const void *key); typedef int (t_hashtable_keycmp)(struct t_hashtable *hashtable, const void *key1, const void *key2); +typedef void (t_hashtable_free_value)(struct t_hashtable *hashtable, + const void *key, void *value); typedef void (t_hashtable_map)(void *data, struct t_hashtable *hashtable, const void *key, const void *value); @@ -95,9 +97,10 @@ struct t_hashtable enum t_hashtable_type type_values; /* type for values: int/str/pointer */ /* callbacks */ - t_hashtable_hash_key *callback_hash_key; /* hash key to integer value */ - t_hashtable_keycmp *callback_keycmp; /* compare two keys */ - + t_hashtable_hash_key *callback_hash_key; /* hash key to int value */ + t_hashtable_keycmp *callback_keycmp; /* compare two keys */ + t_hashtable_free_value *callback_free_value; /* callback to free value */ + /* keys/values as string */ char *keys_values; /* keys/values as string (NULL if */ /* never asked) */ @@ -122,6 +125,9 @@ extern int hashtable_get_integer (struct t_hashtable *hashtable, const char *property); extern const char *hashtable_get_string (struct t_hashtable *hashtable, const char *property); +extern void hashtable_set_pointer (struct t_hashtable *hashtable, + const char *property, + void *pointer); extern int hashtable_add_to_infolist (struct t_hashtable *hashtable, struct t_infolist_item *infolist_item, const char *prefix); diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c index 7029154bb..771872023 100644 --- a/src/plugins/plugin.c +++ b/src/plugins/plugin.c @@ -532,6 +532,7 @@ plugin_load (const char *filename) new_plugin->hashtable_map = &hashtable_map; new_plugin->hashtable_get_integer = &hashtable_get_integer; new_plugin->hashtable_get_string = &hashtable_get_string; + new_plugin->hashtable_set_pointer = &hashtable_set_pointer; new_plugin->hashtable_add_to_infolist = &hashtable_add_to_infolist; new_plugin->hashtable_remove = &hashtable_remove; new_plugin->hashtable_remove_all = &hashtable_remove_all; diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h index 565e9cd1d..8f4550492 100644 --- a/src/plugins/weechat-plugin.h +++ b/src/plugins/weechat-plugin.h @@ -45,7 +45,7 @@ struct timeval; */ /* API version (used to check that plugin has same API and can be loaded) */ -#define WEECHAT_PLUGIN_API_VERSION "20101220-01" +#define WEECHAT_PLUGIN_API_VERSION "20110102-01" /* macros for defining plugin infos */ #define WEECHAT_PLUGIN_NAME(__name) \ @@ -281,6 +281,9 @@ struct t_weechat_plugin const char *property); const char *(*hashtable_get_string) (struct t_hashtable *hashtable, const char *property); + void (*hashtable_set_pointer) (struct t_hashtable *hashtable, + const char *property, + void *pointer); int (*hashtable_add_to_infolist) (struct t_hashtable *hashtable, struct t_infolist_item *infolist_item, const char *prefix); @@ -950,6 +953,10 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin); weechat_plugin->hashtable_get_integer(__hashtable, __property) #define weechat_hashtable_get_string(__hashtable, __property) \ weechat_plugin->hashtable_get_string(__hashtable, __property) +#define weechat_hashtable_set_pointer(__hashtable, __property, \ + __pointer) \ + weechat_plugin->hashtable_set_pointer(__hashtable, __property, \ + __pointer) #define weechat_hashtable_add_to_infolist(__hashtable, __infolist_item, \ __prefix) \ weechat_plugin->hashtable_add_to_infolist(__hashtable, \ -- cgit v1.2.3