]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/dlist.c
Update configure to set x on logrotate
[bacula/bacula] / bacula / src / lib / dlist.c
index 15307bc3b357c98b47aff71bba1dea525cac0342..9dd304e2f24d8a2e8f76fcc81bb3a0e5dc37d6c9 100644 (file)
@@ -10,7 +10,7 @@
  *
  */
 /*
-   Copyright (C) 2000-2003 Kern Sibbald and John Walker
+   Copyright (C) 2000-2004 Kern Sibbald and John Walker
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
  */
 void dlist::append(void *item) 
 {
-   ((dlink *)((char *)item+loffset))->next = NULL;
-   ((dlink *)((char *)item+loffset))->prev = tail;
+   ((dlink *)(((char *)item)+loffset))->next = NULL;
+   ((dlink *)(((char *)item)+loffset))->prev = tail;
    if (tail) {
-      ((dlink *)((char *)tail+loffset))->next = item;
+      ((dlink *)(((char *)tail)+loffset))->next = item;
    }
    tail = item;
    if (head == NULL) {               /* if empty list, */
       head = item;                   /* item is head as well */
    }
+   num_items++;
 }
 
 /*
@@ -55,58 +56,61 @@ void dlist::append(void *item)
  */
 void dlist::prepend(void *item) 
 {
-   ((dlink *)((char *)item+loffset))->next = head;
-   ((dlink *)((char *)item+loffset))->prev = NULL;
+   ((dlink *)(((char *)item)+loffset))->next = head;
+   ((dlink *)(((char *)item)+loffset))->prev = NULL;
    if (head) {
-      ((dlink *)((char *)head+loffset))->prev = item;
+      ((dlink *)(((char *)head)+loffset))->prev = item;
    }
    head = item;
    if (tail == NULL) {               /* if empty list, */                    
       tail = item;                   /* item is tail too */
    }
+   num_items++;
 }
 
 void dlist::insert_before(void *item, void *where)      
 {
    dlink *where_link = (dlink *)((char *)where+loffset);     
 
-   ((dlink *)((char *)item+loffset))->next = where;
-   ((dlink *)((char *)item+loffset))->prev = where_link->prev;
+   ((dlink *)(((char *)item)+loffset))->next = where;
+   ((dlink *)(((char *)item)+loffset))->prev = where_link->prev;
 
    if (where_link->prev) {
-      ((dlink *)((char *)(where_link->prev)+loffset))->next = item;
-      where_link->prev = item;
+      ((dlink *)(((char *)(where_link->prev))+loffset))->next = item;
    }
+   where_link->prev = item;
    if (head == where) {
       head = item;
    }
+   num_items++;
 }
 
 void dlist::insert_after(void *item, void *where)      
 {
    dlink *where_link = (dlink *)((char *)where+loffset);     
 
-   ((dlink *)((char *)item+loffset))->next = where_link->next;
-   ((dlink *)((char *)item+loffset))->prev = where;
+   ((dlink *)(((char *)item)+loffset))->next = where_link->next;
+   ((dlink *)(((char *)item)+loffset))->prev = where;
 
    if (where_link->next) {
-      ((dlink *)((char *)(where_link->next)+loffset))->prev = item;
-      where_link->next = item;
+      ((dlink *)(((char *)(where_link->next))+loffset))->prev = item;
    }
+   where_link->next = item;
    if (tail == where) {
       tail = item;
    }
+   num_items++;
 }
 
 
 void dlist::remove(void *item)
 {
    void *xitem;
-   dlink *ilink = (dlink *)((char *)item+loffset);   /* item's link */
+   dlink *ilink = (dlink *)(((char *)item)+loffset);   /* item's link */
    if (item == head) {
       head = ilink->next;
       if (head) {
-        ((dlink *)((char *)head+loffset))->prev = NULL;
+        ((dlink *)(((char *)head)+loffset))->prev = NULL;
       }
       if (item == tail) {
         tail = ilink->prev;
@@ -114,14 +118,15 @@ void dlist::remove(void *item)
    } else if (item == tail) {
       tail = ilink->prev;
       if (tail) {
-        ((dlink *)((char *)tail+loffset))->next = NULL;
+        ((dlink *)(((char *)tail)+loffset))->next = NULL;
       }
    } else {
       xitem = ilink->next;
-      ((dlink *)((char *)xitem+loffset))->prev = ilink->prev;
+      ((dlink *)(((char *)xitem)+loffset))->prev = ilink->prev;
       xitem = ilink->prev;
-      ((dlink *)((char *)xitem+loffset))->next = ilink->next;
+      ((dlink *)(((char *)xitem)+loffset))->next = ilink->next;
    }
+   num_items--;
 }
 
 void * dlist::next(void *item)
@@ -129,7 +134,7 @@ void * dlist::next(void *item)
    if (item == NULL) {
       return head;
    }
-   return ((dlink *)((char *)item+loffset))->next;
+   return ((dlink *)(((char *)item)+loffset))->next;
 }
 
 void * dlist::prev(void *item)
@@ -137,18 +142,19 @@ void * dlist::prev(void *item)
    if (item == NULL) {
       return tail;
    }
-   return ((dlink *)((char *)item+loffset))->prev;
+   return ((dlink *)(((char *)item)+loffset))->prev;
 }
 
 
-/* Destroy the list and its contents */
+/* Destroy the list contents */
 void dlist::destroy()
 {
    for (void *n=head; n; ) {
-      void *ni = ((dlink *)((char *)n+loffset))->next;
+      void *ni = ((dlink *)(((char *)n)+loffset))->next;
       free(n);
       n = ni;
    }
+   num_items = 0;
 }
 
 
@@ -157,7 +163,7 @@ void dlist::destroy()
 
 struct MYJCR {
    char *buf;
-   dlist link;
+   dlink link;
 };
 
 int main()
@@ -192,7 +198,7 @@ int main()
    jcr_chain->insert_before(jcr, next_jcr);
    
    printf("Print remaining list.\n");
-   for (MYJCR *jcr=NULL; (jcr=(MYJCR *)jcr_chain->next(jcr)); ) {
+   foreach_dlist (jcr, jcr_chain) {
       printf("Dlist item = %s\n", jcr->buf);
       free(jcr->buf);
    }
@@ -221,7 +227,7 @@ int main()
    jcr_chain->insert_before(jcr, next_jcr);
    
    printf("Print remaining list.\n");
-   for (MYJCR *jcr=NULL; (jcr=(MYJCR *)jcr_chain->next(jcr)); ) {
+   foreach_dlist (jcr, jcr_chain) {
       printf("Dlist item = %s\n", jcr->buf);
       free(jcr->buf);
    }