Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5ff053727b92e1aa35fb2062186425e0e0a5d721
[simgrid.git] / src / xbt / dict.c
1 /* $Id$ */
2
3 /* dict - a generic dictionnary, variation over the B-tree concept          */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2003 the OURAGAN project.                                  */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11
12 #include "gras_private.h"
13 #include "dict_private.h"
14
15 #include <stdlib.h> /* malloc() */
16 #include <string.h> /* strlen() */
17
18 #include <stdio.h>
19
20 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(dict,GRAS);
21
22 /*####[ Private prototypes ]#################################################*/
23
24
25 /*####[ Code ]###############################################################*/
26
27 /**
28  * gras_dict_new:
29  *
30  * @whereto: pointer to the destination
31  *
32  * Creates and initialize a new dictionnary
33  */
34 gras_error_t
35 gras_dict_new(gras_dict_t **whereto) {
36   gras_dict_t *dict;
37
38   if (!(dict = calloc(1, sizeof(gras_dict_t))))
39     RAISE_MALLOC;
40
41   dict->head=NULL;
42
43   *whereto = dict;
44   
45   return no_error;
46 }
47 /**
48  * gras_dict_free:
49  * @dict: the dictionnary to be freed
50  *
51  * Frees a cache structure with all its childs.
52  */
53 void
54 gras_dict_free(gras_dict_t **dict)  {
55   if (dict && *dict) {
56     if ((*dict)->head) {
57       gras_dictelm_free( &( (*dict)->head ) );
58       (*dict)->head = NULL;
59     }
60     free(*dict);
61     *dict=NULL;
62   }
63 }
64
65 /**
66  * gras_dict_insert_ext:
67  *
68  * @p_dict: the container
69  * @key: the key to insert the new data
70  * @data: the data to add in the dict
71  * @Returns: a gras_error
72  *
73  * Insert the @data in the structure under the @key, which can be any kind 
74  * of data, as long as its length is provided in @key_len.
75  */
76 gras_error_t
77 gras_dict_insert_ext(gras_dict_t     *p_dict,
78                      const char      *key,
79                      int              key_len,
80                      void            *data,
81                      void_f_pvoid_t  *free_ctn) {
82
83   gras_assert(p_dict);
84
85   return gras_dictelm_insert_ext(&(p_dict->head),
86                                  key, key_len, data, free_ctn);
87 }
88
89 /**
90  * gras_dict_insert:
91  *
92  * @head: the head of the dict
93  * @key: the key to insert the new data
94  * @data: the data to add in the dict
95  * @Returns: a gras_error
96  *
97  * Insert the @data in the structure under the @key, which is a 
98  * null terminated string.
99  */
100 gras_error_t
101 gras_dict_insert(gras_dict_t    *p_dict,
102                  const char     *key,
103                  void           *data,
104                  void_f_pvoid_t *free_ctn) {
105
106   gras_assert(p_dict);
107   
108   return gras_dictelm_insert(&(p_dict->head), key, data, free_ctn);
109 }
110
111 /**
112  * gras_dict_retrieve_ext:
113  *
114  * @dict: the dealer of data
115  * @key: the key to find data
116  * @data: the data that we are looking for
117  * @Returns: gras_error
118  *
119  * Search the given @key. mismatch_error when not found.
120  */
121 gras_error_t
122 gras_dict_retrieve_ext(gras_dict_t    *dict,
123                        const char     *key,
124                        int             key_len,
125                        /* OUT */void **data) {
126
127   gras_assert(dict);
128
129   return gras_dictelm_retrieve_ext(dict->head, key, key_len, data);
130 }
131
132 /**
133  * gras_dict_retrieve:
134  *
135  * @dict: the dealer of data
136  * @key: the key to find data
137  * @data: the data that we are looking for
138  * @Returns: gras_error
139  *
140  * Search the given @key. mismatch_error when not found.
141  */
142 gras_error_t
143 gras_dict_retrieve(gras_dict_t    *dict,
144                    const char     *key,
145                    /* OUT */void **data) {
146   gras_assert(dict);
147
148   return gras_dictelm_retrieve(dict->head, key, data);
149 }
150
151
152 /**
153  * gras_dict_remove_ext:
154  *
155  * @dict: the trash can
156  * @key: the key of the data to be removed
157  * @Returns: gras_error_t
158  *
159  * Remove the entry associated with the given @key
160  */
161 gras_error_t
162 gras_dict_remove_ext(gras_dict_t *dict,
163                      const char  *key,
164                      int          key_len) {
165   gras_assert(dict);
166   
167   return gras_dictelm_remove_ext(dict->head, key, key_len);
168 }
169
170 /**
171  * gras_dict_remove:
172  *
173  * @head: the head of the dict
174  * @key: the key of the data to be removed
175  * @Returns: gras_error_t
176  *
177  * Remove the entry associated with the given @key
178  */
179 gras_error_t
180 gras_dict_remove(gras_dict_t *dict,
181                  const char  *key) {
182   gras_assert(dict);
183
184   return gras_dictelm_remove(dict->head, key);
185 }
186
187
188 /**
189  * gras_dict_dump:
190  *
191  * @dict: the exibitionist
192  * @output: a function to dump each data in the tree
193  * @Returns: gras_error_t
194  *
195  * Ouputs the content of the structure. (for debuging purpose). @ouput is a
196  * function to output the data. If NULL, data won't be displayed.
197  */
198
199 gras_error_t
200 gras_dict_dump(gras_dict_t    *dict,
201                void_f_pvoid_t *output) {
202
203   printf("Dict %p:\n", dict);
204   return gras_dictelm_dump(dict ? dict->head: NULL, output);
205 }
206