Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
A few more tiers file and a convenient perl script for the alnem project. I
[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 "xbt/ex.h"
12 #include "dict_private.h"
13
14 #include <string.h> /* strlen() */
15
16 XBT_LOG_EXTERNAL_CATEGORY(dict);
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(dict_cursor,dict,"To traverse dictionaries");
18
19
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() */
26   xbt_dynar_t   keys;
27   xbt_dynar_t   key_lens;
28   int            pos;
29   int            pos_len;
30   xbt_dictelm_t head;
31 };
32
33 static _XBT_INLINE void
34 _cursor_push_keys(xbt_dict_cursor_t p_cursor,
35                   xbt_dictelm_t     p_elm);
36
37 #undef xbt_dict_CURSOR_DEBUG
38 /*#define xbt_dict_CURSOR_DEBUG 1*/
39
40 /** @brief Creator 
41  *  @param head   the dict
42  */
43 xbt_dict_cursor_t 
44 xbt_dict_cursor_new(const xbt_dict_t head) {
45   xbt_dict_cursor_t res = NULL;
46
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);
50   res->pos      = 0;
51   res->pos_len  = 0;
52   res->head     = head ? head->head : NULL;
53
54   xbt_dict_cursor_rewind(res);
55
56   return res;
57 }
58
59 /**
60  * @brief Destructor
61  * @param cursor poor victim
62  */
63 void
64 xbt_dict_cursor_free(xbt_dict_cursor_t *cursor) {
65   if (*cursor) {
66     xbt_dynar_free(&((*cursor)->keys));
67     xbt_dynar_free(&((*cursor)->key_lens));
68     free(*cursor);
69     *cursor = NULL;
70   }
71 }
72
73 /*
74  * Sanity check to see if the head contains something
75  */
76 static _XBT_INLINE
77 void
78 __cursor_not_null(xbt_dict_cursor_t cursor) {
79
80   xbt_assert0(cursor, "Null cursor");
81
82   if (!cursor->head)
83     THROW0(arg_error,0,"Null headed cursor");
84 }
85
86
87 static _XBT_INLINE
88 void
89 _cursor_push_keys(xbt_dict_cursor_t cursor,
90                   xbt_dictelm_t     elm) {
91   xbt_dictelm_t       child = NULL;
92   int                  i       = 0;
93   static volatile int  count   = 0; /* ??? */
94
95   CDEBUG3(dict_cursor, "Push childs of %p (%.*s) in the cursor", (void*)elm, elm->key_len, elm->key);
96
97   if (!elm->internal) {
98     xbt_dynar_push(cursor->keys,     &elm->key    );
99     xbt_dynar_push(cursor->key_lens, &elm->key_len);
100     count++;
101   }
102
103   xbt_dynar_foreach(elm->sub, i, child) {
104     if (child)
105       _cursor_push_keys(cursor, child);
106   }
107
108   CDEBUG1(dict_cursor, "Count = %d", count);
109 }
110
111 /** @brief Reinitialize the cursor. Mandatory after removal or add in dict. */
112 void
113 xbt_dict_cursor_rewind(xbt_dict_cursor_t cursor) {
114
115   CDEBUG0(dict_cursor, "xbt_dict_cursor_rewind");
116   xbt_assert(cursor);
117
118   xbt_dynar_reset(cursor->keys);
119   xbt_dynar_reset(cursor->key_lens);
120
121   if (!cursor->head)
122     return ;
123
124   _cursor_push_keys(cursor, cursor->head);
125
126   xbt_dynar_cursor_first(cursor->keys,     &cursor->pos    );
127   xbt_dynar_cursor_first(cursor->key_lens, &cursor->pos_len);
128
129 }
130
131 /**
132  * @brief Create the cursor if it does not exists. Rewind it in any case.
133  *
134  * @param      dict   on what to let the cursor iterate
135  * @param[out] cursor dest address
136  */
137 void xbt_dict_cursor_first (const xbt_dict_t   dict,
138                              xbt_dict_cursor_t *cursor){
139
140   if (!*cursor) {
141     DEBUG0("Create the cursor on first use");
142     *cursor=xbt_dict_cursor_new(dict);
143   }
144
145   xbt_dict_cursor_rewind(*cursor);
146 }
147
148
149 /**
150  * \brief Move to the next element. 
151  */
152 void
153 xbt_dict_cursor_step(xbt_dict_cursor_t cursor) {
154   xbt_assert(cursor);
155
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);
161 }
162
163 /**
164  * @brief Get current data, or free the cursor if there is no data left
165  *
166  * @returns true if it's ok, false if there is no more data
167  */
168 int
169 xbt_dict_cursor_get_or_free(xbt_dict_cursor_t  *cursor,
170                             char               **key,
171                             void               **data) {
172   int      key_len = 0;
173   xbt_ex_t e;
174   
175   if (!cursor || !(*cursor))
176     return FALSE;
177
178   if (xbt_dynar_length((*cursor)->keys) <= (*cursor)->pos) {
179     xbt_dict_cursor_free(cursor);
180     return FALSE;
181   }
182     
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);
185
186   TRY {
187     *data = xbt_dictelm_get_ext((*cursor)->head, *key, key_len);
188   } CATCH(e) {
189     if (e.category == mismatch_error) {
190       xbt_dict_cursor_free(cursor);
191       xbt_ex_free(e);
192       return FALSE;
193     }
194     RETHROW;
195   }
196   return TRUE;
197 }
198
199 /**
200  * @brief Get current key
201  * @param cursor: the cursor
202  * @returns the current key
203  */
204 char *
205 xbt_dict_cursor_get_key(xbt_dict_cursor_t   cursor) {
206   __cursor_not_null(cursor);
207
208   return xbt_dynar_get_as(cursor->keys, cursor->pos - 1, char*);
209 }
210
211 /**
212  * @brief Get current data
213  * @param cursor the cursor
214  * @returns the current data
215  */
216 void *
217 xbt_dict_cursor_get_data(xbt_dict_cursor_t   cursor) {
218   char         *key     = NULL;
219   int           key_len = 0;
220
221   __cursor_not_null(cursor);
222
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);
225
226   return xbt_dictelm_get_ext(cursor->head, key, key_len);
227 }
228
229