Index: /trunk/psLib/src/collections/psDlist.c
===================================================================
--- /trunk/psLib/src/collections/psDlist.c	(revision 899)
+++ /trunk/psLib/src/collections/psDlist.c	(revision 900)
@@ -1,3 +1,3 @@
-/** @file psDlist.h
+/** @file psDlist.c
  *  @brief Support for doubly linked lists
  *  @ingroup DataContainers
@@ -6,6 +6,6 @@
  *  @author Robert Daniel DeSonia, MHPCC
  *
- *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-06-04 23:44:36 $
+ *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-06-07 23:18:21 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -22,4 +22,5 @@
 #include "psDlist.h"
 #include "psTrace.h"
+#include "psLogMsg.h"
 
 #define ITER_INIT_HEAD ((void *)1) // next iteration should return head
@@ -88,11 +89,16 @@
 }
 
-psDlist *psDlistAdd(psDlist *list, void *data, int where)
+psDlist* psDlistAdd(psDlist *list, void *data, int where)
 {
     psDlistElem* position;
     psDlistElem* elem = dlistElemAlloc();
+    int cursorIndex = 0;
 
     if (list == NULL) {
-        list = psDlistAlloc(NULL);
+        return NULL;
+    }
+
+    if (data == NULL) {
+        return list;
     }
 
@@ -102,12 +108,14 @@
     if (where <= PS_DLIST_UNKNOWN) {
         /// XXX What is the better way to communicate this failure to the caller?
-        psError(__func__,"The given insert location (%i) for psDlistAdd is invalid. "
-                "Adding to head instead.",where);
+        psLogMsg(__func__,PS_LOG_WARN,
+                 "The given insert location (%i) for psDlistAdd is invalid. Adding to head instead.",
+                 where);
         where = PS_DLIST_HEAD; // given I can't tell caller about this, should just add it somewhere???
     }
 
     if (where > 0 && where > list->size) {
-        psError(__FILE__, "Invalid index %d (only %d elements in psDList); assuming tail.", where,
-                list->size);
+        psLogMsg(__func__,PS_LOG_WARN,
+                 "Invalid index %d (only %d elements in psDList); assuming tail.",
+                 where, list->size);
         where = PS_DLIST_TAIL;
     }
@@ -134,4 +142,5 @@
         dlistSetIterator(list, where, false);
         position = dlistGetIterator(list);
+        cursorIndex = dlistGetIteratorIndex(list);
 
         if (position == NULL) {
@@ -153,4 +162,5 @@
         list->size++;
         list->iter = elem;
+        list->iterIndex = cursorIndex;
     }
 
@@ -177,4 +187,6 @@
 {
     psDlistElem *elem = NULL;  // element to remove
+    int cursorIndex = 0;
+
     if (list == NULL) {
         psError(__func__,"list parameter found to be NULL in %s",__func__);
@@ -207,4 +219,5 @@
     dlistSetIterator(list,which,false);
     elem = dlistGetIterator(list);
+    cursorIndex = dlistGetIteratorIndex(list);
 
     if (elem == NULL) {
@@ -234,4 +247,5 @@
         elem->next->prev = elem->prev;
         list->iter = elem->next;
+        list->iterIndex = cursorIndex;
     }
 
@@ -268,81 +282,75 @@
     if (lockList) {
         pthread_mutex_lock(&list->lock)
-        ;  // don't want the list changing on us while we move about
-    }
-
-    if (where >= list->size) {
-        psError(__func__,"Tried to access an element beyond list end. "
-                "Moved to last element of list instead.");
-        where = list->size-1;
-
-        switch (where) {
-        case PS_DLIST_HEAD:
-            list->iter = ITER_INIT_HEAD;
-            break;
-
-        case PS_DLIST_TAIL:
-            list->iter = ITER_INIT_TAIL;
-            break;
-
-        case PS_DLIST_PREVIOUS:
+        ;
+        // don't want the list changing on us while we move about
+    }
+
+    if (where >= (int)list->size) {
+        list->iter = NULL;
+        return;
+    }
+
+    switch (where) {
+    case PS_DLIST_HEAD:
+        list->iter = ITER_INIT_HEAD;
+        break;
+
+    case PS_DLIST_TAIL:
+        list->iter = ITER_INIT_TAIL;
+        break;
+
+    case PS_DLIST_PREVIOUS:
+        cursor = dlistGetIterator(list);
+        position = dlistGetIteratorIndex(list);
+
+        if (cursor != NULL) {
+            list->iter = cursor->prev;
+            list->iterIndex = position-1;
+        }
+        break;
+
+    case PS_DLIST_NEXT:
+        cursor = dlistGetIterator(list);
+        position = dlistGetIteratorIndex(list);
+
+        if (cursor != NULL) {
+            list->iter = cursor->next;
+            list->iterIndex = position+1;
+        }
+        break;
+
+    case PS_DLIST_CURRENT:
+        break;
+
+    default:
+        if (where <= PS_DLIST_HEAD) { // bascially same as PS_DLIST_UNKNOWN above
+            psError(__func__,"Can't move to an unknown position.  Not moving the iterator position.");
+        } else {
             cursor = dlistGetIterator(list);
-            position = dlistGetIteratorIndex(list);
-
-            if (cursor == NULL) { // list empty?
-                ((psDlist *)list)->iter = ITER_INIT_HEAD;
+            if (cursor == NULL) { // reset the iterator if it is invalid
+                list->iter = ITER_INIT_HEAD;
+                list->iterIndex = 0;
+            }
+
+            int position = dlistGetIteratorIndex(list);
+
+            if (where < position) {
+                int diff = position-where;
+                for (int count=0;count < diff; count++) {
+                    dlistSetIterator(list,PS_DLIST_PREVIOUS,false);
+                }
             } else {
-                if (cursor->prev != NULL) { // don't go past head
-                    list->iter = cursor->prev;
-                    list->iterIndex = position-1;
-                    break;
+                int diff = where-position;
+                for (int count=0;count < diff; count++) {
+                    dlistSetIterator(list,PS_DLIST_NEXT,false);
                 }
             }
-
-        case PS_DLIST_NEXT:
-            cursor = dlistGetIterator(list);
-            position = dlistGetIteratorIndex(list);
-
-            if (cursor == NULL) { // list empty?
-                ((psDlist *)list)->iter = ITER_INIT_HEAD;
-            } else {
-                if (cursor->next != NULL) { // don't go pase tail
-                    list->iter = cursor->next;
-                    list->iterIndex = position+1;
-                    break;
-                }
-            }
-
-        case PS_DLIST_UNKNOWN:
-            psError(__func__,"Can't move to the PS_DLIST_UNKNOWN position.  Not moving the iterator position.");
-            break;
-        case PS_DLIST_CURRENT:
-            break;
-
-        default:
-            if (where < PS_DLIST_HEAD) { // bascially same as PS_DLIST_UNKNOWN above
-                psError(__func__,"Can't move to an unknown position.  Not moving the iterator position.");
-                break;
-            } else {
-                int position = dlistGetIteratorIndex(list);
-
-                if (where < position) {
-                    int diff = position-where;
-                    for (int count=0;count < diff; count++) {
-                        dlistSetIterator(list,PS_DLIST_PREVIOUS,false);
-                    }
-                } else {
-                    int diff = where-position;
-                    for (int count=0;count < diff; count++) {
-                        dlistSetIterator(list,PS_DLIST_NEXT,false);
-                    }
-                }
-            }
-            break;
-        }
-
-        if (lockList) {
-            pthread_mutex_unlock(&list->lock)
-            ;
-        }
+        }
+        break;
+    }
+
+    if (lockList) {
+        pthread_mutex_unlock(&list->lock)
+        ;
     }
 }
@@ -350,4 +358,8 @@
 psDlistElem* dlistGetIterator(psDlist* list)
 {
+    if (list == NULL) {
+        return NULL;
+    }
+
     if (list->iter == ITER_INIT_HEAD) {
         return list->head;
