diff options
author | Kevin McCarthy <kevin@8t8.us> | 2017-01-17 16:09:11 -0800 |
---|---|---|
committer | Kevin McCarthy <kevin@8t8.us> | 2017-01-17 16:09:11 -0800 |
commit | 58d04543821ff89c3c403af9b46d0383dd52ad63 (patch) | |
tree | 74d4b8d02fb19a4ec1812fbc521d55218cf2e6f6 /hash.c | |
parent | 354b88e4c04398073fc09f94a5f9b1daa7653722 (diff) |
Add casecmp and strdup_key flags to hash_create()
Aliases and (in the future), X-Label hashes will require a hash that
strdups the key. Convert the casecmp parameter of hash_create() to a
flags parameter, and add a flag to strdup the keys.
Diffstat (limited to 'hash.c')
-rw-r--r-- | hash.c | 14 |
1 files changed, 10 insertions, 4 deletions
@@ -79,7 +79,7 @@ static int cmp_int_key (union hash_key a, union hash_key b) static HASH *new_hash (int nelem) { - HASH *table = safe_malloc (sizeof (HASH)); + HASH *table = safe_calloc (1, sizeof (HASH)); if (nelem == 0) nelem = 2; table->nelem = nelem; @@ -87,10 +87,10 @@ static HASH *new_hash (int nelem) return table; } -HASH *hash_create (int nelem, int lower) +HASH *hash_create (int nelem, int flags) { HASH *table = new_hash (nelem); - if (lower) + if (flags & MUTT_HASH_STRCASECMP) { table->gen_hash = gen_case_string_hash; table->cmp_key = cmp_case_string_key; @@ -100,6 +100,8 @@ HASH *hash_create (int nelem, int lower) table->gen_hash = gen_string_hash; table->cmp_key = cmp_string_key; } + if (flags & MUTT_HASH_STRDUP_KEYS) + table->strdup_keys = 1; return table; } @@ -159,7 +161,7 @@ static int union_hash_insert (HASH * table, union hash_key key, void *data, int int hash_insert (HASH * table, const char *strkey, void *data, int allow_dup) { union hash_key key; - key.strkey = strkey; + key.strkey = table->strdup_keys ? safe_strdup (strkey) : strkey; return union_hash_insert (table, key, data, allow_dup); } @@ -236,6 +238,8 @@ static void union_hash_delete (HASH *table, union hash_key key, const void *data *last = ptr->next; if (destroy) destroy (ptr->data); + if (table->strdup_keys) + FREE (&ptr->key.strkey); FREE (&ptr); ptr = *last; @@ -285,6 +289,8 @@ void hash_destroy (HASH **ptr, void (*destroy) (void *)) elem = elem->next; if (destroy) destroy (tmp->data); + if (pptr->strdup_keys) + FREE (&tmp->key.strkey); FREE (&tmp); } } |