From 7ebb91f69bdcc74cd7f18518c22f379bf416ae99 Mon Sep 17 00:00:00 2001 From: Sebastien Helleu Date: Sat, 13 Aug 2011 12:00:25 +0200 Subject: [PATCH] core: add function hashtable_dup (duplicate a hashtable) --- src/core/wee-hashtable.c | 49 ++++++++++++++++++++++++++++++++++++++-- src/core/wee-hashtable.h | 2 ++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/core/wee-hashtable.c b/src/core/wee-hashtable.c index b84b12272..41349a1bd 100644 --- a/src/core/wee-hashtable.c +++ b/src/core/wee-hashtable.c @@ -455,8 +455,53 @@ hashtable_map (struct t_hashtable *hashtable, } /* - * hashtable_get_list_keys_map_cb: function called for each variable in hdata - * to build sorted list of keys + * hashtable_duplicate_map_cb: function called for each variable in hashtable + * to duplicate all keys + */ + +void +hashtable_duplicate_map_cb (void *data, + struct t_hashtable *hashtable, + const void *key, const void *value) +{ + struct t_hashtable *hashtable2; + + /* make C compiler happy */ + (void) hashtable; + + hashtable2 = (struct t_hashtable *)data; + if (hashtable2) + hashtable_set (hashtable2, key, value); +} + +/* + * hashtable_dup: duplicate hashtable + */ + +struct t_hashtable * +hashtable_dup (struct t_hashtable *hashtable) +{ + struct t_hashtable *new_hashtable; + + new_hashtable = hashtable_new (hashtable->size, + hashtable_type_string[hashtable->type_keys], + hashtable_type_string[hashtable->type_values], + hashtable->callback_hash_key, + hashtable->callback_keycmp); + if (new_hashtable) + { + new_hashtable->callback_free_value = hashtable->callback_free_value; + hashtable_map (hashtable, + &hashtable_duplicate_map_cb, + new_hashtable); + } + + return new_hashtable; +} + +/* + * hashtable_get_list_keys_map_cb: function called for each variable in + * hashtable to build sorted list of keys */ void diff --git a/src/core/wee-hashtable.h b/src/core/wee-hashtable.h index b11a83429..c4ac29a8b 100644 --- a/src/core/wee-hashtable.h +++ b/src/core/wee-hashtable.h @@ -121,6 +121,8 @@ extern int hashtable_has_key (struct t_hashtable *hashtable, const void *key); extern void hashtable_map (struct t_hashtable *hashtable, t_hashtable_map *callback_map, void *callback_map_data); +extern struct t_hashtable *hashtable_dup (struct t_hashtable *hashtable); +struct t_weelist *hashtable_get_list_keys (struct t_hashtable *hashtable); extern int hashtable_get_integer (struct t_hashtable *hashtable, const char *property); extern const char *hashtable_get_string (struct t_hashtable *hashtable,