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,GRAS);
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_set_t*)malloc(sizeof(gras_set_t));
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_retrieve_ext (set->dict,
81 elm->name, elm->name_len,
82 (void**) &found_in_dict);
83 if (errcode == no_error) {
84 elm->ID=found_in_dict->ID;
85 DEBUG2("Reinsertion of key %s (id %d)", elm->name, elm->ID);
86 TRY(gras_dict_insert_ext(set->dict, elm->name, elm->name_len, elm, free_func));
87 TRY(gras_dynar_set(set->dynar, elm->ID, &elm));
90 } else if (errcode != mismatch_error) {
91 return errcode; /* I expected mismatch_error */
94 elm->ID = gras_dynar_length( set->dynar );
95 TRY(gras_dict_insert_ext(set->dict, elm->name, elm->name_len, elm, free_func));
96 TRY(gras_dynar_set(set->dynar, elm->ID, &elm));
97 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 * Retrieve 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) {
114 return gras_dict_retrieve_ext(set->dict, name, strlen(name), (void**) dst);
117 * gras_set_get_by_name_ext:
119 * @name: Name of the searched cell
120 * @name_len: length of the name, when strlen cannot be trusted
121 * @dst: where to put the found data into
123 * Retrieve a data stored in the cell by providing its name (and the length
124 * of the name, when strlen cannot be trusted because you don't use a char*
125 * as name, you weird guy).
127 gras_error_t gras_set_get_by_name_ext(gras_set_t *set,
130 /* OUT */gras_set_elm_t **dst) {
132 return gras_dict_retrieve_ext (set->dict, name, name_len, (void**)dst);
136 * gras_set_get_by_code:
138 * @name: Name of the searched cell
139 * @name_len: length of the name, when strlen cannot be trusted
140 * @dst: where to put the found data into
142 * Retrieve a data stored in the cell by providing its name (and the length
143 * of the name, when strlen cannot be trusted because you don't use a char*
144 * as name, you weird guy).
146 gras_error_t gras_set_get_by_id (gras_set_t *set,
148 /* OUT */gras_set_elm_t **dst) {
149 if (id < gras_dynar_length(set->dynar) &&
151 gras_dynar_get(set->dynar,id,dst);
154 DEBUG1("Cannot get ID %d: out of bound", id);
155 return mismatch_error;
163 struct gras_set_cursor_ {
169 * gras_set_cursor_first:
170 * @set: on what to let the cursor iterate
171 * @cursor: dest address
173 * Create the cursor if it does not exists. Rewind it in any case.
175 void gras_set_cursor_first (gras_set_t *set,
176 gras_set_cursor_t **cursor) {
180 DEBUG0("Create the cursor on first use");
181 *cursor = (gras_set_cursor_t*)malloc(sizeof(gras_set_cursor_t));
182 gras_assert0(*cursor,
183 "Malloc error during the creation of the cursor");
185 (*cursor)->set = set;
186 gras_dynar_cursor_first(set->dynar, &( (*cursor)->val) );
193 * gras_set_cursor_step:
194 * @cursor: the cursor
196 * Move to the next element.
198 void gras_set_cursor_step (gras_set_cursor_t *cursor) {
199 gras_dynar_cursor_step(cursor->set->dynar, &( cursor->val ) );
203 * gras_set_cursor_get_or_free:
204 * @cursor: the cursor
205 * @Returns: true if it's ok, false if there is no more data
209 int gras_set_cursor_get_or_free (gras_set_cursor_t **curs,
210 gras_set_elm_t **elm) {
211 gras_set_cursor_t *cursor;
213 if (!curs || !(*curs))
218 if (! gras_dynar_cursor_get( cursor->set->dynar,&(cursor->val),elm) ) {