2 /*-------------------------------------------------------------------------*/
7 @version $Revision: 1.25 $
8 @brief Implements a dictionary for string variables.
10 This module implements a simple dictionary object, i.e. a list
11 of string/string associations. This object is useful to store e.g.
12 informations retrieved from a configuration file (ini files).
14 /*--------------------------------------------------------------------------*/
17 $Id: dictionary.c,v 1.25 2007-05-27 13:03:43 ndevilla Exp $
19 $Date: 2007-05-27 13:03:43 $
23 /*---------------------------------------------------------------------------
25 ---------------------------------------------------------------------------*/
27 #include "dictionary.h"
35 /** Maximum value size for integers and doubles. */
38 /** Minimal allocated number of entries in a dictionary */
41 /** Invalid key token */
42 #define DICT_INVALID_KEY ((char*)-1)
45 /*---------------------------------------------------------------------------
47 ---------------------------------------------------------------------------*/
49 /* Doubles the allocated size associated to a pointer */
50 /* 'size' is the current allocated size. */
51 static void * mem_double(void * ptr
, int size
)
55 newptr
= calloc(2*size
, 1);
56 memcpy(newptr
, ptr
, size
);
62 /*---------------------------------------------------------------------------
64 ---------------------------------------------------------------------------*/
66 /*-------------------------------------------------------------------------*/
68 @brief Compute the hash key for a string.
69 @param key Character string to use for key.
70 @return 1 unsigned int on at least 32 bits.
72 This hash function has been taken from an Article in Dr Dobbs Journal.
73 This is normally a collision-free function, distributing keys evenly.
74 The key is stored anyway in the struct so that collision can be avoided
75 by comparing the key itself in last resort.
77 /*--------------------------------------------------------------------------*/
79 unsigned dictionary_hash(char * key
)
86 for (hash
=0, i
=0 ; i
<len
; i
++) {
87 hash
+= (unsigned)key
[i
] ;
98 /*-------------------------------------------------------------------------*/
100 @brief Create a new dictionary object.
101 @param size Optional initial size of the dictionary.
102 @return 1 newly allocated dictionary objet.
104 This function allocates a new dictionary object of given size and returns
105 it. If you do not know in advance (roughly) the number of entries in the
106 dictionary, give size=0.
108 /*--------------------------------------------------------------------------*/
110 dictionary
* dictionary_new(int size
)
114 /* If no size was specified, allocate space for DICTMINSZ */
115 if (size
<DICTMINSZ
) size
=DICTMINSZ
;
117 if (!(d
= (dictionary
*)calloc(1, sizeof(dictionary
)))) {
121 d
->val
= (char **)calloc(size
, sizeof(char*));
122 d
->key
= (char **)calloc(size
, sizeof(char*));
123 d
->hash
= (unsigned int *)calloc(size
, sizeof(unsigned));
128 /*-------------------------------------------------------------------------*/
130 @brief Delete a dictionary object
131 @param d dictionary object to deallocate.
134 Deallocate a dictionary object and all memory associated to it.
136 /*--------------------------------------------------------------------------*/
138 void dictionary_del(dictionary
* d
)
142 if (d
==NULL
) return ;
143 for (i
=0 ; i
<d
->size
; i
++) {
158 /*-------------------------------------------------------------------------*/
160 @brief Get a value from a dictionary.
161 @param d dictionary object to search.
162 @param key Key to look for in the dictionary.
163 @param def Default value to return if key not found.
164 @return 1 pointer to internally allocated character string.
166 This function locates a key in a dictionary and returns a pointer to its
167 value, or the passed 'def' pointer if no such key can be found in
168 dictionary. The returned character pointer points to data internal to the
169 dictionary object, you should not try to free it or modify it.
171 /*--------------------------------------------------------------------------*/
172 char * dictionary_get(dictionary
* d
, char * key
, char * def
)
177 hash
= dictionary_hash(key
);
178 for (i
=0 ; i
<d
->size
; i
++) {
182 if (hash
==d
->hash
[i
]) {
183 /* Compare string, to avoid hash collisions */
184 if (!strcmp(key
, d
->key
[i
])) {
192 /*-------------------------------------------------------------------------*/
194 @brief Get a value from a dictionary, as a char.
195 @param d dictionary object to search.
196 @param key Key to look for in the dictionary.
197 @param def Default value for the key if not found.
200 This function locates a key in a dictionary using dictionary_get,
201 and returns the first char of the found string.
203 /*--------------------------------------------------------------------------*/
204 char dictionary_getchar(dictionary
* d
, char * key
, char def
)
208 if ((v
=dictionary_get(d
,key
,DICT_INVALID_KEY
))==DICT_INVALID_KEY
) {
216 /*-------------------------------------------------------------------------*/
218 @brief Get a value from a dictionary, as an int.
219 @param d dictionary object to search.
220 @param key Key to look for in the dictionary.
221 @param def Default value for the key if not found.
224 This function locates a key in a dictionary using dictionary_get,
225 and applies atoi on it to return an int. If the value cannot be found
226 in the dictionary, the default is returned.
228 /*--------------------------------------------------------------------------*/
229 int dictionary_getint(dictionary
* d
, char * key
, int def
)
233 if ((v
=dictionary_get(d
,key
,DICT_INVALID_KEY
))==DICT_INVALID_KEY
) {
240 /*-------------------------------------------------------------------------*/
242 @brief Get a value from a dictionary, as a double.
243 @param d dictionary object to search.
244 @param key Key to look for in the dictionary.
245 @param def Default value for the key if not found.
248 This function locates a key in a dictionary using dictionary_get,
249 and applies atof on it to return a double. If the value cannot be found
250 in the dictionary, the default is returned.
252 /*--------------------------------------------------------------------------*/
253 double dictionary_getdouble(dictionary
* d
, char * key
, double def
)
257 if ((v
=dictionary_get(d
,key
,DICT_INVALID_KEY
))==DICT_INVALID_KEY
) {
265 /*-------------------------------------------------------------------------*/
267 @brief Set a value in a dictionary.
268 @param d dictionary object to modify.
269 @param key Key to modify or add.
270 @param val Value to add.
273 If the given key is found in the dictionary, the associated value is
274 replaced by the provided one. If the key cannot be found in the
275 dictionary, it is added to it.
277 It is Ok to provide a NULL value for val, but NULL values for the dictionary
278 or the key are considered as errors: the function will return immediately
281 Notice that if you dictionary_set a variable to NULL, a call to
282 dictionary_get will return a NULL value: the variable will be found, and
283 its value (NULL) is returned. In other words, setting the variable
284 content to NULL is equivalent to deleting the variable from the
285 dictionary. It is not possible (in this implementation) to have a key in
286 the dictionary without value.
288 /*--------------------------------------------------------------------------*/
290 void dictionary_set(dictionary
* d
, char * key
, char * val
)
295 if (d
==NULL
|| key
==NULL
) return ;
297 /* Compute hash for this key */
298 hash
= dictionary_hash(key
) ;
299 /* Find if value is already in blackboard */
301 for (i
=0 ; i
<d
->size
; i
++) {
304 if (hash
==d
->hash
[i
]) { /* Same hash value */
305 if (!strcmp(key
, d
->key
[i
])) { /* Same key */
306 /* Found a value: modify and return */
309 d
->val
[i
] = val
? strdup(val
) : NULL
;
310 /* Value has been modified: return */
316 /* Add a new value */
317 /* See if dictionary needs to grow */
320 /* Reached maximum size: reallocate blackboard */
321 d
->val
= (char **)mem_double(d
->val
, d
->size
* sizeof(char*)) ;
322 d
->key
= (char **)mem_double(d
->key
, d
->size
* sizeof(char*)) ;
323 d
->hash
= (unsigned int *)mem_double(d
->hash
, d
->size
* sizeof(unsigned)) ;
329 /* Insert key in the first empty slot */
330 for (i
=0 ; i
<d
->size
; i
++) {
331 if (d
->key
[i
]==NULL
) {
337 d
->key
[i
] = strdup(key
);
338 d
->val
[i
] = val
? strdup(val
) : NULL
;
344 /*-------------------------------------------------------------------------*/
346 @brief Delete a key in a dictionary
347 @param d dictionary object to modify.
348 @param key Key to remove.
351 This function deletes a key in a dictionary. Nothing is done if the
354 /*--------------------------------------------------------------------------*/
355 void dictionary_unset(dictionary
* d
, char * key
)
364 hash
= dictionary_hash(key
);
365 for (i
=0 ; i
<d
->size
; i
++) {
369 if (hash
==d
->hash
[i
]) {
370 /* Compare string, to avoid hash collisions */
371 if (!strcmp(key
, d
->key
[i
])) {
383 if (d
->val
[i
]!=NULL
) {
393 /*-------------------------------------------------------------------------*/
395 @brief Set a key in a dictionary, providing an int.
396 @param d Dictionary to update.
397 @param key Key to modify or add
398 @param val Integer value to store (will be stored as a string).
401 This helper function calls dictionary_set() with the provided integer
402 converted to a string using %d.
404 /*--------------------------------------------------------------------------*/
407 void dictionary_setint(dictionary
* d
, char * key
, int val
)
410 sprintf(sval
, "%d", val
);
411 dictionary_set(d
, key
, sval
);
415 /*-------------------------------------------------------------------------*/
417 @brief Set a key in a dictionary, providing a double.
418 @param d Dictionary to update.
419 @param key Key to modify or add
420 @param val Double value to store (will be stored as a string).
423 This helper function calls dictionary_set() with the provided double
424 converted to a string using %g.
426 /*--------------------------------------------------------------------------*/
429 void dictionary_setdouble(dictionary
* d
, char * key
, double val
)
432 sprintf(sval
, "%g", val
);
433 dictionary_set(d
, key
, sval
);
438 /*-------------------------------------------------------------------------*/
440 @brief Dump a dictionary to an opened file pointer.
441 @param d Dictionary to dump
442 @param f Opened file pointer.
445 Dumps a dictionary onto an opened file pointer. Key pairs are printed out
446 as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as
447 output file pointers.
449 /*--------------------------------------------------------------------------*/
451 void dictionary_dump(dictionary
* d
, FILE * out
)
455 if (d
==NULL
|| out
==NULL
) return ;
457 fprintf(out
, "empty dictionary\n");
460 for (i
=0 ; i
<d
->size
; i
++) {
462 fprintf(out
, "%20s\t[%s]\n",
464 d
->val
[i
] ? d
->val
[i
] : "UNDEF");
475 int main(int argc
, char *argv
[])
482 /* allocate blackboard */
483 printf("allocating...\n");
484 d
= dictionary_new(0);
486 /* Set values in blackboard */
487 printf("setting %d values...\n", NVALS
);
488 for (i
=0 ; i
<NVALS
; i
++) {
489 sprintf(cval
, "%04d", i
);
490 dictionary_set(d
, cval
, "salut");
492 printf("getting %d values...\n", NVALS
);
493 for (i
=0 ; i
<NVALS
; i
++) {
494 sprintf(cval
, "%04d", i
);
495 val
= dictionary_get(d
, cval
, DICT_INVALID_KEY
);
496 if (val
==DICT_INVALID_KEY
) {
497 printf("cannot get value for key [%s]\n", cval
);
500 printf("unsetting %d values...\n", NVALS
);
501 for (i
=0 ; i
<NVALS
; i
++) {
502 sprintf(cval
, "%04d", i
);
503 dictionary_unset(d
, cval
);
506 printf("error deleting values\n");
509 printf("deallocating...\n");
514 /* vim: set ts=4 et sw=4 tw=75 */