commit | author | age
|
d6b4a7
|
1 |
|
G |
2 |
/*-------------------------------------------------------------------------*/ |
|
3 |
/** |
|
4 |
@file dictionary.h |
|
5 |
@author N. Devillard |
|
6 |
@brief Implements a dictionary for string variables. |
|
7 |
@url https://github.com/ndevilla/iniparser |
|
8 |
|
|
9 |
This module implements a simple dictionary object, i.e. a list |
|
10 |
of string/string associations. This object is useful to store e.g. |
|
11 |
informations retrieved from a configuration file (ini files). |
|
12 |
*/ |
|
13 |
/*--------------------------------------------------------------------------*/ |
|
14 |
|
|
15 |
#ifndef _DICTIONARY_H_ |
|
16 |
#define _DICTIONARY_H_ |
|
17 |
|
|
18 |
/*--------------------------------------------------------------------------- |
|
19 |
Includes |
|
20 |
---------------------------------------------------------------------------*/ |
|
21 |
|
|
22 |
#include <stdio.h> |
|
23 |
#include <stdlib.h> |
|
24 |
#include <string.h> |
|
25 |
#include <unistd.h> |
|
26 |
|
|
27 |
#ifdef __cplusplus |
|
28 |
extern "C" { |
|
29 |
#endif |
|
30 |
|
|
31 |
/*--------------------------------------------------------------------------- |
|
32 |
New types |
|
33 |
---------------------------------------------------------------------------*/ |
|
34 |
|
|
35 |
|
|
36 |
/*-------------------------------------------------------------------------*/ |
|
37 |
/** |
|
38 |
@brief Dictionary object |
|
39 |
|
|
40 |
This object contains a list of string/string associations. Each |
|
41 |
association is identified by a unique string key. Looking up values |
|
42 |
in the dictionary is speeded up by the use of a (hopefully collision-free) |
|
43 |
hash function. |
|
44 |
*/ |
|
45 |
/*-------------------------------------------------------------------------*/ |
|
46 |
typedef struct _dictionary_ { |
|
47 |
int n ; /** Number of entries in dictionary */ |
|
48 |
ssize_t size ; /** Storage size */ |
|
49 |
char ** val ; /** List of string values */ |
|
50 |
char ** key ; /** List of string keys */ |
|
51 |
unsigned * hash ; /** List of hash values for keys */ |
|
52 |
} dictionary ; |
|
53 |
|
|
54 |
|
|
55 |
/*--------------------------------------------------------------------------- |
|
56 |
Function prototypes |
|
57 |
---------------------------------------------------------------------------*/ |
|
58 |
|
|
59 |
/*-------------------------------------------------------------------------*/ |
|
60 |
/** |
|
61 |
@brief Compute the hash key for a string. |
|
62 |
@param key Character string to use for key. |
|
63 |
@return 1 unsigned int on at least 32 bits. |
|
64 |
|
|
65 |
This hash function has been taken from an Article in Dr Dobbs Journal. |
|
66 |
This is normally a collision-free function, distributing keys evenly. |
|
67 |
The key is stored anyway in the struct so that collision can be avoided |
|
68 |
by comparing the key itself in last resort. |
|
69 |
*/ |
|
70 |
/*--------------------------------------------------------------------------*/ |
|
71 |
unsigned dictionary_hash(const char * key); |
|
72 |
|
|
73 |
/*-------------------------------------------------------------------------*/ |
|
74 |
/** |
|
75 |
@brief Create a new dictionary object. |
|
76 |
@param size Optional initial size of the dictionary. |
|
77 |
@return 1 newly allocated dictionary object. |
|
78 |
|
|
79 |
This function allocates a new dictionary object of given size and returns |
|
80 |
it. If you do not know in advance (roughly) the number of entries in the |
|
81 |
dictionary, give size=0. |
|
82 |
*/ |
|
83 |
/*--------------------------------------------------------------------------*/ |
|
84 |
dictionary * dictionary_new(size_t size); |
|
85 |
|
|
86 |
/*-------------------------------------------------------------------------*/ |
|
87 |
/** |
|
88 |
@brief Delete a dictionary object |
|
89 |
@param d dictionary object to deallocate. |
|
90 |
@return void |
|
91 |
|
|
92 |
Deallocate a dictionary object and all memory associated to it. |
|
93 |
*/ |
|
94 |
/*--------------------------------------------------------------------------*/ |
|
95 |
void dictionary_del(dictionary * vd); |
|
96 |
|
|
97 |
/*-------------------------------------------------------------------------*/ |
|
98 |
/** |
|
99 |
@brief Get a value from a dictionary. |
|
100 |
@param d dictionary object to search. |
|
101 |
@param key Key to look for in the dictionary. |
|
102 |
@param def Default value to return if key not found. |
|
103 |
@return 1 pointer to internally allocated character string. |
|
104 |
|
|
105 |
This function locates a key in a dictionary and returns a pointer to its |
|
106 |
value, or the passed 'def' pointer if no such key can be found in |
|
107 |
dictionary. The returned character pointer points to data internal to the |
|
108 |
dictionary object, you should not try to free it or modify it. |
|
109 |
*/ |
|
110 |
/*--------------------------------------------------------------------------*/ |
|
111 |
const char * dictionary_get(const dictionary * d, const char * key, const char * def); |
|
112 |
|
|
113 |
|
|
114 |
/*-------------------------------------------------------------------------*/ |
|
115 |
/** |
|
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 |
|
121 |
|
|
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. |
|
125 |
|
|
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 |
|
128 |
in such a case. |
|
129 |
|
|
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. |
|
136 |
|
|
137 |
This function returns non-zero in case of failure. |
|
138 |
*/ |
|
139 |
/*--------------------------------------------------------------------------*/ |
|
140 |
int dictionary_set(dictionary * vd, const char * key, const char * val); |
|
141 |
|
|
142 |
/*-------------------------------------------------------------------------*/ |
|
143 |
/** |
|
144 |
@brief Delete a key in a dictionary |
|
145 |
@param d dictionary object to modify. |
|
146 |
@param key Key to remove. |
|
147 |
@return void |
|
148 |
|
|
149 |
This function deletes a key in a dictionary. Nothing is done if the |
|
150 |
key cannot be found. |
|
151 |
*/ |
|
152 |
/*--------------------------------------------------------------------------*/ |
|
153 |
void dictionary_unset(dictionary * d, const char * key); |
|
154 |
|
|
155 |
|
|
156 |
/*-------------------------------------------------------------------------*/ |
|
157 |
/** |
|
158 |
@brief Dump a dictionary to an opened file pointer. |
|
159 |
@param d Dictionary to dump |
|
160 |
@param f Opened file pointer. |
|
161 |
@return void |
|
162 |
|
|
163 |
Dumps a dictionary onto an opened file pointer. Key pairs are printed out |
|
164 |
as @c [Key]=[Value], one per line. It is Ok to provide stdout or stderr as |
|
165 |
output file pointers. |
|
166 |
*/ |
|
167 |
/*--------------------------------------------------------------------------*/ |
|
168 |
void dictionary_dump(const dictionary * d, FILE * out); |
|
169 |
|
|
170 |
#ifdef __cplusplus |
|
171 |
} |
|
172 |
#endif |
|
173 |
|
|
174 |
#endif |