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 gras_error_t gras_set_new (gras_set_t **dst) {
29 gras_set_t *res=gras_new(gras_set_t,1);
35 TRY(gras_dict_new (&(res->dict)));
36 TRY(gras_dynar_new(&(res->dynar), sizeof(void*),NULL));
48 void gras_set_free(gras_set_t **set) {
50 gras_dict_free ( &( (*set)->dict ) );
51 gras_dynar_free( (*set)->dynar );
59 * @set: set to populate
60 * @elm: element to add.
61 * @free_ctn: How to add the data
63 * Add an element to a set.
65 * elm->name must be set;
66 * elm->name_len is used as is unless it's <= 0 (in which case it's recomputed);
67 * elm->ID is attributed automatically.
69 gras_error_t gras_set_add (gras_set_t *set,
71 void_f_pvoid_t *free_func) {
74 gras_set_elm_t *found_in_dict;
76 if (elm->name_len <= 0) {
77 elm->name_len = strlen(elm->name);
80 errcode = gras_dict_get_ext (set->dict,
81 elm->name, elm->name_len,
82 (void**)&found_in_dict);
83 if (errcode == no_error) {
84 if (elm == found_in_dict) {
85 DEBUG2("Ignoring request to insert the same element twice (key %s ; id %d)",
89 elm->ID=found_in_dict->ID;
90 DEBUG2("Reinsertion of key %s (id %d)", elm->name, elm->ID);
91 TRY(gras_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func));
92 TRY(gras_dynar_set(set->dynar, elm->ID, &elm));
95 } else if (errcode != mismatch_error) {
96 return errcode; /* I expected mismatch_error */
99 elm->ID = gras_dynar_length( set->dynar );
100 TRY(gras_dict_set_ext(set->dict, elm->name, elm->name_len, elm, free_func));
101 TRY(gras_dynar_set(set->dynar, elm->ID, &elm));
102 DEBUG2("Insertion of key '%s' (id %d)", elm->name, elm->ID);
108 * gras_set_get_by_name:
110 * @name: Name of the searched cell
111 * @dst: where to put the found data into
113 * get a data stored in the cell by providing its name.
115 gras_error_t gras_set_get_by_name (gras_set_t *set,
117 /* OUT */gras_set_elm_t **dst) {
118 gras_error_t errcode;
119 errcode = gras_dict_get_ext(set->dict, name, strlen(name), (void**) dst);
120 DEBUG2("Lookup key %s: %s",name,gras_error_name(errcode));
124 * gras_set_get_by_name_ext:
126 * @name: Name of the searched cell
127 * @name_len: length of the name, when strlen cannot be trusted
128 * @dst: where to put the found data into
130 * get a data stored in the cell by providing its name (and the length
131 * of the name, when strlen cannot be trusted because you don't use a char*
132 * as name, you weird guy).
134 gras_error_t gras_set_get_by_name_ext(gras_set_t *set,
137 /* OUT */gras_set_elm_t **dst) {
139 return gras_dict_get_ext (set->dict, name, name_len, (void**)dst);
143 * gras_set_get_by_code:
145 * @id: what you're looking for
146 * @dst: where to put the found data into
148 * get a data stored in the cell by providing its id.
149 * @warning, if the ID does not exists, you're getting into trouble
151 gras_error_t gras_set_get_by_id (gras_set_t *set,
153 /* OUT */gras_set_elm_t **dst) {
155 /* Don't bother checking the bounds, the dynar does so */
157 gras_dynar_get(set->dynar,id,dst);
158 DEBUG3("Lookup type of id %d (of %lu): %s",
159 id, gras_dynar_length(set->dynar), (*dst)->name);
167 struct gras_set_cursor_ {
173 * gras_set_cursor_first:
174 * @set: on what to let the cursor iterate
175 * @cursor: dest address
177 * Create the cursor if it does not exists. Rewind it in any case.
179 void gras_set_cursor_first (gras_set_t *set,
180 gras_set_cursor_t **cursor) {
184 DEBUG0("Create the cursor on first use");
185 *cursor = gras_new(gras_set_cursor_t,1);
186 gras_assert0(*cursor,
187 "Malloc error during the creation of the cursor");
189 (*cursor)->set = set;
190 gras_dynar_cursor_first(set->dynar, &( (*cursor)->val) );
197 * gras_set_cursor_step:
198 * @cursor: the cursor
200 * Move to the next element.
202 void gras_set_cursor_step (gras_set_cursor_t *cursor) {
203 gras_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) );
207 * gras_set_cursor_get_or_free:
208 * @cursor: the cursor
209 * @Returns: true if it's ok, false if there is no more data
213 int gras_set_cursor_get_or_free (gras_set_cursor_t **curs,
214 gras_set_elm_t **elm) {
215 gras_set_cursor_t *cursor;
217 if (!curs || !(*curs))
222 if (! gras_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) {