Eina_Iterator usage

As always when using eina we need to include it:

#include <stdio.h>
#include <Eina.h>
Eina Utility library.

Here we a declare an unimpressive function that prints some data:

static Eina_Bool
print_one(const void *container, void *data, void *fdata)
{
printf("%s\n", (char*)data);
return EINA_TRUE;
}
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition eina_types.h:299
unsigned char Eina_Bool
Type to mimic a boolean.
Definition eina_types.h:287
Note
Returning EINA_TRUE is important so we don't stop iterating over the container.

And here a more interesting function, it uses an iterator to print the contents of a container. What's interesting about it is that it doesn't care the type of container, it works for anything that can provide an iterator:

static void
print_eina_container(Eina_Iterator *it)
{
eina_iterator_foreach(it, print_one, NULL);
printf("\n");
}
void eina_iterator_foreach(Eina_Iterator *iterator, Eina_Each_Cb callback, const void *fdata)
Iterate over the container and execute a callback on each element.
Definition eina_iterator.c:128
struct _Eina_Iterator Eina_Iterator
Abstract type for iterators.
Definition eina_iterator.h:126

And on to our main function were we declare some variables and initialize eina, nothing too special:

int
main(int argc, char **argv)
{
const char *strings[] = {
"unintersting string", "husker", "starbuck", "husker"
};
const char *more_strings[] = {
"very unintersting string",
"what do your hear?",
"nothing but the rain",
"then grab your gun and bring the cat in"
};
Eina_Array *array;
Eina_List *list = NULL;
unsigned short int i;
char *uninteresting;
struct _Eina_Array Eina_Array
Type for a generic vector.
Definition eina_array.h:220
struct _Eina_List Eina_List
Type for a generic double linked list.
Definition eina_list.h:307
int eina_init(void)
Initialize the Eina library.
Definition eina_main.c:244

Next we populate both an array and a list with our strings, for more details see Adding elements to Eina_List and Basic array usage :

array = eina_array_new(4);
for (i = 0; i < 4; i++)
{
eina_array_push(array, strings[i]);
list = eina_list_append(list, more_strings[i]);
}
Eina_Array * eina_array_new(unsigned int step)
Create a new array.
Definition eina_array.c:265
Eina_List * eina_list_append(Eina_List *list, const void *data)
Append the given data to the given linked list.
Definition eina_list.c:530

And now we create an array and because the first element of the container doesn't interest us we skip it:

eina_iterator_next(it, &uninteresting);
Eina_Iterator * eina_array_iterator_new(const Eina_Array *array)
Returned a new iterator associated to an array.
Definition eina_array.c:434
Eina_Bool eina_iterator_next(Eina_Iterator *iterator, void **data)
Return the value of the current element and go to the next one.
Definition eina_iterator.c:116

Having our iterator now pointing to interesting data we go ahead and print:

print_eina_container(it);

As always once data with a structure we free it, but just because we can we do it by asking the iterator for it's container, and then of course free the iterator itself:

void eina_array_free(Eina_Array *array)
Free an array.
Definition eina_array.c:289
void * eina_iterator_container_get(Eina_Iterator *iterator)
Return the container of an iterator.
Definition eina_iterator.c:107
void eina_iterator_free(Eina_Iterator *iterator)
Free an iterator.
Definition eina_iterator.c:96

But so far you're not impressed in Basic array usage an array is also printed, so now we go to the cool stuff and use an iterator to do same stuff to a list:

eina_iterator_next(it, &uninteresting);
print_eina_container(it);
Eina_Iterator * eina_list_iterator_new(const Eina_List *list)
Returned a new iterator associated to a list.
Definition eina_list.c:1409
Note
The only significant diference to the block above is in the function used to create the iterator.

And now we free the list and shut eina down:

return 0;
}
Eina_List * eina_list_free(Eina_List *list)
Free an entire list and all the nodes, ignoring the data contained.
Definition eina_list.c:754
int eina_shutdown(void)
Shut down the Eina library.
Definition eina_main.c:315