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