3 /* dict_cursor - iterators over dictionnaries */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2003,2004 Martin Quinson. */
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 "dict_private.h"
14 #include <string.h> /* strlen() */
16 GRAS_LOG_EXTERNAL_CATEGORY(dict);
17 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(dict_cursor,dict,"To traverse dictionaries");
20 /*####[ Dict cursor functions ]#############################################*/
21 /* To traverse (simple) dicts */
22 /* Don't add or remove entries to the dict while traversing !!! */
23 /*###########################################################################*/
24 struct gras_dict_cursor_ {
25 /* we store all level encountered until here, to backtrack on next() */
27 gras_dynar_t key_lens;
33 static _GRAS_INLINE void
34 _cursor_push_keys(gras_dict_cursor_t p_cursor,
35 gras_dictelm_t p_elm);
37 #undef gras_dict_CURSOR_DEBUG
38 /*#define gras_dict_CURSOR_DEBUG 1*/
41 * gras_dict_cursor_new:
43 * @head: the head of the dict
44 * @cursor: the curent position in the dict
49 gras_dict_cursor_new(const gras_dict_t head) {
50 gras_error_t errcode = no_error;
51 gras_dict_cursor_t res = NULL;
53 res = gras_new(s_gras_dict_cursor_t,1);
54 res->keys = gras_dynar_new(sizeof(char **), NULL);
55 res->key_lens = gras_dynar_new(sizeof(int *), NULL);
58 res->head = head ? head->head : NULL;
60 gras_dict_cursor_rewind(res);
66 * gras_dict_cursor_free:
68 * @cursor: poor victim
70 * Structure destructor
73 gras_dict_cursor_free(gras_dict_cursor_t *cursor) {
75 gras_dynar_free(&((*cursor)->keys));
76 gras_dynar_free(&((*cursor)->key_lens));
85 * Sanity check to see if the head contains something
89 __cursor_not_null(gras_dict_cursor_t cursor) {
91 gras_assert0(cursor, "Null cursor");
94 return mismatch_error;
103 _cursor_push_keys(gras_dict_cursor_t cursor,
104 gras_dictelm_t elm) {
105 gras_error_t errcode = no_error;
106 gras_dictelm_t child = NULL;
108 static volatile int count = 0; /* ??? */
110 CDEBUG1(dict_cursor, "Push childs of %p in the cursor", (void*)elm);
113 gras_dynar_push(cursor->keys, &elm->key );
114 gras_dynar_push(cursor->key_lens, &elm->key_len);
118 gras_dynar_foreach(elm->sub, i, child) {
120 _cursor_push_keys(cursor, child);
123 CDEBUG1(dict_cursor, "Count = %d", count);
127 * gras_dict_cursor_rewind:
128 * @cursor: the cursor
129 * @Returns: gras_error_t
131 * back to the first element
134 gras_dict_cursor_rewind(gras_dict_cursor_t cursor) {
135 gras_error_t errcode = no_error;
137 CDEBUG0(dict_cursor, "gras_dict_cursor_rewind");
140 gras_dynar_reset(cursor->keys);
141 gras_dynar_reset(cursor->key_lens);
146 _cursor_push_keys(cursor, cursor->head);
148 gras_dynar_cursor_first(cursor->keys, &cursor->pos );
149 gras_dynar_cursor_first(cursor->key_lens, &cursor->pos_len);
154 * gras_dict_cursor_first:
155 * @dict: on what to let the cursor iterate
156 * @cursor: dest address
158 * Create the cursor if it does not exists. Rewind it in any case.
160 void gras_dict_cursor_first (const gras_dict_t dict,
161 gras_dict_cursor_t *cursor){
164 DEBUG0("Create the cursor on first use");
165 *cursor=gras_dict_cursor_new(dict);
168 gras_dict_cursor_rewind(*cursor);
173 * gras_dict_cursor_step:
174 * @cursor: the cursor
176 * Move to the next element.
179 gras_dict_cursor_step(gras_dict_cursor_t cursor) {
182 gras_dynar_cursor_step(cursor->keys, &cursor->pos);
183 gras_dynar_cursor_step(cursor->key_lens, &cursor->pos_len);
187 * gras_dict_cursor_get_or_free:
188 * @cursor: the cursor
189 * @Returns: true if it's ok, false if there is no more data
194 gras_dict_cursor_get_or_free(gras_dict_cursor_t *cursor,
197 gras_error_t errcode = no_error;
200 if (!cursor || !(*cursor))
203 if (gras_dynar_length((*cursor)->keys) <= (*cursor)->pos) {
204 gras_dict_cursor_free(cursor);
208 *key = gras_dynar_get_as((*cursor)->keys, (*cursor)->pos, char*);
209 key_len = gras_dynar_get_as((*cursor)->key_lens, (*cursor)->pos_len, int);
211 errcode = gras_dictelm_get_ext((*cursor)->head, *key, key_len, data);
212 if (errcode == mismatch_error) {
213 gras_dict_cursor_free(cursor);
217 gras_assert1(errcode == no_error,
218 "Unexpected problem while retrieving the content of cursor. Got %s",
219 gras_error_name(errcode));
225 * gras_dict_cursor_get_key:
226 * @cursor: the cursor
227 * @key: the current element
228 * @Returns: gras_error_t
233 gras_dict_cursor_get_key(gras_dict_cursor_t cursor,
235 gras_error_t errcode = no_error;
237 TRY(__cursor_not_null(cursor));
239 *key = gras_dynar_get_as(cursor->keys, cursor->pos - 1, char*);
245 * gras_dict_cursor_get_data:
246 * @cursor: the cursor
251 gras_dict_cursor_get_data(gras_dict_cursor_t cursor,
252 /*OUT*/void **data) {
253 gras_error_t errcode = no_error;
257 TRY(__cursor_not_null(cursor));
259 key = gras_dynar_get_as(cursor->keys, cursor->pos-1, char *);
260 key_len = gras_dynar_get_as(cursor->key_lens, cursor->pos_len-1, int);
262 TRY(gras_dictelm_get_ext(cursor->head, key, key_len, data));