3 /* set - data container consisting in dict+dynar */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2004 the GRAS posse. */
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 "gras_private.h"
13 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(set,gros,"data container consisting in dict+dynar");
15 /*####[ Type definition ]####################################################*/
17 gras_dict_t *dict; /* data stored by name */
18 gras_dynar_t *dynar; /* data stored by ID */
21 /*####[ Memory ]############################################################*/
28 void gras_set_new (gras_set_t **dst) {
29 gras_set_t *res=gras_new(gras_set_t,1);
32 gras_dict_new (&(res->dict));
33 gras_dynar_new(&(res->dynar), sizeof(void*),NULL);
44 void gras_set_free(gras_set_t **set) {
46 gras_dict_free ( &( (*set)->dict ) );
47 gras_dynar_free( (*set)->dynar );
55 * @set: set to populate
56 * @elm: element to add.
57 * @free_ctn: How to add the data
59 * Add an element to a set.
61 * elm->name must be set;
62 * elm->name_len is used as is unless it's <= 0 (in which case it's recomputed);
63 * elm->ID is attributed automatically.
65 void gras_set_add (gras_set_t *set,
67 void_f_pvoid_t *free_func) {
70 gras_set_elm_t *found_in_dict;
72 if (elm->name_len <= 0) {
73 elm->name_len = strlen(elm->name);
76 errcode = gras_dict_get_ext (set->dict,
77 elm->name, elm->name_len,
78 (void**)&found_in_dict);
79 if (errcode == no_error) {
80 if (elm == found_in_dict) {
81 DEBUG2("Ignoring request to insert the same element twice (key %s ; id %d)",
85 elm->ID=found_in_dict->ID;
86 DEBUG2("Reinsertion of key %s (id %d)", elm->name, elm->ID);
87 gras_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
88 gras_dynar_set(set->dynar, elm->ID, &elm);
92 gras_assert_error(mismatch_error);
95 elm->ID = gras_dynar_length( set->dynar );
96 gras_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func);
97 gras_dynar_set(set->dynar, elm->ID, &elm);
98 DEBUG2("Insertion of key '%s' (id %d)", elm->name, elm->ID);
103 * gras_set_get_by_name:
105 * @name: Name of the searched cell
106 * @dst: where to put the found data into
108 * get a data stored in the cell by providing its name.
110 gras_error_t gras_set_get_by_name (gras_set_t *set,
112 /* OUT */gras_set_elm_t **dst) {
113 gras_error_t errcode;
114 errcode = gras_dict_get_ext(set->dict, name, strlen(name), (void**) dst);
115 DEBUG2("Lookup key %s: %s",name,gras_error_name(errcode));
119 * gras_set_get_by_name_ext:
121 * @name: Name of the searched cell
122 * @name_len: length of the name, when strlen cannot be trusted
123 * @dst: where to put the found data into
125 * get a data stored in the cell by providing its name (and the length
126 * of the name, when strlen cannot be trusted because you don't use a char*
127 * as name, you weird guy).
129 gras_error_t gras_set_get_by_name_ext(gras_set_t *set,
132 /* OUT */gras_set_elm_t **dst) {
134 return gras_dict_get_ext (set->dict, name, name_len, (void**)dst);
138 * gras_set_get_by_code:
140 * @id: what you're looking for
141 * @dst: where to put the found data into
143 * get a data stored in the cell by providing its id.
144 * @warning, if the ID does not exists, you're getting into trouble
146 gras_error_t gras_set_get_by_id (gras_set_t *set,
148 /* OUT */gras_set_elm_t **dst) {
150 /* Don't bother checking the bounds, the dynar does so */
152 gras_dynar_get(set->dynar,id,dst);
153 DEBUG3("Lookup type of id %d (of %lu): %s",
154 id, gras_dynar_length(set->dynar), (*dst)->name);
162 struct gras_set_cursor_ {
168 * gras_set_cursor_first:
169 * @set: on what to let the cursor iterate
170 * @cursor: dest address
172 * Create the cursor if it does not exists. Rewind it in any case.
174 void gras_set_cursor_first (gras_set_t *set,
175 gras_set_cursor_t **cursor) {
179 DEBUG0("Create the cursor on first use");
180 *cursor = gras_new(gras_set_cursor_t,1);
181 gras_assert0(*cursor,
182 "Malloc error during the creation of the cursor");
184 (*cursor)->set = set;
185 gras_dynar_cursor_first(set->dynar, &( (*cursor)->val) );
192 * gras_set_cursor_step:
193 * @cursor: the cursor
195 * Move to the next element.
197 void gras_set_cursor_step (gras_set_cursor_t *cursor) {
198 gras_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) );
202 * gras_set_cursor_get_or_free:
203 * @cursor: the cursor
204 * @Returns: true if it's ok, false if there is no more data
208 int gras_set_cursor_get_or_free (gras_set_cursor_t **curs,
209 gras_set_elm_t **elm) {
210 gras_set_cursor_t *cursor;
212 if (!curs || !(*curs))
217 if (! gras_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) {