void hashtbl_append(ape_htable_t *htbl, const char *key, int key_len, void *structaddr) { unsigned int key_hash; ape_htable_item_t *hTmp, *hDbl; if (key == NULL) { return; } key_hash = ape_hash_str(key, key_len, htbl->size); hTmp = (ape_htable_item_t *)malloc(sizeof(*hTmp)); hTmp->next = NULL; hTmp->lnext = htbl->first; hTmp->lprev = NULL; hTmp->key.str = malloc(sizeof(char) * (key_len + 1)); hTmp->content.addrs = (void *)structaddr; memcpy(hTmp->key.str, key, key_len + 1); if (htbl->table[key_hash] != NULL) { hDbl = htbl->table[key_hash]; while (hDbl != NULL) { if (strcasecmp(hDbl->key.str, key) == 0) { if (htbl->cleaner) { htbl->cleaner(hTmp); } free(hTmp->key.str); free(hTmp); hDbl->content.addrs = (void *)structaddr; return; } else { hDbl = hDbl->next; } } hTmp->next = htbl->table[key_hash]; } if (htbl->first != NULL) { htbl->first->lprev = hTmp; } htbl->first = hTmp; htbl->table[key_hash] = hTmp; }
void hashtbl_erase(ape_htable_t *htbl, const char *key, int key_len) { unsigned int key_hash; ape_htable_item_t *hTmp, *hPrev; if (key == NULL) { return; } key_hash = ape_hash_str(key, key_len, htbl->size); hTmp = htbl->table[key_hash]; hPrev = NULL; while (hTmp != NULL) { if (strcasecmp(hTmp->key.str, key) == 0) { if (htbl->cleaner) { htbl->cleaner(hTmp); } if (hPrev != NULL) { hPrev->next = hTmp->next; } else { htbl->table[key_hash] = hTmp->next; } if (hTmp->lprev == NULL) { htbl->first = hTmp->lnext; } else { hTmp->lprev->lnext = hTmp->lnext; } if (hTmp->lnext != NULL) { hTmp->lnext->lprev = hTmp->lprev; } free(hTmp->key.str); free(hTmp); return; } hPrev = hTmp; hTmp = hTmp->next; } }
void *hashtbl_seek(ape_htable_t *htbl, const char *key, int key_len) { unsigned int key_hash; ape_htable_item_t *hTmp; if (key == NULL) { return NULL; } key_hash = ape_hash_str(key, key_len); hTmp = htbl->table[key_hash]; while (hTmp != NULL) { if (strcasecmp(hTmp->key.str, key) == 0) { return (void *)(hTmp->addrs); } hTmp = hTmp->next; } return NULL; }
uint32_t hashtbl_seek_val32(ape_htable_t *htbl, const char *key, int key_len) { unsigned int key_hash; ape_htable_item_t *hTmp; if (key == NULL) { return 0; } key_hash = ape_hash_str(key, key_len, htbl->size); hTmp = htbl->table[key_hash]; while (hTmp != NULL) { if (strcasecmp(hTmp->key.str, key) == 0) { return hTmp->content.scalar; } hTmp = hTmp->next; } return 0; }