2 * Sherlock Library -- Object Functions
4 * (c) 1997--2001 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "lib/pools.h"
12 #include "lib/fastbuf.h"
17 #define OBJ_POOL_SIZE 4096
20 obj_dump(struct odes *o)
24 for(a=o->attrs; a; a=a->next)
25 for(b=a; b; b=b->same)
26 printf("%c%s\n", (a==b ? a->attr : ' '), b->val);
30 oa_new(struct odes *o, uns x, byte *v)
32 struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr) + strlen(v));
34 a->next = a->same = NULL;
42 obj_new(struct mempool *pool)
44 struct mempool *lp = pool;
48 lp = mp_new(OBJ_POOL_SIZE);
49 o = mp_alloc(lp, sizeof(struct odes));
51 o->local_pool = (pool == lp) ? NULL : lp;
57 obj_free(struct odes *o)
60 mp_delete(o->local_pool);
64 obj_read(struct fastbuf *f, struct odes *o)
67 struct oattr **last = &o->attrs;
72 while (bgets(f, buf, sizeof(buf)))
76 a = oa_new(o, buf[0], buf+1);
77 if (!la || la->attr != a->attr)
78 for(la=o->attrs; la && la->attr != a->attr; la=la->next)
82 la->last_same->same = a;
96 obj_write(struct fastbuf *f, struct odes *d)
101 for(a=d->attrs; a; a=a->next)
102 for(b=a; b; b=b->same)
105 for(z = b->val; *z; z++)
111 log(L_ERROR, "obj_dump: Found non-ASCII characters (URL might be %s)", obj_find_aval(d, 'U'));
118 obj_find_attr(struct odes *o, uns x)
122 for(a=o->attrs; a && a->attr != x; a=a->next)
128 obj_find_attr_last(struct odes *o, uns x)
130 struct oattr *a = obj_find_attr(o, x);
132 return a ? a->last_same : NULL;
136 obj_del_attr(struct odes *o, struct oattr *a)
138 struct oattr *x, **p, *y, *l;
153 if (y->last_same == x)
168 obj_find_aval(struct odes *o, uns x)
170 struct oattr *a = obj_find_attr(o, x);
172 return a ? a->val : NULL;
176 obj_set_attr(struct odes *o, uns x, byte *v)
178 struct oattr *a, **z;
204 obj_set_attr_num(struct odes *o, uns a, uns v)
209 return obj_set_attr(o, a, x);
213 obj_add_attr(struct odes *o, struct oattr *a, uns x, byte *v)
219 a = obj_find_attr(o, x);
221 return obj_set_attr(o, x, v);
224 a->last_same->same = b;
230 obj_prepend_attr(struct odes *o, uns x, byte *v)
232 struct oattr *a, *b, **z;
244 b->last_same = a->last_same;
255 obj_insert_attr(struct odes *o, struct oattr *first, struct oattr *after, byte *v)
259 b = oa_new(o, first->attr, v);
260 b->same = after->same;
262 if (first->last_same == after)
263 first->last_same = b;