felipealmeida pushed a commit to branch master. http://git.enlightenment.org/core/efl.git/commit/?id=d723990955289a5ca62605262f4bc4cd0aa8d25f
commit d723990955289a5ca62605262f4bc4cd0aa8d25f Author: Felipe Magno de Almeida <fel...@expertisesolutions.com.br> Date: Sun Mar 6 17:24:34 2016 -0300 eina: Add Eina_Iterator implementation for C-array This iterator is convenient when you already have a C-Array and you need to pass this array to a function receiving an Eina_Iterator. int array[] = {1, 2, 3, 4}; int* array2[] = {&array[0], &array[1], &array[2], &array[3], NULL}; Eina_Iterator* iterator = eina_carray_iterator_new((void**)array); --- src/lib/eina/eina_iterator.c | 54 ++++++++++++++++++++++++++++++++++++++++++++ src/lib/eina/eina_iterator.h | 15 ++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/lib/eina/eina_iterator.c b/src/lib/eina/eina_iterator.c index 59dc106..5d62181 100644 --- a/src/lib/eina/eina_iterator.c +++ b/src/lib/eina/eina_iterator.c @@ -174,3 +174,57 @@ eina_iterator_unlock(Eina_Iterator *iterator) return iterator->unlock(iterator); return EINA_TRUE; } + +typedef struct _Eina_Iterator_CArray Eina_Iterator_CArray; + +struct _Eina_Iterator_CArray +{ + Eina_Iterator iterator; + + void** array; + void** current; +}; + +static Eina_Bool +eina_carray_iterator_next(Eina_Iterator_CArray *it, void **data) +{ + if (!it->current || !*it->current) + return EINA_FALSE; + + *data = *it->current++; + + return EINA_TRUE; +} + +static void** +eina_carray_iterator_get_container(Eina_Iterator_CArray *it) +{ + return it->array; +} + +static void +eina_carray_iterator_free(Eina_Iterator_CArray *it) +{ + free(it); +} + +EAPI Eina_Iterator* +eina_carray_iterator_new(void** array) +{ + Eina_Iterator_CArray *it; + + it = calloc(1, sizeof (Eina_Iterator_CArray)); + if (!it) return NULL; + + EINA_MAGIC_SET(&it->iterator, EINA_MAGIC_ITERATOR); + + it->array = it->current = array; + + it->iterator.version = EINA_ITERATOR_VERSION; + it->iterator.next = FUNC_ITERATOR_NEXT(eina_carray_iterator_next); + it->iterator.get_container = FUNC_ITERATOR_GET_CONTAINER( + eina_carray_iterator_get_container); + it->iterator.free = FUNC_ITERATOR_FREE(eina_carray_iterator_free); + + return &it->iterator; +} diff --git a/src/lib/eina/eina_iterator.h b/src/lib/eina/eina_iterator.h index 980dbfc..b379d2e 100644 --- a/src/lib/eina/eina_iterator.h +++ b/src/lib/eina/eina_iterator.h @@ -285,6 +285,21 @@ EAPI Eina_Bool eina_iterator_lock(Eina_Iterator *iterator) EINA_ARG_NONNULL(1); EAPI Eina_Bool eina_iterator_unlock(Eina_Iterator *iterator) EINA_ARG_NONNULL(1); /** + * @brief Creates an Eina_Iterator that iterates through a + * NUL-terminated C array. + * + * @param array The NUL-terminated array + * + * You can create it like this: + * int array[] = {1, 2, 3, 4}; + * int* array2[] = {&array[0], &array[1], &array[2], &array[3], NULL}; + * + * Eina_Iterator* iterator = eina_carray_iterator_new((void**)array); + * + */ +EAPI Eina_Iterator* eina_carray_iterator_new(void** array) EINA_ARG_NONNULL(1) EINA_WARN_UNUSED_RESULT; + +/** * @def EINA_ITERATOR_FOREACH * @brief Macro to iterate over all elements easily. * --