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

@ -31,119 +31,139 @@ dictionary_t dict()
}
dictionary_t dict_n(unsigned int size)
{
dictionary_t d = (dictionary_t) malloc(sizeof(struct __dict));
if(!d) return NULL;
d->map= (map_t)malloc(size*sizeof(chain_t));
if(!d->map)
dictionary_t d = (dictionary_t)malloc(sizeof(struct __dict));
if (!d)
return NULL;
d->map = (map_t)malloc(size * sizeof(chain_t));
if (!d->map)
{
free(d);
return NULL;
}
d->cap = size;
d->size = 0;
for(unsigned int i=0; i< size;i++)
for (unsigned int i = 0; i < size; i++)
d->map[i] = NULL;
return d;
}
chain_t dlookup(dictionary_t dic,const char* key)
chain_t dlookup(dictionary_t dic, const char *key)
{
chain_t np;
if(dic->map == NULL) return NULL;
for (np = dic->map[hash(key,dic->cap)]; np != NULL; np = np->next)
{
if(!np || !np->key)
{
return NULL;
}
if (strcmp(key, np->key) == 0)
return np; /* found */
}
return NULL; /* not found */
}
chain_t __put_el_with_key(dictionary_t dic, const char* key)
{
chain_t np;
unsigned hashval;
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);
np->next = dic->map[hashval];
dic->map[hashval] = np;
dic->size++;
}
// found
return np;
}
chain_t dput(dictionary_t dic,const char* key, void* value)
{
chain_t np = __put_el_with_key(dic,key);
if(np == NULL)
if (dic->map == NULL)
return NULL;
for (np = dic->map[hash(key, dic->cap)]; np != NULL; np = np->next)
{
if(value) free(value);
if (!np || !np->key)
{
return NULL;
}
if (strcmp(key, np->key) == 0)
return np; /* found */
}
return NULL; /* not found */
}
chain_t __put_el_with_key(dictionary_t dic, const char *key)
{
chain_t np, it;
unsigned hashval;
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
return np;
}
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);
return NULL;
}
if(np->value && value) free(np->value);
if (np->value && value)
free(np->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);
chain_t np = dic->map[hashval];
if(np!=NULL && strcmp(key,np->key)==0)
if (np != NULL && strcmp(key, np->key) == 0)
{
dic->size--;
dic->map[hashval] = np->next;
np->next = NULL;
return np;
}
for (np= dic->map[hashval]; np != NULL; np = np->next)
if (np->next!=NULL&&strcmp(key, np->next->key) == 0)
{
for (np = dic->map[hashval]; np != NULL; np = np->next)
if (np->next != NULL && strcmp(key, np->next->key) == 0)
{
chain_t ret = np->next;
np->next = np->next->next; /* found */
np->next = np->next->next; /* found */
dic->size--;
ret->next = NULL;
return ret;
}
return NULL; /* not found */
return ret;
}
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);
if(as == NULL) return NULL;
chain_t as = dlookup(dic, key);
if (as == NULL)
return NULL;
return as->value;
}
void free_association(chain_t * asoc)
void free_association(chain_t *asoc)
{
while( (*asoc) != NULL )
while ((*asoc) != NULL)
{
chain_t a = *asoc;
(* asoc) = (*asoc) ->next;
if(a->key)
(*asoc) = (*asoc)->next;
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){
for(unsigned int i = 0; i < dic->cap; i++)
void freedict(dictionary_t dic)
{
for (unsigned int i = 0; i < dic->cap; i++)
free_association(&(dic->map[i]));
free(dic->map);
free(dic);
}