4 /* Copyright (c) 2000-2007 by Nicolas Devillard.
5 * Copyright (x) 2009 by Tim Post <tinkertim@gmail.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
32 /** @addtogroup ciniparser
37 * @author N. Devillard
39 * @version $Revision: 1.12 $
40 * @brief Implements a dictionary for string variables.
42 * This module implements a simple dictionary object, i.e. a list
43 * of string/string associations. This object is useful to store e.g.
44 * information retrieved from a configuration file (ini files).
49 * @brief Dictionary object
50 * @param n Number of entries in the dictionary
51 * @param size Storage size
52 * @param val List of string values
53 * @param key List of string keys
54 * @param hash List of hash values for keys
56 * This object contains a list of string/string associations. Each
57 * association is identified by a unique string key. Looking up values
58 * in the dictionary is speeded up by the use of a (hopefully collision-free)
61 typedef struct _dictionary_ {
70 * @brief Compute the hash key for a string.
71 * @param key Character string to use for key.
72 * @return 1 unsigned int on at least 32 bits.
74 * This hash function has been taken from an Article in Dr Dobbs Journal.
75 * This is normally a collision-free function, distributing keys evenly.
76 * The key is stored anyway in the struct so that collision can be avoided
77 * by comparing the key itself in last resort.
79 unsigned dictionary_hash(char *key);
82 * @brief Create a new dictionary object.
83 * @param size Optional initial size of the dictionary.
84 * @return allocated dictionary object on success, NULL on failure
86 * This function allocates a new dictionary object of given size and returns
87 * it. If you do not know in advance (roughly) the number of entries in the
88 * dictionary, give size=0.
90 dictionary *dictionary_new(int size);
93 * @brief Delete a dictionary object
94 * @param d dictionary object to deallocate.
97 * Deallocate a dictionary object and all memory associated to it.
99 void dictionary_del(dictionary *vd);
102 * @brief Get a value from a dictionary.
103 * @param d dictionary object to search.
104 * @param key Key to look for in the dictionary.
105 * @param def Default value to return if key not found.
106 * @return 1 pointer to internally allocated character string.
108 * This function locates a key in a dictionary and returns a pointer to its
109 * value, or the passed 'def' pointer if no such key can be found in
110 * dictionary. The returned character pointer points to data internal to the
111 * dictionary object, you should not try to free it or modify it.
113 char *dictionary_get(dictionary *d, char *key, char *def);
116 * @brief Set a value in a dictionary.
117 * @param d dictionary object to modify.
118 * @param key Key to modify or add.
119 * @param val Value to add.
120 * @return int 0 if Ok, anything else otherwise
122 * If the given key is found in the dictionary, the associated value is
123 * replaced by the provided one. If the key cannot be found in the
124 * dictionary, it is added to it.
126 * It is Ok to provide a NULL value for val, but NULL values for the dictionary
127 * or the key are considered as errors: the function will return immediately
130 * Notice that if you dictionary_set a variable to NULL, a call to
131 * dictionary_get will return a NULL value: the variable will be found, and
132 * its value (NULL) is returned. In other words, setting the variable
133 * content to NULL is equivalent to deleting the variable from the
134 * dictionary. It is not possible (in this implementation) to have a key in
135 * the dictionary without value.
137 * This function returns non-zero in case of failure.
139 int dictionary_set(dictionary *vd, char *key, char *val);
142 * @brief Delete a key in a dictionary
143 * @param d dictionary object to modify.
144 * @param key Key to remove.
147 * This function deletes a key in a dictionary. Nothing is done if the
148 * key cannot be found.
150 void dictionary_unset(dictionary *d, char *key);
153 * @brief Dump a dictionary to an opened file pointer.
154 * @param d Dictionary to dump
155 * @param out Opened file pointer
158 * Dumps a dictionary onto an opened file pointer. Key pairs are printed out
159 * as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as
160 * output file pointers.
162 void dictionary_dump(dictionary *d, FILE *out);