EFL 1.7 svn doobies
[profile/ivi/eina.git] / src / examples / eina_inlist_01.c
1 // Compile with:
2 // gcc -g eina_inlist_01.c -o eina_inlist_01 `pkg-config --cflags --libs eina`
3
4 #include <Eina.h>
5 #include <stdio.h>
6
7 struct my_struct {
8      EINA_INLIST;
9      int a, b;
10 };
11
12 int
13 sort_cb(const void *d1, const void *d2)
14 {
15    const Eina_Inlist *l1, *l2;
16    const struct my_struct *x1, *x2;
17
18    l1 = d1;
19    l2 = d2;
20
21    x1 = EINA_INLIST_CONTAINER_GET(l1, struct my_struct);
22    x2 = EINA_INLIST_CONTAINER_GET(l2, struct my_struct);
23
24    return x1->a - x2->a;
25 }
26
27 int
28 main(void)
29 {
30    struct my_struct *d, *cur;
31    Eina_Inlist *list, *itr, *tmp;
32
33    eina_init();
34
35    d = malloc(sizeof(*d));
36    d->a = 1;
37    d->b = 10;
38    list = eina_inlist_append(NULL, EINA_INLIST_GET(d));
39
40    d = malloc(sizeof(*d));
41    d->a = 2;
42    d->b = 20;
43    list = eina_inlist_append(list, EINA_INLIST_GET(d));
44
45    d = malloc(sizeof(*d));
46    d->a = 3;
47    d->b = 30;
48    list = eina_inlist_prepend(list, EINA_INLIST_GET(d));
49
50    printf("list=%p\n", list);
51    EINA_INLIST_FOREACH(list, cur)
52      printf("\ta=%d, b=%d\n", cur->a, cur->b);
53
54    list = eina_inlist_promote(list, EINA_INLIST_GET(d));
55
56    d = malloc(sizeof(*d));
57    d->a = 4;
58    d->b = 40;
59    list = eina_inlist_append_relative(list, EINA_INLIST_GET(d), list);
60
61    list = eina_inlist_demote(list, EINA_INLIST_GET(d));
62
63    list = eina_inlist_sort(list, sort_cb);
64
65    printf("list after sort=%p\n", list);
66    EINA_INLIST_FOREACH(list, cur)
67      printf("\ta=%d, b=%d\n", cur->a, cur->b);
68
69    tmp = eina_inlist_find(list, EINA_INLIST_GET(d));
70    if (tmp)
71      cur = EINA_INLIST_CONTAINER_GET(tmp, struct my_struct);
72    else
73      cur = NULL;
74
75    if (d != cur)
76      printf("wrong node! cur=%p\n", cur);
77
78    list = eina_inlist_remove(list, EINA_INLIST_GET(d));
79    free(d);
80    printf("list=%p\n", list);
81    for (itr = list; itr != NULL; itr = itr->next)
82      {
83         cur = EINA_INLIST_CONTAINER_GET(itr, struct my_struct);
84         printf("\ta=%d, b=%d\n", cur->a, cur->b);
85      }
86
87    while (list)
88      {
89         struct my_struct *aux = EINA_INLIST_CONTAINER_GET(list,
90                                                           struct my_struct);
91         list = eina_inlist_remove(list, list);
92         free(aux);
93      }
94
95    eina_shutdown();
96
97    return 0;
98 }