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/pools.h"
13 #include "lib/fastbuf.h"
14 #include "lib/object.h"
20 obj_dump(struct odes *o)
22 for(struct oattr *a=o->attrs; a; a=a->next)
23 for(struct oattr *b=a; b; b=b->same)
24 printf("%c%s\n", (a==b ? a->attr : ' '), b->val);
28 oa_new(struct odes *o, uns x, byte *v)
30 struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr) + strlen(v)+1);
32 a->next = a->same = NULL;
34 a->val = (byte*) (a+1);
40 oa_new_ref(struct odes *o, uns x, byte *v)
42 struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr));
44 a->next = a->same = NULL;
51 obj_new(struct mempool *pool)
53 struct odes *o = mp_alloc(pool, sizeof(struct odes));
56 o->cached_attr = NULL;
61 obj_read(struct fastbuf *f, struct odes *o)
63 byte buf[MAX_ATTR_SIZE];
65 while (bgets(f, buf, sizeof(buf)))
69 obj_add_attr(o, buf[0], buf+1);
75 obj_read_multi(struct fastbuf *f, struct odes *o)
77 /* Read a multi-part object ending with either EOF or a NUL character */
78 byte buf[MAX_ATTR_SIZE];
79 while (bpeekc(f) > 0 && bgets(f, buf, sizeof(buf)))
81 obj_add_attr(o, buf[0], buf+1);
85 obj_write(struct fastbuf *f, struct odes *d)
87 for(struct oattr *a=d->attrs; a; a=a->next)
88 for(struct oattr *b=a; b; b=b->same)
91 for (z = b->val; *z; z++)
92 if (*z < ' ' && *z != '\t')
94 log(L_ERROR, "obj_dump: Found non-ASCII characters (URL might be %s)", obj_find_aval(d, 'U'));
97 ASSERT(z - b->val <= MAX_ATTR_SIZE-2);
98 bput_attr_str(f, a->attr, b->val);
103 obj_write_nocheck(struct fastbuf *f, struct odes *d)
105 for(struct oattr *a=d->attrs; a; a=a->next)
106 for(struct oattr *b=a; b; b=b->same)
107 bput_attr_str(f, a->attr, b->val);
111 obj_find_attr(struct odes *o, uns x)
114 for(a=o->attrs; a && a->attr != x; a=a->next)
120 obj_find_attr_last(struct odes *o, uns x)
122 struct oattr *a = obj_find_attr(o, x);
133 obj_del_attr(struct odes *o, struct oattr *a)
135 struct oattr *x, **p, *y, *l;
138 o->cached_attr = NULL;
164 obj_find_aval(struct odes *o, uns x)
166 struct oattr *a = obj_find_attr(o, x);
167 return a ? a->val : NULL;
171 obj_set_attr(struct odes *o, uns x, byte *v)
173 struct oattr *a, **z;
200 obj_set_attr_num(struct odes *o, uns a, uns v)
205 return obj_set_attr(o, a, x);
208 static inline struct oattr *
209 obj_add_attr_internal(struct odes *o, struct oattr *b)
213 if (!(a = o->cached_attr) || a->attr != b->attr)
215 if (!(a = obj_find_attr(o, b->attr)))
231 obj_add_attr(struct odes *o, uns x, byte *v)
233 return obj_add_attr_internal(o, oa_new(o, x, v));
237 obj_add_attr_ref(struct odes *o, uns x, byte *v)
239 return obj_add_attr_internal(o, oa_new_ref(o, x, v));
243 obj_prepend_attr(struct odes *o, uns x, byte *v)
245 struct oattr *a, *b, **z;
267 obj_insert_attr(struct odes *o, struct oattr *first, struct oattr *after, byte *v)
269 struct oattr *b = oa_new(o, first->attr, v);
270 b->same = after->same;
276 obj_move_attr_to_head(struct odes *o, uns x)
278 struct oattr *a, **z;
295 obj_move_attr_to_tail(struct odes *o, uns x)
297 struct oattr *a, **z;