3 /* cbps - persistant states for callbacks */
5 /* Authors: Olivier Aumage, Martin Quinson */
6 /* Copyright (C) 2003, 2004 da GRAS posse. */
8 /* This program is free software; you can redistribute it and/or modify it
9 under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "DataDesc/datadesc_private.h"
12 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(cbps,datadesc);
15 gras_datadesc_type_t *type;
19 struct s_gras_dd_cbps {
20 gras_dict_t *space; /* varname x dynar of gras_dd_cbps_elm_t */
21 gras_dynar_t *frames; /* of dynar of names defined within this frame
22 (and to pop when we leave it) */
23 gras_dynar_t *globals;
26 static void free_string(void *d);
28 static void free_string(void *d){
33 gras_dd_cbps_new(gras_dd_cbps_t **dst) {
37 if (!(res=malloc(sizeof(gras_dd_cbps_t))))
40 TRY(gras_dict_new(&(res->space)));
41 /* no leak, the content is freed manually on block_end */
42 TRY(gras_dynar_new(&(res->frames), sizeof(gras_dynar_t*), NULL));
43 TRY(gras_dynar_new(&(res->globals), sizeof(char*), NULL));
45 gras_dd_cbps_block_begin(res);
51 gras_dd_cbps_free(gras_dd_cbps_t **state) {
53 gras_dd_cbps_block_end(*state);
54 gras_dict_free ( &( (*state)->space ) );
55 gras_dynar_free( (*state)->frames );
56 gras_dynar_free( (*state)->globals );
65 * Declare a new element in the PS, and give it a value. If an element of that
66 * name already exists, it is masked by the one given here, and will be
67 * seeable again only after a pop to remove the value this push adds.
70 gras_dd_cbps_push(gras_dd_cbps_t *ps,
73 gras_datadesc_type_t *ddt) {
75 gras_dynar_t *varstack,*frame;
76 gras_dd_cbps_elm_t *p_var;
78 char *varname = strdup(name);
80 DEBUG2("push(%s,%p)",name,data);
81 errcode = gras_dict_get(ps->space, name, (void **)&varstack);
83 if (errcode == mismatch_error) {
84 DEBUG1("Create a new variable stack for '%s' into the space",name);
85 gras_dynar_new(&varstack, sizeof (gras_dd_cbps_elm_t *), NULL);
86 gras_dict_set(ps->space, varname, (void **)varstack, NULL);
87 /* leaking, you think? only if you do not close all the openned blocks ;)*/
88 } else if (errcode != no_error) {
92 p_var = calloc(1, sizeof(gras_dd_cbps_elm_t));
96 gras_dynar_push(varstack, &p_var);
98 gras_dynar_pop(ps->frames, &frame);
99 DEBUG4("Push %s (%p @%p) into frame %p",varname,varname,&varname,frame);
100 gras_dynar_push(frame, &varname);
101 gras_dynar_push(ps->frames, &frame);
108 * Retrieve an element from the PS, and remove it from the PS. If it's not
109 * present in the current block, it will fail (with abort) and not search
110 * in upper blocks since this denotes a programmation error.
113 gras_dd_cbps_pop (gras_dd_cbps_t *ps,
115 gras_datadesc_type_t **ddt,
117 gras_dynar_t *varstack,*frame;
118 gras_dd_cbps_elm_t *var = NULL;
120 gras_error_t errcode;
122 DEBUG1("pop(%s)",name);
123 /* FIXME: Error handling */
124 errcode = gras_dict_get(ps->space, name, (void **)&varstack);
125 if (errcode == mismatch_error) {
126 RAISE1(mismatch_error,"Asked to pop the non-existant %s",
129 gras_dynar_pop(varstack, &var);
131 if (!gras_dynar_length(varstack)) {
132 DEBUG1("Last incarnation of %s poped. Kill it",name);
133 gras_dict_remove(ps->space, name);
134 gras_dynar_free(varstack);
143 gras_dynar_pop(ps->frames, &frame);
145 int l = gras_dynar_length(frame);
150 gras_dynar_get(frame, l, &_name);
151 if (!strcmp(name, _name)) {
152 gras_dynar_remove_at(frame, l, &_name);
158 gras_dynar_push(ps->frames, &frame);
167 * Change the value of an element in the PS.
168 * If it's not present in the current block, look in the upper ones.
169 * If it's not present in any of them, modify in the globals
170 * If not present there neither, the code may segfault (Oli?).
172 * Once a reference to an element of that name is found somewhere in the PS,
173 * its value is changed.
176 gras_dd_cbps_set (gras_dd_cbps_t *ps,
179 gras_datadesc_type_t *ddt) {
181 gras_dynar_t *p_dynar = NULL;
182 gras_dd_cbps_elm_t *p_elm = NULL;
183 gras_error_t errcode;
185 DEBUG1("set(%s)",name);
186 errcode = gras_dict_get(ps->space, name, (void **)&p_dynar);
188 if (errcode == mismatch_error) {
189 gras_dynar_new(&p_dynar, sizeof (gras_dd_cbps_elm_t *), NULL);
190 gras_dict_set(ps->space, name, (void **)p_dynar, NULL);
192 p_elm = calloc(1, sizeof(gras_dd_cbps_elm_t));
193 gras_dynar_push(ps->globals, &name);
195 gras_dynar_pop(p_dynar, &p_elm);
201 gras_dynar_push(p_dynar, &p_elm);
208 * Get the value of an element in the PS without modifying it.
209 * (note that you get the content of the data struct and not a copy to it)
210 * If it's not present in the current block, look in the upper ones.
211 * If it's not present in any of them, look in the globals
212 * If not present there neither, the code may segfault (Oli?).
215 gras_dd_cbps_get (gras_dd_cbps_t *ps,
217 gras_datadesc_type_t **ddt) {
219 gras_dynar_t *p_dynar = NULL;
220 gras_dd_cbps_elm_t *p_elm = NULL;
222 DEBUG1("get(%s)",name);
223 /* FIXME: Error handling */
224 gras_dict_get(ps->space, name, (void **)&p_dynar);
225 gras_dynar_pop(p_dynar, &p_elm);
226 gras_dynar_push(p_dynar, &p_elm);
237 * gras_dd_cbps_block_begin:
239 * Begins a new block.
241 * Blocks are usefull to remove a whole set of declarations you don't even know
243 * E.g., they constitute an elegent solution to recursive data structures.
245 * push/pop may be used in some cases for that, but if your recursive data
246 * struct contains other structs needing themselves callbacks, you have to
247 * use block_{begin,end} to do the trick.
251 gras_dd_cbps_block_begin(gras_dd_cbps_t *ps) {
253 gras_dynar_t *p_dynar = NULL;
255 DEBUG0(">>> Block begin");
256 gras_dynar_new(&p_dynar, sizeof (char *), NULL);
257 gras_dynar_push(ps->frames, &p_dynar);
261 * gras_dd_cbps_block_begin:
263 * End the current block, and go back to the upper one.
266 gras_dd_cbps_block_end(gras_dd_cbps_t *ps) {
268 gras_dynar_t *frame = NULL;
272 gras_assert0(gras_dynar_length(ps->frames),
273 "More block_end than block_begin");
274 gras_dynar_pop(ps->frames, &frame);
276 gras_dynar_foreach(frame, cursor, name) {
278 gras_dynar_t *varstack = NULL;
279 gras_dd_cbps_elm_t *var = NULL;
281 DEBUG2("Get ride of %s (%p)",name,name);
282 gras_dict_get(ps->space, name, (void **)&varstack);
283 gras_dynar_pop(varstack, &var);
285 if (!gras_dynar_length(varstack)) {
286 gras_dict_remove(ps->space, name);
287 gras_dynar_free_container(varstack); /*already empty, save a test ;) */
290 if (var->data) free(var->data);
294 gras_dynar_free_container(frame);/* we just emptied it */
295 DEBUG0("<<< Block end");