ant-http/lib/dictionary.c

170 lines
3.7 KiB
C
Raw Normal View History

2015-11-24 17:58:32 +01:00
/*
The MIT License (MIT)
Copyright (c) 2015 LE Xuan Sang xsang.le@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
2020-08-25 16:40:24 +02:00
#include <string.h>
2019-12-15 12:10:06 +01:00
#include "utils.h"
2020-08-25 16:40:24 +02:00
#include "dictionary.h"
2015-10-22 11:39:11 +02:00
2019-12-15 12:10:06 +01:00
dictionary_t dict()
2015-10-22 11:39:11 +02:00
{
2019-12-15 12:10:06 +01:00
return dict_n(DHASHSIZE);
}
dictionary_t dict_n(unsigned int size)
{
2020-09-22 16:49:43 +02:00
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)
2019-12-15 12:10:06 +01:00
{
free(d);
return NULL;
}
d->cap = size;
d->size = 0;
2020-09-22 16:49:43 +02:00
for (unsigned int i = 0; i < size; i++)
2019-12-15 12:10:06 +01:00
d->map[i] = NULL;
2015-10-22 11:39:11 +02:00
return d;
}
2020-09-22 16:49:43 +02:00
chain_t dlookup(dictionary_t dic, const char *key)
2015-10-22 11:39:11 +02:00
{
2019-12-15 12:10:06 +01:00
chain_t np;
2020-09-22 16:49:43 +02:00
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 */
2015-10-22 11:39:11 +02:00
}
2020-09-22 16:49:43 +02:00
chain_t __put_el_with_key(dictionary_t dic, const char *key)
2015-10-22 11:39:11 +02:00
{
2020-09-22 16:49:43 +02:00
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;
2019-12-15 12:10:06 +01:00
np->value = NULL;
2020-09-22 16:49:43 +02:00
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;
}
2019-12-15 12:10:06 +01:00
dic->size++;
2020-09-22 16:49:43 +02:00
}
// found
2020-09-22 16:49:43 +02:00
return np;
2015-10-22 11:39:11 +02:00
}
2020-09-22 16:49:43 +02:00
chain_t dput(dictionary_t dic, const char *key, void *value)
2015-10-22 11:39:11 +02:00
{
2020-09-22 16:49:43 +02:00
chain_t np = __put_el_with_key(dic, key);
if (np == NULL)
{
2020-09-22 16:49:43 +02:00
if (value)
free(value);
return NULL;
}
2020-09-22 16:49:43 +02:00
if (np->value && value)
free(np->value);
2015-11-24 17:58:32 +01:00
np->value = value;
2020-09-22 16:49:43 +02:00
return np;
2015-10-22 11:39:11 +02:00
}
2020-09-22 16:49:43 +02:00
chain_t dremove(dictionary_t dic, const char *key)
2015-10-22 11:39:11 +02:00
{
2020-09-22 16:49:43 +02:00
if (dic->map == NULL)
return 0;
2019-12-15 12:10:06 +01:00
int hashval = hash(key, dic->cap);
chain_t np = dic->map[hashval];
2020-09-22 16:49:43 +02:00
if (np != NULL && strcmp(key, np->key) == 0)
2015-10-22 11:39:11 +02:00
{
2019-12-15 12:10:06 +01:00
dic->size--;
dic->map[hashval] = np->next;
np->next = NULL;
return np;
2015-10-22 11:39:11 +02:00
}
2020-09-22 16:49:43 +02:00
for (np = dic->map[hashval]; np != NULL; np = np->next)
if (np->next != NULL && strcmp(key, np->next->key) == 0)
{
2019-12-15 12:10:06 +01:00
chain_t ret = np->next;
2020-09-22 16:49:43 +02:00
np->next = np->next->next; /* found */
2019-12-15 12:10:06 +01:00
dic->size--;
ret->next = NULL;
2020-09-22 16:49:43 +02:00
return ret;
}
return NULL; /* not found */
2015-10-22 11:39:11 +02:00
}
2020-09-22 16:49:43 +02:00
void *dvalue(dictionary_t dic, const char *key)
2015-10-22 11:39:11 +02:00
{
2020-09-22 16:49:43 +02:00
chain_t as = dlookup(dic, key);
if (as == NULL)
return NULL;
2015-11-24 17:58:32 +01:00
return as->value;
2015-10-22 11:39:11 +02:00
}
2020-09-22 16:49:43 +02:00
void free_association(chain_t *asoc)
2018-03-02 19:04:00 +01:00
{
2020-09-22 16:49:43 +02:00
while ((*asoc) != NULL)
2018-03-02 19:04:00 +01:00
{
2019-12-15 12:10:06 +01:00
chain_t a = *asoc;
2020-09-22 16:49:43 +02:00
(*asoc) = (*asoc)->next;
if (a->key)
2018-03-02 19:04:00 +01:00
{
free(a->key);
2020-09-22 16:49:43 +02:00
if (a->value)
free(a->value);
2018-03-02 19:04:00 +01:00
}
free(a);
}
}
2020-09-22 16:49:43 +02:00
void freedict(dictionary_t dic)
{
for (unsigned int i = 0; i < dic->cap; i++)
2019-12-15 12:10:06 +01:00
free_association(&(dic->map[i]));
free(dic->map);
2018-03-02 19:04:00 +01:00
free(dic);
}