diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/eina/src/examples/eina_inlist_03.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/libraries/eina/src/examples/eina_inlist_03.c b/libraries/eina/src/examples/eina_inlist_03.c new file mode 100644 index 0000000..a39a784 --- /dev/null +++ b/libraries/eina/src/examples/eina_inlist_03.c | |||
@@ -0,0 +1,73 @@ | |||
1 | // Compile with: | ||
2 | // gcc -g `pkg-config --cflags --libs eina` eina_inlist_01.c -o eina_inlist_01 | ||
3 | #include <Eina.h> | ||
4 | #include <stdio.h> | ||
5 | |||
6 | struct my_struct { | ||
7 | EINA_INLIST; | ||
8 | Eina_Inlist even; | ||
9 | int a, b; | ||
10 | }; | ||
11 | |||
12 | #define EVEN_INLIST_GET(Inlist) (& ((Inlist)->even)) | ||
13 | |||
14 | #define EVEN_INLIST_CONTAINER_GET(ptr, type) \ | ||
15 | ((type *)((char *)ptr - offsetof(type, even))) | ||
16 | |||
17 | int | ||
18 | main(void) | ||
19 | { | ||
20 | struct my_struct *d, *cur; | ||
21 | int i; | ||
22 | |||
23 | Eina_Inlist *list = NULL, *list_even = NULL, *itr; | ||
24 | |||
25 | eina_init(); | ||
26 | |||
27 | for (i = 0; i < 100; i++) | ||
28 | { | ||
29 | d = malloc(sizeof(*d)); | ||
30 | d->a = i; | ||
31 | d->b = i * 10; | ||
32 | list = eina_inlist_append(list, EINA_INLIST_GET(d)); | ||
33 | if ((i % 2) == 0) | ||
34 | list_even = eina_inlist_prepend(list_even, EVEN_INLIST_GET(d)); | ||
35 | } | ||
36 | |||
37 | printf("list=%p\n", list); | ||
38 | EINA_INLIST_FOREACH(list, cur) | ||
39 | printf("\ta=%d, b=%d\n", cur->a, cur->b); | ||
40 | |||
41 | printf("list_even=%p\n", list_even); | ||
42 | for (itr = list_even; itr != NULL; itr = itr->next) | ||
43 | { | ||
44 | cur = EVEN_INLIST_CONTAINER_GET(itr, struct my_struct); | ||
45 | printf("\ta=%d, b=%d\n", cur->a, cur->b); | ||
46 | } | ||
47 | |||
48 | printf("list count=%d\n", eina_inlist_count(list)); | ||
49 | printf("list_even count=%d\n\n", eina_inlist_count(list_even)); | ||
50 | |||
51 | itr = list_even; | ||
52 | while (itr) | ||
53 | { | ||
54 | Eina_Inlist *next = itr->next; | ||
55 | cur = EVEN_INLIST_CONTAINER_GET(itr, struct my_struct); | ||
56 | if ((cur->a % 3) == 0) | ||
57 | list_even = eina_inlist_remove(list_even, itr); | ||
58 | itr = next; | ||
59 | } | ||
60 | printf("list count=%d\n", eina_inlist_count(list)); | ||
61 | printf("list_even count=%d\n\n", eina_inlist_count(list_even)); | ||
62 | |||
63 | while (list) | ||
64 | { | ||
65 | Eina_Inlist *aux = list; | ||
66 | list = eina_inlist_remove(list, list); | ||
67 | free(aux); | ||
68 | } | ||
69 | |||
70 | eina_shutdown(); | ||
71 | |||
72 | return 0; | ||
73 | } | ||