Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Plug a memleak when using the parser
[simgrid.git] / src / xbt / dict.c
1 /* $Id$ */
2
3 /* dict - a generic dictionnary, variation over the B-tree concept          */
4
5 /* Copyright (c) 2003,2004 Martin Quinson. All rights reserved.             */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "dict_private.h"
11
12 #include <stdlib.h> /* malloc() */
13 #include <string.h> /* strlen() */
14
15 #include <stdio.h>
16
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dict,xbt,
18    "Dictionaries provide the same functionnalities than hash tables");
19 /*####[ Private prototypes ]#################################################*/
20
21 /*####[ Code ]###############################################################*/
22
23 /**
24  * \ingroup XBT_dict
25  *
26  * \return pointer to the destination
27  *
28  * Creates and initialize a new dictionnary
29  */
30 xbt_dict_t 
31 xbt_dict_new(void) {
32   xbt_dict_t res= xbt_new(s_xbt_dict_t,1);
33   res->head=NULL;
34   return res;
35 }
36 /**
37  * \ingroup XBT_dict
38  * \param dict the dictionnary to be freed
39  *
40  * Frees a cache structure with all its childs.
41  */
42 void
43 xbt_dict_free(xbt_dict_t *dict)  {
44   if (dict && *dict) {
45     if ((*dict)->head) {
46       xbt_dictelm_free( &( (*dict)->head ) );
47       (*dict)->head = NULL;
48     }
49     xbt_free(*dict);
50     *dict=NULL;
51   }
52 }
53
54 /**
55  * \ingroup XBT_dict
56  *
57  * \param dict the container
58  * \param key the key to set the new data
59  * \param key_len the size of the #key
60  * \param data the data to add in the dict
61  * \param free_ctn function to call with (#key as argument) when 
62  *        #key is removed from the dictionnary
63  *
64  * set the #data in the structure under the #key, which can be any kind 
65  * of data, as long as its length is provided in #key_len.
66  */
67 void
68 xbt_dict_set_ext(xbt_dict_t      dict,
69                   const char      *key,
70                   int              key_len,
71                   void            *data,
72                   void_f_pvoid_t  *free_ctn) {
73
74   xbt_assert(dict);
75
76   xbt_dictelm_set_ext(&(dict->head),
77                        key, key_len, data, free_ctn);
78 }
79
80 /**
81  * \ingroup XBT_dict
82  *
83  * \param dict the head of the dict
84  * \param key the key to set the new data
85  * \param data the data to add in the dict
86  * \param free_ctn function to call with (#key as argument) when 
87  *        #key is removed from the dictionnary
88  *
89  * set the #data in the structure under the #key, which is a 
90  * null terminated string.
91  */
92 void
93 xbt_dict_set(xbt_dict_t     dict,
94               const char     *key,
95               void           *data,
96               void_f_pvoid_t *free_ctn) {
97
98   xbt_assert(dict);
99   
100   xbt_dictelm_set(&(dict->head), key, data, free_ctn);
101 }
102
103 /**
104  * \ingroup XBT_dict
105  *
106  * \param dict the dealer of data
107  * \param key the key to find data
108  * \param key_len the size of the #key
109  * \param data the data that we are looking for
110  * \return xbt_error
111  *
112  * Search the given #key. mismatch_error when not found.
113  */
114 xbt_error_t
115 xbt_dict_get_ext(xbt_dict_t     dict,
116                   const char     *key,
117                   int             key_len,
118                   /* OUT */void **data) {
119
120   xbt_assert(dict);
121
122   return xbt_dictelm_get_ext(dict->head, key, key_len, data);
123 }
124
125 /**
126  * \ingroup XBT_dict
127  *
128  * \param dict the dealer of data
129  * \param key the key to find data
130  * \param data the data that we are looking for
131  * \return xbt_error
132  *
133  * Search the given #key. mismatch_error when not found.
134  */
135 xbt_error_t
136 xbt_dict_get(xbt_dict_t     dict,
137               const char     *key,
138               /* OUT */void **data) {
139   xbt_assert(dict);
140
141   return xbt_dictelm_get(dict->head, key, data);
142 }
143
144
145 /**
146  * \ingroup XBT_dict
147  *
148  * \param dict the trash can
149  * \param key the key of the data to be removed
150  * \param key_len the size of the #key
151  * \return xbt_error_t
152  *
153  * Remove the entry associated with the given #key
154  */
155 xbt_error_t
156 xbt_dict_remove_ext(xbt_dict_t  dict,
157                      const char  *key,
158                      int          key_len) {
159   xbt_assert(dict);
160   
161   return xbt_dictelm_remove_ext(dict->head, key, key_len);
162 }
163
164 /**
165  * \ingroup XBT_dict
166  *
167  * \param dict the head of the dict
168  * \param key the key of the data to be removed
169  * \return xbt_error_t
170  *
171  * Remove the entry associated with the given #key
172  */
173 xbt_error_t
174 xbt_dict_remove(xbt_dict_t  dict,
175                  const char  *key) {
176   if (!dict) 
177      RAISE1(mismatch_error,"Asked to remove key %s from NULL dict",key);
178
179   return xbt_dictelm_remove(dict->head, key);
180 }
181
182
183 /**
184  * \ingroup XBT_dict
185  *
186  * \param dict the exibitionist
187  * \param output a function to dump each data in the tree
188  * \return xbt_error_t
189  *
190  * Ouputs the content of the structure. (for debuging purpose). #ouput is a
191  * function to output the data. If NULL, data won't be displayed.
192  */
193
194 void
195 xbt_dict_dump(xbt_dict_t     dict,
196                void_f_pvoid_t *output) {
197
198   printf("Dict %p:\n", (void*)dict);
199   xbt_dictelm_dump(dict ? dict->head: NULL, output);
200 }