source: libabac/abac_param.c @ 202a7f9

mei_rt2mei_rt2_fix_1meiyap-rt1rt2
Last change on this file since 202a7f9 was 718ad924, checked in by Mei <mei@…>, 12 years ago

able to parse rt1 without condition
updated examples

  • Property mode set to 100644
File size: 4.6 KB
Line 
1
2/***********************************************************
3   abac_param.c
4***********************************************************/
5#include <assert.h>
6#include <stdlib.h>
7#include <stdio.h>
8#include <string.h>
9
10#include "abac_list.h"
11#include "abac_util.h"
12
13#include "abac_pl.h"
14
15struct _abac_condition_t {
16    char *string;
17};
18
19struct _abac_param_t {
20    int type;
21    char *name;
22    abac_condition_t *constraint;
23}; 
24
25/* new, free, add one */
26struct _abac_param_list_t {
27    abac_list_t *list;
28};
29
30/******************************************************************/
31
32static abac_condition_t *abac_condition_new(char *condition)
33{
34     abac_condition_t *ptr=abac_xmalloc(sizeof(abac_condition_t));
35     ptr->string=condition;
36     return ptr;
37}
38
39static void abac_condition_free(abac_condition_t *ptr)
40{
41     free(ptr->string);
42     free(ptr);
43}
44
45abac_param_t *abac_param_new(int type, char *name, char *cond)
46{
47     abac_condition_t *constraint=NULL;
48     if (cond) {
49         constraint=abac_condition_new(cond);
50     }
51     abac_param_t *ptr=abac_xmalloc(sizeof(abac_param_t));
52     ptr->type=type;
53     ptr->name=name;
54     ptr->constraint=constraint;
55     return ptr;
56}
57
58void abac_param_free(abac_param_t *ptr)
59{
60    if(ptr->constraint) {
61         abac_condition_free(ptr->constraint);
62    }
63    free(ptr->name);
64    free(ptr);
65}
66
67abac_param_list_t *abac_param_list_new(abac_param_t *param)
68{
69    abac_param_list_t *ptr=abac_xmalloc(sizeof(abac_param_list_t));
70    ptr->list=abac_list_new();
71    abac_list_add(ptr->list, param);
72    return ptr;
73}
74
75abac_param_list_t *abac_param_list_free(abac_param_list_t *ptr)
76{
77    abac_list_t *list=ptr->list;
78    abac_param_t  *cur;
79    abac_list_foreach(list, cur,
80           abac_param_free(cur);
81    );
82    abac_list_free(list);
83    free(ptr);
84}
85
86abac_param_list_t *abac_param_list_add_param(abac_param_list_t *ptr, abac_param_t *param)
87{
88    abac_list_t *list=ptr->list;
89    abac_list_add(list, param);
90    return ptr;
91}
92
93abac_param_t *abac_param_add_constraint(abac_param_t *ptr, abac_condition_t *cond)
94{
95    if(ptr->constraint) {
96        abac_condition_free(ptr->constraint);
97    }
98    ptr->constraint = cond;
99    return ptr;
100}
101
102/* name or name:cond */
103static char *abac_param_string_with_condition(abac_param_t *ptr)
104{
105    char *tmp=NULL;
106    char *cond=NULL;
107    char *name=ptr->name;
108    int len=strlen(name);
109    if(ptr->constraint) {
110       cond=ptr->constraint->string;
111       len=len+strlen(cond)+1;
112    }
113    tmp = abac_xmalloc(len);
114    if(cond)
115       sprintf(tmp,"%s:%s", name, cond);
116       else
117           sprintf(tmp,"%s", name);
118    return tmp;
119}
120
121/* len of name or name:cond */
122static int abac_param_len_with_condition(abac_param_t *ptr)
123{
124    char *cond=NULL;
125    char *name=ptr->name;
126    int len=strlen(name);
127    if(ptr->constraint) {
128       cond=ptr->constraint->string;
129       len=len+strlen(cond)+1;
130    }
131    return len;
132}
133
134/* just name */
135static char *abac_param_string(abac_param_t *ptr)
136{
137    return strdup(ptr->name);
138}
139
140/* just len of name */
141static int abac_param_len(abac_param_t *ptr)
142{
143    return strlen(ptr->name);
144}
145
146/* param1:cond1,param2:cond2 */
147char* abac_param_list_string_with_condition(abac_param_list_t *ptr)
148{
149    if(ptr->list == NULL)
150        return "";
151
152    abac_param_t  *cur;
153    char *tmp;
154    int len=0;
155    /* collect up all the lens so can allocate once */
156    abac_list_foreach(ptr->list, cur,
157           int l=abac_param_len_with_condition(cur);
158           len=len+l+1;
159    );
160    tmp=abac_xmalloc(len * sizeof(char));
161    /* collect up all the string */
162    int first=1;
163    abac_list_foreach(ptr->list, cur,
164           char *s=abac_param_string_with_condition(cur);
165           if(first) {
166               strcpy(tmp,s);
167               first=0;
168               } else {
169                  strcat(tmp,",");
170                  strcat(tmp,s);
171           }
172           free(s);
173    );
174    return tmp;
175}
176
177/* param1,param2 */
178char* abac_param_list_string(abac_param_list_t *ptr)
179{
180    abac_list_t *list=ptr->list;
181    if(list == NULL)
182        return "";
183    abac_param_t  *cur;
184    char *tmp;
185    int len=0;
186    /* collect up all the lens so can allocate once */
187    abac_list_foreach(list, cur,
188           // collect up the len
189           int l=abac_param_len(cur);
190           len=len+l+1;
191    );
192    tmp=abac_xmalloc(len * sizeof(char));
193    /* collect up all the string */
194    int first=1;
195    abac_list_foreach(list, cur,
196           char *s=abac_param_string(cur);
197           if(first) {
198               strcpy(tmp,s);
199               first=0;
200               } else {
201                  strcat(tmp,",");
202                  strcat(tmp,s);
203           }
204           free(s);
205    );
206    return tmp;
207}
Note: See TracBrowser for help on using the repository browser.