Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
71780ebc6dbc4b962d8dd2b0955327d335ea23ac
[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     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   CDEBUG3(dict_cursor, "Push childs of %p (%.*s) in the cursor", (void*)elm, elm->key_len, elm->key);
98
99   if (!elm->internal) {
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   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);
163 }
164
165 /**
166  * @brief Get current data, or free the cursor if there is no data left
167  *
168  * @returns true if it's ok, false if there is no more data
169  */
170 int
171 xbt_dict_cursor_get_or_free(xbt_dict_cursor_t  *cursor,
172                             char               **key,
173                             void               **data) {
174   xbt_error_t  errcode = no_error;
175   int           key_len = 0;
176   
177   if (!cursor || !(*cursor))
178     return FALSE;
179
180   if (xbt_dynar_length((*cursor)->keys) <= (*cursor)->pos) {
181     xbt_dict_cursor_free(cursor);
182     return FALSE;
183   }
184     
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);
187
188   errcode = xbt_dictelm_get_ext((*cursor)->head, *key, key_len, data);
189   if (errcode == mismatch_error) {
190     xbt_dict_cursor_free(cursor);
191     return FALSE;
192   }
193
194   xbt_assert1(errcode == no_error,
195                "Unexpected problem while retrieving the content of cursor. Got %s",
196                xbt_error_name(errcode));
197
198   return TRUE;
199 }
200
201 /**
202  * @brief Get current key
203  * @param cursor: the cursor
204  * @param key where to put the key
205  */
206 xbt_error_t
207 xbt_dict_cursor_get_key(xbt_dict_cursor_t   cursor,
208                          /*OUT*/char        **key) {
209   xbt_error_t errcode = no_error;
210
211   TRYOLD(__cursor_not_null(cursor));
212
213   *key = xbt_dynar_get_as(cursor->keys, cursor->pos - 1, char*);
214
215   return errcode;
216 }
217
218 /**
219  * @brief Get current data
220  * @param cursor the cursor
221  * @param data where to put the data
222  */
223 xbt_error_t
224 xbt_dict_cursor_get_data(xbt_dict_cursor_t   cursor,
225                           /*OUT*/void        **data) {
226   xbt_error_t  errcode = no_error;
227   char         *key     = NULL;
228   int           key_len = 0;
229
230   TRYOLD(__cursor_not_null(cursor));
231
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);
234
235   TRYOLD(xbt_dictelm_get_ext(cursor->head, key, key_len, data));
236
237   return errcode;
238 }
239
240