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,GRAS);
22 /*####[ Private prototypes ]#################################################*/
25 /*####[ Code ]###############################################################*/
30 * @whereto: pointer to the destination
32 * Creates and initialize a new dictionnary
35 gras_dict_new(gras_dict_t **whereto) {
38 if (!(dict = calloc(1, sizeof(gras_dict_t))))
49 * @dict: the dictionnary to be freed
51 * Frees a cache structure with all its childs.
54 gras_dict_free(gras_dict_t **dict) {
57 gras_dictelm_free( &( (*dict)->head ) );
68 * @p_dict: the container
69 * @key: the key to set the new data
70 * @data: the data to add in the dict
71 * @Returns: a gras_error
73 * set 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.
77 gras_dict_set_ext(gras_dict_t *p_dict,
81 void_f_pvoid_t *free_ctn) {
85 return gras_dictelm_set_ext(&(p_dict->head),
86 key, key_len, data, free_ctn);
92 * @head: the head of the dict
93 * @key: the key to set the new data
94 * @data: the data to add in the dict
95 * @Returns: a gras_error
97 * set the @data in the structure under the @key, which is a
98 * null terminated string.
101 gras_dict_set(gras_dict_t *p_dict,
104 void_f_pvoid_t *free_ctn) {
108 return gras_dictelm_set(&(p_dict->head), key, data, free_ctn);
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
119 * Search the given @key. mismatch_error when not found.
122 gras_dict_get_ext(gras_dict_t *dict,
125 /* OUT */void **data) {
129 return gras_dictelm_get_ext(dict->head, key, key_len, data);
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
140 * Search the given @key. mismatch_error when not found.
143 gras_dict_get(gras_dict_t *dict,
145 /* OUT */void **data) {
148 return gras_dictelm_get(dict->head, key, data);
153 * gras_dict_remove_ext:
155 * @dict: the trash can
156 * @key: the key of the data to be removed
157 * @Returns: gras_error_t
159 * Remove the entry associated with the given @key
162 gras_dict_remove_ext(gras_dict_t *dict,
167 return gras_dictelm_remove_ext(dict->head, key, key_len);
173 * @head: the head of the dict
174 * @key: the key of the data to be removed
175 * @Returns: gras_error_t
177 * Remove the entry associated with the given @key
180 gras_dict_remove(gras_dict_t *dict,
183 RAISE1(mismatch_error,"Asked to remove key %s from NULL dict",key);
185 return gras_dictelm_remove(dict->head, key);
192 * @dict: the exibitionist
193 * @output: a function to dump each data in the tree
194 * @Returns: gras_error_t
196 * Ouputs the content of the structure. (for debuging purpose). @ouput is a
197 * function to output the data. If NULL, data won't be displayed.
201 gras_dict_dump(gras_dict_t *dict,
202 void_f_pvoid_t *output) {
204 printf("Dict %p:\n", dict);
205 return gras_dictelm_dump(dict ? dict->head: NULL, output);