Project

General

Profile

Bug #262 » 0001-hash_insert-check-if-key-isn-t-already-here-before.patch

Pierre-Louis Bonicoli, 2011-11-13 16:32

View differences:

src/util.c
568 568
	struct hash_item *it;
569 569

  
570 570
	assert(hash && key);
571
	if (hash_get(hash, key))
571
	if (hash_has_key(hash, key))
572 572
		fatal("Element with key %s already in hash %x\n", key, hash);
573 573

  
574 574
	it = bip_malloc(sizeof(struct hash_item));
......
601 601
	return hi->item;
602 602
}
603 603

  
604
int hash_has_key(hash_t *hash, const char *key)
605
{
606
	hash_iterator_t hi;
607
    struct hash_item *it;
608
	for (hash_it_init(hash, &hi); hash_it_key(&hi); hash_it_next(&hi)) {
609
        it = list_it_item(&hi.lit);
610
		if (hi.lit.list->cmp(it, key) == 0)
611
            return 1;
612
    }
613
    return 0;
614
}
615

  
604 616
void *hash_remove_if_exists(hash_t *hash, const char *key)
605 617
{
606 618
	assert(hash && key);
src/util.h
159 159
void *hash_it_remove(hash_iterator_t *li);
160 160
list_t *hash_keys(hash_t *hash);
161 161
void hash_rename_key(hash_t *h, const char *oldk, const char *newk);
162
int hash_has_key(hash_t *hash, const char *key);
162 163

  
163 164
int is_valid_nick(char *str);
164 165
int is_valid_username(char *str);
165
- 
(1-1/2)