allow ordered dictionary

This commit is contained in:
lxsang 2020-09-22 16:49:43 +02:00
parent 1e4856b08a
commit 9da33124bc
3 changed files with 91 additions and 70 deletions

Binary file not shown.

View File

@ -211,7 +211,7 @@ static int config_handler(void *conf, const char *section, const char *name,
p = (port_config_t *)malloc(sizeof(port_config_t)); p = (port_config_t *)malloc(sizeof(port_config_t));
p->htdocs = NULL; p->htdocs = NULL;
p->sock = -1; p->sock = -1;
p->rules = dict(); p->rules = dict_n(1);
dput(pconfig->ports, buf, p); dput(pconfig->ports, buf, p);
p->port = atoi(buf); p->port = atoi(buf);
} }
@ -725,6 +725,7 @@ char *apply_rules(dictionary_t rules, const char *host, char *url)
{ {
k = it->key; k = it->key;
v = (char *)it->value; v = (char *)it->value;
//printf("[%s]: [%s]\n", k, v);
// 1 group // 1 group
if (rule_check(k, v, host, url, query_string, url)) if (rule_check(k, v, host, url, query_string, url))
{ {

View File

@ -32,7 +32,8 @@ dictionary_t dict()
dictionary_t dict_n(unsigned int size) dictionary_t dict_n(unsigned int size)
{ {
dictionary_t d = (dictionary_t)malloc(sizeof(struct __dict)); dictionary_t d = (dictionary_t)malloc(sizeof(struct __dict));
if(!d) return NULL; if (!d)
return NULL;
d->map = (map_t)malloc(size * sizeof(chain_t)); d->map = (map_t)malloc(size * sizeof(chain_t));
if (!d->map) if (!d->map)
{ {
@ -48,7 +49,8 @@ dictionary_t dict_n(unsigned int size)
chain_t dlookup(dictionary_t dic, const char *key) chain_t dlookup(dictionary_t dic, const char *key)
{ {
chain_t np; chain_t np;
if(dic->map == NULL) return NULL; if (dic->map == NULL)
return NULL;
for (np = dic->map[hash(key, dic->cap)]; np != NULL; np = np->next) for (np = dic->map[hash(key, dic->cap)]; np != NULL; np = np->next)
{ {
if (!np || !np->key) if (!np || !np->key)
@ -62,17 +64,33 @@ chain_t dlookup(dictionary_t dic,const char* key)
} }
chain_t __put_el_with_key(dictionary_t dic, const char *key) chain_t __put_el_with_key(dictionary_t dic, const char *key)
{ {
chain_t np; chain_t np, it;
unsigned hashval; unsigned hashval;
if(dic->map == NULL) return NULL; if (dic->map == NULL)
if ((np = dlookup(dic,key)) == NULL) { /* not found */ return NULL;
if ((np = dlookup(dic, key)) == NULL)
{ /* not found */
np = (chain_t)malloc(sizeof(*np)); np = (chain_t)malloc(sizeof(*np));
if (np == NULL || (np->key = strdup(key)) == NULL) if (np == NULL || (np->key = strdup(key)) == NULL)
return NULL; return NULL;
np->value = NULL; np->value = NULL;
hashval = hash(key, dic->cap); hashval = hash(key, dic->cap);
it = dic->map[hashval];
while (it && it->next != NULL)
{
it = it->next;
}
if (it == NULL)
{
np->next = dic->map[hashval]; np->next = dic->map[hashval];
dic->map[hashval] = np; dic->map[hashval] = np;
}
else
{
it->next = np;
np->next = NULL;
}
dic->size++; dic->size++;
} }
// found // found
@ -83,16 +101,19 @@ chain_t dput(dictionary_t dic,const char* key, void* value)
chain_t np = __put_el_with_key(dic, key); chain_t np = __put_el_with_key(dic, key);
if (np == NULL) if (np == NULL)
{ {
if(value) free(value); if (value)
free(value);
return NULL; return NULL;
} }
if(np->value && value) free(np->value); if (np->value && value)
free(np->value);
np->value = value; np->value = value;
return np; return np;
} }
chain_t dremove(dictionary_t dic, const char *key) chain_t dremove(dictionary_t dic, const char *key)
{ {
if(dic->map == NULL) return 0; if (dic->map == NULL)
return 0;
int hashval = hash(key, dic->cap); int hashval = hash(key, dic->cap);
chain_t np = dic->map[hashval]; chain_t np = dic->map[hashval];
if (np != NULL && strcmp(key, np->key) == 0) if (np != NULL && strcmp(key, np->key) == 0)
@ -112,12 +133,12 @@ chain_t dremove(dictionary_t dic, const char* key)
return ret; return ret;
} }
return NULL; /* not found */ return NULL; /* not found */
} }
void *dvalue(dictionary_t dic, const char *key) void *dvalue(dictionary_t dic, const char *key)
{ {
chain_t as = dlookup(dic, key); chain_t as = dlookup(dic, key);
if(as == NULL) return NULL; if (as == NULL)
return NULL;
return as->value; return as->value;
} }
@ -132,18 +153,17 @@ void free_association(chain_t * asoc)
if (a->key) if (a->key)
{ {
free(a->key); free(a->key);
if(a->value) free(a->value); if (a->value)
free(a->value);
} }
free(a); free(a);
} }
} }
void freedict(dictionary_t dic){ void freedict(dictionary_t dic)
{
for (unsigned int i = 0; i < dic->cap; i++) for (unsigned int i = 0; i < dic->cap; i++)
free_association(&(dic->map[i])); free_association(&(dic->map[i]));
free(dic->map); free(dic->map);
free(dic); free(dic);
} }