2 * Sherlock Library -- Object Functions
4 * (c) 1997--2004 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.
12 #include "lib/pools.h"
13 #include "lib/fastbuf.h"
14 #include "lib/object.h"
20 obj_dump(struct odes *o)
22 for(struct oattr *a=o->attrs; a; a=a->next)
23 for(struct oattr *b=a; b; b=b->same)
24 printf("%c%s\n", (a==b ? a->attr : ' '), b->val);
28 oa_new(struct odes *o, uns x, byte *v)
30 struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr) + strlen(v)+1);
32 a->next = a->same = NULL;
34 a->val = (byte*) (a+1);
40 oa_new_ref(struct odes *o, uns x, byte *v)
42 struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr));
44 a->next = a->same = NULL;
51 obj_new(struct mempool *pool)
53 struct odes *o = mp_alloc(pool, sizeof(struct odes));
56 o->cached_attr = NULL;
61 obj_find_attr(struct odes *o, uns x)
64 for(a=o->attrs; a && a->attr != x; a=a->next)
70 obj_find_attr_last(struct odes *o, uns x)
72 struct oattr *a = obj_find_attr(o, x);
83 obj_del_attr(struct odes *o, struct oattr *a)
85 struct oattr *x, **p, *y, *l;
88 o->cached_attr = NULL;
114 obj_find_aval(struct odes *o, uns x)
116 struct oattr *a = obj_find_attr(o, x);
117 return a ? a->val : NULL;
121 obj_set_attr(struct odes *o, uns x, byte *v)
123 struct oattr *a, **z;
150 obj_set_attr_num(struct odes *o, uns a, uns v)
155 return obj_set_attr(o, a, x);
158 static inline struct oattr *
159 obj_add_attr_internal(struct odes *o, struct oattr *b)
163 if (!(a = o->cached_attr) || a->attr != b->attr)
165 if (!(a = obj_find_attr(o, b->attr)))
181 obj_add_attr(struct odes *o, uns x, byte *v)
183 return obj_add_attr_internal(o, oa_new(o, x, v));
187 obj_add_attr_ref(struct odes *o, uns x, byte *v)
189 return obj_add_attr_internal(o, oa_new_ref(o, x, v));
193 obj_prepend_attr(struct odes *o, uns x, byte *v)
195 struct oattr *a, *b, **z;
217 obj_insert_attr(struct odes *o, struct oattr *first, struct oattr *after, byte *v)
219 struct oattr *b = oa_new(o, first->attr, v);
220 b->same = after->same;
226 obj_move_attr_to_head(struct odes *o, uns x)
228 struct oattr *a, **z;
245 obj_move_attr_to_tail(struct odes *o, uns x)
247 struct oattr *a, **z;