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. */
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 * @param head the dict
44 xbt_dict_cursor_new(const xbt_dict_t head) {
45 xbt_dict_cursor_t res = NULL;
47 res = xbt_new(s_xbt_dict_cursor_t,1);
48 res->keys = xbt_dynar_new(sizeof(char **), NULL);
49 res->key_lens = xbt_dynar_new(sizeof(int *), NULL);
52 res->head = head ? head->head : NULL;
54 xbt_dict_cursor_rewind(res);
61 * @param cursor poor victim
64 xbt_dict_cursor_free(xbt_dict_cursor_t *cursor) {
66 xbt_dynar_free(&((*cursor)->keys));
67 xbt_dynar_free(&((*cursor)->key_lens));
74 * Sanity check to see if the head contains something
78 __cursor_not_null(xbt_dict_cursor_t cursor) {
80 xbt_assert0(cursor, "Null cursor");
83 THROW0(arg_error,0,"Null headed cursor");
89 _cursor_push_keys(xbt_dict_cursor_t cursor,
91 xbt_dictelm_t child = NULL;
93 static volatile int count = 0; /* ??? */
95 CDEBUG3(dict_cursor, "Push childs of %p (%.*s) in the cursor", (void*)elm, elm->key_len, elm->key);
98 xbt_dynar_push(cursor->keys, &elm->key );
99 xbt_dynar_push(cursor->key_lens, &elm->key_len);
103 xbt_dynar_foreach(elm->sub, i, child) {
105 _cursor_push_keys(cursor, child);
108 CDEBUG1(dict_cursor, "Count = %d", count);
111 /** @brief Reinitialize the cursor. Mandatory after removal or add in dict. */
113 xbt_dict_cursor_rewind(xbt_dict_cursor_t cursor) {
115 CDEBUG0(dict_cursor, "xbt_dict_cursor_rewind");
118 xbt_dynar_reset(cursor->keys);
119 xbt_dynar_reset(cursor->key_lens);
124 _cursor_push_keys(cursor, cursor->head);
126 xbt_dynar_cursor_first(cursor->keys, &cursor->pos );
127 xbt_dynar_cursor_first(cursor->key_lens, &cursor->pos_len);
132 * @brief Create the cursor if it does not exists. Rewind it in any case.
134 * @param dict on what to let the cursor iterate
135 * @param[out] cursor dest address
137 void xbt_dict_cursor_first (const xbt_dict_t dict,
138 xbt_dict_cursor_t *cursor){
141 DEBUG0("Create the cursor on first use");
142 *cursor=xbt_dict_cursor_new(dict);
145 xbt_dict_cursor_rewind(*cursor);
150 * \brief Move to the next element.
153 xbt_dict_cursor_step(xbt_dict_cursor_t cursor) {
156 DEBUG2("step cursor. Current=%.*s",
157 xbt_dynar_get_as(cursor->key_lens,cursor->pos_len,int),
158 xbt_dynar_get_as(cursor->keys,cursor->pos,char *));
159 xbt_dynar_cursor_step(cursor->keys, &cursor->pos);
160 xbt_dynar_cursor_step(cursor->key_lens, &cursor->pos_len);
164 * @brief Get current data, or free the cursor if there is no data left
166 * @returns true if it's ok, false if there is no more data
169 xbt_dict_cursor_get_or_free(xbt_dict_cursor_t *cursor,
175 if (!cursor || !(*cursor))
178 if (xbt_dynar_length((*cursor)->keys) <= (*cursor)->pos) {
179 xbt_dict_cursor_free(cursor);
183 *key = xbt_dynar_get_as((*cursor)->keys, (*cursor)->pos, char*);
184 key_len = xbt_dynar_get_as((*cursor)->key_lens, (*cursor)->pos_len, int);
187 *data = xbt_dictelm_get_ext((*cursor)->head, *key, key_len);
189 if (e.category == mismatch_error) {
190 xbt_dict_cursor_free(cursor);
200 * @brief Get current key
201 * @param cursor: the cursor
202 * @param key where to put the key
205 xbt_dict_cursor_get_key(xbt_dict_cursor_t cursor) {
206 __cursor_not_null(cursor);
208 return xbt_dynar_get_as(cursor->keys, cursor->pos - 1, char*);
212 * @brief Get current data
213 * @param cursor the cursor
214 * @param data where to put the data
217 xbt_dict_cursor_get_data(xbt_dict_cursor_t cursor) {
221 __cursor_not_null(cursor);
223 key = xbt_dynar_get_as(cursor->keys, cursor->pos-1, char *);
224 key_len = xbt_dynar_get_as(cursor->key_lens, cursor->pos_len-1, int);
226 return xbt_dictelm_get_ext(cursor->head, key, key_len);