53 #define DICT_INVALID_KEY ((char*)-1)
60 static void *mem_double(
void *ptr,
int size)
64 newptr = calloc(2 * size, 1);
68 memcpy(newptr, ptr, size);
82 for (hash = 0, i = 0; i < len; i++) {
83 hash += (unsigned) key[i];
104 d->val = (
char **) calloc(size,
sizeof(
char *));
105 d->key = (
char **) calloc(size,
sizeof(
char *));
106 d->hash = (
unsigned int *) calloc(size,
sizeof(
unsigned));
116 for (i = 0; i < d->size; i++) {
117 if (d->key[i] != NULL)
119 if (d->val[i] != NULL)
135 for (i=0; i < d->size; i++) {
136 if (d->key[i] == NULL)
139 if (hash == d->hash[i]) {
141 if (!strcmp(key, d->key[i])) {
154 if (d==NULL || key==NULL)
161 for (i = 0; i < d->size; i++) {
162 if (d->key[i] == NULL)
165 if (hash == d->hash[i]) {
167 if (!strcmp(key, d->key[i])) {
169 if (d->val[i] != NULL)
171 d->val[i] = val ? strdup(val) : NULL;
181 if (d->n == d->size) {
183 d->val = (
char **) mem_double(d->val, d->size *
sizeof(
char *));
184 d->key = (
char **) mem_double(d->key, d->size *
sizeof(
char *));
185 d->hash = (
unsigned int *)
186 mem_double(d->hash, d->size *
sizeof(
unsigned));
187 if ((d->val == NULL) || (d->key == NULL) || (d->hash == NULL))
195 for (i = 0; i < d->size; i++) {
196 if (d->key[i] == NULL) {
202 d->key[i] = strdup(key);
203 d->val[i] = val ? strdup(val) : NULL;
218 for (i = 0; i < d->size; i++) {
219 if (d->key[i] == NULL)
222 if (hash == d->hash[i]) {
224 if (!strcmp(key, d->key[i])) {
236 if (d->val[i]!=NULL) {
249 if (d == NULL || out == NULL)
252 fprintf(out,
"empty dictionary\n");
255 for (i = 0; i < d->size; i++) {
257 fprintf(out,
"%20s\t[%s]\n",
259 d->val[i] ? d->val[i] :
"UNDEF");
int dictionary_set(dictionary *d, const char *key, const char *val)
Set a value in a dictionary.
const char * dictionary_get(dictionary *d, const char *key, const char *def)
Get a value from a dictionary.
dictionary * dictionary_new(int size)
Create a new dictionary object.
void dictionary_del(dictionary *d)
Delete a dictionary object.
void dictionary_unset(dictionary *d, const char *key)
Delete a key in a dictionary.
void dictionary_dump(dictionary *d, FILE *out)
Dump a dictionary to an opened file pointer.
unsigned dictionary_hash(const char *key)
Compute the hash key for a string.
Implements a dictionary for string variables.This module implements a simple dictionary object...