BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file NCDObject.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29  
30 #include <misc/offset.h>
31 #include <misc/debug.h>
32 #include <ncd/NCDVal.h>
33  
34 #include "NCDObject.h"
35  
36 int NCDObject_no_getvar (const NCDObject *obj, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out_value)
37 {
38 return 0;
39 }
40  
41 int NCDObject_no_getobj (const NCDObject *obj, NCD_string_id_t name, NCDObject *out_object)
42 {
43 return 0;
44 }
45  
46 NCDObject NCDObject_Build (NCD_string_id_t type, void *data_ptr, NCDObject_func_getvar func_getvar, NCDObject_func_getobj func_getobj)
47 {
48 ASSERT(type >= -1)
49 ASSERT(func_getvar)
50 ASSERT(func_getobj)
51  
52 NCDObject obj;
53 obj.type = type;
54 obj.data_int = 0;
55 obj.data_ptr = data_ptr;
56 obj.method_user = data_ptr;
57 obj.func_getvar = func_getvar;
58 obj.func_getobj = func_getobj;
59 obj.pobj = NULL;
60  
61 return obj;
62 }
63  
64 NCDObject NCDObject_BuildFull (NCD_string_id_t type, void *data_ptr, int data_int, void *method_user, NCDObject_func_getvar func_getvar, NCDObject_func_getobj func_getobj, NCDPersistentObj *pobj)
65 {
66 ASSERT(type >= -1)
67 ASSERT(func_getvar)
68 ASSERT(func_getobj)
69  
70 NCDObject obj;
71 obj.type = type;
72 obj.data_int = data_int;
73 obj.data_ptr = data_ptr;
74 obj.method_user = method_user;
75 obj.func_getvar = func_getvar;
76 obj.func_getobj = func_getobj;
77 obj.pobj = pobj;
78  
79 return obj;
80 }
81  
82 NCD_string_id_t NCDObject_Type (const NCDObject *o)
83 {
84 return o->type;
85 }
86  
87 void * NCDObject_DataPtr (const NCDObject *o)
88 {
89 return o->data_ptr;
90 }
91  
92 int NCDObject_DataInt (const NCDObject *o)
93 {
94 return o->data_int;
95 }
96  
97 void * NCDObject_MethodUser (const NCDObject *o)
98 {
99 return o->method_user;
100 }
101  
102 int NCDObject_GetVar (const NCDObject *o, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out_value)
103 {
104 ASSERT(name >= 0)
105 ASSERT(mem)
106 ASSERT(out_value)
107  
108 int res = o->func_getvar(o, name, mem, out_value);
109  
110 ASSERT(res == 0 || res == 1)
111 ASSERT(res == 0 || (NCDVal_Assert(*out_value), 1))
112  
113 return res;
114 }
115  
116 int NCDObject_GetObj (const NCDObject *o, NCD_string_id_t name, NCDObject *out_object)
117 {
118 ASSERT(name >= 0)
119 ASSERT(out_object)
120  
121 int res = o->func_getobj(o, name, out_object);
122  
123 ASSERT(res == 0 || res == 1)
124  
125 return res;
126 }
127  
128 NCDPersistentObj * NCDObject_Pobj (const NCDObject *o)
129 {
130 return o->pobj;
131 }
132  
133 static NCDObject NCDObject__dig_into_object (NCDObject object)
134 {
135 NCDObject obj2;
136 while (NCDObject_GetObj(&object, NCD_STRING_EMPTY, &obj2)) {
137 object = obj2;
138 }
139  
140 return object;
141 }
142  
143 int NCDObject_ResolveVarExprCompact (const NCDObject *o, const NCD_string_id_t *names, size_t num_names, NCDValMem *mem, NCDValRef *out_value)
144 {
145 ASSERT(num_names == 0 || names)
146 ASSERT(mem)
147 ASSERT(out_value)
148  
149 NCDObject object = NCDObject__dig_into_object(*o);
150  
151 while (num_names > 0) {
152 NCDObject obj2;
153 if (!NCDObject_GetObj(&object, *names, &obj2)) {
154 if (num_names == 1 && NCDObject_GetVar(&object, *names, mem, out_value)) {
155 return 1;
156 }
157  
158 return 0;
159 }
160  
161 object = NCDObject__dig_into_object(obj2);
162  
163 names++;
164 num_names--;
165 }
166  
167 return NCDObject_GetVar(&object, NCD_STRING_EMPTY, mem, out_value);
168 }
169  
170 int NCDObject_ResolveObjExprCompact (const NCDObject *o, const NCD_string_id_t *names, size_t num_names, NCDObject *out_object)
171 {
172 ASSERT(num_names == 0 || names)
173 ASSERT(out_object)
174  
175 NCDObject object = NCDObject__dig_into_object(*o);
176  
177 while (num_names > 0) {
178 NCDObject obj2;
179 if (!NCDObject_GetObj(&object, *names, &obj2)) {
180 return 0;
181 }
182  
183 object = NCDObject__dig_into_object(obj2);
184  
185 names++;
186 num_names--;
187 }
188  
189 *out_object = object;
190 return 1;
191 }
192  
193 void NCDPersistentObj_Init (NCDPersistentObj *o, NCDPersistentObj_func_retobj func_retobj)
194 {
195 ASSERT(func_retobj)
196  
197 o->func_retobj = func_retobj;
198 LinkedList0_Init(&o->refs_list);
199 }
200  
201 void NCDPersistentObj_Free (NCDPersistentObj *o)
202 {
203 for (LinkedList0Node *ln = LinkedList0_GetFirst(&o->refs_list); ln; ln = LinkedList0Node_Next(ln)) {
204 NCDObjRef *ref = UPPER_OBJECT(ln, NCDObjRef, refs_list_node);
205 ASSERT(ref->pobj == o)
206 ref->pobj = NULL;
207 }
208 }
209  
210 void NCDObjRef_Init (NCDObjRef *o, NCDPersistentObj *pobj)
211 {
212 o->pobj = pobj;
213 if (o->pobj) {
214 LinkedList0_Prepend(&o->pobj->refs_list, &o->refs_list_node);
215 }
216 }
217  
218 void NCDObjRef_Free (NCDObjRef *o)
219 {
220 if (o->pobj) {
221 LinkedList0_Remove(&o->pobj->refs_list, &o->refs_list_node);
222 }
223 }
224  
225 int NCDObjRef_Deref (NCDObjRef *o, NCDObject *res)
226 {
227 ASSERT(res)
228  
229 if (!o->pobj) {
230 return 0;
231 }
232 *res = o->pobj->func_retobj(o->pobj);
233 return 1;
234 }