summaryrefslogtreecommitdiff
path: root/reference/c/eina/src/eina_list.c
blob: 69cad22fd47918250b160bf385a4e96a1ba04598 (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
#define EFL_EO_API_SUPPORT 1
#define EFL_BETA_API_SUPPORT 1

#include <stdio.h>

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

/*
 * Eina List examples.
 *
 * These examples demonstrate how to work with eina_list data and methods.
 * We create a simple list of names by appending strings to an empty list
 * and then run various mutations and print each result.
 */

static Eina_List *
_list_create()
{
   Eina_List *list = NULL;

   // passing NULL as the list parameter will create a new list
   list = eina_list_append(list, "Adama");
   list = eina_list_append(list, "Baltar");
   list = eina_list_append(list, "Roslin");

   return list;
}

static void
_list_free(Eina_List *list)
{
   // here we do not need to use EINA_LIST_FREE as our data is not leaking
   eina_list_free(list);
}

EAPI_MAIN void
efl_main(void *data EINA_UNUSED, const Efl_Event *ev EINA_UNUSED)
{
   Eina_List *list, *item, *sorted;
   void *item_data;

   list = _list_create();

   // print our list with a simple foreach
   printf("List size: %d\n", eina_list_count(list));
   printf("List content:\n");
   EINA_LIST_FOREACH(list, item, item_data)
     printf("  %s\n", (char*)item_data);

   // insert some more elements
   list = eina_list_prepend(list, "Cain");
   list = eina_list_prepend_relative(list, "Tigh", "Baltar");

   printf("New list content:\n");
   EINA_LIST_FOREACH(list, item, item_data)
     printf("  %s\n", (char*)item_data);

   // promote an item to the top of the list
   list = eina_list_promote_list(list, eina_list_nth_list(list, 1));
   list = eina_list_remove(list, "Cain");

   printf("List content after promotion:\n");
   EINA_LIST_FOREACH(list, item, item_data)
     printf("  %s\n", (char*)item_data);

   // we can sort the list with any callback, here we use strcmp
   sorted = eina_list_sort(list, 0, (Eina_Compare_Cb)strcmp);
   printf("List content sorted:\n");
   EINA_LIST_FOREACH(sorted, item, item_data)
     printf("  %s\n", (char*)item_data);

   // and foreach can be in reverse too
   printf("List content reverse sorted:\n");
   EINA_LIST_REVERSE_FOREACH(sorted, item, item_data)
     printf("  %s\n", (char*)item_data);

   _list_free(list);

   efl_exit(0);
}
EFL_MAIN()