| 1 |
|
|---|
| 2 | /*-------------------------------------------------------------------------*/
|
|---|
| 3 | /**
|
|---|
| 4 | @file dictionary.h
|
|---|
| 5 | @author N. Devillard
|
|---|
| 6 | @date Aug 2000
|
|---|
| 7 | @version $Revision: 1.11 $
|
|---|
| 8 | @brief Implements a dictionary for string variables.
|
|---|
| 9 |
|
|---|
| 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).
|
|---|
| 13 | */
|
|---|
| 14 | /*--------------------------------------------------------------------------*/
|
|---|
| 15 |
|
|---|
| 16 | /*
|
|---|
| 17 | $Id: dictionary.h,v 1.11 2002-06-17 09:30:46 ndevilla Exp $
|
|---|
| 18 | $Author: ndevilla $
|
|---|
| 19 | $Date: 2002-06-17 09:30:46 $
|
|---|
| 20 | $Revision: 1.11 $
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | #ifndef _DICTIONARY_H_
|
|---|
| 24 | #define _DICTIONARY_H_
|
|---|
| 25 |
|
|---|
| 26 | /*---------------------------------------------------------------------------
|
|---|
| 27 | Includes
|
|---|
| 28 | ---------------------------------------------------------------------------*/
|
|---|
| 29 |
|
|---|
| 30 | #include <stdio.h>
|
|---|
| 31 | #include <stdlib.h>
|
|---|
| 32 | #include <string.h>
|
|---|
| 33 | #include <unistd.h>
|
|---|
| 34 |
|
|---|
| 35 | /*---------------------------------------------------------------------------
|
|---|
| 36 | New types
|
|---|
| 37 | ---------------------------------------------------------------------------*/
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | /*-------------------------------------------------------------------------*/
|
|---|
| 41 | /**
|
|---|
| 42 | @brief Dictionary object
|
|---|
| 43 |
|
|---|
| 44 | This object contains a list of string/string associations. Each
|
|---|
| 45 | association is identified by a unique string key. Looking up values
|
|---|
| 46 | in the dictionary is speeded up by the use of a (hopefully collision-free)
|
|---|
| 47 | hash function.
|
|---|
| 48 | */
|
|---|
| 49 | /*-------------------------------------------------------------------------*/
|
|---|
| 50 | typedef struct _dictionary_ {
|
|---|
| 51 | int n ; /** Number of entries in dictionary */
|
|---|
| 52 | int size ; /** Storage size */
|
|---|
| 53 | char ** val ; /** List of string values */
|
|---|
| 54 | char ** key ; /** List of string keys */
|
|---|
| 55 | unsigned * hash ; /** List of hash values for keys */
|
|---|
| 56 | } dictionary ;
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 | /*---------------------------------------------------------------------------
|
|---|
| 60 | Function prototypes
|
|---|
| 61 | ---------------------------------------------------------------------------*/
|
|---|
| 62 |
|
|---|
| 63 | /*-------------------------------------------------------------------------*/
|
|---|
| 64 | /**
|
|---|
| 65 | @brief Compute the hash key for a string.
|
|---|
| 66 | @param key Character string to use for key.
|
|---|
| 67 | @return 1 unsigned int on at least 32 bits.
|
|---|
| 68 |
|
|---|
| 69 | This hash function has been taken from an Article in Dr Dobbs Journal.
|
|---|
| 70 | This is normally a collision-free function, distributing keys evenly.
|
|---|
| 71 | The key is stored anyway in the struct so that collision can be avoided
|
|---|
| 72 | by comparing the key itself in last resort.
|
|---|
| 73 | */
|
|---|
| 74 | /*--------------------------------------------------------------------------*/
|
|---|
| 75 | unsigned dictionary_hash(char * key);
|
|---|
| 76 |
|
|---|
| 77 | /*-------------------------------------------------------------------------*/
|
|---|
| 78 | /**
|
|---|
| 79 | @brief Create a new dictionary object.
|
|---|
| 80 | @param size Optional initial size of the dictionary.
|
|---|
| 81 | @return 1 newly allocated dictionary objet.
|
|---|
| 82 |
|
|---|
| 83 | This function allocates a new dictionary object of given size and returns
|
|---|
| 84 | it. If you do not know in advance (roughly) the number of entries in the
|
|---|
| 85 | dictionary, give size=0.
|
|---|
| 86 | */
|
|---|
| 87 | /*--------------------------------------------------------------------------*/
|
|---|
| 88 | dictionary * dictionary_new(int size);
|
|---|
| 89 |
|
|---|
| 90 | /*-------------------------------------------------------------------------*/
|
|---|
| 91 | /**
|
|---|
| 92 | @brief Delete a dictionary object
|
|---|
| 93 | @param d dictionary object to deallocate.
|
|---|
| 94 | @return void
|
|---|
| 95 |
|
|---|
| 96 | Deallocate a dictionary object and all memory associated to it.
|
|---|
| 97 | */
|
|---|
| 98 | /*--------------------------------------------------------------------------*/
|
|---|
| 99 | void dictionary_del(dictionary * vd);
|
|---|
| 100 |
|
|---|
| 101 | /*-------------------------------------------------------------------------*/
|
|---|
| 102 | /**
|
|---|
| 103 | @brief Get a value from a dictionary.
|
|---|
| 104 | @param d dictionary object to search.
|
|---|
| 105 | @param key Key to look for in the dictionary.
|
|---|
| 106 | @param def Default value to return if key not found.
|
|---|
| 107 | @return 1 pointer to internally allocated character string.
|
|---|
| 108 |
|
|---|
| 109 | This function locates a key in a dictionary and returns a pointer to its
|
|---|
| 110 | value, or the passed 'def' pointer if no such key can be found in
|
|---|
| 111 | dictionary. The returned character pointer points to data internal to the
|
|---|
| 112 | dictionary object, you should not try to free it or modify it.
|
|---|
| 113 | */
|
|---|
| 114 | /*--------------------------------------------------------------------------*/
|
|---|
| 115 | char * dictionary_get(dictionary * d, char * key, char * def);
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 | /*-------------------------------------------------------------------------*/
|
|---|
| 119 | /**
|
|---|
| 120 | @brief Get a value from a dictionary, as a char.
|
|---|
| 121 | @param d dictionary object to search.
|
|---|
| 122 | @param key Key to look for in the dictionary.
|
|---|
| 123 | @param def Default value for the key if not found.
|
|---|
| 124 | @return char
|
|---|
| 125 |
|
|---|
| 126 | This function locates a key in a dictionary using dictionary_get,
|
|---|
| 127 | and returns the first char of the found string.
|
|---|
| 128 | */
|
|---|
| 129 | /*--------------------------------------------------------------------------*/
|
|---|
| 130 | char dictionary_getchar(dictionary * d, char * key, char def) ;
|
|---|
| 131 |
|
|---|
| 132 | /*-------------------------------------------------------------------------*/
|
|---|
| 133 | /**
|
|---|
| 134 | @brief Get a value from a dictionary, as an int.
|
|---|
| 135 | @param d dictionary object to search.
|
|---|
| 136 | @param key Key to look for in the dictionary.
|
|---|
| 137 | @param def Default value for the key if not found.
|
|---|
| 138 | @return int
|
|---|
| 139 |
|
|---|
| 140 | This function locates a key in a dictionary using dictionary_get,
|
|---|
| 141 | and applies atoi on it to return an int. If the value cannot be found
|
|---|
| 142 | in the dictionary, the default is returned.
|
|---|
| 143 | */
|
|---|
| 144 | /*--------------------------------------------------------------------------*/
|
|---|
| 145 | int dictionary_getint(dictionary * d, char * key, int def);
|
|---|
| 146 |
|
|---|
| 147 | /*-------------------------------------------------------------------------*/
|
|---|
| 148 | /**
|
|---|
| 149 | @brief Get a value from a dictionary, as a double.
|
|---|
| 150 | @param d dictionary object to search.
|
|---|
| 151 | @param key Key to look for in the dictionary.
|
|---|
| 152 | @param def Default value for the key if not found.
|
|---|
| 153 | @return double
|
|---|
| 154 |
|
|---|
| 155 | This function locates a key in a dictionary using dictionary_get,
|
|---|
| 156 | and applies atof on it to return a double. If the value cannot be found
|
|---|
| 157 | in the dictionary, the default is returned.
|
|---|
| 158 | */
|
|---|
| 159 | /*--------------------------------------------------------------------------*/
|
|---|
| 160 | double dictionary_getdouble(dictionary * d, char * key, double def);
|
|---|
| 161 |
|
|---|
| 162 | /*-------------------------------------------------------------------------*/
|
|---|
| 163 | /**
|
|---|
| 164 | @brief Set a value in a dictionary.
|
|---|
| 165 | @param d dictionary object to modify.
|
|---|
| 166 | @param key Key to modify or add.
|
|---|
| 167 | @param val Value to add.
|
|---|
| 168 | @return void
|
|---|
| 169 |
|
|---|
| 170 | If the given key is found in the dictionary, the associated value is
|
|---|
| 171 | replaced by the provided one. If the key cannot be found in the
|
|---|
| 172 | dictionary, it is added to it.
|
|---|
| 173 |
|
|---|
| 174 | It is Ok to provide a NULL value for val, but NULL values for the dictionary
|
|---|
| 175 | or the key are considered as errors: the function will return immediately
|
|---|
| 176 | in such a case.
|
|---|
| 177 |
|
|---|
| 178 | Notice that if you dictionary_set a variable to NULL, a call to
|
|---|
| 179 | dictionary_get will return a NULL value: the variable will be found, and
|
|---|
| 180 | its value (NULL) is returned. In other words, setting the variable
|
|---|
| 181 | content to NULL is equivalent to deleting the variable from the
|
|---|
| 182 | dictionary. It is not possible (in this implementation) to have a key in
|
|---|
| 183 | the dictionary without value.
|
|---|
| 184 | */
|
|---|
| 185 | /*--------------------------------------------------------------------------*/
|
|---|
| 186 | void dictionary_set(dictionary * vd, char * key, char * val);
|
|---|
| 187 |
|
|---|
| 188 | /*-------------------------------------------------------------------------*/
|
|---|
| 189 | /**
|
|---|
| 190 | @brief Delete a key in a dictionary
|
|---|
| 191 | @param d dictionary object to modify.
|
|---|
| 192 | @param key Key to remove.
|
|---|
| 193 | @return void
|
|---|
| 194 |
|
|---|
| 195 | This function deletes a key in a dictionary. Nothing is done if the
|
|---|
| 196 | key cannot be found.
|
|---|
| 197 | */
|
|---|
| 198 | /*--------------------------------------------------------------------------*/
|
|---|
| 199 | void dictionary_unset(dictionary * d, char * key);
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 | /*-------------------------------------------------------------------------*/
|
|---|
| 203 | /**
|
|---|
| 204 | @brief Set a key in a dictionary, providing an int.
|
|---|
| 205 | @param d Dictionary to update.
|
|---|
| 206 | @param key Key to modify or add
|
|---|
| 207 | @param val Integer value to store (will be stored as a string).
|
|---|
| 208 | @return void
|
|---|
| 209 |
|
|---|
| 210 | This helper function calls dictionary_set() with the provided integer
|
|---|
| 211 | converted to a string using %d.
|
|---|
| 212 | */
|
|---|
| 213 | /*--------------------------------------------------------------------------*/
|
|---|
| 214 | void dictionary_setint(dictionary * d, char * key, int val);
|
|---|
| 215 |
|
|---|
| 216 | /*-------------------------------------------------------------------------*/
|
|---|
| 217 | /**
|
|---|
| 218 | @brief Set a key in a dictionary, providing a double.
|
|---|
| 219 | @param d Dictionary to update.
|
|---|
| 220 | @param key Key to modify or add
|
|---|
| 221 | @param val Double value to store (will be stored as a string).
|
|---|
| 222 | @return void
|
|---|
| 223 |
|
|---|
| 224 | This helper function calls dictionary_set() with the provided double
|
|---|
| 225 | converted to a string using %g.
|
|---|
| 226 | */
|
|---|
| 227 | /*--------------------------------------------------------------------------*/
|
|---|
| 228 | void dictionary_setdouble(dictionary * d, char * key, double val);
|
|---|
| 229 |
|
|---|
| 230 | /*-------------------------------------------------------------------------*/
|
|---|
| 231 | /**
|
|---|
| 232 | @brief Dump a dictionary to an opened file pointer.
|
|---|
| 233 | @param d Dictionary to dump
|
|---|
| 234 | @param f Opened file pointer.
|
|---|
| 235 | @return void
|
|---|
| 236 |
|
|---|
| 237 | Dumps a dictionary onto an opened file pointer. Key pairs are printed out
|
|---|
| 238 | as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as
|
|---|
| 239 | output file pointers.
|
|---|
| 240 | */
|
|---|
| 241 | /*--------------------------------------------------------------------------*/
|
|---|
| 242 | void dictionary_dump(dictionary * d, FILE * out);
|
|---|
| 243 |
|
|---|
| 244 | #endif
|
|---|