aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/utils
diff options
context:
space:
mode:
Diffstat (limited to 'Source/charon/utils')
-rw-r--r--Source/charon/utils/iterator.h13
-rw-r--r--Source/charon/utils/linked_list.c38
2 files changed, 50 insertions, 1 deletions
diff --git a/Source/charon/utils/iterator.h b/Source/charon/utils/iterator.h
index 69ee828f4..de81db8e9 100644
--- a/Source/charon/utils/iterator.h
+++ b/Source/charon/utils/iterator.h
@@ -44,6 +44,19 @@ typedef struct iterator_t iterator_t;
struct iterator_t {
/**
+ * @brief Iterate over all items.
+ *
+ * The easy way to iterate over items.
+ *
+ * @param this calling object
+ * @param[out] value item
+ * @return
+ * - TRUE, if more elements are avaiable,
+ * - FALSE otherwise
+ */
+ bool (*iterate) (iterator_t *this, void** value);
+
+ /**
* @brief Moves to the next element, if available.
*
* A newly created iterator_t object doesn't point to any item.
diff --git a/Source/charon/utils/linked_list.c b/Source/charon/utils/linked_list.c
index f3e686a5a..7ad07dbdd 100644
--- a/Source/charon/utils/linked_list.c
+++ b/Source/charon/utils/linked_list.c
@@ -156,7 +156,42 @@ struct private_iterator_t {
/**
* Implementation of iterator_t.has_next.
*/
-bool iterator_has_next(private_iterator_t *this)
+static bool iterate(private_iterator_t *this, void** value)
+{
+ if (this->list->count == 0)
+ {
+ return FALSE;
+ }
+ if (this->current == NULL)
+ {
+ this->current = (this->forward) ? this->list->first : this->list->last;
+ *value = this->current->value;
+ return TRUE;
+ }
+ if (this->forward)
+ {
+ if (this->current->next == NULL)
+ {
+ return FALSE;
+ }
+ this->current = this->current->next;
+ *value = this->current->value;
+ return TRUE;
+ }
+ /* backward */
+ if (this->current->previous == NULL)
+ {
+ return FALSE;
+ }
+ this->current = this->current->previous;
+ *value = this->current->value;
+ return TRUE;
+}
+
+/**
+ * Implementation of iterator_t.has_next.
+ */
+static bool iterator_has_next(private_iterator_t *this)
{
if (this->list->count == 0)
{
@@ -632,6 +667,7 @@ static iterator_t *create_iterator (private_linked_list_t *linked_list,bool forw
{
private_iterator_t *this = allocator_alloc_thing(private_iterator_t);
+ this->public.iterate = (bool (*) (iterator_t *this, void **value)) iterate;
this->public.has_next = (bool (*) (iterator_t *this)) iterator_has_next;
this->public.current = (status_t (*) (iterator_t *this, void **value)) iterator_current;
this->public.insert_before = (void (*) (iterator_t *this, void *item)) insert_before;