X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fobject.c;h=6a2a90c5b5d46aeec6c8709be842121bd553f41a;hb=aac897f2337471637d9a47b38e1b7b35ab0359b5;hp=fb957bd7e61169271226a5689738e30f8a7fc8ae;hpb=47af8a9cb91d8935819285341953802eb8466540;p=libucw.git diff --git a/lib/object.c b/lib/object.c index fb957bd7..6a2a90c5 100644 --- a/lib/object.c +++ b/lib/object.c @@ -1,19 +1,21 @@ /* * Sherlock Library -- Object Functions * - * (c) 1997--2003 Martin Mares + * (c) 1997--2004 Martin Mares + * (c) 2004 Robert Spalek * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. */ #include "lib/lib.h" -#include "lib/pools.h" +#include "lib/mempool.h" #include "lib/fastbuf.h" #include "lib/object.h" #include #include +#include void obj_dump(struct odes *o) @@ -26,14 +28,26 @@ obj_dump(struct odes *o) static struct oattr * oa_new(struct odes *o, uns x, byte *v) { - struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr) + strlen(v)); + struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr) + strlen(v)+1); a->next = a->same = NULL; a->attr = x; + a->val = (byte*) (a+1); strcpy(a->val, v); return a; } +static struct oattr * +oa_new_ref(struct odes *o, uns x, byte *v) +{ + struct oattr *a = mp_alloc(o->pool, sizeof(struct oattr)); + + a->next = a->same = NULL; + a->attr = x; + a->val = v; + return a; +} + struct odes * obj_new(struct mempool *pool) { @@ -44,52 +58,6 @@ obj_new(struct mempool *pool) return o; } -int -obj_read(struct fastbuf *f, struct odes *o) -{ - byte buf[MAX_ATTR_SIZE]; - - while (bgets(f, buf, sizeof(buf))) - { - if (!buf[0]) - return 1; - obj_add_attr(o, buf[0], buf+1); - } - return 0; -} - -void -obj_write(struct fastbuf *f, struct odes *d) -{ - for(struct oattr *a=d->attrs; a; a=a->next) - for(struct oattr *b=a; b; b=b->same) - { - byte *z; - bputc(f, a->attr); - for(z = b->val; *z; z++) - if (*z >= ' ' || *z == '\t') - bputc(f, *z); - else - { - bputc(f, '?'); - log(L_ERROR, "obj_dump: Found non-ASCII characters (URL might be %s)", obj_find_aval(d, 'U')); - } - ASSERT(z - b->val <= MAX_ATTR_SIZE-2); - bputc(f, '\n'); - } -} - -void -obj_write_nocheck(struct fastbuf *f, struct odes *d) -{ - for(struct oattr *a=d->attrs; a; a=a->next) - for(struct oattr *b=a; b; b=b->same) - { - bputc(f, a->attr); - bputsn(f, b->val); - } -} - struct oattr * obj_find_attr(struct odes *o, uns x) { @@ -147,10 +115,16 @@ byte * obj_find_aval(struct odes *o, uns x) { struct oattr *a = obj_find_attr(o, x); - return a ? a->val : NULL; } +uns +obj_find_anum(struct odes *o, uns x, uns def) +{ + struct oattr *a = obj_find_attr(o, x); + return a ? (uns)atol(a->val) : def; +} + struct oattr * obj_set_attr(struct odes *o, uns x, byte *v) { @@ -189,18 +163,20 @@ obj_set_attr_num(struct odes *o, uns a, uns v) return obj_set_attr(o, a, x); } -struct oattr * -obj_add_attr(struct odes *o, uns x, byte *v) +static inline struct oattr * +obj_add_attr_internal(struct odes *o, struct oattr *b) { - struct oattr *a, *b; + struct oattr *a, **z; - b = oa_new(o, x, v); - if (!(a = o->cached_attr) || a->attr != x) + if (!(a = o->cached_attr) || a->attr != b->attr) { - if (!(a = obj_find_attr(o, x))) + z = &o->attrs; + while ((a = *z) && a->attr != b->attr) + z = &a->next; + if (!a) { - b->next = o->attrs; - o->attrs = b; + *z = b; + /* b->next is NULL */ goto done; } } @@ -212,6 +188,18 @@ obj_add_attr(struct odes *o, uns x, byte *v) return b; } +struct oattr * +obj_add_attr(struct odes *o, uns x, byte *v) +{ + return obj_add_attr_internal(o, oa_new(o, x, v)); +} + +struct oattr * +obj_add_attr_ref(struct odes *o, uns x, byte *v) +{ + return obj_add_attr_internal(o, oa_new_ref(o, x, v)); +} + struct oattr * obj_prepend_attr(struct odes *o, uns x, byte *v) {