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/mempool.h"
13 #include "lib/fastbuf.h"
14 #include "lib/object.h"
21 obj_dump(struct odes *o)
23 for(struct oattr *a=o->attrs; a; a=a->next)
24 for(struct oattr *b=a; b; b=b->same)
25 printf("%c%s\n", (a==b ? a->attr : ' '), b->val);
29 oa_new(struct odes *o, uns x, byte *v)
31 struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr) + strlen(v)+1);
33 a->next = a->same = NULL;
35 a->val = (byte*) (a+1);
41 oa_new_ref(struct odes *o, uns x, byte *v)
43 struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr));
45 a->next = a->same = NULL;
52 obj_new(struct mempool *pool)
54 struct odes *o = mp_alloc(pool, sizeof(struct odes));
57 o->cached_attr = NULL;
62 obj_find_attr(struct odes *o, uns x)
65 for(a=o->attrs; a && a->attr != x; a=a->next)
71 obj_find_attr_last(struct odes *o, uns x)
73 struct oattr *a = obj_find_attr(o, x);
84 obj_del_attr(struct odes *o, struct oattr *a)
86 struct oattr *x, **p, *y, *l;
89 o->cached_attr = NULL;
115 obj_find_aval(struct odes *o, uns x)
117 struct oattr *a = obj_find_attr(o, x);
118 return a ? a->val : NULL;
122 obj_find_anum(struct odes *o, uns x, uns def)
124 struct oattr *a = obj_find_attr(o, x);
125 return a ? (uns)atol(a->val) : def;
129 obj_set_attr(struct odes *o, uns x, byte *v)
131 struct oattr *a, **z;
158 obj_set_attr_num(struct odes *o, uns a, uns v)
163 return obj_set_attr(o, a, x);
166 static inline struct oattr *
167 obj_add_attr_internal(struct odes *o, struct oattr *b)
169 struct oattr *a, **z;
171 if (!(a = o->cached_attr) || a->attr != b->attr)
174 while ((a = *z) && a->attr != b->attr)
179 /* b->next is NULL */
192 obj_add_attr(struct odes *o, uns x, byte *v)
194 return obj_add_attr_internal(o, oa_new(o, x, v));
198 obj_add_attr_ref(struct odes *o, uns x, byte *v)
200 return obj_add_attr_internal(o, oa_new_ref(o, x, v));
204 obj_prepend_attr(struct odes *o, uns x, byte *v)
206 struct oattr *a, *b, **z;
228 obj_insert_attr(struct odes *o, struct oattr *first, struct oattr *after, byte *v)
230 struct oattr *b = oa_new(o, first->attr, v);
231 b->same = after->same;
237 obj_move_attr_to_head(struct odes *o, uns x)
239 struct oattr *a, **z;
256 obj_move_attr_to_tail(struct odes *o, uns x)
258 struct oattr *a, **z;