3 /* dict - a generic dictionnary, variation over the B-tree concept */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2003 the OURAGAN project. */
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. */
11 #include "dict_private.h"
13 #include <stdlib.h> /* malloc() */
14 #include <string.h> /* strlen() */
18 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(dict,xbt,
19 "Dictionaries provide the same functionnalities than hash tables");
21 /*####[ Private prototypes ]#################################################*/
24 /*####[ Code ]###############################################################*/
29 * @whereto: pointer to the destination
31 * Creates and initialize a new dictionnary
35 gras_dict_t res= gras_new(s_gras_dict_t,1);
41 * @dict: the dictionnary to be freed
43 * Frees a cache structure with all its childs.
46 gras_dict_free(gras_dict_t *dict) {
49 gras_dictelm_free( &( (*dict)->head ) );
60 * @p_dict: the container
61 * @key: the key to set the new data
62 * @data: the data to add in the dict
63 * @Returns: a gras_error
65 * set the @data in the structure under the @key, which can be any kind
66 * of data, as long as its length is provided in @key_len.
69 gras_dict_set_ext(gras_dict_t dict,
73 void_f_pvoid_t *free_ctn) {
77 gras_dictelm_set_ext(&(dict->head),
78 key, key_len, data, free_ctn);
84 * @head: the head of the dict
85 * @key: the key to set the new data
86 * @data: the data to add in the dict
87 * @Returns: a gras_error
89 * set the @data in the structure under the @key, which is a
90 * null terminated string.
93 gras_dict_set(gras_dict_t dict,
96 void_f_pvoid_t *free_ctn) {
100 gras_dictelm_set(&(dict->head), key, data, free_ctn);
106 * @dict: the dealer of data
107 * @key: the key to find data
108 * @data: the data that we are looking for
109 * @Returns: gras_error
111 * Search the given @key. mismatch_error when not found.
114 gras_dict_get_ext(gras_dict_t dict,
117 /* OUT */void **data) {
121 return gras_dictelm_get_ext(dict->head, key, key_len, data);
127 * @dict: the dealer of data
128 * @key: the key to find data
129 * @data: the data that we are looking for
130 * @Returns: gras_error
132 * Search the given @key. mismatch_error when not found.
135 gras_dict_get(gras_dict_t dict,
137 /* OUT */void **data) {
140 return gras_dictelm_get(dict->head, key, data);
145 * gras_dict_remove_ext:
147 * @dict: the trash can
148 * @key: the key of the data to be removed
149 * @Returns: gras_error_t
151 * Remove the entry associated with the given @key
154 gras_dict_remove_ext(gras_dict_t dict,
159 return gras_dictelm_remove_ext(dict->head, key, key_len);
165 * @head: the head of the dict
166 * @key: the key of the data to be removed
167 * @Returns: gras_error_t
169 * Remove the entry associated with the given @key
172 gras_dict_remove(gras_dict_t dict,
175 RAISE1(mismatch_error,"Asked to remove key %s from NULL dict",key);
177 return gras_dictelm_remove(dict->head, key);
184 * @dict: the exibitionist
185 * @output: a function to dump each data in the tree
186 * @Returns: gras_error_t
188 * Ouputs the content of the structure. (for debuging purpose). @ouput is a
189 * function to output the data. If NULL, data won't be displayed.
193 gras_dict_dump(gras_dict_t dict,
194 void_f_pvoid_t *output) {
196 printf("Dict %p:\n", (void*)dict);
197 gras_dictelm_dump(dict ? dict->head: NULL, output);