]> mj.ucw.cz Git - libucw.git/blobdiff - lib/object.c
Minor optimizations.
[libucw.git] / lib / object.c
index 2cbc32b55569b7c2307b54efc6b2c4399fc4d811..371d9b3647d7e6443f09dd838d34716acc78b84f 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *     Sherlock Library -- Object Functions
  *
 /*
  *     Sherlock Library -- Object Functions
  *
- *     (c) 1997--2001 Martin Mares <mj@ucw.cz>
+ *     (c) 1997--2004 Martin Mares <mj@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
  *
  *     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/fastbuf.h"
 #include "lib/lib.h"
 #include "lib/pools.h"
 #include "lib/fastbuf.h"
+#include "lib/object.h"
 
 #include <string.h>
 #include <stdio.h>
 
 
 #include <string.h>
 #include <stdio.h>
 
-#define OBJ_POOL_SIZE 4096
-
 void
 obj_dump(struct odes *o)
 {
 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);
 }
 
 static struct oattr *
 oa_new(struct odes *o, uns x, byte *v)
 {
       printf("%c%s\n", (a==b ? a->attr : ' '), b->val);
 }
 
 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->next = a->same = NULL;
   a->attr = x;
+  a->val = (byte*) (a+1);
   strcpy(a->val, v);
   return a;
 }
 
   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)
 {
 struct odes *
 obj_new(struct mempool *pool)
 {
-  struct mempool *lp = pool;
-  struct odes *o;
-
-  if (!lp)
-    lp = mp_new(OBJ_POOL_SIZE);
-  o = mp_alloc(lp, sizeof(struct odes));
-  o->pool = lp;
-  o->local_pool = (pool == lp) ? NULL : lp;
+  struct odes *o = mp_alloc(pool, sizeof(struct odes));
+  o->pool = pool;
   o->attrs = NULL;
   o->cached_attr = NULL;
   return o;
 }
 
   o->attrs = NULL;
   o->cached_attr = NULL;
   return o;
 }
 
-void
-obj_free(struct odes *o)
-{
-  if (o->local_pool)
-    mp_delete(o->local_pool);
-}
-
 int
 obj_read(struct fastbuf *f, struct odes *o)
 {
 int
 obj_read(struct fastbuf *f, struct odes *o)
 {
-  byte buf[1024];
+  byte buf[MAX_ATTR_SIZE];
 
   while (bgets(f, buf, sizeof(buf)))
     {
 
   while (bgets(f, buf, sizeof(buf)))
     {
@@ -75,14 +71,22 @@ obj_read(struct fastbuf *f, struct odes *o)
 }
 
 void
 }
 
 void
-obj_write(struct fastbuf *f, struct odes *d)
+obj_read_multi(struct fastbuf *f, struct odes *o)
 {
 {
-  struct oattr *a, *b;
-  byte *z;
+  /* Read a multi-part object ending with either EOF or a NUL character */
+  byte buf[MAX_ATTR_SIZE];
+  while (bpeekc(f) > 0 && bgets(f, buf, sizeof(buf)))
+    if (buf[0])
+      obj_add_attr(o, buf[0], buf+1);
+}
 
 
-  for(a=d->attrs; a; a=a->next)
-    for(b=a; b; b=b->same)
+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, a->attr);
        for(z = b->val; *z; z++)
          if (*z >= ' ' || *z == '\t')
@@ -92,15 +96,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'));
            }
              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');
       }
 }
 
        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;
 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;
   for(a=o->attrs; a && a->attr != x; a=a->next)
     ;
   return a;
@@ -154,7 +169,6 @@ byte *
 obj_find_aval(struct odes *o, uns x)
 {
   struct oattr *a = obj_find_attr(o, x);
 obj_find_aval(struct odes *o, uns x)
 {
   struct oattr *a = obj_find_attr(o, x);
-
   return a ? a->val : NULL;
 }
 
   return a ? a->val : NULL;
 }
 
@@ -196,15 +210,14 @@ obj_set_attr_num(struct odes *o, uns a, uns v)
   return obj_set_attr(o, a, x);
 }
 
   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;
 
 
-  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)))
+      if (!(a = obj_find_attr(o, b->attr)))
        {
          b->next = o->attrs;
          o->attrs = b;
        {
          b->next = o->attrs;
          o->attrs = b;
@@ -219,6 +232,18 @@ obj_add_attr(struct odes *o, uns x, byte *v)
   return b;
 }
 
   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)
 {
 struct oattr *
 obj_prepend_attr(struct odes *o, uns x, byte *v)
 {
@@ -246,10 +271,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 *
 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;
 }
   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;
+    }
+}