30 #define DICT_INVALID_KEY ((char*)-1)
46 static char * xstrdup(
const char * s)
54 t = (
char*) malloc(len) ;
74 new_val = (
char**) calloc(d->
size * 2,
sizeof *d->
val);
75 new_key = (
char**) calloc(d->
size * 2,
sizeof *d->
key);
76 new_hash = (
unsigned*) calloc(d->
size * 2,
sizeof *d->
hash);
77 if (!new_val || !new_key || !new_hash) {
88 memcpy(new_val, d->
val, d->
size *
sizeof(
char *));
89 memcpy(new_key, d->
key, d->
size *
sizeof(
char *));
90 memcpy(new_hash, d->
hash, d->
size *
sizeof(
unsigned));
128 for (hash=0, i=0 ; i<len ; i++) {
129 hash += (unsigned)key[i] ;
161 d->
val = (
char**) calloc(size,
sizeof *d->
val);
162 d->
key = (
char**) calloc(size,
sizeof *d->
key);
163 d->
hash = (
unsigned*) calloc(size,
sizeof *d->
hash);
181 if (d==NULL) return ;
182 for (i=0 ; i<d->
size ; i++) {
215 for (i=0 ; i<d->
size ; i++) {
219 if (hash==d->
hash[i]) {
221 if (!strcmp(key, d->
key[i])) {
260 if (d==NULL || key==NULL)
return -1 ;
266 for (i=0 ; i<d->
size ; i++) {
269 if (hash==d->
hash[i]) {
270 if (!strcmp(key, d->
key[i])) {
274 d->
val[i] = (val ? xstrdup(val) : NULL);
285 if (dictionary_grow(d) != 0)
292 for (i=d->
n ; d->
key[i] ; ) {
293 if(++i == d->
size) i = 0;
296 d->
key[i] = xstrdup(key);
297 d->
val[i] = (val ? xstrdup(val) : NULL) ;
319 if (key == NULL || d == NULL) {
324 for (i=0 ; i<d->
size ; i++) {
328 if (hash==d->
hash[i]) {
330 if (!strcmp(key, d->
key[i])) {
342 if (d->
val[i]!=NULL) {
367 if (d==NULL || out==NULL) return ;
369 fprintf(out,
"empty dictionary\n");
372 for (i=0 ; i<d->
size ; i++) {
374 fprintf(out,
"%20s\t[%s]\n",
376 d->
val[i] ? d->
val[i] :
"UNDEF");
void dictionary_del(dictionary *d)
Delete a dictionary object.
void dictionary_unset(dictionary *d, const char *key)
Delete a key in a dictionary.
unsigned dictionary_hash(const char *key)
Compute the hash key for a string.
const char * dictionary_get(const dictionary *d, const char *key, const char *def)
Get a value from a dictionary.
int dictionary_set(dictionary *d, const char *key, const char *val)
Set a value in a dictionary.
void dictionary_dump(const dictionary *d, FILE *out)
Dump a dictionary to an opened file pointer.
dictionary * dictionary_new(size_t size)
Create a new dictionary object.
Implements a dictionary for string variables.