From abb574ec4e8a610c7902d40eb384dd5cfe358843 Mon Sep 17 00:00:00 2001 From: Sebastien Helleu Date: Fri, 9 Aug 2013 23:00:12 +0200 Subject: [PATCH] core: add "callback_free_key" in hashtable --- doc/en/weechat_plugin_api.en.txt | 2 ++ doc/fr/weechat_plugin_api.fr.txt | 2 ++ doc/it/weechat_plugin_api.it.txt | 3 +++ src/core/wee-hashtable.c | 42 ++++++++++++++++++++++---------- src/core/wee-hashtable.h | 3 +++ 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/doc/en/weechat_plugin_api.en.txt b/doc/en/weechat_plugin_api.en.txt index 639b92d02..fbc9c85c3 100644 --- a/doc/en/weechat_plugin_api.en.txt +++ b/doc/en/weechat_plugin_api.en.txt @@ -3690,6 +3690,8 @@ Arguments: * 'hashtable': hashtable pointer * 'property' and 'value': property name, with its value: +** 'callback_free_key': set callback function used to free keys in hashtable + (_new in version 0.4.2_) ** 'callback_free_value': set callback function used to free values in hashtable C example: diff --git a/doc/fr/weechat_plugin_api.fr.txt b/doc/fr/weechat_plugin_api.fr.txt index 36d9ff7b8..188cb5cf0 100644 --- a/doc/fr/weechat_plugin_api.fr.txt +++ b/doc/fr/weechat_plugin_api.fr.txt @@ -3734,6 +3734,8 @@ Paramètres : * 'hashtable' : pointeur vers la hashtable * 'property' et 'value' : nom de la propriété, avec sa valeur : +** 'callback_free_key' : définit la fonction "callback" pour libérer les clés de + la hashtable (_nouveau dans la version 0.4.2_) ** 'callback_free_value' : définit la fonction "callback" pour libérer les valeurs de la hashtable diff --git a/doc/it/weechat_plugin_api.it.txt b/doc/it/weechat_plugin_api.it.txt index 4a5f2bf08..3e1955738 100644 --- a/doc/it/weechat_plugin_api.it.txt +++ b/doc/it/weechat_plugin_api.it.txt @@ -3694,6 +3694,9 @@ Argomenti: * 'hashtable': puntatore alla tabella hash * 'property' e 'value': nome della proprietà, con il proprio valore: +// TRANSLATION MISSING +** 'callback_free_key': set callback function used to free keys in hashtable + (_novità nella versione 0.4.2_) ** 'callback_free_value': imposta la funzione callback usata per liberare i valori nella tabella hash diff --git a/src/core/wee-hashtable.c b/src/core/wee-hashtable.c index 5ea3fea08..42eb6cadf 100644 --- a/src/core/wee-hashtable.c +++ b/src/core/wee-hashtable.c @@ -218,6 +218,7 @@ hashtable_new (int size, new_hashtable->callback_keycmp = (callback_keycmp) ? callback_keycmp : &hashtable_keycmp_default_cb; + new_hashtable->callback_free_key = NULL; new_hashtable->callback_free_value = NULL; } return new_hashtable; @@ -288,18 +289,28 @@ void hashtable_free_key (struct t_hashtable *hashtable, struct t_hashtable_item *item) { - switch (hashtable->type_keys) + if (hashtable->callback_free_key) { - case HASHTABLE_INTEGER: - case HASHTABLE_STRING: - case HASHTABLE_BUFFER: - case HASHTABLE_TIME: - free (item->key); - break; - case HASHTABLE_POINTER: - break; - case HASHTABLE_NUM_TYPES: - break; + (void) (hashtable->callback_free_key) (hashtable, + item->key, + item->value); + } + else + { + switch (hashtable->type_keys) + { + case HASHTABLE_INTEGER: + case HASHTABLE_STRING: + case HASHTABLE_BUFFER: + case HASHTABLE_TIME: + if (item->key) + free (item->key); + break; + case HASHTABLE_POINTER: + break; + case HASHTABLE_NUM_TYPES: + break; + } } } @@ -659,6 +670,7 @@ hashtable_dup (struct t_hashtable *hashtable) hashtable->callback_keycmp); if (new_hashtable) { + new_hashtable->callback_free_key = hashtable->callback_free_key; new_hashtable->callback_free_value = hashtable->callback_free_value; hashtable_map (hashtable, &hashtable_duplicate_map_cb, @@ -1000,7 +1012,9 @@ hashtable_set_pointer (struct t_hashtable *hashtable, const char *property, { if (hashtable && property) { - if (string_strcasecmp (property, "callback_free_value") == 0) + if (string_strcasecmp (property, "callback_free_key") == 0) + hashtable->callback_free_key = pointer; + else if (string_strcasecmp (property, "callback_free_value") == 0) hashtable->callback_free_value = pointer; } } @@ -1184,6 +1198,9 @@ hashtable_print_log (struct t_hashtable *hashtable, const char *name) hashtable_type_string[hashtable->type_values]); log_printf (" callback_hash_key. . . : 0x%lx", hashtable->callback_hash_key); log_printf (" callback_keycmp. . . . : 0x%lx", hashtable->callback_keycmp); + log_printf (" callback_free_key. . . : 0x%lx", hashtable->callback_free_key); + log_printf (" callback_free_value. . : 0x%lx", hashtable->callback_free_value); + log_printf (" keys_values. . . . . . : '%s'", hashtable->keys_values); for (i = 0; i < hashtable->size; i++) { @@ -1238,5 +1255,4 @@ hashtable_print_log (struct t_hashtable *hashtable, const char *name) log_printf (" next_item. . . . . : 0x%lx", ptr_item->next_item); } } - log_printf (" keys_values. . . . . . : '%s'", hashtable->keys_values); } diff --git a/src/core/wee-hashtable.h b/src/core/wee-hashtable.h index 3a07c30a1..456368062 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_key)(struct t_hashtable *hashtable, + void *key, const void *value); typedef void (t_hashtable_free_value)(struct t_hashtable *hashtable, const void *key, void *value); typedef void (t_hashtable_map)(void *data, @@ -102,6 +104,7 @@ struct t_hashtable /* callbacks */ t_hashtable_hash_key *callback_hash_key; /* hash key to int value */ t_hashtable_keycmp *callback_keycmp; /* compare two keys */ + t_hashtable_free_key *callback_free_key; /* callback to free key */ t_hashtable_free_value *callback_free_value; /* callback to free value */ /* keys/values as string */