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;
41 obj_new(struct mempool *pool)
43 struct mempool *lp = pool;
47 lp = mp_new(OBJ_POOL_SIZE);
48 o = mp_alloc(lp, sizeof(struct odes));
50 o->local_pool = (pool == lp) ? NULL : lp;
52 o->cached_attr = NULL;
57 obj_free(struct odes *o)
60 mp_delete(o->local_pool);
64 obj_read(struct fastbuf *f, struct odes *o)
68 while (bgets(f, buf, sizeof(buf)))
72 obj_add_attr(o, buf[0], buf+1);
78 obj_write(struct fastbuf *f, struct odes *d)
83 for(a=d->attrs; a; a=a->next)
84 for(b=a; b; b=b->same)
87 for(z = b->val; *z; z++)
88 if (*z >= ' ' || *z == '\t')
93 log(L_ERROR, "obj_dump: Found non-ASCII characters (URL might be %s)", obj_find_aval(d, 'U'));
100 obj_find_attr(struct odes *o, uns x)
104 for(a=o->attrs; a && a->attr != x; a=a->next)
110 obj_find_attr_last(struct odes *o, uns x)
112 struct oattr *a = obj_find_attr(o, x);
123 obj_del_attr(struct odes *o, struct oattr *a)
125 struct oattr *x, **p, *y, *l;
128 o->cached_attr = NULL;
154 obj_find_aval(struct odes *o, uns x)
156 struct oattr *a = obj_find_attr(o, x);
158 return a ? a->val : NULL;
162 obj_set_attr(struct odes *o, uns x, byte *v)
164 struct oattr *a, **z;
191 obj_set_attr_num(struct odes *o, uns a, uns v)
196 return obj_set_attr(o, a, x);
200 obj_add_attr(struct odes *o, uns x, byte *v)
205 if (!(a = o->cached_attr) || a->attr != x)
207 if (!(a = obj_find_attr(o, x)))
223 obj_prepend_attr(struct odes *o, uns x, byte *v)
225 struct oattr *a, *b, **z;
247 obj_insert_attr(struct odes *o, struct oattr *first, struct oattr *after, byte *v)
251 b = oa_new(o, first->attr, v);
252 b->same = after->same;