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 * @param head the dict
43 xbt_dict_cursor_new(const xbt_dict_t head) {
44 xbt_dict_cursor_t res = NULL;
46 res = xbt_new(s_xbt_dict_cursor_t,1);
47 res->keys = xbt_dynar_new(sizeof(char **), NULL);
48 res->key_lens = xbt_dynar_new(sizeof(int *), NULL);
51 res->head = head ? head->head : NULL;
53 xbt_dict_cursor_rewind(res);
60 * @param cursor poor victim
63 xbt_dict_cursor_free(xbt_dict_cursor_t *cursor) {
65 xbt_dynar_free(&((*cursor)->keys));
66 xbt_dynar_free(&((*cursor)->key_lens));
73 * Sanity check to see if the head contains something
77 __cursor_not_null(xbt_dict_cursor_t cursor) {
79 xbt_assert0(cursor, "Null cursor");
82 return mismatch_error;
91 _cursor_push_keys(xbt_dict_cursor_t cursor,
93 xbt_dictelm_t child = NULL;
95 static volatile int count = 0; /* ??? */
97 CDEBUG3(dict_cursor, "Push childs of %p (%.*s) in the cursor", (void*)elm, elm->key_len, elm->key);
100 xbt_dynar_push(cursor->keys, &elm->key );
101 xbt_dynar_push(cursor->key_lens, &elm->key_len);
105 xbt_dynar_foreach(elm->sub, i, child) {
107 _cursor_push_keys(cursor, child);
110 CDEBUG1(dict_cursor, "Count = %d", count);
113 /** @brief Reinitialize the cursor. Mandatory after removal or add in dict. */
115 xbt_dict_cursor_rewind(xbt_dict_cursor_t cursor) {
117 CDEBUG0(dict_cursor, "xbt_dict_cursor_rewind");
120 xbt_dynar_reset(cursor->keys);
121 xbt_dynar_reset(cursor->key_lens);
126 _cursor_push_keys(cursor, cursor->head);
128 xbt_dynar_cursor_first(cursor->keys, &cursor->pos );
129 xbt_dynar_cursor_first(cursor->key_lens, &cursor->pos_len);
134 * @brief Create the cursor if it does not exists. Rewind it in any case.
136 * @param dict on what to let the cursor iterate
137 * @param[out] cursor dest address
139 void xbt_dict_cursor_first (const xbt_dict_t dict,
140 xbt_dict_cursor_t *cursor){
143 DEBUG0("Create the cursor on first use");
144 *cursor=xbt_dict_cursor_new(dict);
147 xbt_dict_cursor_rewind(*cursor);
152 * \brief Move to the next element.
155 xbt_dict_cursor_step(xbt_dict_cursor_t cursor) {
158 DEBUG2("step cursor. Current=%.*s",
159 xbt_dynar_get_as(cursor->key_lens,cursor->pos_len,int),
160 xbt_dynar_get_as(cursor->keys,cursor->pos,char *));
161 xbt_dynar_cursor_step(cursor->keys, &cursor->pos);
162 xbt_dynar_cursor_step(cursor->key_lens, &cursor->pos_len);
166 * @brief Get current data, or free the cursor if there is no data left
168 * @returns true if it's ok, false if there is no more data
171 xbt_dict_cursor_get_or_free(xbt_dict_cursor_t *cursor,
174 xbt_error_t errcode = no_error;
177 if (!cursor || !(*cursor))
180 if (xbt_dynar_length((*cursor)->keys) <= (*cursor)->pos) {
181 xbt_dict_cursor_free(cursor);
185 *key = xbt_dynar_get_as((*cursor)->keys, (*cursor)->pos, char*);
186 key_len = xbt_dynar_get_as((*cursor)->key_lens, (*cursor)->pos_len, int);
188 errcode = xbt_dictelm_get_ext((*cursor)->head, *key, key_len, data);
189 if (errcode == mismatch_error) {
190 xbt_dict_cursor_free(cursor);
194 xbt_assert1(errcode == no_error,
195 "Unexpected problem while retrieving the content of cursor. Got %s",
196 xbt_error_name(errcode));
202 * @brief Get current key
203 * @param cursor: the cursor
204 * @param key where to put the key
207 xbt_dict_cursor_get_key(xbt_dict_cursor_t cursor,
209 xbt_error_t errcode = no_error;
211 TRY(__cursor_not_null(cursor));
213 *key = xbt_dynar_get_as(cursor->keys, cursor->pos - 1, char*);
219 * @brief Get current data
220 * @param cursor the cursor
221 * @param data where to put the data
224 xbt_dict_cursor_get_data(xbt_dict_cursor_t cursor,
225 /*OUT*/void **data) {
226 xbt_error_t errcode = no_error;
230 TRY(__cursor_not_null(cursor));
232 key = xbt_dynar_get_as(cursor->keys, cursor->pos-1, char *);
233 key_len = xbt_dynar_get_as(cursor->key_lens, cursor->pos_len-1, int);
235 TRY(xbt_dictelm_get_ext(cursor->head, key, key_len, data));