* src/vm/zip.c (zip_open): Don't add directories to the zipfile
[cacao.git] / src / toolbox / list.c
index e2c000b18f957fc49fd5dbbbd1ed87bff7373651..39a547ed937e751b419c48331c7d6e0c54392579 100644 (file)
@@ -1,10 +1,9 @@
-/* toolbox/list.c - 
+/* src/toolbox/list.c - double linked list
 
-   Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
-   Institut f. Computersprachen, TU Wien
-   R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser, M. Probst,
-   S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich,
-   J. Wenninger
+   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
+   E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
+   J. Wenninger, Institut f. Computersprachen - TU Wien
 
    This file is part of CACAO.
 
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
-   Contact: cacao@complang.tuwien.ac.at
+   Contact: cacao@cacaojvm.org
 
    Authors: Reinhard Grafl
 
-   $Id: list.c 1552 2004-11-19 15:14:58Z twisti $
+   $Id: list.c 4394 2006-01-31 22:27:23Z twisti $
 
 */
 
 
+#include "config.h"
+
 #include <stdlib.h>
 
-#include "global.h"
+#include "vm/types.h"
+
 #include "toolbox/list.h"
 
 
@@ -46,15 +48,15 @@ void list_init(list *l, int nodeoffset)
 }
 
 
-void list_addlast(list *l, void *element)
+void list_addfirst(list *l, void *element)
 {
-       listnode *n = (listnode*) (((char*) element) + l->nodeoffset);
+       listnode *n = (listnode *) (((u1 *) element) + l->nodeoffset);
 
-       if (l->last) {
-               n->prev = l->last;
-               n->next = NULL;
-               l->last->next = n;
-               l->last = n;
+       if (l->first) {
+               n->prev = NULL;
+               n->next = l->first;
+               l->first->prev = n;
+               l->first = n;
 
        } else {
                n->prev = NULL;
@@ -65,15 +67,15 @@ void list_addlast(list *l, void *element)
 }
 
 
-void list_addfirst(list *l, void *element)
+void list_addlast(list *l, void *element)
 {
-       listnode *n = (listnode*) (((char*) element) + l->nodeoffset);
+       listnode *n = (listnode *) (((u1 *) element) + l->nodeoffset);
 
-       if (l->first) {
-               n->prev = NULL;
-               n->next = l->first;
-               l->first->prev = n;
-               l->first = n;
+       if (l->last) {
+               n->prev = l->last;
+               n->next = NULL;
+               l->last->next = n;
+               l->last = n;
 
        } else {
                n->prev = NULL;
@@ -84,23 +86,52 @@ void list_addfirst(list *l, void *element)
 }
 
 
+/* list_add_before *************************************************************
+
+   Adds the element newelement to the list l before element.
+
+   [ A ] <-> [ newn ] <-> [ n ] <-> [ B ]
+
+*******************************************************************************/
+
+void list_add_before(list *l, void *element, void *newelement)
+{
+       listnode *n    = (listnode *) (((u1 *) element) + l->nodeoffset);
+       listnode *newn = (listnode *) (((u1 *) newelement) + l->nodeoffset);
+
+       /* set the new links */
+
+       newn->prev = n->prev;
+       newn->next = n;
+
+       if (newn->prev)
+               newn->prev->next = newn;
+
+       n->prev = newn;
+
+       /* set list's first and last if necessary */
+
+       if (l->first == n)
+               l->first = newn;
+
+       if (l->last == n)
+               l->last = newn;
+}
+
+
 void list_remove(list *l, void *element)
 {
-       listnode *n = (listnode*) (((char*) element) + l->nodeoffset);
+       listnode *n = (listnode *) (((u1 *) element) + l->nodeoffset);
        
-       if (n->next) {
+       if (n->next)
                n->next->prev = n->prev;
-
-       } else {
+       else
                l->last = n->prev;
-       }
 
-       if (n->prev) {
+       if (n->prev)
                n->prev->next = n->next;
-
-       } else {
+       else
                l->first = n->next;
-       }
 
        n->next = NULL;
        n->prev = NULL;
@@ -112,7 +143,7 @@ void *list_first(list *l)
        if (!l->first)
                return NULL;
 
-       return ((char*) l->first) - l->nodeoffset;
+       return ((u1 *) l->first) - l->nodeoffset;
 }
 
 
@@ -121,7 +152,7 @@ void *list_last(list *l)
        if (!l->last)
                return NULL;
 
-       return ((char*) l->last) - l->nodeoffset;
+       return ((u1 *) l->last) - l->nodeoffset;
 }
 
 
@@ -129,12 +160,12 @@ void *list_next(list *l, void *element)
 {
        listnode *n;
 
-       n = (listnode*) (((char*) element) + l->nodeoffset);
+       n = (listnode *) (((u1 *) element) + l->nodeoffset);
 
        if (!n->next)
                return NULL;
 
-       return ((char*) n->next) - l->nodeoffset;
+       return ((u1 *) n->next) - l->nodeoffset;
 }
 
        
@@ -142,12 +173,12 @@ void *list_prev(list *l, void *element)
 {
        listnode *n;
 
-       n = (listnode*) (((char*) element) + l->nodeoffset);
+       n = (listnode *) (((u1 *) element) + l->nodeoffset);
 
        if (!n->prev)
                return NULL;
 
-       return ((char*) n->prev) - l->nodeoffset;
+       return ((u1 *) n->prev) - l->nodeoffset;
 }