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;
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;
table->gen_hash = gen_string_hash;
table->cmp_key = cmp_string_key;
}
+ if (flags & MUTT_HASH_STRDUP_KEYS)
+ table->strdup_keys = 1;
return table;
}
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);
}
*last = ptr->next;
if (destroy)
destroy (ptr->data);
+ if (table->strdup_keys)
+ FREE (&ptr->key.strkey);
FREE (&ptr);
table->curnelem--;
elem = elem->next;
if (destroy)
destroy (tmp->data);
+ if (pptr->strdup_keys)
+ FREE (&tmp->key.strkey);
FREE (&tmp);
}
}
typedef struct
{
int nelem, curnelem;
+ int strdup_keys; /* if set, the key->strkey is strdup'ed */
struct hash_elem **table;
unsigned int (*gen_hash)(union hash_key, unsigned int);
int (*cmp_key)(union hash_key, union hash_key);
}
HASH;
+/* flags for hash_create() */
+#define MUTT_HASH_STRCASECMP (1<<0) /* use strcasecmp() to compare keys */
+#define MUTT_HASH_STRDUP_KEYS (1<<1) /* make a copy of the keys */
+
HASH *hash_create (int nelem, int lower);
HASH *int_hash_create (int nelem);
err.dptr = err.data;
Groups = hash_create (1031, 0);
- ReverseAlias = hash_create (1031, 1);
+ ReverseAlias = hash_create (1031, MUTT_HASH_STRCASECMP);
#ifdef USE_NOTMUCH
TagTransforms = hash_create (64, 1);
TagFormats = hash_create (64, 0);