Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Interface revolution: do not try to survive to malloc failure
[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,gros,
21    "Dictionaries provide the same functionnalities than hash tables");
22
23 /*####[ Private prototypes ]#################################################*/
24
25
26 /*####[ Code ]###############################################################*/
27
28 /**
29  * gras_dict_new:
30  *
31  * @whereto: pointer to the destination
32  *
33  * Creates and initialize a new dictionnary
34  */
35 void
36 gras_dict_new(gras_dict_t **whereto) {
37
38   (*whereto) = gras_new(gras_dict_t,1);
39   (*whereto)->head=NULL;
40
41 }
42 /**
43  * gras_dict_free:
44  * @dict: the dictionnary to be freed
45  *
46  * Frees a cache structure with all its childs.
47  */
48 void
49 gras_dict_free(gras_dict_t **dict)  {
50   if (dict && *dict) {
51     if ((*dict)->head) {
52       gras_dictelm_free( &( (*dict)->head ) );
53       (*dict)->head = NULL;
54     }
55     free(*dict);
56     *dict=NULL;
57   }
58 }
59
60 /**
61  * gras_dict_set_ext:
62  *
63  * @p_dict: the container
64  * @key: the key to set the new data
65  * @data: the data to add in the dict
66  * @Returns: a gras_error
67  *
68  * set the @data in the structure under the @key, which can be any kind 
69  * of data, as long as its length is provided in @key_len.
70  */
71 void
72 gras_dict_set_ext(gras_dict_t     *p_dict,
73                   const char      *key,
74                   int              key_len,
75                   void            *data,
76                   void_f_pvoid_t  *free_ctn) {
77
78   gras_assert(p_dict);
79
80   gras_dictelm_set_ext(&(p_dict->head),
81                        key, key_len, data, free_ctn);
82 }
83
84 /**
85  * gras_dict_set:
86  *
87  * @head: the head of the dict
88  * @key: the key to set the new data
89  * @data: the data to add in the dict
90  * @Returns: a gras_error
91  *
92  * set the @data in the structure under the @key, which is a 
93  * null terminated string.
94  */
95 void
96 gras_dict_set(gras_dict_t    *p_dict,
97                  const char     *key,
98                  void           *data,
99                  void_f_pvoid_t *free_ctn) {
100
101   gras_assert(p_dict);
102   
103   gras_dictelm_set(&(p_dict->head), key, data, free_ctn);
104 }
105
106 /**
107  * gras_dict_get_ext:
108  *
109  * @dict: the dealer of data
110  * @key: the key to find data
111  * @data: the data that we are looking for
112  * @Returns: gras_error
113  *
114  * Search the given @key. mismatch_error when not found.
115  */
116 gras_error_t
117 gras_dict_get_ext(gras_dict_t    *dict,
118                        const char     *key,
119                        int             key_len,
120                        /* OUT */void **data) {
121
122   gras_assert(dict);
123
124   return gras_dictelm_get_ext(dict->head, key, key_len, data);
125 }
126
127 /**
128  * gras_dict_get:
129  *
130  * @dict: the dealer of data
131  * @key: the key to find data
132  * @data: the data that we are looking for
133  * @Returns: gras_error
134  *
135  * Search the given @key. mismatch_error when not found.
136  */
137 gras_error_t
138 gras_dict_get(gras_dict_t    *dict,
139                    const char     *key,
140                    /* OUT */void **data) {
141   gras_assert(dict);
142
143   return gras_dictelm_get(dict->head, key, data);
144 }
145
146
147 /**
148  * gras_dict_remove_ext:
149  *
150  * @dict: the trash can
151  * @key: the key of the data to be removed
152  * @Returns: gras_error_t
153  *
154  * Remove the entry associated with the given @key
155  */
156 gras_error_t
157 gras_dict_remove_ext(gras_dict_t *dict,
158                      const char  *key,
159                      int          key_len) {
160   gras_assert(dict);
161   
162   return gras_dictelm_remove_ext(dict->head, key, key_len);
163 }
164
165 /**
166  * gras_dict_remove:
167  *
168  * @head: the head of the dict
169  * @key: the key of the data to be removed
170  * @Returns: gras_error_t
171  *
172  * Remove the entry associated with the given @key
173  */
174 gras_error_t
175 gras_dict_remove(gras_dict_t *dict,
176                  const char  *key) {
177   if (!dict) 
178      RAISE1(mismatch_error,"Asked to remove key %s from NULL dict",key);
179
180   return gras_dictelm_remove(dict->head, key);
181 }
182
183
184 /**
185  * gras_dict_dump:
186  *
187  * @dict: the exibitionist
188  * @output: a function to dump each data in the tree
189  * @Returns: gras_error_t
190  *
191  * Ouputs the content of the structure. (for debuging purpose). @ouput is a
192  * function to output the data. If NULL, data won't be displayed.
193  */
194
195 void
196 gras_dict_dump(gras_dict_t    *dict,
197                void_f_pvoid_t *output) {
198
199   printf("Dict %p:\n", (void*)dict);
200   gras_dictelm_dump(dict ? dict->head: NULL, output);
201 }
202