2 /*-------------------------------------------------------------------------*/
7 @version $Revision: 1.23 $
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.23 2002/06/17 09:30:46 ndevilla Exp $
19 $Date: 2002/06/17 09:30:46 $
23 /*---------------------------------------------------------------------------
25 ---------------------------------------------------------------------------*/
27 #include "dictionary.h"
28 #include "inistrlib.h"
36 /** Maximum value size for integers and doubles. */
39 /** Minimal allocated number of entries in a dictionary */
42 /** Invalid key token */
43 #define DICT_INVALID_KEY ((char*)-1)
46 /*---------------------------------------------------------------------------
48 ---------------------------------------------------------------------------*/
50 /* Doubles the allocated size associated to a pointer */
51 /* 'size' is the current allocated size. */
52 static void * mem_double(void * ptr
, int size
)
56 newptr
= calloc(2*size
, 1);
57 memcpy(newptr
, ptr
, size
);
63 /*---------------------------------------------------------------------------
65 ---------------------------------------------------------------------------*/
67 /*-------------------------------------------------------------------------*/
69 @brief Compute the hash key for a string.
70 @param key Character string to use for key.
71 @return 1 unsigned int on at least 32 bits.
73 This hash function has been taken from an Article in Dr Dobbs Journal.
74 This is normally a collision-free function, distributing keys evenly.
75 The key is stored anyway in the struct so that collision can be avoided
76 by comparing the key itself in last resort.
78 /*--------------------------------------------------------------------------*/
80 unsigned dictionary_hash(char * key
)
86 len
= (int)strlen(key
);
87 for (hash
=0, i
=0 ; i
<len
; i
++) {
88 hash
+= (unsigned)key
[i
] ;
99 /*-------------------------------------------------------------------------*/
101 @brief Create a new dictionary object.
102 @param size Optional initial size of the dictionary.
103 @return 1 newly allocated dictionary objet.
105 This function allocates a new dictionary object of given size and returns
106 it. If you do not know in advance (roughly) the number of entries in the
107 dictionary, give size=0.
109 /*--------------------------------------------------------------------------*/
111 dictionary
* dictionary_new(int size
, int caseSensitive
)
115 /* If no size was specified, allocate space for DICTMINSZ */
116 if (size
<DICTMINSZ
) size
=DICTMINSZ
;
118 d
= calloc(1, sizeof(dictionary
));
120 d
->caseSensitive
= caseSensitive
;
121 d
->val
= calloc(size
, sizeof(char*));
122 d
->key
= calloc(size
, sizeof(char*));
123 d
->hash
= 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
? inistrdup(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
= mem_double(d
->val
, d
->size
* sizeof(char*)) ;
322 d
->key
= mem_double(d
->key
, d
->size
* sizeof(char*)) ;
323 d
->hash
= 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
] = inistrdup(key
);
338 d
->val
[i
] = val
? inistrdup(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
)
360 hash
= dictionary_hash(key
);
361 for (i
=0 ; i
<d
->size
; i
++) {
365 if (hash
==d
->hash
[i
]) {
366 /* Compare string, to avoid hash collisions */
367 if (!strcmp(key
, d
->key
[i
])) {
379 if (d
->val
[i
]!=NULL
) {
389 /*-------------------------------------------------------------------------*/
391 @brief Set a key in a dictionary, providing an int.
392 @param d Dictionary to update.
393 @param key Key to modify or add
394 @param val Integer value to store (will be stored as a string).
397 This helper function calls dictionary_set() with the provided integer
398 converted to a string using %d.
400 /*--------------------------------------------------------------------------*/
403 void dictionary_setint(dictionary
* d
, char * key
, int val
)
406 sprintf(sval
, "%d", val
);
407 dictionary_set(d
, key
, sval
);
411 /*-------------------------------------------------------------------------*/
413 @brief Set a key in a dictionary, providing a double.
414 @param d Dictionary to update.
415 @param key Key to modify or add
416 @param val Double value to store (will be stored as a string).
419 This helper function calls dictionary_set() with the provided double
420 converted to a string using %g.
422 /*--------------------------------------------------------------------------*/
425 void dictionary_setdouble(dictionary
* d
, char * key
, double val
)
428 sprintf(sval
, "%g", val
);
429 dictionary_set(d
, key
, sval
);
434 /*-------------------------------------------------------------------------*/
436 @brief Dump a dictionary to an opened file pointer.
437 @param d Dictionary to dump
438 @param f Opened file pointer.
441 Dumps a dictionary onto an opened file pointer. Key pairs are printed out
442 as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as
443 output file pointers.
445 /*--------------------------------------------------------------------------*/
447 void dictionary_dump(dictionary
* d
, FILE * out
)
451 if (d
==NULL
|| out
==NULL
) return ;
453 fprintf(out
, "empty dictionary\n");
456 for (i
=0 ; i
<d
->size
; i
++) {
458 fprintf(out
, "%20s\t[%s]\n",
460 d
->val
[i
] ? d
->val
[i
] : "UNDEF");
471 int main(int argc
, char *argv
[])
478 /* allocate blackboard */
479 printf("allocating...\n");
480 d
= dictionary_new(0);
482 /* Set values in blackboard */
483 printf("setting %d values...\n", NVALS
);
484 for (i
=0 ; i
<NVALS
; i
++) {
485 sprintf(cval
, "%04d", i
);
486 dictionary_set(d
, cval
, "salut");
488 printf("getting %d values...\n", NVALS
);
489 for (i
=0 ; i
<NVALS
; i
++) {
490 sprintf(cval
, "%04d", i
);
491 val
= dictionary_get(d
, cval
, DICT_INVALID_KEY
);
492 if (val
==DICT_INVALID_KEY
) {
493 printf("cannot get value for key [%s]\n", cval
);
496 printf("unsetting %d values...\n", NVALS
);
497 for (i
=0 ; i
<NVALS
; i
++) {
498 sprintf(cval
, "%04d", i
);
499 dictionary_unset(d
, cval
);
502 printf("error deleting values\n");
505 printf("deallocating...\n");
510 /* vim: set ts=4 et sw=4 tw=75 */