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)+1);
31 a->next = a->same = NULL;
33 a->val = (byte*) (a+1);
39 oa_new_ref(struct odes *o, uns x, byte *v)
41 struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr));
43 a->next = a->same = NULL;
50 obj_new(struct mempool *pool)
52 struct odes *o = mp_alloc(pool, sizeof(struct odes));
55 o->cached_attr = NULL;
60 obj_read(struct fastbuf *f, struct odes *o)
62 byte buf[MAX_ATTR_SIZE];
64 while (bgets(f, buf, sizeof(buf)))
68 obj_add_attr(o, buf[0], buf+1);
74 obj_read_multi(struct fastbuf *f, struct odes *o)
76 /* Read a multi-part object ending with either EOF or a NUL character */
77 byte buf[MAX_ATTR_SIZE];
78 while (bpeekc(f) > 0 && bgets(f, buf, sizeof(buf)))
80 obj_add_attr(o, buf[0], buf+1);
84 obj_write(struct fastbuf *f, struct odes *d)
86 for(struct oattr *a=d->attrs; a; a=a->next)
87 for(struct oattr *b=a; b; b=b->same)
91 for(z = b->val; *z; z++)
92 if (*z >= ' ' || *z == '\t')
97 log(L_ERROR, "obj_dump: Found non-ASCII characters (URL might be %s)", obj_find_aval(d, 'U'));
99 ASSERT(z - b->val <= MAX_ATTR_SIZE-2);
105 obj_write_nocheck(struct fastbuf *f, struct odes *d)
107 for(struct oattr *a=d->attrs; a; a=a->next)
108 for(struct oattr *b=a; b; b=b->same)
116 obj_find_attr(struct odes *o, uns x)
119 for(a=o->attrs; a && a->attr != x; a=a->next)
125 obj_find_attr_last(struct odes *o, uns x)
127 struct oattr *a = obj_find_attr(o, x);
138 obj_del_attr(struct odes *o, struct oattr *a)
140 struct oattr *x, **p, *y, *l;
143 o->cached_attr = NULL;
169 obj_find_aval(struct odes *o, uns x)
171 struct oattr *a = obj_find_attr(o, x);
173 return a ? a->val : NULL;
177 obj_set_attr(struct odes *o, uns x, byte *v)
179 struct oattr *a, **z;
206 obj_set_attr_num(struct odes *o, uns a, uns v)
211 return obj_set_attr(o, a, x);
214 static inline struct oattr *
215 obj_add_attr_internal(struct odes *o, uns x, byte *v, uns just_ref)
220 b = oa_new_ref(o, x, v);
223 if (!(a = o->cached_attr) || a->attr != x)
225 if (!(a = obj_find_attr(o, x)))
241 obj_add_attr(struct odes *o, uns x, byte *v)
243 return obj_add_attr_internal(o, x, v, 0);
247 obj_add_attr_ref(struct odes *o, uns x, byte *v)
249 return obj_add_attr_internal(o, x, v, 1);
253 obj_prepend_attr(struct odes *o, uns x, byte *v)
255 struct oattr *a, *b, **z;
277 obj_insert_attr(struct odes *o, struct oattr *first, struct oattr *after, byte *v)
279 struct oattr *b = oa_new(o, first->attr, v);
280 b->same = after->same;
286 obj_move_attr_to_head(struct odes *o, uns x)
288 struct oattr *a, **z;
305 obj_move_attr_to_tail(struct odes *o, uns x)
307 struct oattr *a, **z;