diff options
author | Sebastien Helleu <flashcode@flashtux.org> | 2011-01-02 16:32:54 +0100 |
---|---|---|
committer | Sebastien Helleu <flashcode@flashtux.org> | 2011-01-02 16:32:54 +0100 |
commit | 44e16c05110a19fd8c622d3aeb46179d05d8ec69 (patch) | |
tree | 6804c2803ad8d51a72150270040427484ba8993b /src/core | |
parent | fcfe8544410de238f6329439b2dadb6cc6a8192a (diff) | |
download | weechat-44e16c05110a19fd8c622d3aeb46179d05d8ec69.zip |
Add function "hashtable_set_pointer" in plugin API
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/wee-hashtable.c | 64 | ||||
-rw-r--r-- | src/core/wee-hashtable.h | 12 |
2 files changed, 66 insertions, 10 deletions
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; @@ -233,6 +236,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" * return 1 if ok, 0 if error @@ -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); @@ -733,6 +768,21 @@ hashtable_get_string (struct t_hashtable *hashtable, const char *property) } /* + * 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); |