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 XBT_LOG_EXTERNAL_CATEGORY(dict);
17 XBT_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 xbt_dict_cursor_ {
25 /* we store all level encountered until here, to backtrack on next() */
33 static _XBT_INLINE void
34 _cursor_push_keys(xbt_dict_cursor_t p_cursor,
37 #undef xbt_dict_CURSOR_DEBUG
38 /*#define xbt_dict_CURSOR_DEBUG 1*/
41 * xbt_dict_cursor_new:
43 * @head: the head of the dict
44 * @cursor: the curent position in the dict
49 xbt_dict_cursor_new(const xbt_dict_t head) {
50 xbt_dict_cursor_t res = NULL;
52 res = xbt_new(s_xbt_dict_cursor_t,1);
53 res->keys = xbt_dynar_new(sizeof(char **), NULL);
54 res->key_lens = xbt_dynar_new(sizeof(int *), NULL);
57 res->head = head ? head->head : NULL;
59 xbt_dict_cursor_rewind(res);
65 * xbt_dict_cursor_free:
67 * @cursor: poor victim
69 * Structure destructor
72 xbt_dict_cursor_free(xbt_dict_cursor_t *cursor) {
74 xbt_dynar_free(&((*cursor)->keys));
75 xbt_dynar_free(&((*cursor)->key_lens));
84 * Sanity check to see if the head contains something
88 __cursor_not_null(xbt_dict_cursor_t cursor) {
90 xbt_assert0(cursor, "Null cursor");
93 return mismatch_error;
102 _cursor_push_keys(xbt_dict_cursor_t cursor,
104 xbt_dictelm_t child = NULL;
106 static volatile int count = 0; /* ??? */
108 CDEBUG1(dict_cursor, "Push childs of %p in the cursor", (void*)elm);
111 xbt_dynar_push(cursor->keys, &elm->key );
112 xbt_dynar_push(cursor->key_lens, &elm->key_len);
116 xbt_dynar_foreach(elm->sub, i, child) {
118 _cursor_push_keys(cursor, child);
121 CDEBUG1(dict_cursor, "Count = %d", count);
125 * xbt_dict_cursor_rewind:
126 * @cursor: the cursor
127 * @Returns: xbt_error_t
129 * back to the first element
132 xbt_dict_cursor_rewind(xbt_dict_cursor_t cursor) {
134 CDEBUG0(dict_cursor, "xbt_dict_cursor_rewind");
137 xbt_dynar_reset(cursor->keys);
138 xbt_dynar_reset(cursor->key_lens);
143 _cursor_push_keys(cursor, cursor->head);
145 xbt_dynar_cursor_first(cursor->keys, &cursor->pos );
146 xbt_dynar_cursor_first(cursor->key_lens, &cursor->pos_len);
151 * xbt_dict_cursor_first:
152 * @dict: on what to let the cursor iterate
153 * @cursor: dest address
155 * Create the cursor if it does not exists. Rewind it in any case.
157 void xbt_dict_cursor_first (const xbt_dict_t dict,
158 xbt_dict_cursor_t *cursor){
161 DEBUG0("Create the cursor on first use");
162 *cursor=xbt_dict_cursor_new(dict);
165 xbt_dict_cursor_rewind(*cursor);
170 * xbt_dict_cursor_step:
171 * @cursor: the cursor
173 * Move to the next element.
176 xbt_dict_cursor_step(xbt_dict_cursor_t cursor) {
179 xbt_dynar_cursor_step(cursor->keys, &cursor->pos);
180 xbt_dynar_cursor_step(cursor->key_lens, &cursor->pos_len);
184 * xbt_dict_cursor_get_or_free:
185 * @cursor: the cursor
186 * @Returns: true if it's ok, false if there is no more data
191 xbt_dict_cursor_get_or_free(xbt_dict_cursor_t *cursor,
194 xbt_error_t errcode = no_error;
197 if (!cursor || !(*cursor))
200 if (xbt_dynar_length((*cursor)->keys) <= (*cursor)->pos) {
201 xbt_dict_cursor_free(cursor);
205 *key = xbt_dynar_get_as((*cursor)->keys, (*cursor)->pos, char*);
206 key_len = xbt_dynar_get_as((*cursor)->key_lens, (*cursor)->pos_len, int);
208 errcode = xbt_dictelm_get_ext((*cursor)->head, *key, key_len, data);
209 if (errcode == mismatch_error) {
210 xbt_dict_cursor_free(cursor);
214 xbt_assert1(errcode == no_error,
215 "Unexpected problem while retrieving the content of cursor. Got %s",
216 xbt_error_name(errcode));
222 * xbt_dict_cursor_get_key:
223 * @cursor: the cursor
224 * @key: the current element
225 * @Returns: xbt_error_t
230 xbt_dict_cursor_get_key(xbt_dict_cursor_t cursor,
232 xbt_error_t errcode = no_error;
234 TRY(__cursor_not_null(cursor));
236 *key = xbt_dynar_get_as(cursor->keys, cursor->pos - 1, char*);
242 * xbt_dict_cursor_get_data:
243 * @cursor: the cursor
248 xbt_dict_cursor_get_data(xbt_dict_cursor_t cursor,
249 /*OUT*/void **data) {
250 xbt_error_t errcode = no_error;
254 TRY(__cursor_not_null(cursor));
256 key = xbt_dynar_get_as(cursor->keys, cursor->pos-1, char *);
257 key_len = xbt_dynar_get_as(cursor->key_lens, cursor->pos_len-1, int);
259 TRY(xbt_dictelm_get_ext(cursor->head, key, key_len, data));