cleaned up some things, fixed some mem leaks
[riven-wahrk.git] / src / dictionary.h
blob3a4fc2050b37a8bcbd924a1789f54f6f1c1e6bb0
2 /*
4 * Copyright (c) 2000-2007 by Nicolas Devillard.
5 * MIT License
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
27 /*-------------------------------------------------------------------------*/
28 /**
29 @file dictionary.h
30 @author N. Devillard
31 @date Sep 2007
32 @version $Revision: 1.12 $
33 @brief Implements a dictionary for string variables.
35 This module implements a simple dictionary object, i.e. a list
36 of string/string associations. This object is useful to store e.g.
37 informations retrieved from a configuration file (ini files).
39 /*--------------------------------------------------------------------------*/
42 $Id: dictionary.h,v 1.12 2007-11-23 21:37:00 ndevilla Exp $
43 $Author: ndevilla $
44 $Date: 2007-11-23 21:37:00 $
45 $Revision: 1.12 $
48 #ifndef _DICTIONARY_H_
49 #define _DICTIONARY_H_
51 /*---------------------------------------------------------------------------
52 Includes
53 ---------------------------------------------------------------------------*/
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
60 /*---------------------------------------------------------------------------
61 New types
62 ---------------------------------------------------------------------------*/
65 /*-------------------------------------------------------------------------*/
66 /**
67 @brief Dictionary object
69 This object contains a list of string/string associations. Each
70 association is identified by a unique string key. Looking up values
71 in the dictionary is speeded up by the use of a (hopefully collision-free)
72 hash function.
74 /*-------------------------------------------------------------------------*/
75 typedef struct _dictionary_ {
76 int n ; /** Number of entries in dictionary */
77 int size ; /** Storage size */
78 char ** val ; /** List of string values */
79 char ** key ; /** List of string keys */
80 unsigned * hash ; /** List of hash values for keys */
81 } dictionary ;
84 /*---------------------------------------------------------------------------
85 Function prototypes
86 ---------------------------------------------------------------------------*/
88 /*-------------------------------------------------------------------------*/
89 /**
90 @brief Compute the hash key for a string.
91 @param key Character string to use for key.
92 @return 1 unsigned int on at least 32 bits.
94 This hash function has been taken from an Article in Dr Dobbs Journal.
95 This is normally a collision-free function, distributing keys evenly.
96 The key is stored anyway in the struct so that collision can be avoided
97 by comparing the key itself in last resort.
99 /*--------------------------------------------------------------------------*/
100 unsigned dictionary_hash(char * key);
102 /*-------------------------------------------------------------------------*/
104 @brief Create a new dictionary object.
105 @param size Optional initial size of the dictionary.
106 @return 1 newly allocated dictionary objet.
108 This function allocates a new dictionary object of given size and returns
109 it. If you do not know in advance (roughly) the number of entries in the
110 dictionary, give size=0.
112 /*--------------------------------------------------------------------------*/
113 dictionary * dictionary_new(int size);
115 /*-------------------------------------------------------------------------*/
117 @brief Delete a dictionary object
118 @param d dictionary object to deallocate.
119 @return void
121 Deallocate a dictionary object and all memory associated to it.
123 /*--------------------------------------------------------------------------*/
124 void dictionary_del(dictionary * vd);
126 /*-------------------------------------------------------------------------*/
128 @brief Get a value from a dictionary.
129 @param d dictionary object to search.
130 @param key Key to look for in the dictionary.
131 @param def Default value to return if key not found.
132 @return 1 pointer to internally allocated character string.
134 This function locates a key in a dictionary and returns a pointer to its
135 value, or the passed 'def' pointer if no such key can be found in
136 dictionary. The returned character pointer points to data internal to the
137 dictionary object, you should not try to free it or modify it.
139 /*--------------------------------------------------------------------------*/
140 char * dictionary_get(dictionary * d, char * key, char * def);
143 /*-------------------------------------------------------------------------*/
145 @brief Set a value in a dictionary.
146 @param d dictionary object to modify.
147 @param key Key to modify or add.
148 @param val Value to add.
149 @return int 0 if Ok, anything else otherwise
151 If the given key is found in the dictionary, the associated value is
152 replaced by the provided one. If the key cannot be found in the
153 dictionary, it is added to it.
155 It is Ok to provide a NULL value for val, but NULL values for the dictionary
156 or the key are considered as errors: the function will return immediately
157 in such a case.
159 Notice that if you dictionary_set a variable to NULL, a call to
160 dictionary_get will return a NULL value: the variable will be found, and
161 its value (NULL) is returned. In other words, setting the variable
162 content to NULL is equivalent to deleting the variable from the
163 dictionary. It is not possible (in this implementation) to have a key in
164 the dictionary without value.
166 This function returns non-zero in case of failure.
168 /*--------------------------------------------------------------------------*/
169 int dictionary_set(dictionary * vd, char * key, char * val);
171 /*-------------------------------------------------------------------------*/
173 @brief Delete a key in a dictionary
174 @param d dictionary object to modify.
175 @param key Key to remove.
176 @return void
178 This function deletes a key in a dictionary. Nothing is done if the
179 key cannot be found.
181 /*--------------------------------------------------------------------------*/
182 void dictionary_unset(dictionary * d, char * key);
185 /*-------------------------------------------------------------------------*/
187 @brief Dump a dictionary to an opened file pointer.
188 @param d Dictionary to dump
189 @param f Opened file pointer.
190 @return void
192 Dumps a dictionary onto an opened file pointer. Key pairs are printed out
193 as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as
194 output file pointers.
196 /*--------------------------------------------------------------------------*/
197 void dictionary_dump(dictionary * d, FILE * out);
199 #endif