Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mv include/xbt/context.h src/include/xbt/context.h since users shouldn't mess with it
[simgrid.git] / src / xbt / dict_cursor.c
1 /* $Id$ */
2
3 /* dict_cursor - iterators over dictionnaries                               */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
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. */
9
10 #include "xbt/misc.h"
11 #include "dict_private.h"
12
13 #include <string.h> /* strlen() */
14
15 XBT_LOG_EXTERNAL_CATEGORY(dict);
16 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dict_cursor,dict,"To traverse dictionaries");
17
18
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() */
25   xbt_dynar_t   keys;
26   xbt_dynar_t   key_lens;
27   int            pos;
28   int            pos_len;
29   xbt_dictelm_t head;
30 };
31
32 static _XBT_INLINE void
33 _cursor_push_keys(xbt_dict_cursor_t p_cursor,
34                   xbt_dictelm_t     p_elm);
35
36 #undef xbt_dict_CURSOR_DEBUG
37 /*#define xbt_dict_CURSOR_DEBUG 1*/
38
39 /** @brief Creator 
40  *  @param head   the dict
41  */
42 xbt_dict_cursor_t 
43 xbt_dict_cursor_new(const xbt_dict_t head) {
44   xbt_dict_cursor_t res = NULL;
45
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);
49   res->pos      = 0;
50   res->pos_len  = 0;
51   res->head     = head ? head->head : NULL;
52
53   xbt_dict_cursor_rewind(res);
54
55   return res;
56 }
57
58 /**
59  * @brief Destructor
60  * @param cursor poor victim
61  */
62 void
63 xbt_dict_cursor_free(xbt_dict_cursor_t *cursor) {
64   if (*cursor) {
65     xbt_dynar_free(&((*cursor)->keys));
66     xbt_dynar_free(&((*cursor)->key_lens));
67     xbt_free(*cursor);
68     *cursor = NULL;
69   }
70 }
71
72 /*
73  * Sanity check to see if the head contains something
74  */
75 static _XBT_INLINE
76 xbt_error_t
77 __cursor_not_null(xbt_dict_cursor_t cursor) {
78
79   xbt_assert0(cursor, "Null cursor");
80
81   if (!cursor->head) {
82     return mismatch_error;
83   }
84
85   return no_error;
86 }
87
88
89 static _XBT_INLINE
90 void
91 _cursor_push_keys(xbt_dict_cursor_t cursor,
92                   xbt_dictelm_t     elm) {
93   xbt_dictelm_t       child = NULL;
94   int                  i       = 0;
95   static volatile int  count   = 0; /* ??? */
96
97   CDEBUG1(dict_cursor, "Push childs of %p in the cursor", (void*)elm);
98
99   if (elm->content) {
100     xbt_dynar_push(cursor->keys,     &elm->key    );
101     xbt_dynar_push(cursor->key_lens, &elm->key_len);
102     count++;
103   }
104
105   xbt_dynar_foreach(elm->sub, i, child) {
106     if (child)
107       _cursor_push_keys(cursor, child);
108   }
109
110   CDEBUG1(dict_cursor, "Count = %d", count);
111 }
112
113 /** @brief Reinitialize the cursor. Mandatory after removal or add in dict. */
114 void
115 xbt_dict_cursor_rewind(xbt_dict_cursor_t cursor) {
116
117   CDEBUG0(dict_cursor, "xbt_dict_cursor_rewind");
118   xbt_assert(cursor);
119
120   xbt_dynar_reset(cursor->keys);
121   xbt_dynar_reset(cursor->key_lens);
122
123   if (!cursor->head)
124     return ;
125
126   _cursor_push_keys(cursor, cursor->head);
127
128   xbt_dynar_cursor_first(cursor->keys,     &cursor->pos    );
129   xbt_dynar_cursor_first(cursor->key_lens, &cursor->pos_len);
130
131 }
132
133 /**
134  * @brief Create the cursor if it does not exists. Rewind it in any case.
135  *
136  * @param      dict   on what to let the cursor iterate
137  * @param[out] cursor dest address
138  */
139 void xbt_dict_cursor_first (const xbt_dict_t   dict,
140                              xbt_dict_cursor_t *cursor){
141
142   if (!*cursor) {
143     DEBUG0("Create the cursor on first use");
144     *cursor=xbt_dict_cursor_new(dict);
145   }
146
147   xbt_dict_cursor_rewind(*cursor);
148 }
149
150
151 /**
152  * \brief Move to the next element. 
153  */
154 void
155 xbt_dict_cursor_step(xbt_dict_cursor_t cursor) {
156   xbt_assert(cursor);
157
158   xbt_dynar_cursor_step(cursor->keys,     &cursor->pos);
159   xbt_dynar_cursor_step(cursor->key_lens, &cursor->pos_len);
160 }
161
162 /**
163  * @brief Get current data, or free the cursor if there is no data left
164  *
165  * @returns true if it's ok, false if there is no more data
166  */
167 int
168 xbt_dict_cursor_get_or_free(xbt_dict_cursor_t  *cursor,
169                              char               **key,
170                              void               **data) {
171   xbt_error_t  errcode = no_error;
172   int           key_len = 0;
173   
174   if (!cursor || !(*cursor))
175     return FALSE;
176
177   if (xbt_dynar_length((*cursor)->keys) <= (*cursor)->pos) {
178     xbt_dict_cursor_free(cursor);
179     return FALSE;
180   }
181     
182   *key    = xbt_dynar_get_as((*cursor)->keys,     (*cursor)->pos,     char*);
183   key_len = xbt_dynar_get_as((*cursor)->key_lens, (*cursor)->pos_len, int);
184
185   errcode = xbt_dictelm_get_ext((*cursor)->head, *key, key_len, data);
186   if (errcode == mismatch_error) {
187     xbt_dict_cursor_free(cursor);
188     return FALSE;
189   }
190
191   xbt_assert1(errcode == no_error,
192                "Unexpected problem while retrieving the content of cursor. Got %s",
193                xbt_error_name(errcode));
194
195   return TRUE;
196 }
197
198 /**
199  * @brief Get current key
200  * @param cursor: the cursor
201  * @param key where to put the key
202  */
203 xbt_error_t
204 xbt_dict_cursor_get_key(xbt_dict_cursor_t   cursor,
205                          /*OUT*/char        **key) {
206   xbt_error_t errcode = no_error;
207
208   TRY(__cursor_not_null(cursor));
209
210   *key = xbt_dynar_get_as(cursor->keys, cursor->pos - 1, char*);
211
212   return errcode;
213 }
214
215 /**
216  * @brief Get current data
217  * @param cursor the cursor
218  * @param data where to put the data
219  */
220 xbt_error_t
221 xbt_dict_cursor_get_data(xbt_dict_cursor_t   cursor,
222                           /*OUT*/void        **data) {
223   xbt_error_t  errcode = no_error;
224   char         *key     = NULL;
225   int           key_len = 0;
226
227   TRY(__cursor_not_null(cursor));
228
229   key     = xbt_dynar_get_as(cursor->keys,     cursor->pos-1,  char *);
230   key_len = xbt_dynar_get_as(cursor->key_lens, cursor->pos_len-1, int);
231
232   TRY(xbt_dictelm_get_ext(cursor->head, key, key_len, data));
233
234   return errcode;
235 }
236
237