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) {
38 (*whereto) = gras_new(gras_dict_t,1);
39 (*whereto)->head=NULL;
44 * @dict: the dictionnary to be freed
46 * Frees a cache structure with all its childs.
49 gras_dict_free(gras_dict_t **dict) {
52 gras_dictelm_free( &( (*dict)->head ) );
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
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.
72 gras_dict_set_ext(gras_dict_t *p_dict,
76 void_f_pvoid_t *free_ctn) {
80 gras_dictelm_set_ext(&(p_dict->head),
81 key, key_len, data, free_ctn);
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
92 * set the @data in the structure under the @key, which is a
93 * null terminated string.
96 gras_dict_set(gras_dict_t *p_dict,
99 void_f_pvoid_t *free_ctn) {
103 gras_dictelm_set(&(p_dict->head), key, data, free_ctn);
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
114 * Search the given @key. mismatch_error when not found.
117 gras_dict_get_ext(gras_dict_t *dict,
120 /* OUT */void **data) {
124 return gras_dictelm_get_ext(dict->head, key, key_len, data);
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
135 * Search the given @key. mismatch_error when not found.
138 gras_dict_get(gras_dict_t *dict,
140 /* OUT */void **data) {
143 return gras_dictelm_get(dict->head, key, data);
148 * gras_dict_remove_ext:
150 * @dict: the trash can
151 * @key: the key of the data to be removed
152 * @Returns: gras_error_t
154 * Remove the entry associated with the given @key
157 gras_dict_remove_ext(gras_dict_t *dict,
162 return gras_dictelm_remove_ext(dict->head, key, key_len);
168 * @head: the head of the dict
169 * @key: the key of the data to be removed
170 * @Returns: gras_error_t
172 * Remove the entry associated with the given @key
175 gras_dict_remove(gras_dict_t *dict,
178 RAISE1(mismatch_error,"Asked to remove key %s from NULL dict",key);
180 return gras_dictelm_remove(dict->head, key);
187 * @dict: the exibitionist
188 * @output: a function to dump each data in the tree
189 * @Returns: gras_error_t
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.
196 gras_dict_dump(gras_dict_t *dict,
197 void_f_pvoid_t *output) {
199 printf("Dict %p:\n", (void*)dict);
200 gras_dictelm_dump(dict ? dict->head: NULL, output);