source: thirdparty/SZ/sz/include/dictionary.h @ 2c47b73

Revision 2c47b73, 6.5 KB checked in by Hal Finkel <hfinkel@…>, 6 years ago (diff)

more work on adding SZ (latest version)

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