2 * Sherlock Library -- Object Functions
4 * (c) 1997--2006 Martin Mares <mj@ucw.cz>
5 * (c) 2004 Robert Spalek <robert@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
11 #include "sherlock/sherlock.h"
12 #include "ucw/mempool.h"
13 #include "ucw/fastbuf.h"
14 #include "sherlock/object.h"
21 obj_dump(struct odes *d)
23 for(struct oattr *a=d->attrs; a; a=a->next)
24 for(struct oattr *b=a; b; b=b->same)
25 if (a->attr >= OBJ_ATTR_SON)
27 printf("(%c\n", a->attr - OBJ_ATTR_SON);
32 printf("%c%s\n", (a==b ? a->attr : ' '), b->val);
36 obj_dump_indented(struct odes *d, uns indent)
38 for(struct oattr *a=d->attrs; a; a=a->next)
39 for(struct oattr *b=a; b; b=b->same)
41 for (uns i=0; i<indent; i++)
43 if (a->attr >= OBJ_ATTR_SON)
45 printf("(%c\n", a->attr - OBJ_ATTR_SON);
46 obj_dump_indented(b->son, indent+1);
47 for (uns i=0; i<=indent; i++)
52 printf("%c%s\n", (a==b ? a->attr : ' '), b->val);
57 oa_new(struct odes *o, uns x, byte *v)
60 struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr) + l);
62 a->next = a->same = NULL;
64 a->val = (byte*) (a+1);
70 oa_new_ref(struct odes *o, uns x, byte *v)
72 struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr));
74 a->next = a->same = NULL;
81 oa_new_son(struct odes *o, uns x, struct odes *son)
83 struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr));
85 a->next = a->same = NULL;
93 obj_new(struct mempool *pool)
95 struct odes *o = mp_alloc(pool, sizeof(struct odes));
98 o->cached_attr = NULL;
104 obj_find_attr(struct odes *o, uns x)
107 for(a=o->attrs; a && a->attr != x; a=a->next)
113 obj_find_attr_last(struct odes *o, uns x)
115 struct oattr *a = obj_find_attr(o, x);
126 obj_del_attr(struct odes *o, struct oattr *a)
128 struct oattr *x, **p;
131 o->cached_attr = NULL;
141 x->same->next = x->next;
166 obj_find_aval(struct odes *o, uns x)
168 struct oattr *a = obj_find_attr(o, x);
169 return a ? a->val : NULL;
173 obj_find_anum(struct odes *o, uns x, uns def)
175 struct oattr *a = obj_find_attr(o, x);
176 return a ? (uns)atol(a->val) : def;
180 obj_find_x32(struct odes *o, uns x, u32 def)
182 struct oattr *a = obj_find_attr(o, x);
183 return a ? (u32)strtoul(a->val, NULL, 16) : def;
187 obj_find_x64(struct odes *o, uns x, u64 def)
189 struct oattr *a = obj_find_attr(o, x);
190 return a ? (u64)strtoull(a->val, NULL, 16) : def;
194 obj_set_attr(struct odes *o, uns x, byte *v)
196 struct oattr *a, **z;
223 obj_set_attr_num(struct odes *o, uns a, uns v)
228 return obj_set_attr(o, a, x);
231 static inline struct oattr *
232 obj_link_attr(struct odes *o, struct oattr *b)
234 struct oattr *a, **z;
236 if (!(a = o->cached_attr) || a->attr != b->attr)
239 while ((a = *z) && a->attr != b->attr)
244 /* b->next is NULL */
257 obj_add_attr(struct odes *o, uns x, byte *v)
259 return obj_link_attr(o, oa_new(o, x, v));
263 obj_add_attr_ref(struct odes *o, uns x, byte *v)
265 return obj_link_attr(o, oa_new_ref(o, x, v));
269 obj_add_attr_num(struct odes *o, uns a, uns v)
274 return obj_add_attr(o, a, x);
278 obj_add_attr_son(struct odes *o, uns x, struct odes *son)
280 return obj_link_attr(o, oa_new_son(o, x, son));
284 obj_prepend_attr(struct odes *o, uns x, byte *v)
286 struct oattr *a, *b, **z;
308 obj_insert_attr(struct odes *o, struct oattr *first, struct oattr *after, byte *v)
310 struct oattr *b = oa_new(o, first->attr, v);
311 b->same = after->same;
317 obj_move_attr_to_head(struct odes *o, uns x)
319 struct oattr *a, **z;
336 obj_move_attr_to_tail(struct odes *o, uns x)
338 struct oattr *a, **z;
357 obj_find_son(struct odes *o, uns x)
359 ASSERT(x >= OBJ_ATTR_SON);
360 struct oattr *a = obj_find_attr(o, x);
361 return a ? a->son : NULL;
365 obj_add_son_ref(struct odes *o, uns x, struct odes *son)
367 struct oattr *oa = oa_new_son(o, x, son);
368 obj_link_attr(o, oa);
373 obj_add_son(struct odes *o, uns x)
375 struct odes *son = obj_new(o->pool);
376 obj_add_son_ref(o, x, son);
380 static void obj_clone_attr_list(struct odes *dest, struct odes *src);
382 static struct oattr *
383 obj_clone_attr(struct odes *dest, struct oattr *a)
385 struct oattr *res = NULL, **rr = &res;
386 if (a->attr < OBJ_ATTR_SON)
390 *rr = oa_new(dest, a->attr, a->val);
398 struct odes *dson = obj_new(dest->pool);
399 *rr = oa_new_son(dest, a->attr, dson);
401 obj_clone_attr_list(dson, a->son);
408 obj_clone_attr_list(struct odes *dest, struct odes *src)
410 struct oattr **p = &dest->attrs;
411 for (struct oattr *a = src->attrs; a; a=a->next)
413 *p = obj_clone_attr(dest, a);
419 obj_add_attr_clone(struct odes *o, struct oattr *a)
421 obj_link_attr(o, obj_clone_attr(o, a));
425 obj_clone(struct mempool *pool, struct odes *src)
427 struct odes *dest = obj_new(pool);
428 obj_clone_attr_list(dest, src);