ant-http/lib/dictionary.c

178 lines
4.0 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.
*/
2015-10-22 11:39:11 +02:00
#include "dictionary.h"
2019-12-15 12:10:06 +01:00
#include "utils.h"
2020-01-15 18:27:28 +01:00
#include "list.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)
{
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++)
d->map[i] = NULL;
2015-10-22 11:39:11 +02:00
return d;
}
2019-12-15 12:10:06 +01: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;
if(dic->map == NULL) return NULL;
for (np = dic->map[hash(key,dic->cap)]; np != NULL; np = np->next)
2015-10-22 11:39:11 +02:00
{
if(!np || !np->key)
{
return NULL;
}
if (strcmp(key, np->key) == 0)
return np; /* found */
}
return NULL; /* not found */
}
2019-12-15 12:10:06 +01:00
chain_t __put_el_with_key(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;
2015-10-22 11:39:11 +02:00
unsigned hashval;
2019-12-15 12:10:06 +01:00
if(dic->map == NULL) return NULL;
2015-10-22 11:39:11 +02:00
if ((np = dlookup(dic,key)) == NULL) { /* not found */
2019-12-15 12:10:06 +01:00
np = (chain_t) malloc(sizeof(*np));
2020-01-15 18:27:28 +01:00
if (np == NULL)
2015-10-22 11:39:11 +02:00
return NULL;
2020-01-15 18:27:28 +01:00
if((np->key = strdup(key)) == NULL)
{
free(np);
return NULL;
}
2019-12-15 12:10:06 +01:00
np->value = NULL;
hashval = hash(key, dic->cap);
np->next = dic->map[hashval];
dic->map[hashval] = np;
dic->size++;
2015-10-22 11:39:11 +02:00
}
// found
2015-10-22 11:39:11 +02:00
return np;
}
2020-01-15 18:27:28 +01:00
static void free_ditem_value(void* value, antd_dict_item_type_t type)
{
switch (type)
{
case ANTD_DI_HEAP:
if(value)
free(value);
break;
case ANTD_DI_LIST:
if(value)
list_free((list_t*)&value);
break;
case ANTD_DI_DIC:
if(value)
freedict(value);
default:
break;
}
}
chain_t insert(dictionary_t dic,const char* key, void* value, antd_dict_item_type_t type)
2015-10-22 11:39:11 +02:00
{
2019-12-15 12:10:06 +01:00
chain_t np = __put_el_with_key(dic,key);
if(np == NULL)
{
2020-01-15 18:27:28 +01:00
free_ditem_value(value, type);
return NULL;
}
2020-01-15 18:27:28 +01:00
if(np->value && value) free_ditem_value(np->value, np->type);
np->type = type;
2015-11-24 17:58:32 +01:00
np->value = value;
2015-10-22 11:39:11 +02:00
return np;
}
2020-01-15 18:27:28 +01:00
2019-12-15 12:10:06 +01:00
chain_t dremove(dictionary_t dic, const char* key)
2015-10-22 11:39:11 +02:00
{
2019-12-15 12:10:06 +01:00
if(dic->map == NULL) return 0;
int hashval = hash(key, dic->cap);
chain_t np = dic->map[hashval];
2015-10-22 11:39:11 +02:00
if(np!=NULL && strcmp(key,np->key)==0)
{
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
}
2019-12-15 12:10:06 +01:00
for (np= dic->map[hashval]; np != NULL; np = np->next)
2015-10-22 11:39:11 +02:00
if (np->next!=NULL&&strcmp(key, np->next->key) == 0)
{
2019-12-15 12:10:06 +01:00
chain_t ret = np->next;
2015-10-22 11:39:11 +02:00
np->next = np->next->next; /* found */
2019-12-15 12:10:06 +01:00
dic->size--;
ret->next = NULL;
return ret;
2015-10-22 11:39:11 +02:00
}
2019-12-15 12:10:06 +01:00
return NULL; /* not found */
2015-10-22 11:39:11 +02:00
}
2019-12-15 12:10:06 +01:00
void* dvalue(dictionary_t dic, const char* key)
2015-10-22 11:39:11 +02:00
{
2019-12-15 12:10:06 +01:00
chain_t as = dlookup(dic,key);
2015-10-22 11:39:11 +02:00
if(as == NULL) return NULL;
2015-11-24 17:58:32 +01:00
return as->value;
2015-10-22 11:39:11 +02:00
}
2019-12-15 12:10:06 +01:00
void free_association(chain_t * asoc)
2018-03-02 19:04:00 +01:00
{
while( (*asoc) != NULL )
{
2019-12-15 12:10:06 +01:00
chain_t a = *asoc;
2018-03-02 19:04:00 +01:00
(* asoc) = (*asoc) ->next;
if(a->key)
{
free(a->key);
2020-01-15 18:27:28 +01:00
if(a->value) free_ditem_value(a->value, a->type);
2018-03-02 19:04:00 +01:00
}
free(a);
}
}
2019-12-15 12:10:06 +01:00
void freedict(dictionary_t dic){
for(unsigned int i = 0; i < dic->cap; i++)
free_association(&(dic->map[i]));
free(dic->map);
2018-03-02 19:04:00 +01:00
free(dic);
}
2015-10-22 11:39:11 +02:00