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 "gras/DataDesc/datadesc_private.h"
12 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(cbps,datadesc);
15 gras_datadesc_type_t *type;
20 gras_dynar_t *lints; /* simple stack of long integers (easy interface) */
22 gras_dict_t *space; /* varname x dynar of gras_cbps_elm_t */
23 gras_dynar_t *frames; /* of dynar of names defined within this frame
24 (and to pop when we leave it) */
25 gras_dynar_t *globals;
28 static void free_string(void *d);
30 static void free_string(void *d){
35 gras_cbps_new(gras_cbps_t **dst) {
39 if (!(res=malloc(sizeof(gras_cbps_t))))
42 TRY(gras_dynar_new(&(res->lints), sizeof(int), NULL));
44 TRY(gras_dict_new(&(res->space)));
45 /* no leak, the content is freed manually on block_end */
46 TRY(gras_dynar_new(&(res->frames), sizeof(gras_dynar_t*), NULL));
47 TRY(gras_dynar_new(&(res->globals), sizeof(char*), NULL));
49 gras_cbps_block_begin(res);
55 gras_cbps_free(gras_cbps_t **state) {
57 gras_dynar_free( (*state)->lints );
59 gras_cbps_block_end(*state);
60 gras_dict_free ( &( (*state)->space ) );
61 gras_dynar_free( (*state)->frames );
62 gras_dynar_free( (*state)->globals );
71 * Declare a new element in the PS, and give it a value. If an element of that
72 * name already exists, it is masked by the one given here, and will be
73 * seeable again only after a pop to remove the value this push adds.
76 gras_cbps_v_push(gras_cbps_t *ps,
79 gras_datadesc_type_t *ddt) {
81 gras_dynar_t *varstack,*frame;
82 gras_cbps_elm_t *p_var;
84 char *varname = strdup(name);
86 DEBUG2("push(%s,%p)",name,data);
87 errcode = gras_dict_get(ps->space, name, (void **)&varstack);
89 if (errcode == mismatch_error) {
90 DEBUG1("Create a new variable stack for '%s' into the space",name);
91 gras_dynar_new(&varstack, sizeof (gras_cbps_elm_t *), NULL);
92 gras_dict_set(ps->space, varname, (void **)varstack, NULL);
93 /* leaking, you think? only if you do not close all the openned blocks ;)*/
94 } else if (errcode != no_error) {
98 p_var = calloc(1, sizeof(gras_cbps_elm_t));
102 gras_dynar_push(varstack, &p_var);
104 gras_dynar_pop(ps->frames, &frame);
105 DEBUG4("Push %s (%p @%p) into frame %p",varname,varname,&varname,frame);
106 gras_dynar_push(frame, &varname);
107 gras_dynar_push(ps->frames, &frame);
114 * Retrieve an element from the PS, and remove it from the PS. If it's not
115 * present in the current block, it will fail (with abort) and not search
116 * in upper blocks since this denotes a programmation error.
119 gras_cbps_v_pop (gras_cbps_t *ps,
121 gras_datadesc_type_t **ddt,
123 gras_dynar_t *varstack,*frame;
124 gras_cbps_elm_t *var = NULL;
126 gras_error_t errcode;
128 DEBUG1("pop(%s)",name);
129 /* FIXME: Error handling */
130 errcode = gras_dict_get(ps->space, name, (void **)&varstack);
131 if (errcode == mismatch_error) {
132 RAISE1(mismatch_error,"Asked to pop the non-existant %s",
135 gras_dynar_pop(varstack, &var);
137 if (!gras_dynar_length(varstack)) {
138 DEBUG1("Last incarnation of %s poped. Kill it",name);
139 gras_dict_remove(ps->space, name);
140 gras_dynar_free(varstack);
149 gras_dynar_pop(ps->frames, &frame);
151 int l = gras_dynar_length(frame);
156 gras_dynar_get(frame, l, &_name);
157 if (!strcmp(name, _name)) {
158 gras_dynar_remove_at(frame, l, &_name);
164 gras_dynar_push(ps->frames, &frame);
173 * Change the value of an element in the PS.
174 * If it's not present in the current block, look in the upper ones.
175 * If it's not present in any of them, modify in the globals
176 * If not present there neither, the code may segfault (Oli?).
178 * Once a reference to an element of that name is found somewhere in the PS,
179 * its value is changed.
182 gras_cbps_v_set (gras_cbps_t *ps,
185 gras_datadesc_type_t *ddt) {
187 gras_dynar_t *p_dynar = NULL;
188 gras_cbps_elm_t *p_elm = NULL;
189 gras_error_t errcode;
191 DEBUG1("set(%s)",name);
192 errcode = gras_dict_get(ps->space, name, (void **)&p_dynar);
194 if (errcode == mismatch_error) {
195 gras_dynar_new(&p_dynar, sizeof (gras_cbps_elm_t *), NULL);
196 gras_dict_set(ps->space, name, (void **)p_dynar, NULL);
198 p_elm = calloc(1, sizeof(gras_cbps_elm_t));
199 gras_dynar_push(ps->globals, &name);
201 gras_dynar_pop(p_dynar, &p_elm);
207 gras_dynar_push(p_dynar, &p_elm);
214 * Get the value of an element in the PS without modifying it.
215 * (note that you get the content of the data struct and not a copy to it)
216 * If it's not present in the current block, look in the upper ones.
217 * If it's not present in any of them, look in the globals
218 * If not present there neither, the code may segfault (Oli?).
221 gras_cbps_v_get (gras_cbps_t *ps,
223 gras_datadesc_type_t **ddt) {
225 gras_dynar_t *p_dynar = NULL;
226 gras_cbps_elm_t *p_elm = NULL;
228 DEBUG1("get(%s)",name);
229 /* FIXME: Error handling */
230 gras_dict_get(ps->space, name, (void **)&p_dynar);
231 gras_dynar_pop(p_dynar, &p_elm);
232 gras_dynar_push(p_dynar, &p_elm);
243 * gras_cbps_block_begin:
245 * Begins a new block.
247 * Blocks are usefull to remove a whole set of declarations you don't even know
249 * E.g., they constitute an elegent solution to recursive data structures.
251 * push/pop may be used in some cases for that, but if your recursive data
252 * struct contains other structs needing themselves callbacks, you have to
253 * use block_{begin,end} to do the trick.
257 gras_cbps_block_begin(gras_cbps_t *ps) {
259 gras_dynar_t *p_dynar = NULL;
261 DEBUG0(">>> Block begin");
262 gras_dynar_new(&p_dynar, sizeof (char *), NULL);
263 gras_dynar_push(ps->frames, &p_dynar);
267 * gras_cbps_block_begin:
269 * End the current block, and go back to the upper one.
272 gras_cbps_block_end(gras_cbps_t *ps) {
274 gras_dynar_t *frame = NULL;
278 gras_assert0(gras_dynar_length(ps->frames),
279 "More block_end than block_begin");
280 gras_dynar_pop(ps->frames, &frame);
282 gras_dynar_foreach(frame, cursor, name) {
284 gras_dynar_t *varstack = NULL;
285 gras_cbps_elm_t *var = NULL;
287 DEBUG2("Get ride of %s (%p)",name,name);
288 gras_dict_get(ps->space, name, (void **)&varstack);
289 gras_dynar_pop(varstack, &var);
291 if (!gras_dynar_length(varstack)) {
292 gras_dict_remove(ps->space, name);
293 gras_dynar_free_container(varstack); /*already empty, save a test ;) */
296 if (var->data) free(var->data);
300 gras_dynar_free_container(frame);/* we just emptied it */
301 DEBUG0("<<< Block end");
308 * Push a new long integer value into the cbps.
311 gras_cbps_i_push(gras_cbps_t *ps,
313 gras_error_t errcode;
314 DEBUG1("push %d as a size",val);
315 TRYFAIL(gras_dynar_push(ps->lints,&val));
320 * Pop the lastly pushed long integer value from the cbps.
323 gras_cbps_i_pop(gras_cbps_t *ps) {
326 gras_assert0(gras_dynar_length(ps->lints) > 0,
327 "gras_cbps_i_pop: no value to pop");
328 gras_dynar_pop(ps->lints, &ret);
329 DEBUG1("pop %d as a size",ret);
334 * gras_datadesc_cb_pop:
336 * Generic cb returning the lastly pushed value
338 int gras_datadesc_cb_pop(gras_cbps_t *vars, void *data) {
339 return gras_cbps_i_pop(vars);
343 * gras_datadesc_cb_push_int:
345 * Cb to push an integer. Must be attached to the field you want to push
347 void gras_datadesc_cb_push_int(gras_cbps_t *vars, void *data) {
349 gras_cbps_i_push(vars, (int) *i);
353 * gras_datadesc_cb_push_uint:
355 * Cb to push an unsigned integer. Must be attached to the field you want to push
357 void gras_datadesc_cb_push_uint(gras_cbps_t *vars, void *data) {
358 unsigned int *i = (unsigned int*)data;
359 gras_cbps_i_push(vars, (int) *i);
363 * gras_datadesc_cb_push_lint:
365 * Cb to push an long integer. Must be attached to the field you want to push
367 void gras_datadesc_cb_push_lint(gras_cbps_t *vars, void *data) {
368 long int *i = (long int*)data;
369 gras_cbps_i_push(vars, (int) *i);
372 * gras_datadesc_cb_push_ulint:
374 * Cb to push an long integer. Must be attached to the field you want to push
376 void gras_datadesc_cb_push_ulint(gras_cbps_t *vars, void *data) {
377 unsigned long int *i = (unsigned long int*)data;
378 gras_cbps_i_push(vars, (int) *i);