diff options
-rw-r--r-- | src/libstrongswan/utils/linked_list.c | 20 | ||||
-rw-r--r-- | src/libstrongswan/utils/linked_list.h | 7 |
2 files changed, 23 insertions, 4 deletions
diff --git a/src/libstrongswan/utils/linked_list.c b/src/libstrongswan/utils/linked_list.c index aaea530bb..e4c1a3308 100644 --- a/src/libstrongswan/utils/linked_list.c +++ b/src/libstrongswan/utils/linked_list.c @@ -110,11 +110,20 @@ struct private_enumerator_t { * current item */ element_t *current; + + /** + * enumerator has enumerated all items + */ + bool finished; }; METHOD(enumerator_t, enumerate, bool, private_enumerator_t *this, void **item) { + if (this->finished) + { + return FALSE; + } if (!this->current) { this->current = this->list->first; @@ -125,6 +134,7 @@ METHOD(enumerator_t, enumerate, bool, } if (!this->current) { + this->finished = TRUE; return FALSE; } *item = this->current->value; @@ -151,6 +161,7 @@ METHOD(linked_list_t, reset_enumerator, void, private_linked_list_t *this, private_enumerator_t *enumerator) { enumerator->current = NULL; + enumerator->finished = FALSE; } METHOD(linked_list_t, get_count, int, @@ -267,7 +278,14 @@ METHOD(linked_list_t, insert_before, void, current = enumerator->current; if (!current) { - this->public.insert_last(&this->public, item); + if (enumerator->finished) + { + this->public.insert_last(&this->public, item); + } + else + { + this->public.insert_first(&this->public, item); + } return; } element = element_create(item); diff --git a/src/libstrongswan/utils/linked_list.h b/src/libstrongswan/utils/linked_list.h index 47c003d32..cb3d53e58 100644 --- a/src/libstrongswan/utils/linked_list.h +++ b/src/libstrongswan/utils/linked_list.h @@ -95,9 +95,10 @@ struct linked_list_t { /** * Inserts a new item before the item the enumerator currently points to. * - * If the enumerator's position is invalid, e.g. at the end of the list, - * the item is inserted last. This is helpful when inserting items into a - * sorted list. + * If this method is called before starting the enumeration the item is + * inserted first. If it is called after all items have been enumerated + * the item is inserted last. This is helpful when inserting items into + * a sorted list. * * @note The position of the enumerator is not changed. * |