1 /*-------------------------------------------------------------------------*/
6 @brief Implements a dictionary for string variables.
8 This module implements a simple dictionary object, i.e. a list
9 of string/string associations. This object is useful to store e.g.
10 informations retrieved from a configuration file (ini files).
12 /*--------------------------------------------------------------------------*/
16 /*---------------------------------------------------------------------------
18 ---------------------------------------------------------------------------*/
21 #endif /* HAVE_CONFIG_H */
23 #include <atalk/dictionary.h>
24 #include <atalk/compat.h>
31 /** Maximum value size for integers and doubles. */
34 /** Minimal allocated number of entries in a dictionary */
37 /** Invalid key token */
38 #define DICT_INVALID_KEY ((char*)-1)
40 /*---------------------------------------------------------------------------
42 ---------------------------------------------------------------------------*/
44 #define MAXKEYSIZE 1024
45 static char *makekey(const char *section, const char *entry)
47 static char buf[MAXKEYSIZE];
49 strlcpy(buf, section, MAXKEYSIZE);
51 strlcat(buf, ":", MAXKEYSIZE);
52 strlcat(buf, entry, MAXKEYSIZE);
58 /* Doubles the allocated size associated to a pointer */
59 /* 'size' is the current allocated size. */
60 static void * mem_double(void * ptr, int size)
64 newptr = calloc(2*size, 1);
68 memcpy(newptr, ptr, size);
73 /*-------------------------------------------------------------------------*/
75 @brief Duplicate a string
76 @param s String to duplicate
77 @return Pointer to a newly allocated string, to be freed with free()
79 This is a replacement for strdup(). This implementation is provided
80 for systems that do not have it.
82 /*--------------------------------------------------------------------------*/
83 static char * xstrdup(char * s)
88 t = malloc(strlen(s)+1) ;
95 /*---------------------------------------------------------------------------
97 ---------------------------------------------------------------------------*/
98 /*-------------------------------------------------------------------------*/
100 @brief Compute the hash key for a string.
101 @param key Character string to use for key.
102 @return 1 unsigned int on at least 32 bits.
104 This hash function has been taken from an Article in Dr Dobbs Journal.
105 This is normally a collision-free function, distributing keys evenly.
106 The key is stored anyway in the struct so that collision can be avoided
107 by comparing the key itself in last resort.
109 /*--------------------------------------------------------------------------*/
110 unsigned dictionary_hash(char * key)
117 for (hash=0, i=0 ; i<len ; i++) {
118 hash += (unsigned)key[i] ;
128 /*-------------------------------------------------------------------------*/
130 @brief Create a new dictionary object.
131 @param size Optional initial size of the dictionary.
132 @return 1 newly allocated dictionary objet.
134 This function allocates a new dictionary object of given size and returns
135 it. If you do not know in advance (roughly) the number of entries in the
136 dictionary, give size=0.
138 /*--------------------------------------------------------------------------*/
139 dictionary * dictionary_new(int size)
143 /* If no size was specified, allocate space for DICTMINSZ */
144 if (size<DICTMINSZ) size=DICTMINSZ ;
146 if (!(d = (dictionary *)calloc(1, sizeof(dictionary)))) {
150 d->val = (char **)calloc(size, sizeof(char*));
151 d->key = (char **)calloc(size, sizeof(char*));
152 d->hash = (unsigned int *)calloc(size, sizeof(unsigned));
156 /*-------------------------------------------------------------------------*/
158 @brief Delete a dictionary object
159 @param d dictionary object to deallocate.
162 Deallocate a dictionary object and all memory associated to it.
164 /*--------------------------------------------------------------------------*/
165 void dictionary_del(dictionary * d)
169 if (d==NULL) return ;
170 for (i=0 ; i<d->size ; i++) {
183 /*-------------------------------------------------------------------------*/
185 @brief Get a value from a dictionary.
186 @param d dictionary object to search.
187 @param key Key to look for in the dictionary.
188 @param def Default value to return if key not found.
189 @return 1 pointer to internally allocated character string.
191 This function locates a key in a dictionary and returns a pointer to its
192 value, or the passed 'def' pointer if no such key can be found in
193 dictionary. The returned character pointer points to data internal to the
194 dictionary object, you should not try to free it or modify it.
196 /*--------------------------------------------------------------------------*/
197 const char * dictionary_get(const dictionary * d, const char *section, const char * key, const char * def)
202 hash = dictionary_hash(makekey(section, key));
203 for (i=0 ; i<d->size ; i++) {
207 if (hash==d->hash[i]) {
208 /* Compare string, to avoid hash collisions */
209 if (!strcmp(makekey(section, key), d->key[i])) {
217 /*-------------------------------------------------------------------------*/
219 @brief Set a value in a dictionary.
220 @param d dictionary object to modify.
221 @param key Key to modify or add.
222 @param val Value to add.
223 @return int 0 if Ok, anything else otherwise
225 If the given key is found in the dictionary, the associated value is
226 replaced by the provided one. If the key cannot be found in the
227 dictionary, it is added to it.
229 It is Ok to provide a NULL value for val, but NULL values for the dictionary
230 or the key are considered as errors: the function will return immediately
233 Notice that if you dictionary_set a variable to NULL, a call to
234 dictionary_get will return a NULL value: the variable will be found, and
235 its value (NULL) is returned. In other words, setting the variable
236 content to NULL is equivalent to deleting the variable from the
237 dictionary. It is not possible (in this implementation) to have a key in
238 the dictionary without value.
240 This function returns non-zero in case of failure.
242 /*--------------------------------------------------------------------------*/
243 int dictionary_set(dictionary * d, char *section, char * key, char * val)
248 if (d==NULL || section==NULL) return -1 ;
250 /* Compute hash for this key */
251 hash = dictionary_hash(makekey(section, key));
252 /* Find if value is already in dictionary */
254 for (i=0 ; i<d->size ; i++) {
257 if (hash==d->hash[i]) { /* Same hash value */
258 if (!strcmp(makekey(section, key), d->key[i])) { /* Same key */
259 /* Found a value: modify and return */
262 d->val[i] = val ? xstrdup(val) : NULL ;
263 /* Value has been modified: return */
269 /* Add a new value */
270 /* See if dictionary needs to grow */
273 /* Reached maximum size: reallocate dictionary */
274 d->val = (char **)mem_double(d->val, d->size * sizeof(char*)) ;
275 d->key = (char **)mem_double(d->key, d->size * sizeof(char*)) ;
276 d->hash = (unsigned int *)mem_double(d->hash, d->size * sizeof(unsigned)) ;
277 if ((d->val==NULL) || (d->key==NULL) || (d->hash==NULL)) {
278 /* Cannot grow dictionary */
285 /* Insert key in the first empty slot */
286 for (i=0 ; i<d->size ; i++) {
287 if (d->key[i]==NULL) {
293 d->key[i] = xstrdup(makekey(section, key));
294 d->val[i] = val ? xstrdup(val) : NULL ;
300 /*-------------------------------------------------------------------------*/
302 @brief Delete a key in a dictionary
303 @param d dictionary object to modify.
304 @param key Key to remove.
307 This function deletes a key in a dictionary. Nothing is done if the
310 /*--------------------------------------------------------------------------*/
311 void dictionary_unset(dictionary * d, char *section, char * key)
320 hash = dictionary_hash(makekey(section, key));
321 for (i=0 ; i<d->size ; i++) {
325 if (hash==d->hash[i]) {
326 /* Compare string, to avoid hash collisions */
327 if (!strcmp(makekey(section, key), d->key[i])) {
339 if (d->val[i]!=NULL) {
348 /*-------------------------------------------------------------------------*/
350 @brief Dump a dictionary to an opened file pointer.
351 @param d Dictionary to dump
352 @param f Opened file pointer.
355 Dumps a dictionary onto an opened file pointer. Key pairs are printed out
356 as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as
357 output file pointers.
359 /*--------------------------------------------------------------------------*/
360 void dictionary_dump(dictionary * d, FILE * out)
364 if (d==NULL || out==NULL) return ;
366 fprintf(out, "empty dictionary\n");
369 for (i=0 ; i<d->size ; i++) {
371 fprintf(out, "%20s\t[%s]\n",
373 d->val[i] ? d->val[i] : "UNDEF");