00001 00002 /*-------------------------------------------------------------------------*/ 00003 /** 00004 @file dictionary.h 00005 @author N. Devillard 00006 @date Aug 2000 00007 @version $Revision: 1.11 $ 00008 @brief Implements a dictionary for string variables. 00009 00010 This module implements a simple dictionary object, i.e. a list 00011 of string/string associations. This object is useful to store e.g. 00012 informations retrieved from a configuration file (ini files). 00013 */ 00014 /*--------------------------------------------------------------------------*/ 00015 00016 /* 00017 $Id: dictionary.h,v 1.11 2002/06/17 09:30:46 ndevilla Exp $ 00018 $Author: ndevilla $ 00019 $Date: 2002/06/17 09:30:46 $ 00020 $Revision: 1.11 $ 00021 */ 00022 00023 #ifndef _DICTIONARY_H_ 00024 #define _DICTIONARY_H_ 00025 00026 /*--------------------------------------------------------------------------- 00027 Includes 00028 ---------------------------------------------------------------------------*/ 00029 00030 #include <stdio.h> 00031 #include <stdlib.h> 00032 #include <string.h> 00033 #include <unistd.h> 00034 00035 /*--------------------------------------------------------------------------- 00036 New types 00037 ---------------------------------------------------------------------------*/ 00038 00039 00040 /*-------------------------------------------------------------------------*/ 00041 /** 00042 @brief Dictionary object 00043 00044 This object contains a list of string/string associations. Each 00045 association is identified by a unique string key. Looking up values 00046 in the dictionary is speeded up by the use of a (hopefully collision-free) 00047 hash function. 00048 */ 00049 /*-------------------------------------------------------------------------*/ 00050 typedef struct _dictionary_ { 00051 int n ; /** Number of entries in dictionary */ 00052 int size ; /** Storage size */ 00053 char ** val ; /** List of string values */ 00054 char ** key ; /** List of string keys */ 00055 unsigned * hash ; /** List of hash values for keys */ 00056 } dictionary ; 00057 00058 00059 /*--------------------------------------------------------------------------- 00060 Function prototypes 00061 ---------------------------------------------------------------------------*/ 00062 00063 /*-------------------------------------------------------------------------*/ 00064 /** 00065 @brief Compute the hash key for a string. 00066 @param key Character string to use for key. 00067 @return 1 unsigned int on at least 32 bits. 00068 00069 This hash function has been taken from an Article in Dr Dobbs Journal. 00070 This is normally a collision-free function, distributing keys evenly. 00071 The key is stored anyway in the struct so that collision can be avoided 00072 by comparing the key itself in last resort. 00073 */ 00074 /*--------------------------------------------------------------------------*/ 00075 unsigned dictionary_hash(char * key); 00076 00077 /*-------------------------------------------------------------------------*/ 00078 /** 00079 @brief Create a new dictionary object. 00080 @param size Optional initial size of the dictionary. 00081 @return 1 newly allocated dictionary objet. 00082 00083 This function allocates a new dictionary object of given size and returns 00084 it. If you do not know in advance (roughly) the number of entries in the 00085 dictionary, give size=0. 00086 */ 00087 /*--------------------------------------------------------------------------*/ 00088 dictionary * dictionary_new(int size); 00089 00090 /*-------------------------------------------------------------------------*/ 00091 /** 00092 @brief Delete a dictionary object 00093 @param d dictionary object to deallocate. 00094 @return void 00095 00096 Deallocate a dictionary object and all memory associated to it. 00097 */ 00098 /*--------------------------------------------------------------------------*/ 00099 void dictionary_del(dictionary * vd); 00100 00101 /*-------------------------------------------------------------------------*/ 00102 /** 00103 @brief Get a value from a dictionary. 00104 @param d dictionary object to search. 00105 @param key Key to look for in the dictionary. 00106 @param def Default value to return if key not found. 00107 @return 1 pointer to internally allocated character string. 00108 00109 This function locates a key in a dictionary and returns a pointer to its 00110 value, or the passed 'def' pointer if no such key can be found in 00111 dictionary. The returned character pointer points to data internal to the 00112 dictionary object, you should not try to free it or modify it. 00113 */ 00114 /*--------------------------------------------------------------------------*/ 00115 char * dictionary_get(dictionary * d, char * key, char * def); 00116 00117 00118 /*-------------------------------------------------------------------------*/ 00119 /** 00120 @brief Get a value from a dictionary, as a char. 00121 @param d dictionary object to search. 00122 @param key Key to look for in the dictionary. 00123 @param def Default value for the key if not found. 00124 @return char 00125 00126 This function locates a key in a dictionary using dictionary_get, 00127 and returns the first char of the found string. 00128 */ 00129 /*--------------------------------------------------------------------------*/ 00130 char dictionary_getchar(dictionary * d, char * key, char def) ; 00131 00132 /*-------------------------------------------------------------------------*/ 00133 /** 00134 @brief Get a value from a dictionary, as an int. 00135 @param d dictionary object to search. 00136 @param key Key to look for in the dictionary. 00137 @param def Default value for the key if not found. 00138 @return int 00139 00140 This function locates a key in a dictionary using dictionary_get, 00141 and applies atoi on it to return an int. If the value cannot be found 00142 in the dictionary, the default is returned. 00143 */ 00144 /*--------------------------------------------------------------------------*/ 00145 int dictionary_getint(dictionary * d, char * key, int def); 00146 00147 /*-------------------------------------------------------------------------*/ 00148 /** 00149 @brief Get a value from a dictionary, as a double. 00150 @param d dictionary object to search. 00151 @param key Key to look for in the dictionary. 00152 @param def Default value for the key if not found. 00153 @return double 00154 00155 This function locates a key in a dictionary using dictionary_get, 00156 and applies atof on it to return a double. If the value cannot be found 00157 in the dictionary, the default is returned. 00158 */ 00159 /*--------------------------------------------------------------------------*/ 00160 double dictionary_getdouble(dictionary * d, char * key, double def); 00161 00162 /*-------------------------------------------------------------------------*/ 00163 /** 00164 @brief Set a value in a dictionary. 00165 @param d dictionary object to modify. 00166 @param key Key to modify or add. 00167 @param val Value to add. 00168 @return void 00169 00170 If the given key is found in the dictionary, the associated value is 00171 replaced by the provided one. If the key cannot be found in the 00172 dictionary, it is added to it. 00173 00174 It is Ok to provide a NULL value for val, but NULL values for the dictionary 00175 or the key are considered as errors: the function will return immediately 00176 in such a case. 00177 00178 Notice that if you dictionary_set a variable to NULL, a call to 00179 dictionary_get will return a NULL value: the variable will be found, and 00180 its value (NULL) is returned. In other words, setting the variable 00181 content to NULL is equivalent to deleting the variable from the 00182 dictionary. It is not possible (in this implementation) to have a key in 00183 the dictionary without value. 00184 */ 00185 /*--------------------------------------------------------------------------*/ 00186 void dictionary_set(dictionary * vd, char * key, char * val); 00187 00188 /*-------------------------------------------------------------------------*/ 00189 /** 00190 @brief Delete a key in a dictionary 00191 @param d dictionary object to modify. 00192 @param key Key to remove. 00193 @return void 00194 00195 This function deletes a key in a dictionary. Nothing is done if the 00196 key cannot be found. 00197 */ 00198 /*--------------------------------------------------------------------------*/ 00199 void dictionary_unset(dictionary * d, char * key); 00200 00201 00202 /*-------------------------------------------------------------------------*/ 00203 /** 00204 @brief Set a key in a dictionary, providing an int. 00205 @param d Dictionary to update. 00206 @param key Key to modify or add 00207 @param val Integer value to store (will be stored as a string). 00208 @return void 00209 00210 This helper function calls dictionary_set() with the provided integer 00211 converted to a string using %d. 00212 */ 00213 /*--------------------------------------------------------------------------*/ 00214 void dictionary_setint(dictionary * d, char * key, int val); 00215 00216 /*-------------------------------------------------------------------------*/ 00217 /** 00218 @brief Set a key in a dictionary, providing a double. 00219 @param d Dictionary to update. 00220 @param key Key to modify or add 00221 @param val Double value to store (will be stored as a string). 00222 @return void 00223 00224 This helper function calls dictionary_set() with the provided double 00225 converted to a string using %g. 00226 */ 00227 /*--------------------------------------------------------------------------*/ 00228 void dictionary_setdouble(dictionary * d, char * key, double val); 00229 00230 /*-------------------------------------------------------------------------*/ 00231 /** 00232 @brief Dump a dictionary to an opened file pointer. 00233 @param d Dictionary to dump 00234 @param f Opened file pointer. 00235 @return void 00236 00237 Dumps a dictionary onto an opened file pointer. Key pairs are printed out 00238 as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as 00239 output file pointers. 00240 */ 00241 /*--------------------------------------------------------------------------*/ 00242 void dictionary_dump(dictionary * d, FILE * out); 00243 00244 #endif