summaryrefslogtreecommitdiff
path: root/reference/c/eina/src/eina_iterator.c
blob: 257e9ed237c166e9bc7a470a4986260beaf2d3c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#define EFL_EO_API_SUPPORT 1
#define EFL_BETA_API_SUPPORT 1

#include <stdio.h>

#include <Eina.h>
#include <Efl_Core.h>

/*
 * Eina Iterator examples.
 *
 * These examples demonstrate how to work with eina_iterator methods.
 * Both an eina_list and an eina_array are created and an iterator obtained
 * for both. You can see how we can use iterators irrespective of the source
 * and also that there are different ways to work with iterating content.
 */

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

static void
_print_iterator(Eina_Iterator *it)
{
   printf("--iterator start--\n");
   eina_iterator_foreach(it, _print_item, NULL);
   printf("-- iterator end --\n");
}

static Eina_Array *
_create_array()
{
   const char *strings[] =
   {
      "name strings",
      "husker",
      "starbuck",
      "boomer"
   };

   Eina_Array *array;
   int i;

   array = eina_array_new(4);
   for (i = 0; i < 4; i++)
     eina_array_push(array, strings[i]);

   return array;
}

static Eina_List *
_create_list()
{
   const char *more_strings[] = {
      "sentence strings",
      "what do your hear?",
      "nothing but the rain",
      "then grab your gun and bring the cat in"
   };

   Eina_List *list = NULL;
   int i;

   for (i = 0; i < 4; i++)
     list = eina_list_append(list, more_strings[i]);

   return list;
}

EAPI_MAIN void
efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
{
   Eina_Array *array;
   Eina_List *list;
   Eina_Iterator *it;
   char *s;

   // create an eina_array and iterate through it's contents
   array = _create_array();
   it = eina_array_iterator_new(array);
   _print_iterator(it);
   eina_iterator_free(it);
   eina_array_free(array);

   // perform the same iteration with an eina_list but without
   // using a callback.
   list = _create_list();
   it = eina_list_iterator_new(list);
   EINA_ITERATOR_FOREACH(it, s)
     _print_item(eina_iterator_container_get(it), s, NULL);
   eina_iterator_free(it);
   eina_list_free(list);

   efl_exit(0);
}
EFL_MAIN()