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. */
12 #include "gras_private.h"
13 #include "dict_private.h"
15 #include <stdlib.h> /* malloc() */
16 #include <string.h> /* strlen() */
20 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(dict,gros,
21 "Dictionaries provide the same functionnalities than hash tables");
23 /*####[ Private prototypes ]#################################################*/
26 /*####[ Code ]###############################################################*/
31 * @whereto: pointer to the destination
33 * Creates and initialize a new dictionnary
36 gras_dict_new(gras_dict_t **whereto) {
39 if (!(dict = calloc(1, sizeof(gras_dict_t))))
50 * @dict: the dictionnary to be freed
52 * Frees a cache structure with all its childs.
55 gras_dict_free(gras_dict_t **dict) {
58 gras_dictelm_free( &( (*dict)->head ) );
69 * @p_dict: the container
70 * @key: the key to set the new data
71 * @data: the data to add in the dict
72 * @Returns: a gras_error
74 * set the @data in the structure under the @key, which can be any kind
75 * of data, as long as its length is provided in @key_len.
78 gras_dict_set_ext(gras_dict_t *p_dict,
82 void_f_pvoid_t *free_ctn) {
86 return gras_dictelm_set_ext(&(p_dict->head),
87 key, key_len, data, free_ctn);
93 * @head: the head of the dict
94 * @key: the key to set the new data
95 * @data: the data to add in the dict
96 * @Returns: a gras_error
98 * set the @data in the structure under the @key, which is a
99 * null terminated string.
102 gras_dict_set(gras_dict_t *p_dict,
105 void_f_pvoid_t *free_ctn) {
109 return gras_dictelm_set(&(p_dict->head), key, data, free_ctn);
115 * @dict: the dealer of data
116 * @key: the key to find data
117 * @data: the data that we are looking for
118 * @Returns: gras_error
120 * Search the given @key. mismatch_error when not found.
123 gras_dict_get_ext(gras_dict_t *dict,
126 /* OUT */void **data) {
130 return gras_dictelm_get_ext(dict->head, key, key_len, data);
136 * @dict: the dealer of data
137 * @key: the key to find data
138 * @data: the data that we are looking for
139 * @Returns: gras_error
141 * Search the given @key. mismatch_error when not found.
144 gras_dict_get(gras_dict_t *dict,
146 /* OUT */void **data) {
149 return gras_dictelm_get(dict->head, key, data);
154 * gras_dict_remove_ext:
156 * @dict: the trash can
157 * @key: the key of the data to be removed
158 * @Returns: gras_error_t
160 * Remove the entry associated with the given @key
163 gras_dict_remove_ext(gras_dict_t *dict,
168 return gras_dictelm_remove_ext(dict->head, key, key_len);
174 * @head: the head of the dict
175 * @key: the key of the data to be removed
176 * @Returns: gras_error_t
178 * Remove the entry associated with the given @key
181 gras_dict_remove(gras_dict_t *dict,
184 RAISE1(mismatch_error,"Asked to remove key %s from NULL dict",key);
186 return gras_dictelm_remove(dict->head, key);
193 * @dict: the exibitionist
194 * @output: a function to dump each data in the tree
195 * @Returns: gras_error_t
197 * Ouputs the content of the structure. (for debuging purpose). @ouput is a
198 * function to output the data. If NULL, data won't be displayed.
202 gras_dict_dump(gras_dict_t *dict,
203 void_f_pvoid_t *output) {
205 printf("Dict %p:\n", dict);
206 return gras_dictelm_dump(dict ? dict->head: NULL, output);