- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files
- Examples

Eina_Iterator usage

As always when using eina we need to include it:

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

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

static Eina_Bool
print_one(const void *container EINA_UNUSED, void *data, void *fdata EINA_UNUSED)
{
printf("%s\n", (char*)data);
return EINA_TRUE;
}

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");
}

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

int
main(int argc EINA_UNUSED, char **argv EINA_UNUSED)
{
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;
Eina_Iterator *it;
unsigned short int i;
char *uninteresting;
eina_init();

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]);
}

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

it = eina_array_iterator_new(array);
if (!eina_iterator_next(it, (void **)&uninteresting))

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

return -1;
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:

eina_array_free(eina_iterator_container_get(it));
eina_iterator_free(it);

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:

it = eina_list_iterator_new(list);
if (!eina_iterator_next(it, (void **)&uninteresting))
return -1;
print_eina_container(it);
eina_iterator_free(it);

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:

eina_list_free(list);
eina_shutdown();
return 0;
}