ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / Soft / Lib / iniparser3.0b / src / dictionary.h
1
2 /*-------------------------------------------------------------------------*/
3 /**
4    @file    dictionary.h
5    @author  N. Devillard
6    @date    Sep 2007
7    @version $Revision: 1.1.2.1 $
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.1.2.1 2010-02-12 10:19:23 kleplat Exp $
18         $Author: kleplat $
19         $Date: 2010-02-12 10:19:23 $
20         $Revision: 1.1.2.1 $
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 #ifndef _WIN32
34 #include <unistd.h>
35 #endif
36
37 /*---------------------------------------------------------------------------
38                                                                 New types
39  ---------------------------------------------------------------------------*/
40
41 typedef enum _dictionary_value_type_ {
42   INI_SECTION = 0x00,
43   INI_STRING  = 0x01,
44   INI_INT     = 0x02,
45   INI_FLOAT   = 0x03,
46   INI_DOUBLE  = 0x04,
47   INI_BOOLEAN = 0x05,
48   INI_VECTOR  = 0x06,
49   INI_MATRIX  = 0x07,
50   INI_UNKNOW  = 0x08,
51   INI_VECTOR21= 0x09
52 } dictionary_value_type;
53
54 typedef struct _dictionary_value_ {
55   dictionary_value_type type;
56   char* val;
57   void* ptr;
58   void (*callback)(void); 
59   char rw;
60   signed int scope; /* default scope (common,appli,user profile,session) */
61 } dictionary_value;
62
63 /*-------------------------------------------------------------------------*/
64 /**
65   @brief        Dictionary object
66
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)
70   hash function.
71  */
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 */
80 } dictionary ;
81
82
83 /*---------------------------------------------------------------------------
84                                                         Function prototypes
85  ---------------------------------------------------------------------------*/
86
87 /*-------------------------------------------------------------------------*/
88 /**
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.
92
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.
97  */
98 /*--------------------------------------------------------------------------*/
99 unsigned dictionary_hash(const char * key);
100
101 /*-------------------------------------------------------------------------*/
102 /**
103   @brief    Create a new dictionary object.
104   @param    size    Optional initial size of the dictionary.
105   @return   1 newly allocated dictionary objet.
106
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.
110  */
111 /*--------------------------------------------------------------------------*/
112 dictionary * dictionary_new(int size);
113
114 /*-------------------------------------------------------------------------*/
115 /**
116   @brief    Delete a dictionary object
117   @param    d   dictionary object to deallocate.
118   @return   void
119
120   Deallocate a dictionary object and all memory associated to it.
121  */
122 /*--------------------------------------------------------------------------*/
123 void dictionary_del(dictionary * vd);
124
125 /*-------------------------------------------------------------------------*/
126 /**
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.
131
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.
136  */
137 /*--------------------------------------------------------------------------*/
138 dictionary_value* dictionary_get(dictionary * d, const char * key);
139
140
141 /*-------------------------------------------------------------------------*/
142 /**
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
148
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.
152
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
155   in such a case.
156
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.
163
164   This function returns non-zero in case of failure.
165  */
166 /*--------------------------------------------------------------------------*/
167 dictionary_value* dictionary_set(dictionary * d, const char * key, char * val, int type, void* ptr,void (*cb)(void));
168
169 /*-------------------------------------------------------------------------*/
170 /**
171   @brief    Delete a key in a dictionary
172   @param    d       dictionary object to modify.
173   @param    key     Key to remove.
174   @return   void
175
176   This function deletes a key in a dictionary. Nothing is done if the
177   key cannot be found.
178  */
179 /*--------------------------------------------------------------------------*/
180 void dictionary_unset(dictionary * d, const char * key);
181
182
183 /*-------------------------------------------------------------------------*/
184 /**
185   @brief    Dump a dictionary to an opened file pointer.
186   @param    d   Dictionary to dump
187   @param    f   Opened file pointer.
188   @return   void
189
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.
193  */
194 /*--------------------------------------------------------------------------*/
195 void dictionary_dump(dictionary * d, FILE * out);
196
197 #endif