//push and retrieve string variables //daan hogema TUG (c) 2 aug 2002 /* to be added later (all TBD): -reordering linked list upon each deed read, so as to optimize access. Or other method -coupling between these variables and certain int's or double's in the C-exeutable, e.g. by storing pointers variables may start with control chars, haging meaning \003. local variable values may start with control chars, having following meaning: \001... user defined function \002 built-in function future, TBD: \004 as pointer to int \005 as pointer to coordinate \006 as pointer to double */ #ifndef D_VAR_INCLUDED #include #include "string.h" struct d_var_struct { char *name; char *value; struct d_var_struct *next; }; struct d_var_struct *d_var_root=NULL; int d_var_locallevel=0; //local variables are only accessible in correct nesting level char *d_var_local(char *name) {//makes local variable name from a global variable name static char locals[StringSize]; if(d_var_locallevel==0)return(name); sprintf(locals,"\003%d.%s",d_var_locallevel,name); return(locals); } void d_var_push(char *name, char *value) { void dd_syntax_error(); //predeclaration struct d_var_struct **h; h=&(d_var_root); while(*h) { if(Gelijk(name,(*h)->name)) { if((*h)->value[0]=='\002') dd_syntax_error("attempt to redefine built-in function %s",name); if(S_len(value)==S_len((*h)->value)) Copy(value,(*h)->value); else { S_free((*h)->value); (*h)->value=S_dup(value); } return; } h=&((*h)->next); } //*h==NULL: create new entry *h=(struct d_var_struct *)malloc(sizeof(struct d_var_struct)); (*h)->name=S_dup(name); (*h)->value=S_dup(value); (*h)->next=NULL; } #define d_var_push_local(a,b) d_var_push((d_var_local(a)),b) void d_var_push_int(char *name, int a) { char value[20]; sprintf(value,"%d",a); d_var_push(name, value); } void d_var_push_double(char *name, double a) { char value[20]; S_dtos(a,value); d_var_push(name, value); } char *d_var_value(char *name) { struct d_var_struct *h; h=d_var_root; while(h) { if(Gelijk(name,h->name)) { return(h->value); } h=h->next; } //h==NULL: variable entry not in list return(NULL); } char *d_var_value_any(char *name) //reads variable contents local, if not, global { char *a,*b; b=d_var_local(name); a=d_var_value(b); // printf("{%s=%s}",b,a); if(!a)a=d_var_value(name); return(a); } char *d_var_value_local(char *name) //reads variable contents local { char *a,*b; b=d_var_local(name); a=d_var_value(b); return(a); } int d_var_value_int(char *name) {return(atoi(d_var_value(name)));} void d_var_push_any(char *a, char *b) //pushes first local if declared, otherwise anyway in global { if(d_var_locallevel>0) { if(d_var_value_local(a)) { d_var_push_local(a,b); return; } } d_var_push(a,b); } void d_var_list(void) { struct d_var_struct *h; h=d_var_root; while(h) { printf("[%s]:%s\n",h->name,h->value); h=h->next; } } #define D_VAR_INCLUDED #endif