3 /* dict_cursor - iterators over dictionnaries */
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "dict_private.h"
13 #include <string.h> /* strlen() */
15 XBT_LOG_EXTERNAL_CATEGORY(dict);
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dict_cursor,dict,"To traverse dictionaries");
19 /*####[ Dict cursor functions ]#############################################*/
20 /* To traverse (simple) dicts */
21 /* Don't add or remove entries to the dict while traversing !!! */
22 /*###########################################################################*/
23 struct xbt_dict_cursor_ {
24 /* we store all level encountered until here, to backtrack on next() */
32 static _XBT_INLINE void
33 _cursor_push_keys(xbt_dict_cursor_t p_cursor,
36 #undef xbt_dict_CURSOR_DEBUG
37 /*#define xbt_dict_CURSOR_DEBUG 1*/
40 * xbt_dict_cursor_new:
42 * @head: the head of the dict
43 * @cursor: the curent position in the dict
48 xbt_dict_cursor_new(const xbt_dict_t head) {
49 xbt_dict_cursor_t res = NULL;
51 res = xbt_new(s_xbt_dict_cursor_t,1);
52 res->keys = xbt_dynar_new(sizeof(char **), NULL);
53 res->key_lens = xbt_dynar_new(sizeof(int *), NULL);
56 res->head = head ? head->head : NULL;
58 xbt_dict_cursor_rewind(res);
64 * xbt_dict_cursor_free:
66 * @cursor: poor victim
68 * Structure destructor
71 xbt_dict_cursor_free(xbt_dict_cursor_t *cursor) {
73 xbt_dynar_free(&((*cursor)->keys));
74 xbt_dynar_free(&((*cursor)->key_lens));
83 * Sanity check to see if the head contains something
87 __cursor_not_null(xbt_dict_cursor_t cursor) {
89 xbt_assert0(cursor, "Null cursor");
92 return mismatch_error;
101 _cursor_push_keys(xbt_dict_cursor_t cursor,
103 xbt_dictelm_t child = NULL;
105 static volatile int count = 0; /* ??? */
107 CDEBUG1(dict_cursor, "Push childs of %p in the cursor", (void*)elm);
110 xbt_dynar_push(cursor->keys, &elm->key );
111 xbt_dynar_push(cursor->key_lens, &elm->key_len);
115 xbt_dynar_foreach(elm->sub, i, child) {
117 _cursor_push_keys(cursor, child);
120 CDEBUG1(dict_cursor, "Count = %d", count);
124 * xbt_dict_cursor_rewind:
125 * @cursor: the cursor
126 * @Returns: xbt_error_t
128 * back to the first element
131 xbt_dict_cursor_rewind(xbt_dict_cursor_t cursor) {
133 CDEBUG0(dict_cursor, "xbt_dict_cursor_rewind");
136 xbt_dynar_reset(cursor->keys);
137 xbt_dynar_reset(cursor->key_lens);
142 _cursor_push_keys(cursor, cursor->head);
144 xbt_dynar_cursor_first(cursor->keys, &cursor->pos );
145 xbt_dynar_cursor_first(cursor->key_lens, &cursor->pos_len);
150 * xbt_dict_cursor_first:
151 * @dict: on what to let the cursor iterate
152 * @cursor: dest address
154 * Create the cursor if it does not exists. Rewind it in any case.
156 void xbt_dict_cursor_first (const xbt_dict_t dict,
157 xbt_dict_cursor_t *cursor){
160 DEBUG0("Create the cursor on first use");
161 *cursor=xbt_dict_cursor_new(dict);
164 xbt_dict_cursor_rewind(*cursor);
169 * xbt_dict_cursor_step:
170 * @cursor: the cursor
172 * Move to the next element.
175 xbt_dict_cursor_step(xbt_dict_cursor_t cursor) {
178 xbt_dynar_cursor_step(cursor->keys, &cursor->pos);
179 xbt_dynar_cursor_step(cursor->key_lens, &cursor->pos_len);
183 * xbt_dict_cursor_get_or_free:
184 * @cursor: the cursor
185 * @Returns: true if it's ok, false if there is no more data
190 xbt_dict_cursor_get_or_free(xbt_dict_cursor_t *cursor,
193 xbt_error_t errcode = no_error;
196 if (!cursor || !(*cursor))
199 if (xbt_dynar_length((*cursor)->keys) <= (*cursor)->pos) {
200 xbt_dict_cursor_free(cursor);
204 *key = xbt_dynar_get_as((*cursor)->keys, (*cursor)->pos, char*);
205 key_len = xbt_dynar_get_as((*cursor)->key_lens, (*cursor)->pos_len, int);
207 errcode = xbt_dictelm_get_ext((*cursor)->head, *key, key_len, data);
208 if (errcode == mismatch_error) {
209 xbt_dict_cursor_free(cursor);
213 xbt_assert1(errcode == no_error,
214 "Unexpected problem while retrieving the content of cursor. Got %s",
215 xbt_error_name(errcode));
221 * xbt_dict_cursor_get_key:
222 * @cursor: the cursor
223 * @key: the current element
224 * @Returns: xbt_error_t
229 xbt_dict_cursor_get_key(xbt_dict_cursor_t cursor,
231 xbt_error_t errcode = no_error;
233 TRY(__cursor_not_null(cursor));
235 *key = xbt_dynar_get_as(cursor->keys, cursor->pos - 1, char*);
241 * xbt_dict_cursor_get_data:
242 * @cursor: the cursor
247 xbt_dict_cursor_get_data(xbt_dict_cursor_t cursor,
248 /*OUT*/void **data) {
249 xbt_error_t errcode = no_error;
253 TRY(__cursor_not_null(cursor));
255 key = xbt_dynar_get_as(cursor->keys, cursor->pos-1, char *);
256 key_len = xbt_dynar_get_as(cursor->key_lens, cursor->pos_len-1, int);
258 TRY(xbt_dictelm_get_ext(cursor->head, key, key_len, data));