53 #define DICT_INVALID_KEY ((char*)-1)
60 static void* mem_double(
void* ptr,
int size)
64 newptr = calloc(2 * size, 1);
67 memcpy(newptr, ptr, size);
81 for (hash = 0, i = 0; i < len; i++) {
82 hash += (unsigned)key[i];
103 d->val = (
char**)calloc(size,
sizeof(
char*));
104 d->key = (
char**)calloc(size,
sizeof(
char*));
105 d->hash = (
unsigned int*)calloc(size,
sizeof(
unsigned));
115 for (i = 0; i < d->size; i++) {
116 if (d->key[i] != NULL)
118 if (d->val[i] != NULL)
134 for (i = 0; i < d->size; i++) {
135 if (d->key[i] == NULL)
138 if (hash == d->hash[i]) {
140 if (!strcmp(key, d->key[i]))
152 if (d == NULL || key == NULL)
159 for (i = 0; i < d->size; i++) {
160 if (d->key[i] == NULL)
163 if (hash == d->hash[i]) {
165 if (!strcmp(key, d->key[i])) {
167 if (d->val[i] != NULL)
169 d->val[i] = val ? strdup(val) : NULL;
179 if (d->n == d->size) {
181 d->val = (
char**)mem_double(d->val, d->size *
sizeof(
char*));
182 d->key = (
char**)mem_double(d->key, d->size *
sizeof(
char*));
183 d->hash = (
unsigned int*)
184 mem_double(d->hash, d->size *
sizeof(
unsigned));
185 if ((d->val == NULL) || (d->key == NULL) || (d->hash == NULL))
193 for (i = 0; i < d->size; i++) {
194 if (d->key[i] == NULL)
199 d->key[i] = strdup(key);
200 d->val[i] = val ? strdup(val) : NULL;
215 for (i = 0; i < d->size; i++) {
216 if (d->key[i] == NULL)
219 if (hash == d->hash[i]) {
221 if (!strcmp(key, d->key[i]))
232 if (d->val[i] != NULL) {
245 if (d == NULL || out == NULL)
248 fprintf(out,
"empty dictionary\n");
251 for (i = 0; i < d->size; i++) {
253 fprintf(out,
"%20s\t[%s]\n",
255 d->val[i] ? d->val[i] :
"UNDEF");
int dictionary_set(dictionary *d, const char *key, const char *val)
Set a value in a dictionary.
#define DICTMINSZ
Minimal allocated number of entries 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...