2 /*-------------------------------------------------------------------------*/
7 @version $Revision: 1.1.2.1 $
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.h,v 1.1.2.1 2010-02-12 10:19:23 kleplat Exp $
19 $Date: 2010-02-12 10:19:23 $
23 #ifndef _DICTIONARY_H_
24 #define _DICTIONARY_H_
26 /*---------------------------------------------------------------------------
28 ---------------------------------------------------------------------------*/
37 /*---------------------------------------------------------------------------
39 ---------------------------------------------------------------------------*/
41 typedef enum _dictionary_value_type_ {
52 } dictionary_value_type;
54 typedef struct _dictionary_value_ {
55 dictionary_value_type type;
58 void (*callback)(void);
60 signed int scope; /* default scope (common,appli,user profile,session) */
63 /*-------------------------------------------------------------------------*/
65 @brief Dictionary object
67 This object contains a list of string/string associations. Each
68 association is identified by a unique string key. Looking up values
69 in the dictionary is speeded up by the use of a (hopefully collision-free)
72 /*-------------------------------------------------------------------------*/
73 typedef struct _dictionary_ {
74 int n; /** Number of entries in dictionary */
75 int size; /** Storage size */
76 // char** val; /** List of string values */
77 dictionary_value* values;
78 char** key; /** List of string keys */
79 unsigned* hash; /** List of hash values for keys */
83 /*---------------------------------------------------------------------------
85 ---------------------------------------------------------------------------*/
87 /*-------------------------------------------------------------------------*/
89 @brief Compute the hash key for a string.
90 @param key Character string to use for key.
91 @return 1 unsigned int on at least 32 bits.
93 This hash function has been taken from an Article in Dr Dobbs Journal.
94 This is normally a collision-free function, distributing keys evenly.
95 The key is stored anyway in the struct so that collision can be avoided
96 by comparing the key itself in last resort.
98 /*--------------------------------------------------------------------------*/
99 unsigned dictionary_hash(const char * key);
101 /*-------------------------------------------------------------------------*/
103 @brief Create a new dictionary object.
104 @param size Optional initial size of the dictionary.
105 @return 1 newly allocated dictionary objet.
107 This function allocates a new dictionary object of given size and returns
108 it. If you do not know in advance (roughly) the number of entries in the
109 dictionary, give size=0.
111 /*--------------------------------------------------------------------------*/
112 dictionary * dictionary_new(int size);
114 /*-------------------------------------------------------------------------*/
116 @brief Delete a dictionary object
117 @param d dictionary object to deallocate.
120 Deallocate a dictionary object and all memory associated to it.
122 /*--------------------------------------------------------------------------*/
123 void dictionary_del(dictionary * vd);
125 /*-------------------------------------------------------------------------*/
127 @brief Get a value from a dictionary.
128 @param d dictionary object to search.
129 @param key Key to look for in the dictionary.
130 @return 1 pointer to internally allocated character string.
132 This function locates a key in a dictionary and returns a pointer to its
133 value, or the passed 'def' pointer if no such key can be found in
134 dictionary. The returned character pointer points to data internal to the
135 dictionary object, you should not try to free it or modify it.
137 /*--------------------------------------------------------------------------*/
138 dictionary_value* dictionary_get(dictionary * d, const char * key);
141 /*-------------------------------------------------------------------------*/
143 @brief Set a value in a dictionary.
144 @param d dictionary object to modify.
145 @param key Key to modify or add.
146 @param val Value to add.
147 @return int 0 if Ok, anything else otherwise
149 If the given key is found in the dictionary, the associated value is
150 replaced by the provided one. If the key cannot be found in the
151 dictionary, it is added to it.
153 It is Ok to provide a NULL value for val, but NULL values for the dictionary
154 or the key are considered as errors: the function will return immediately
157 Notice that if you dictionary_set a variable to NULL, a call to
158 dictionary_get will return a NULL value: the variable will be found, and
159 its value (NULL) is returned. In other words, setting the variable
160 content to NULL is equivalent to deleting the variable from the
161 dictionary. It is not possible (in this implementation) to have a key in
162 the dictionary without value.
164 This function returns non-zero in case of failure.
166 /*--------------------------------------------------------------------------*/
167 dictionary_value* dictionary_set(dictionary * d, const char * key, char * val, int type, void* ptr,void (*cb)(void));
169 /*-------------------------------------------------------------------------*/
171 @brief Delete a key in a dictionary
172 @param d dictionary object to modify.
173 @param key Key to remove.
176 This function deletes a key in a dictionary. Nothing is done if the
179 /*--------------------------------------------------------------------------*/
180 void dictionary_unset(dictionary * d, const char * key);
183 /*-------------------------------------------------------------------------*/
185 @brief Dump a dictionary to an opened file pointer.
186 @param d Dictionary to dump
187 @param f Opened file pointer.
190 Dumps a dictionary onto an opened file pointer. Key pairs are printed out
191 as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as
192 output file pointers.
194 /*--------------------------------------------------------------------------*/
195 void dictionary_dump(dictionary * d, FILE * out);