1
0
mirror of https://github.com/lxsang/ant-http synced 2024-07-03 13:39:46 +02:00

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->htdocs = NULL;
p->sock = -1;
p->rules = dict();
p->rules = dict_n(1);
dput(pconfig->ports, buf, p);
p->port = atoi(buf);
}
@ -725,6 +725,7 @@ char *apply_rules(dictionary_t rules, const char *host, char *url)
{
k = it->key;
v = (char *)it->value;
//printf("[%s]: [%s]\n", k, v);
// 1 group
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 d = (dictionary_t)malloc(sizeof(struct __dict));
if(!d) return NULL;
if (!d)
return NULL;
d->map = (map_t)malloc(size * sizeof(chain_t));
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 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)
{
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 np;
chain_t np, it;
unsigned hashval;
if(dic->map == NULL) return NULL;
if ((np = dlookup(dic,key)) == NULL) { /* not found */
if (dic->map == NULL)
return NULL;
if ((np = dlookup(dic, key)) == NULL)
{ /* not found */
np = (chain_t)malloc(sizeof(*np));
if (np == NULL || (np->key = strdup(key)) == NULL)
return NULL;
np->value = NULL;
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];
dic->map[hashval] = np;
}
else
{
it->next = np;
np->next = NULL;
}
dic->size++;
}
// 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);
if (np == NULL)
{
if(value) free(value);
if (value)
free(value);
return NULL;
}
if(np->value && value) free(np->value);
if (np->value && value)
free(np->value);
np->value = value;
return np;
}
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);
chain_t np = dic->map[hashval];
if (np != NULL && strcmp(key, np->key) == 0)
@ -112,12 +133,12 @@ chain_t dremove(dictionary_t dic, const char* key)
return ret;
}
return NULL; /* not found */
}
void *dvalue(dictionary_t dic, const char *key)
{
chain_t as = dlookup(dic, key);
if(as == NULL) return NULL;
if (as == NULL)
return NULL;
return as->value;
}
@ -132,18 +153,17 @@ void free_association(chain_t * asoc)
if (a->key)
{
free(a->key);
if(a->value) free(a->value);
if (a->value)
free(a->value);
}
free(a);
}
}
void freedict(dictionary_t dic){
void freedict(dictionary_t dic)
{
for (unsigned int i = 0; i < dic->cap; i++)
free_association(&(dic->map[i]));
free(dic->map);
free(dic);
}