X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fobject.c;h=fb957bd7e61169271226a5689738e30f8a7fc8ae;hb=81a17ab471944888106c70a719e99d6e6ba96f22;hp=daa5c9736c4d3513bf02ade21d248b96323c81f8;hpb=a6c4d0101fc28d46f0ebf2401ea4e187a7276077;p=libucw.git diff --git a/lib/object.c b/lib/object.c index daa5c973..fb957bd7 100644 --- a/lib/object.c +++ b/lib/object.c @@ -10,19 +10,16 @@ #include "lib/lib.h" #include "lib/pools.h" #include "lib/fastbuf.h" +#include "lib/object.h" #include #include -#define OBJ_POOL_SIZE 4096 - void obj_dump(struct odes *o) { - struct oattr *a, *b; - - for(a=o->attrs; a; a=a->next) - for(b=a; b; b=b->same) + for(struct oattr *a=o->attrs; a; a=a->next) + for(struct oattr *b=a; b; b=b->same) printf("%c%s\n", (a==b ? a->attr : ' '), b->val); } @@ -40,9 +37,7 @@ oa_new(struct odes *o, uns x, byte *v) struct odes * obj_new(struct mempool *pool) { - struct odes *o; - - o = mp_alloc(lp, sizeof(struct odes)); + struct odes *o = mp_alloc(pool, sizeof(struct odes)); o->pool = pool; o->attrs = NULL; o->cached_attr = NULL; @@ -52,7 +47,7 @@ obj_new(struct mempool *pool) int obj_read(struct fastbuf *f, struct odes *o) { - byte buf[1024]; + byte buf[MAX_ATTR_SIZE]; while (bgets(f, buf, sizeof(buf))) { @@ -66,12 +61,10 @@ obj_read(struct fastbuf *f, struct odes *o) void obj_write(struct fastbuf *f, struct odes *d) { - struct oattr *a, *b; - byte *z; - - for(a=d->attrs; a; a=a->next) - for(b=a; b; b=b->same) + 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') @@ -81,15 +74,26 @@ obj_write(struct fastbuf *f, struct odes *d) 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) { struct oattr *a; - for(a=o->attrs; a && a->attr != x; a=a->next) ; return a; @@ -235,10 +239,48 @@ obj_prepend_attr(struct odes *o, uns x, byte *v) struct oattr * obj_insert_attr(struct odes *o, struct oattr *first, struct oattr *after, byte *v) { - struct oattr *b; - - b = oa_new(o, first->attr, v); + struct oattr *b = oa_new(o, first->attr, v); b->same = after->same; after->same = b; return b; } + +void +obj_move_attr_to_head(struct odes *o, uns x) +{ + struct oattr *a, **z; + + z = &o->attrs; + while (a = *z) + { + if (a->attr == x) + { + *z = a->next; + a->next = o->attrs; + o->attrs = a; + break; + } + z = &a->next; + } +} + +void +obj_move_attr_to_tail(struct odes *o, uns x) +{ + struct oattr *a, **z; + + z = &o->attrs; + while (a = *z) + { + if (a->attr == x) + { + *z = a->next; + while (*z) + z = &(*z)->next; + *z = a; + a->next = NULL; + break; + } + z = &a->next; + } +}