2 * Sherlock Library -- Object Functions
4 * (c) 1997--2003 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"
13 #include "lib/object.h"
19 obj_dump(struct odes *o)
21 for(struct oattr *a=o->attrs; a; a=a->next)
22 for(struct oattr *b=a; b; b=b->same)
23 printf("%c%s\n", (a==b ? a->attr : ' '), b->val);
27 oa_new(struct odes *o, uns x, byte *v)
29 struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr) + strlen(v));
31 a->next = a->same = NULL;
38 obj_new(struct mempool *pool)
40 struct odes *o = mp_alloc(pool, sizeof(struct odes));
43 o->cached_attr = NULL;
48 obj_read(struct fastbuf *f, struct odes *o)
50 byte buf[MAX_ATTR_SIZE];
52 while (bgets(f, buf, sizeof(buf)))
56 obj_add_attr(o, buf[0], buf+1);
62 obj_write(struct fastbuf *f, struct odes *d)
64 for(struct oattr *a=d->attrs; a; a=a->next)
65 for(struct oattr *b=a; b; b=b->same)
69 for(z = b->val; *z; z++)
70 if (*z >= ' ' || *z == '\t')
75 log(L_ERROR, "obj_dump: Found non-ASCII characters (URL might be %s)", obj_find_aval(d, 'U'));
77 ASSERT(z - b->val <= MAX_ATTR_SIZE-2);
83 obj_write_nocheck(struct fastbuf *f, struct odes *d)
85 for(struct oattr *a=d->attrs; a; a=a->next)
86 for(struct oattr *b=a; b; b=b->same)
94 obj_find_attr(struct odes *o, uns x)
97 for(a=o->attrs; a && a->attr != x; a=a->next)
103 obj_find_attr_last(struct odes *o, uns x)
105 struct oattr *a = obj_find_attr(o, x);
116 obj_del_attr(struct odes *o, struct oattr *a)
118 struct oattr *x, **p, *y, *l;
121 o->cached_attr = NULL;
147 obj_find_aval(struct odes *o, uns x)
149 struct oattr *a = obj_find_attr(o, x);
151 return a ? a->val : NULL;
155 obj_set_attr(struct odes *o, uns x, byte *v)
157 struct oattr *a, **z;
184 obj_set_attr_num(struct odes *o, uns a, uns v)
189 return obj_set_attr(o, a, x);
193 obj_add_attr(struct odes *o, uns x, byte *v)
198 if (!(a = o->cached_attr) || a->attr != x)
200 if (!(a = obj_find_attr(o, x)))
216 obj_prepend_attr(struct odes *o, uns x, byte *v)
218 struct oattr *a, *b, **z;
240 obj_insert_attr(struct odes *o, struct oattr *first, struct oattr *after, byte *v)
242 struct oattr *b = oa_new(o, first->attr, v);
243 b->same = after->same;
249 obj_move_attr_to_head(struct odes *o, uns x)
251 struct oattr *a, **z;
268 obj_move_attr_to_tail(struct odes *o, uns x)
270 struct oattr *a, **z;