/*********************************************************** abac_param.c ***********************************************************/ #include #include #include #include #include "abac_list.h" #include "abac_util.h" #include "abac_pl.h" struct _abac_condition_t { char *string; }; struct _abac_param_t { int type; char *name; abac_condition_t *constraint; }; /* new, free, add one */ struct _abac_param_list_t { abac_list_t *list; }; /******************************************************************/ static abac_condition_t *abac_condition_new(char *condition) { abac_condition_t *ptr=abac_xmalloc(sizeof(abac_condition_t)); ptr->string=condition; return ptr; } static void abac_condition_free(abac_condition_t *ptr) { free(ptr->string); free(ptr); } abac_param_t *abac_param_new(int type, char *name, char *cond) { abac_condition_t *constraint=NULL; if (cond) { constraint=abac_condition_new(cond); } abac_param_t *ptr=abac_xmalloc(sizeof(abac_param_t)); ptr->type=type; ptr->name=name; ptr->constraint=constraint; return ptr; } void abac_param_free(abac_param_t *ptr) { if(ptr->constraint) { abac_condition_free(ptr->constraint); } free(ptr->name); free(ptr); } abac_param_list_t *abac_param_list_new(abac_param_t *param) { abac_param_list_t *ptr=abac_xmalloc(sizeof(abac_param_list_t)); ptr->list=abac_list_new(); abac_list_add(ptr->list, param); return ptr; } abac_param_list_t *abac_param_list_free(abac_param_list_t *ptr) { abac_list_t *list=ptr->list; abac_param_t *cur; abac_list_foreach(list, cur, abac_param_free(cur); ); abac_list_free(list); free(ptr); } abac_param_list_t *abac_param_list_add_param(abac_param_list_t *ptr, abac_param_t *param) { abac_list_t *list=ptr->list; abac_list_add(list, param); return ptr; } abac_param_t *abac_param_add_constraint(abac_param_t *ptr, abac_condition_t *cond) { if(ptr->constraint) { abac_condition_free(ptr->constraint); } ptr->constraint = cond; return ptr; } /* name or name:cond */ static char *abac_param_string_with_condition(abac_param_t *ptr) { char *tmp=NULL; char *cond=NULL; char *name=ptr->name; int len=strlen(name); if(ptr->constraint) { cond=ptr->constraint->string; len=len+strlen(cond)+1; } tmp = abac_xmalloc(len); if(cond) sprintf(tmp,"%s:%s", name, cond); else sprintf(tmp,"%s", name); return tmp; } /* len of name or name:cond */ static int abac_param_len_with_condition(abac_param_t *ptr) { char *cond=NULL; char *name=ptr->name; int len=strlen(name); if(ptr->constraint) { cond=ptr->constraint->string; len=len+strlen(cond)+1; } return len; } /* just name */ static char *abac_param_string(abac_param_t *ptr) { return strdup(ptr->name); } /* just len of name */ static int abac_param_len(abac_param_t *ptr) { return strlen(ptr->name); } /* param1:cond1,param2:cond2 */ char* abac_param_list_string_with_condition(abac_param_list_t *ptr) { if(ptr->list == NULL) return ""; abac_param_t *cur; char *tmp; int len=0; /* collect up all the lens so can allocate once */ abac_list_foreach(ptr->list, cur, int l=abac_param_len_with_condition(cur); len=len+l+1; ); tmp=abac_xmalloc(len * sizeof(char)); /* collect up all the string */ int first=1; abac_list_foreach(ptr->list, cur, char *s=abac_param_string_with_condition(cur); if(first) { strcpy(tmp,s); first=0; } else { strcat(tmp,","); strcat(tmp,s); } free(s); ); return tmp; } /* param1,param2 */ char* abac_param_list_string(abac_param_list_t *ptr) { abac_list_t *list=ptr->list; if(list == NULL) return ""; abac_param_t *cur; char *tmp; int len=0; /* collect up all the lens so can allocate once */ abac_list_foreach(list, cur, // collect up the len int l=abac_param_len(cur); len=len+l+1; ); tmp=abac_xmalloc(len * sizeof(char)); /* collect up all the string */ int first=1; abac_list_foreach(list, cur, char *s=abac_param_string(cur); if(first) { strcpy(tmp,s); first=0; } else { strcat(tmp,","); strcat(tmp,s); } free(s); ); return tmp; }