cleanup of BD style game elements in level editor
[rocksndiamonds.git] / src / libgame / hash.c
index 2b856f6810df38cc739c62e7b2695b5586b1cc69..b3595ad4ce4517f8f4b50ab5a7ee847ae1513422 100644 (file)
@@ -423,18 +423,24 @@ hashtable_iterator(struct hashtable *h)
 /* key - return the key of the (key, value) pair at the current position */
 
 void *
-hashtable_iterator_key(struct hashtable_itr *i)
+hashtable_iterator_key(struct hashtable_itr *itr)
 {
-  return i->e->k;
+  if (itr == NULL || itr->e == NULL)
+    return NULL;
+
+  return itr->e->k;
 }
 
 /*****************************************************************************/
 /* value - return the value of the (key, value) pair at the current position */
 
 void *
-hashtable_iterator_value(struct hashtable_itr *i)
+hashtable_iterator_value(struct hashtable_itr *itr)
 {
-  return i->e->v;
+  if (itr == NULL || itr->e == NULL)
+    return NULL;
+
+  return itr->e->v;
 }
 
 /*****************************************************************************/
@@ -529,17 +535,21 @@ hashtable_foreach_remove(struct hashtable *h, hashtable_remove_fn fn, void *user
 
   free(itr);
 
-  struct hashtable_itr *itr_remove = hashtable_iterator(remove);
   unsigned int num_removed = 0;
 
-  do
+  if (hashtable_count(remove) > 0)
   {
-    hashtable_remove(h, hashtable_iterator_key(itr_remove));
-    num_removed++;
-  }
-  while (hashtable_iterator_advance(itr_remove));
+    struct hashtable_itr *itr_remove = hashtable_iterator(remove);
 
-  free(itr_remove);
+    do
+    {
+      hashtable_remove(h, hashtable_iterator_key(itr_remove));
+      num_removed++;
+    }
+    while (hashtable_iterator_advance(itr_remove));
+
+    free(itr_remove);
+  }
 
   hashtable_destroy(remove);