diff options
author | Vincent Torri <vincent.torri@gmail.com> | 2012-09-16 21:15:43 +0000 |
---|---|---|
committer | Vincent Torri <vincent.torri@gmail.com> | 2012-09-16 21:15:43 +0000 |
commit | 103b912f667bc4bfb9318bef91ccf5f077ca6761 (patch) | |
tree | 149a86cf6e6c2c74e6342edbb43c406633b7d982 /src | |
parent | c83143c8896752e139c82259e22c56715c313e7b (diff) |
merge: and the eina examples
SVN revision: 76715
Diffstat (limited to 'src')
54 files changed, 4947 insertions, 2 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 24ceadb561..529892093b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am | |||
@@ -1,8 +1,8 @@ | |||
1 | MAINTAINERCLEANFILES = Makefile.in | 1 | MAINTAINERCLEANFILES = Makefile.in |
2 | 2 | ||
3 | SUBDIRS = lib include modules bin scripts benchmarks tests | 3 | SUBDIRS = lib include modules bin scripts benchmarks tests examples |
4 | 4 | ||
5 | .PHONY: benchmark benchmark-e17 | 5 | .PHONY: benchmark benchmark-e17 examples |
6 | 6 | ||
7 | benchmark: | 7 | benchmark: |
8 | @make -C lib | 8 | @make -C lib |
@@ -11,3 +11,10 @@ benchmark: | |||
11 | benchmark-e17: | 11 | benchmark-e17: |
12 | @make -C lib | 12 | @make -C lib |
13 | @$(MAKE) -C benchmarks benchmark-e17 | 13 | @$(MAKE) -C benchmarks benchmark-e17 |
14 | |||
15 | examples: | ||
16 | @make -C lib | ||
17 | @$(MAKE) -C examples examples | ||
18 | |||
19 | install-examples: | ||
20 | @$(MAKE) -C examples install-examples | ||
diff --git a/src/examples/Makefile.am b/src/examples/Makefile.am new file mode 100644 index 0000000000..d6fbe3e3fe --- /dev/null +++ b/src/examples/Makefile.am | |||
@@ -0,0 +1,9 @@ | |||
1 | MAINTAINERCLEANFILES = Makefile.in | ||
2 | |||
3 | SUBDIRS = eina | ||
4 | |||
5 | examples: | ||
6 | @$(MAKE) -C eina examples | ||
7 | |||
8 | install-examples: | ||
9 | @$(MAKE) -C eina install-examples \ No newline at end of file | ||
diff --git a/src/examples/eina/Makefile.am b/src/examples/eina/Makefile.am new file mode 100644 index 0000000000..4656e47241 --- /dev/null +++ b/src/examples/eina/Makefile.am | |||
@@ -0,0 +1,120 @@ | |||
1 | MAINTAINERCLEANFILES = Makefile.in | ||
2 | |||
3 | AM_CPPFLAGS = \ | ||
4 | -I. \ | ||
5 | -I$(top_srcdir)/src/include/eina \ | ||
6 | -I$(top_builddir)/src/include/eina | ||
7 | |||
8 | LDADD = $(top_builddir)/src/lib/eina/libeina.la | ||
9 | |||
10 | SRCS = \ | ||
11 | eina_accessor_01.c \ | ||
12 | eina_array_01.c \ | ||
13 | eina_array_02.c \ | ||
14 | eina_error_01.c \ | ||
15 | eina_file_01.c \ | ||
16 | eina_hash_01.c \ | ||
17 | eina_hash_02.c \ | ||
18 | eina_hash_03.c \ | ||
19 | eina_hash_04.c \ | ||
20 | eina_hash_05.c \ | ||
21 | eina_hash_06.c \ | ||
22 | eina_hash_07.c \ | ||
23 | eina_hash_08.c \ | ||
24 | eina_iterator_01.c \ | ||
25 | eina_list_01.c \ | ||
26 | eina_list_02.c \ | ||
27 | eina_list_03.c \ | ||
28 | eina_list_04.c \ | ||
29 | eina_log_01.c \ | ||
30 | eina_log_02.c \ | ||
31 | eina_log_03.c \ | ||
32 | eina_inlist_01.c \ | ||
33 | eina_inlist_02.c \ | ||
34 | eina_inlist_03.c \ | ||
35 | eina_str_01.c \ | ||
36 | eina_strbuf_01.c \ | ||
37 | eina_stringshare_01.c \ | ||
38 | eina_tiler_01.c \ | ||
39 | eina_simple_xml_parser_01.c \ | ||
40 | eina_value_01.c \ | ||
41 | eina_value_02.c \ | ||
42 | eina_value_03.c \ | ||
43 | eina_inarray_01.c \ | ||
44 | eina_inarray_02.c | ||
45 | #eina_magic_01.c \ | ||
46 | #eina_model_01.c \ | ||
47 | #eina_model_02.c \ | ||
48 | #eina_model_03.c | ||
49 | |||
50 | EXTRA_PROGRAMS = \ | ||
51 | eina_accessor_01 \ | ||
52 | eina_array_01 \ | ||
53 | eina_array_02 \ | ||
54 | eina_error_01 \ | ||
55 | eina_file_01 \ | ||
56 | eina_hash_01 \ | ||
57 | eina_hash_02 \ | ||
58 | eina_hash_03 \ | ||
59 | eina_hash_04 \ | ||
60 | eina_hash_05 \ | ||
61 | eina_hash_06 \ | ||
62 | eina_hash_07 \ | ||
63 | eina_hash_08 \ | ||
64 | eina_iterator_01 \ | ||
65 | eina_list_01 \ | ||
66 | eina_list_02 \ | ||
67 | eina_list_03 \ | ||
68 | eina_list_04 \ | ||
69 | eina_log_01 \ | ||
70 | eina_log_02 \ | ||
71 | eina_log_03 \ | ||
72 | eina_inlist_01 \ | ||
73 | eina_inlist_02 \ | ||
74 | eina_inlist_03 \ | ||
75 | eina_str_01 \ | ||
76 | eina_strbuf_01 \ | ||
77 | eina_stringshare_01 \ | ||
78 | eina_magic_01 \ | ||
79 | eina_simple_xml_parser_01 \ | ||
80 | eina_value_01 \ | ||
81 | eina_value_02 \ | ||
82 | eina_value_03 \ | ||
83 | eina_inarray_01 \ | ||
84 | eina_inarray_02 | ||
85 | #eina_model_01 \ | ||
86 | #eina_model_02 \ | ||
87 | #eina_model_03 \ | ||
88 | #eina_model_04 | ||
89 | |||
90 | #eina_model_04_SOURCES = \ | ||
91 | #eina_model_04_animal.c \ | ||
92 | #eina_model_04_child.c \ | ||
93 | #eina_model_04_human.c \ | ||
94 | #eina_model_04_main.c \ | ||
95 | #eina_model_04_parrot.c \ | ||
96 | #eina_model_04_whistler.c \ | ||
97 | #eina_model_04_animal.h \ | ||
98 | #eina_model_04_child.h \ | ||
99 | #eina_model_04_human.h \ | ||
100 | #eina_model_04_parrot.h \ | ||
101 | #eina_model_04_whistler.h | ||
102 | |||
103 | if BUILD_TILER_EXAMPLE | ||
104 | AM_CPPFLAGS += @ECORE_EVAS_CFLAGS@ | ||
105 | EXTRA_PROGRAMS += eina_tiler_01 | ||
106 | eina_tiler_01_LDADD = $(top_builddir)/src/lib/eina/libeina.la @ECORE_EVAS_LIBS@ | ||
107 | endif | ||
108 | |||
109 | examples: $(EXTRA_PROGRAMS) | ||
110 | |||
111 | install-examples: | ||
112 | mkdir -p $(pkgdatadir)/examples | ||
113 | $(install_sh_DATA) -c $(SRCS) $(pkgdatadir)/examples | ||
114 | |||
115 | unsinstall-local: | ||
116 | for f in $(SRCS) ; do \ | ||
117 | rm -f $(pkgdatadir)/examples/$$f ; \ | ||
118 | done | ||
119 | |||
120 | EXTRA_DIST = addr_book.txt chat.xml | ||
diff --git a/src/examples/eina/addr_book.txt b/src/examples/eina/addr_book.txt new file mode 100644 index 0000000000..43abc44452 --- /dev/null +++ b/src/examples/eina/addr_book.txt | |||
@@ -0,0 +1,7 @@ | |||
1 | Kara Thrace starbuck@bsg.com | ||
2 | Sharon Valerii boomer@bsg.com | ||
3 | Sharon Aghaton athena@bsg.com | ||
4 | Karl Aghaton helo@bsg.com | ||
5 | Louanne Katraine kat@bsg.com | ||
6 | |||
7 | Lee Adama apolo@bsg.com | ||
diff --git a/src/examples/eina/chat.xml b/src/examples/eina/chat.xml new file mode 100644 index 0000000000..140269c717 --- /dev/null +++ b/src/examples/eina/chat.xml | |||
@@ -0,0 +1,24 @@ | |||
1 | <?xml version="1.0" encoding="UTF-8"?> | ||
2 | <!DOCTYPE board SYSTEM "tp-0.1.dtd"> | ||
3 | <board site="http://linuxfr.org/"> | ||
4 | <post id="3211"> | ||
5 | <login>houplaboom</login> | ||
6 | <message>Is this an XML test?</message> | ||
7 | </post> | ||
8 | <post id="3212"> | ||
9 | <login>fredx</login> | ||
10 | <message>I'm seeing bald people.</message> | ||
11 | </post> | ||
12 | <post id="3213"> | ||
13 | <login>NedFlanders</login> | ||
14 | <message>Don't call him!</message> | ||
15 | </post> | ||
16 | <post id="3214"> | ||
17 | <login>Single</login> | ||
18 | <message>I'm not bald!</message> | ||
19 | </post> | ||
20 | <post id="3215"> | ||
21 | <login>ngc891</login> | ||
22 | <message>It was an XML test.</message> | ||
23 | </post> | ||
24 | </board> | ||
diff --git a/src/examples/eina/eina_accessor_01.c b/src/examples/eina/eina_accessor_01.c new file mode 100644 index 0000000000..da413c6e72 --- /dev/null +++ b/src/examples/eina/eina_accessor_01.c | |||
@@ -0,0 +1,55 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_accessor_01.c -o eina_accessor_01 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | |||
6 | #include <Eina.h> | ||
7 | |||
8 | int | ||
9 | main(int argc, char **argv) | ||
10 | { | ||
11 | const char *strings[] = { | ||
12 | "even", "odd", "even", "odd", "even", "odd", "even", "odd", "even", "odd" | ||
13 | }; | ||
14 | const char *more_strings[] = { | ||
15 | "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" | ||
16 | }; | ||
17 | Eina_Array *array; | ||
18 | Eina_List *list = NULL; | ||
19 | Eina_Accessor *acc; | ||
20 | unsigned short int i; | ||
21 | void *data; | ||
22 | |||
23 | eina_init(); | ||
24 | |||
25 | array = eina_array_new(10); | ||
26 | |||
27 | for (i = 0; i < 10; i++) | ||
28 | { | ||
29 | eina_array_push(array, strings[i]); | ||
30 | list = eina_list_append(list, more_strings[i]); | ||
31 | } | ||
32 | |||
33 | acc = eina_array_accessor_new(array); | ||
34 | for(i = 1; i < 10; i += 2) | ||
35 | { | ||
36 | eina_accessor_data_get(acc, i, &data); | ||
37 | printf("%s\n", (const char *)data); | ||
38 | } | ||
39 | eina_accessor_free(acc); | ||
40 | eina_array_free(array); | ||
41 | |||
42 | acc = eina_list_accessor_new(list); | ||
43 | for(i = 1; i < 10; i += 2) | ||
44 | { | ||
45 | eina_accessor_data_get(acc, i, &data); | ||
46 | printf("%s\n", (const char *)data); | ||
47 | } | ||
48 | |||
49 | eina_list_free(eina_accessor_container_get(acc)); | ||
50 | eina_accessor_free(acc); | ||
51 | |||
52 | eina_shutdown(); | ||
53 | |||
54 | return 0; | ||
55 | } | ||
diff --git a/src/examples/eina/eina_array_01.c b/src/examples/eina/eina_array_01.c new file mode 100644 index 0000000000..43a332d87d --- /dev/null +++ b/src/examples/eina/eina_array_01.c | |||
@@ -0,0 +1,51 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_array_01.c -o eina_array_01 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <string.h> | ||
6 | |||
7 | #include <Eina.h> | ||
8 | |||
9 | static Eina_Bool | ||
10 | _print(const void *container, void *data, void *fdata) | ||
11 | { | ||
12 | printf("%s\n", (char *)data); | ||
13 | return EINA_TRUE; | ||
14 | } | ||
15 | |||
16 | int | ||
17 | main(int argc, char **argv) | ||
18 | { | ||
19 | const char* strings[] = { | ||
20 | "helo", "hera", "starbuck", "kat", "boomer", | ||
21 | "hotdog", "longshot", "jammer", "crashdown", "hardball", | ||
22 | "duck", "racetrack", "apolo", "husker", "freaker", | ||
23 | "skulls", "bulldog", "flat top", "hammerhead", "gonzo" | ||
24 | }; | ||
25 | Eina_Array *array; | ||
26 | Eina_Array_Iterator iterator; | ||
27 | char *item; | ||
28 | unsigned int i; | ||
29 | |||
30 | eina_init(); | ||
31 | |||
32 | array = eina_array_new(10); | ||
33 | eina_array_step_set(array, sizeof(*array), 20); | ||
34 | |||
35 | for (i = 0; i < 20; i++) | ||
36 | eina_array_push(array, strdup(strings[i])); | ||
37 | |||
38 | printf("array count: %d\n", eina_array_count(array)); | ||
39 | eina_array_foreach(array, _print, NULL); | ||
40 | |||
41 | printf("Top gun: %s\n", (char*)eina_array_data_get(array, 2)); | ||
42 | |||
43 | while (eina_array_count(array)) | ||
44 | free(eina_array_pop(array)); | ||
45 | |||
46 | eina_array_free(array); | ||
47 | |||
48 | eina_shutdown(); | ||
49 | |||
50 | return 0; | ||
51 | } | ||
diff --git a/src/examples/eina/eina_array_02.c b/src/examples/eina/eina_array_02.c new file mode 100644 index 0000000000..6b2903b21a --- /dev/null +++ b/src/examples/eina/eina_array_02.c | |||
@@ -0,0 +1,57 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_array_02.c -o eina_array_02 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <string.h> | ||
6 | |||
7 | #include <Eina.h> | ||
8 | |||
9 | Eina_Bool keep(void *data, void *gdata) | ||
10 | { | ||
11 | if (strlen((const char*)data) <= 5) | ||
12 | return EINA_TRUE; | ||
13 | return EINA_FALSE; | ||
14 | } | ||
15 | |||
16 | int | ||
17 | main(int argc, char **argv) | ||
18 | { | ||
19 | const char* strs[] = { | ||
20 | "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", | ||
21 | "ten", "eleven", "twelve", "thirteen", "fourtenn", "fifteen", "sixteen", | ||
22 | "seventeen", "eighteen", "nineteen", "twenty" | ||
23 | }; | ||
24 | const char* strings[] = { | ||
25 | "helo", "hera", "starbuck", "kat", "boomer", | ||
26 | "hotdog", "longshot", "jammer", "crashdown", "hardball", | ||
27 | "duck", "racetrack", "apolo", "husker", "freaker", | ||
28 | "skulls", "bulldog", "flat top", "hammerhead", "gonzo" | ||
29 | }; | ||
30 | Eina_Array *array; | ||
31 | Eina_Array_Iterator iterator; | ||
32 | const char *item; | ||
33 | unsigned int i; | ||
34 | |||
35 | eina_init(); | ||
36 | |||
37 | array = eina_array_new(10); | ||
38 | |||
39 | for (i = 0; i < 20; i++) | ||
40 | eina_array_push(array, strs[i]); | ||
41 | |||
42 | eina_array_clean(array); | ||
43 | for (i = 0; i < 20; i++) | ||
44 | eina_array_push(array, strings[i]); | ||
45 | |||
46 | eina_array_data_set(array, 17, "flattop"); | ||
47 | |||
48 | eina_array_remove(array, keep, NULL); | ||
49 | EINA_ARRAY_ITER_NEXT(array, i, item, iterator) | ||
50 | printf("item #%d: %s\n", i, item); | ||
51 | |||
52 | eina_array_free(array); | ||
53 | |||
54 | eina_shutdown(); | ||
55 | |||
56 | return 0; | ||
57 | } | ||
diff --git a/src/examples/eina/eina_error_01.c b/src/examples/eina/eina_error_01.c new file mode 100644 index 0000000000..25a8ab25f8 --- /dev/null +++ b/src/examples/eina/eina_error_01.c | |||
@@ -0,0 +1,81 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_error_01.c -o eina_error_01 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdlib.h> | ||
5 | #include <stdio.h> | ||
6 | |||
7 | #include <eina_main.h> | ||
8 | #include <eina_error.h> | ||
9 | |||
10 | Eina_Error MY_ERROR_NEGATIVE; | ||
11 | Eina_Error MY_ERROR_NULL; | ||
12 | |||
13 | void *data_new() | ||
14 | { | ||
15 | eina_error_set(0); | ||
16 | |||
17 | eina_error_set(MY_ERROR_NULL); | ||
18 | return NULL; | ||
19 | } | ||
20 | |||
21 | int test(int n) | ||
22 | { | ||
23 | eina_error_set(0); | ||
24 | |||
25 | if (n < 0) | ||
26 | { | ||
27 | eina_error_set(MY_ERROR_NEGATIVE); | ||
28 | return 0; | ||
29 | } | ||
30 | |||
31 | return 1; | ||
32 | } | ||
33 | |||
34 | int main(void) | ||
35 | { | ||
36 | void *data; | ||
37 | |||
38 | if (!eina_init()) | ||
39 | { | ||
40 | printf ("Error during the initialization of eina_error module\n"); | ||
41 | return EXIT_FAILURE; | ||
42 | } | ||
43 | |||
44 | MY_ERROR_NEGATIVE = eina_error_msg_static_register("Negative number"); | ||
45 | MY_ERROR_NULL = eina_error_msg_static_register("NULL pointer"); | ||
46 | |||
47 | data = data_new(); | ||
48 | if (!data) | ||
49 | { | ||
50 | Eina_Error err; | ||
51 | |||
52 | err = eina_error_get(); | ||
53 | if (err) | ||
54 | printf("Error during memory allocation: %s\n", | ||
55 | eina_error_msg_get(err)); | ||
56 | } | ||
57 | |||
58 | if (!test(0)) | ||
59 | { | ||
60 | Eina_Error err; | ||
61 | |||
62 | err = eina_error_get(); | ||
63 | if (err) | ||
64 | printf("Error during test function: %s\n", | ||
65 | eina_error_msg_get(err)); | ||
66 | } | ||
67 | |||
68 | if (!test(-1)) | ||
69 | { | ||
70 | Eina_Error err; | ||
71 | |||
72 | err = eina_error_get(); | ||
73 | if (err) | ||
74 | printf("Error during test function: %s\n", | ||
75 | eina_error_msg_get(err)); | ||
76 | } | ||
77 | |||
78 | eina_shutdown(); | ||
79 | |||
80 | return EXIT_SUCCESS; | ||
81 | } | ||
diff --git a/src/examples/eina/eina_file_01.c b/src/examples/eina/eina_file_01.c new file mode 100644 index 0000000000..2c945b3ad7 --- /dev/null +++ b/src/examples/eina/eina_file_01.c | |||
@@ -0,0 +1,45 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_file_01.c -o eina_file_01 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <Eina.h> | ||
6 | |||
7 | static void | ||
8 | _print_cb(const char *name, const char *path, void *data) | ||
9 | { | ||
10 | printf("file %s in %s\n", name, path); | ||
11 | } | ||
12 | |||
13 | int | ||
14 | main(int argc, char **argv) | ||
15 | { | ||
16 | Eina_Iterator *it; | ||
17 | const char *f_name; | ||
18 | const Eina_File_Direct_Info *f_info; | ||
19 | |||
20 | eina_init(); | ||
21 | |||
22 | eina_file_dir_list("/home/", EINA_FALSE, _print_cb, NULL); | ||
23 | |||
24 | it = eina_file_ls("/home/"); | ||
25 | EINA_ITERATOR_FOREACH(it, f_name) | ||
26 | { | ||
27 | printf("%s\n", f_name); | ||
28 | eina_stringshare_del(f_name); | ||
29 | } | ||
30 | eina_iterator_free(it); | ||
31 | |||
32 | it = eina_file_stat_ls("/home/"); | ||
33 | EINA_ITERATOR_FOREACH(it, f_info) | ||
34 | printf("%s if of type %d\n", f_info->path, f_info->type); | ||
35 | eina_iterator_free(it); | ||
36 | |||
37 | it = eina_file_direct_ls("/home/"); | ||
38 | EINA_ITERATOR_FOREACH(it, f_info) | ||
39 | printf("%s if of type %d\n", f_info->path, f_info->type); | ||
40 | eina_iterator_free(it); | ||
41 | |||
42 | eina_shutdown(); | ||
43 | |||
44 | return 0; | ||
45 | } | ||
diff --git a/src/examples/eina/eina_hash_01.c b/src/examples/eina/eina_hash_01.c new file mode 100644 index 0000000000..16418feeac --- /dev/null +++ b/src/examples/eina/eina_hash_01.c | |||
@@ -0,0 +1,198 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_hash_01.c -o eina_hash_01 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <string.h> | ||
6 | #include <Eina.h> | ||
7 | |||
8 | /* | ||
9 | * Eina Hash - phonebook | ||
10 | * | ||
11 | * This example demonstrate the use of Eina Hash by implementing a phonebook | ||
12 | * that stores its contact data into the hash. | ||
13 | * | ||
14 | * It indexes the phone numbers by Contact Full Name, so it's a hash with | ||
15 | * string keys. | ||
16 | */ | ||
17 | |||
18 | struct _Phone_Entry { | ||
19 | const char *name; // Full name. | ||
20 | const char *number; // Phone number. | ||
21 | }; | ||
22 | |||
23 | typedef struct _Phone_Entry Phone_Entry; | ||
24 | |||
25 | static Phone_Entry _start_entries[] = { | ||
26 | { "Wolfgang Amadeus Mozart", "+01 23 456-78910" }, | ||
27 | { "Ludwig van Beethoven", "+12 34 567-89101" }, | ||
28 | { "Richard Georg Strauss", "+23 45 678-91012" }, | ||
29 | { "Heitor Villa-Lobos", "+34 56 789-10123" }, | ||
30 | { NULL, NULL } | ||
31 | }; // _start_entries | ||
32 | |||
33 | static void | ||
34 | _phone_entry_free_cb(void *data) | ||
35 | { | ||
36 | free(data); | ||
37 | } | ||
38 | |||
39 | static Eina_Bool | ||
40 | _phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key, | ||
41 | void *data, void *fdata) | ||
42 | { | ||
43 | const char *name = key; | ||
44 | const char *number = data; | ||
45 | printf("%s: %s\n", name, number); | ||
46 | |||
47 | // Return EINA_FALSE to stop this callback from being called | ||
48 | return EINA_TRUE; | ||
49 | } | ||
50 | |||
51 | int | ||
52 | main(int argc, const char *argv[]) | ||
53 | { | ||
54 | Eina_Hash *phone_book = NULL; | ||
55 | int i; | ||
56 | const char *entry_name = "Heitor Villa-Lobos"; | ||
57 | char *phone = NULL; | ||
58 | Eina_Bool r; | ||
59 | Eina_Iterator *it; | ||
60 | void *data; | ||
61 | |||
62 | eina_init(); | ||
63 | |||
64 | phone_book = eina_hash_string_superfast_new(_phone_entry_free_cb); | ||
65 | |||
66 | // Add initial entries to our hash | ||
67 | for (i = 0; _start_entries[i].name != NULL; i++) | ||
68 | { | ||
69 | eina_hash_add(phone_book, _start_entries[i].name, | ||
70 | strdup(_start_entries[i].number)); | ||
71 | } | ||
72 | |||
73 | // Look for a specific entry and get its phone number | ||
74 | phone = eina_hash_find(phone_book, entry_name); | ||
75 | if (phone) | ||
76 | { | ||
77 | printf("Printing entry.\n"); | ||
78 | printf("Name: %s\n", entry_name); | ||
79 | printf("Number: %s\n\n", phone); | ||
80 | } | ||
81 | |||
82 | // Delete this entry | ||
83 | r = eina_hash_del(phone_book, entry_name, NULL); | ||
84 | printf("Hash entry successfully deleted? %d\n\n", r); | ||
85 | |||
86 | // Modify the pointer data of an entry and free the old one | ||
87 | phone = eina_hash_modify(phone_book, "Richard Georg Strauss", | ||
88 | strdup("+23 45 111-11111")); | ||
89 | free(phone); | ||
90 | |||
91 | // Modify or add an entry to the hash with eina_hash_set | ||
92 | // Let's first add a new entry | ||
93 | eina_error_set(0); | ||
94 | phone = eina_hash_set(phone_book, "Raul Seixas", | ||
95 | strdup("+55 01 234-56789")); | ||
96 | if (!phone) | ||
97 | { | ||
98 | Eina_Error err = eina_error_get(); | ||
99 | if (!err) | ||
100 | { | ||
101 | printf("No previous phone found for Raul Seixas. "); | ||
102 | printf("Creating new entry.\n"); | ||
103 | } | ||
104 | else | ||
105 | printf("Error when setting phone for Raul Seixas\n"); | ||
106 | } | ||
107 | else | ||
108 | { | ||
109 | printf("Old phone for Raul Seixas was %s\n", phone); | ||
110 | free(phone); | ||
111 | } | ||
112 | |||
113 | printf("\n"); | ||
114 | |||
115 | // Now change the phone number | ||
116 | eina_error_set(0); | ||
117 | phone = eina_hash_set(phone_book, "Raul Seixas", | ||
118 | strdup("+55 02 222-22222")); | ||
119 | if (phone) | ||
120 | { | ||
121 | printf("Changing phone for Raul Seixas to +55 02 222-22222. "); | ||
122 | printf("Old phone was %s\n", phone); | ||
123 | free(phone); | ||
124 | } | ||
125 | else | ||
126 | { | ||
127 | Eina_Error err = eina_error_get(); | ||
128 | if (err) | ||
129 | printf("Error when changing phone for Raul Seixas\n"); | ||
130 | else | ||
131 | { | ||
132 | printf("No previous phone found for Raul Seixas. "); | ||
133 | printf("Creating new entry.\n"); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | // There are many ways to iterate over our Phone book. | ||
138 | // First, iterate showing the names and associated numbers. | ||
139 | printf("List of phones:\n"); | ||
140 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
141 | printf("\n"); | ||
142 | |||
143 | // Now iterate using an iterator | ||
144 | printf("List of phones:\n"); | ||
145 | it = eina_hash_iterator_tuple_new(phone_book); | ||
146 | while (eina_iterator_next(it, &data)) | ||
147 | { | ||
148 | Eina_Hash_Tuple *t = data; | ||
149 | const char *name = t->key; | ||
150 | const char *number = t->data; | ||
151 | printf("%s: %s\n", name, number); | ||
152 | } | ||
153 | eina_iterator_free(it); // Always free the iterator after its use | ||
154 | printf("\n"); | ||
155 | |||
156 | // Just iterate over the keys (names) | ||
157 | printf("List of names in the phone book:\n"); | ||
158 | it = eina_hash_iterator_key_new(phone_book); | ||
159 | while (eina_iterator_next(it, &data)) | ||
160 | { | ||
161 | const char *name = data; | ||
162 | printf("%s\n", name); | ||
163 | } | ||
164 | eina_iterator_free(it); | ||
165 | printf("\n"); | ||
166 | |||
167 | // Just iterate over the data (numbers) | ||
168 | printf("List of numbers in the phone book:\n"); | ||
169 | it = eina_hash_iterator_data_new(phone_book); | ||
170 | while (eina_iterator_next(it, &data)) | ||
171 | { | ||
172 | const char *number = data; | ||
173 | printf("%s\n", number); | ||
174 | } | ||
175 | eina_iterator_free(it); | ||
176 | printf("\n"); | ||
177 | |||
178 | // Check how many items are in the phone book | ||
179 | printf("There are %d items in the hash.\n\n", | ||
180 | eina_hash_population(phone_book)); | ||
181 | |||
182 | // Change the name (key) on an entry | ||
183 | eina_hash_move(phone_book, "Raul Seixas", "Alceu Valenca"); | ||
184 | printf("List of phones after change:\n"); | ||
185 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
186 | printf("\n"); | ||
187 | |||
188 | // Empty the phone book, but don't destroy it | ||
189 | eina_hash_free_buckets(phone_book); | ||
190 | printf("There are %d items in the hash.\n\n", | ||
191 | eina_hash_population(phone_book)); | ||
192 | |||
193 | // Phone book could still be used, but we are freeing it since we are | ||
194 | // done for now | ||
195 | eina_hash_free(phone_book); | ||
196 | |||
197 | eina_shutdown(); | ||
198 | } | ||
diff --git a/src/examples/eina/eina_hash_02.c b/src/examples/eina/eina_hash_02.c new file mode 100644 index 0000000000..7baec93ea2 --- /dev/null +++ b/src/examples/eina/eina_hash_02.c | |||
@@ -0,0 +1,147 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_hash_02.c -o eina_hash_02 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <string.h> | ||
6 | #include <Eina.h> | ||
7 | |||
8 | /* | ||
9 | * Eina Hash - Two more types of hash | ||
10 | * | ||
11 | * This example demonstrate two other types of hash in action - using | ||
12 | * eina_hash_stringshared_new and eina_hash_new. | ||
13 | * | ||
14 | * It indexes the phone numbers by Contact Full Name, so it's a hash with string | ||
15 | * keys, exactly the same as the other example. | ||
16 | */ | ||
17 | |||
18 | struct _Phone_Entry { | ||
19 | const char *name; // Full name. | ||
20 | const char *number; // Phone number. | ||
21 | }; | ||
22 | |||
23 | typedef struct _Phone_Entry Phone_Entry; | ||
24 | |||
25 | static Phone_Entry _start_entries[] = { | ||
26 | { "Wolfgang Amadeus Mozart", "+01 23 456-78910" }, | ||
27 | { "Ludwig van Beethoven", "+12 34 567-89101" }, | ||
28 | { "Richard Georg Strauss", "+23 45 678-91012" }, | ||
29 | { "Heitor Villa-Lobos", "+34 56 789-10123" }, | ||
30 | { NULL, NULL } | ||
31 | }; | ||
32 | |||
33 | static void | ||
34 | _phone_entry_free_cb(void *data) | ||
35 | { | ||
36 | free(data); | ||
37 | } | ||
38 | |||
39 | static void | ||
40 | _phone_book_stringshared_free_cb(void *data) | ||
41 | { | ||
42 | Phone_Entry *e = data; | ||
43 | eina_stringshare_del(e->name); | ||
44 | eina_stringshare_del(e->number); | ||
45 | free(e); | ||
46 | } | ||
47 | |||
48 | static Eina_Bool | ||
49 | _phone_book_stringshared_foreach_cb(const Eina_Hash *phone_book, | ||
50 | const void *key, void *data, void *fdata) | ||
51 | { | ||
52 | Phone_Entry *e = data; | ||
53 | const char *name = e->name; // e->name == key | ||
54 | const char *number = e->number; | ||
55 | printf("%s: %s\n", name, number); | ||
56 | |||
57 | return EINA_TRUE; | ||
58 | } | ||
59 | |||
60 | static void | ||
61 | example_hash_stringshared(void) | ||
62 | { | ||
63 | Eina_Hash *phone_book = NULL; | ||
64 | int i; | ||
65 | |||
66 | // Create the hash as before | ||
67 | phone_book = eina_hash_stringshared_new(_phone_book_stringshared_free_cb); | ||
68 | |||
69 | // Add initial entries to our hash, using direct_add | ||
70 | for (i = 0; _start_entries[i].name != NULL; i++) | ||
71 | { | ||
72 | Phone_Entry *e = malloc(sizeof(Phone_Entry)); | ||
73 | e->name = eina_stringshare_add(_start_entries[i].name); | ||
74 | e->number = eina_stringshare_add(_start_entries[i].number); | ||
75 | // Since we are storing the key (name) in our struct, we can use | ||
76 | // eina_hash_direct_add. It could be used in the previous example | ||
77 | // too, since each key is already stored in the _start_entries | ||
78 | // static array, but we started it with the default add function. | ||
79 | eina_hash_direct_add(phone_book, e->name, e); | ||
80 | } | ||
81 | |||
82 | // Iterate over the elements | ||
83 | printf("List of phones:\n"); | ||
84 | eina_hash_foreach(phone_book, _phone_book_stringshared_foreach_cb, NULL); | ||
85 | printf("\n"); | ||
86 | |||
87 | eina_hash_free(phone_book); | ||
88 | } | ||
89 | |||
90 | static unsigned int | ||
91 | _phone_book_string_key_length(const char *key) | ||
92 | { | ||
93 | if (!key) | ||
94 | return 0; | ||
95 | |||
96 | return (int)strlen(key) + 1; | ||
97 | } | ||
98 | |||
99 | static int | ||
100 | _phone_book_string_key_cmp(const char *key1, int key1_length, | ||
101 | const char *key2, int key2_length) | ||
102 | { | ||
103 | return strcmp(key1, key2); | ||
104 | } | ||
105 | |||
106 | static void | ||
107 | example_hash_big(void) | ||
108 | { | ||
109 | Eina_Hash *phone_book = NULL; | ||
110 | int i; | ||
111 | const char *phone; | ||
112 | |||
113 | // Create the same hash as used in eina_hash_01.c, but | ||
114 | // use 1024 (2 ^ 10) buckets. | ||
115 | phone_book = eina_hash_new(EINA_KEY_LENGTH(_phone_book_string_key_length), | ||
116 | EINA_KEY_CMP(_phone_book_string_key_cmp), | ||
117 | EINA_KEY_HASH(eina_hash_superfast), | ||
118 | _phone_entry_free_cb, | ||
119 | 10); | ||
120 | for (i = 0; _start_entries[i].name != NULL; i++) | ||
121 | { | ||
122 | eina_hash_add(phone_book, _start_entries[i].name, | ||
123 | strdup(_start_entries[i].number)); | ||
124 | } | ||
125 | |||
126 | // Look for a specific entry and get its phone number | ||
127 | phone = eina_hash_find(phone_book, "Heitor Villa-Lobos"); | ||
128 | if (phone) | ||
129 | { | ||
130 | printf("Printing entry.\n"); | ||
131 | printf("Name: Heitor Villa-Lobos\n"); | ||
132 | printf("Number: %s\n\n", phone); | ||
133 | } | ||
134 | |||
135 | eina_hash_free(phone_book); | ||
136 | } | ||
137 | |||
138 | int | ||
139 | main(int argc, const char *argv[]) | ||
140 | { | ||
141 | eina_init(); | ||
142 | |||
143 | example_hash_stringshared(); | ||
144 | example_hash_big(); | ||
145 | |||
146 | eina_shutdown(); | ||
147 | } | ||
diff --git a/src/examples/eina/eina_hash_03.c b/src/examples/eina/eina_hash_03.c new file mode 100644 index 0000000000..ba543d4179 --- /dev/null +++ b/src/examples/eina/eina_hash_03.c | |||
@@ -0,0 +1,198 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_hash_03.c -o eina_hash_03 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <string.h> | ||
6 | #include <Eina.h> | ||
7 | |||
8 | /* | ||
9 | * Eina Hash - phonebook | ||
10 | * | ||
11 | * This example demonstrate the use of Eina Hash by implementing a phonebook | ||
12 | * that stores its contact data into the hash. | ||
13 | * | ||
14 | * It indexes the phone numbers by Contact Full Name, so it's a hash with | ||
15 | * string keys. | ||
16 | */ | ||
17 | |||
18 | struct _Phone_Entry { | ||
19 | const char *name; // Full name. | ||
20 | const char *number; // Phone number. | ||
21 | }; | ||
22 | |||
23 | typedef struct _Phone_Entry Phone_Entry; | ||
24 | |||
25 | static Phone_Entry _start_entries[] = { | ||
26 | { "Wolfgang Amadeus Mozart", "+01 23 456-78910" }, | ||
27 | { "Ludwig van Beethoven", "+12 34 567-89101" }, | ||
28 | { "Richard Georg Strauss", "+23 45 678-91012" }, | ||
29 | { "Heitor Villa-Lobos", "+34 56 789-10123" }, | ||
30 | { NULL, NULL } | ||
31 | }; // _start_entries | ||
32 | |||
33 | static void | ||
34 | _phone_entry_free_cb(void *data) | ||
35 | { | ||
36 | free(data); | ||
37 | } | ||
38 | |||
39 | static Eina_Bool | ||
40 | _phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key, | ||
41 | void *data, void *fdata) | ||
42 | { | ||
43 | const char *name = key; | ||
44 | const char *number = data; | ||
45 | printf("%s: %s\n", name, number); | ||
46 | |||
47 | // Return EINA_FALSE to stop this callback from being called | ||
48 | return EINA_TRUE; | ||
49 | } | ||
50 | |||
51 | int | ||
52 | main(int argc, const char *argv[]) | ||
53 | { | ||
54 | Eina_Hash *phone_book = NULL; | ||
55 | int i; | ||
56 | const char *entry_name = "Heitor Villa-Lobos"; | ||
57 | char *phone = NULL; | ||
58 | Eina_Bool r; | ||
59 | Eina_Iterator *it; | ||
60 | void *data; | ||
61 | |||
62 | eina_init(); | ||
63 | |||
64 | phone_book = eina_hash_string_small_new(_phone_entry_free_cb); | ||
65 | |||
66 | // Add initial entries to our hash | ||
67 | for (i = 0; _start_entries[i].name != NULL; i++) | ||
68 | { | ||
69 | eina_hash_add(phone_book, _start_entries[i].name, | ||
70 | strdup(_start_entries[i].number)); | ||
71 | } | ||
72 | |||
73 | // Look for a specific entry and get its phone number | ||
74 | phone = eina_hash_find(phone_book, entry_name); | ||
75 | if (phone) | ||
76 | { | ||
77 | printf("Printing entry.\n"); | ||
78 | printf("Name: %s\n", entry_name); | ||
79 | printf("Number: %s\n\n", phone); | ||
80 | } | ||
81 | |||
82 | // Delete this entry | ||
83 | r = eina_hash_del(phone_book, entry_name, NULL); | ||
84 | printf("Hash entry successfully deleted? %d\n\n", r); | ||
85 | |||
86 | // Modify the pointer data of an entry and free the old one | ||
87 | phone = eina_hash_modify(phone_book, "Richard Georg Strauss", | ||
88 | strdup("+23 45 111-11111")); | ||
89 | free(phone); | ||
90 | |||
91 | // Modify or add an entry to the hash with eina_hash_set | ||
92 | // Let's first add a new entry | ||
93 | eina_error_set(0); | ||
94 | phone = eina_hash_set(phone_book, "Raul Seixas", | ||
95 | strdup("+55 01 234-56789")); | ||
96 | if (!phone) | ||
97 | { | ||
98 | Eina_Error err = eina_error_get(); | ||
99 | if (!err) | ||
100 | { | ||
101 | printf("No previous phone found for Raul Seixas. "); | ||
102 | printf("Creating new entry.\n"); | ||
103 | } | ||
104 | else | ||
105 | printf("Error when setting phone for Raul Seixas\n"); | ||
106 | } | ||
107 | else | ||
108 | { | ||
109 | printf("Old phone for Raul Seixas was %s\n", phone); | ||
110 | free(phone); | ||
111 | } | ||
112 | |||
113 | printf("\n"); | ||
114 | |||
115 | // Now change the phone number | ||
116 | eina_error_set(0); | ||
117 | phone = eina_hash_set(phone_book, "Raul Seixas", | ||
118 | strdup("+55 02 222-22222")); | ||
119 | if (phone) | ||
120 | { | ||
121 | printf("Changing phone for Raul Seixas to +55 02 222-22222. "); | ||
122 | printf("Old phone was %s\n", phone); | ||
123 | free(phone); | ||
124 | } | ||
125 | else | ||
126 | { | ||
127 | Eina_Error err = eina_error_get(); | ||
128 | if (err) | ||
129 | printf("Error when changing phone for Raul Seixas\n"); | ||
130 | else | ||
131 | { | ||
132 | printf("No previous phone found for Raul Seixas. "); | ||
133 | printf("Creating new entry.\n"); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | // There are many ways to iterate over our Phone book. | ||
138 | // First, iterate showing the names and associated numbers. | ||
139 | printf("List of phones:\n"); | ||
140 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
141 | printf("\n"); | ||
142 | |||
143 | // Now iterate using an iterator | ||
144 | printf("List of phones:\n"); | ||
145 | it = eina_hash_iterator_tuple_new(phone_book); | ||
146 | while (eina_iterator_next(it, &data)) | ||
147 | { | ||
148 | Eina_Hash_Tuple *t = data; | ||
149 | const char *name = t->key; | ||
150 | const char *number = t->data; | ||
151 | printf("%s: %s\n", name, number); | ||
152 | } | ||
153 | eina_iterator_free(it); // Always free the iterator after its use | ||
154 | printf("\n"); | ||
155 | |||
156 | // Just iterate over the keys (names) | ||
157 | printf("List of names in the phone book:\n"); | ||
158 | it = eina_hash_iterator_key_new(phone_book); | ||
159 | while (eina_iterator_next(it, &data)) | ||
160 | { | ||
161 | const char *name = data; | ||
162 | printf("%s\n", name); | ||
163 | } | ||
164 | eina_iterator_free(it); | ||
165 | printf("\n"); | ||
166 | |||
167 | // Just iterate over the data (numbers) | ||
168 | printf("List of numbers in the phone book:\n"); | ||
169 | it = eina_hash_iterator_data_new(phone_book); | ||
170 | while (eina_iterator_next(it, &data)) | ||
171 | { | ||
172 | const char *number = data; | ||
173 | printf("%s\n", number); | ||
174 | } | ||
175 | eina_iterator_free(it); | ||
176 | printf("\n"); | ||
177 | |||
178 | // Check how many items are in the phone book | ||
179 | printf("There are %d items in the hash.\n\n", | ||
180 | eina_hash_population(phone_book)); | ||
181 | |||
182 | // Change the name (key) on an entry | ||
183 | eina_hash_move(phone_book, "Raul Seixas", "Alceu Valenca"); | ||
184 | printf("List of phones after change:\n"); | ||
185 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
186 | printf("\n"); | ||
187 | |||
188 | // Empty the phone book, but don't destroy it | ||
189 | eina_hash_free_buckets(phone_book); | ||
190 | printf("There are %d items in the hash.\n\n", | ||
191 | eina_hash_population(phone_book)); | ||
192 | |||
193 | // Phone book could still be used, but we are freeing it since we are | ||
194 | // done for now | ||
195 | eina_hash_free(phone_book); | ||
196 | |||
197 | eina_shutdown(); | ||
198 | } | ||
diff --git a/src/examples/eina/eina_hash_04.c b/src/examples/eina/eina_hash_04.c new file mode 100644 index 0000000000..da73e04b1b --- /dev/null +++ b/src/examples/eina/eina_hash_04.c | |||
@@ -0,0 +1,198 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_hash_04.c -o eina_hash_04 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <string.h> | ||
6 | #include <Eina.h> | ||
7 | |||
8 | /* | ||
9 | * Eina Hash - phonebook | ||
10 | * | ||
11 | * This example demonstrate the use of Eina Hash by implementing a phonebook | ||
12 | * that stores its contact data into the hash. | ||
13 | * | ||
14 | * It indexes the phone numbers by Contact Full Name, so it's a hash with | ||
15 | * string keys. | ||
16 | */ | ||
17 | |||
18 | struct _Phone_Entry { | ||
19 | const char *name; // Full name. | ||
20 | const char *number; // Phone number. | ||
21 | }; | ||
22 | |||
23 | typedef struct _Phone_Entry Phone_Entry; | ||
24 | |||
25 | static Phone_Entry _start_entries[] = { | ||
26 | { "Wolfgang Amadeus Mozart", "+01 23 456-78910" }, | ||
27 | { "Ludwig van Beethoven", "+12 34 567-89101" }, | ||
28 | { "Richard Georg Strauss", "+23 45 678-91012" }, | ||
29 | { "Heitor Villa-Lobos", "+34 56 789-10123" }, | ||
30 | { NULL, NULL } | ||
31 | }; // _start_entries | ||
32 | |||
33 | static void | ||
34 | _phone_entry_free_cb(void *data) | ||
35 | { | ||
36 | free(data); | ||
37 | } | ||
38 | |||
39 | static Eina_Bool | ||
40 | _phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key, | ||
41 | void *data, void *fdata) | ||
42 | { | ||
43 | const char *name = key; | ||
44 | const char *number = data; | ||
45 | printf("%s: %s\n", name, number); | ||
46 | |||
47 | // Return EINA_FALSE to stop this callback from being called | ||
48 | return EINA_TRUE; | ||
49 | } | ||
50 | |||
51 | int | ||
52 | main(int argc, const char *argv[]) | ||
53 | { | ||
54 | Eina_Hash *phone_book = NULL; | ||
55 | int i; | ||
56 | const char *entry_name = "Heitor Villa-Lobos"; | ||
57 | char *phone = NULL; | ||
58 | Eina_Bool r; | ||
59 | Eina_Iterator *it; | ||
60 | void *data; | ||
61 | |||
62 | eina_init(); | ||
63 | |||
64 | phone_book = eina_hash_string_djb2_new(_phone_entry_free_cb); | ||
65 | |||
66 | // Add initial entries to our hash | ||
67 | for (i = 0; _start_entries[i].name != NULL; i++) | ||
68 | { | ||
69 | eina_hash_add(phone_book, _start_entries[i].name, | ||
70 | strdup(_start_entries[i].number)); | ||
71 | } | ||
72 | |||
73 | // Look for a specific entry and get its phone number | ||
74 | phone = eina_hash_find(phone_book, entry_name); | ||
75 | if (phone) | ||
76 | { | ||
77 | printf("Printing entry.\n"); | ||
78 | printf("Name: %s\n", entry_name); | ||
79 | printf("Number: %s\n\n", phone); | ||
80 | } | ||
81 | |||
82 | // Delete this entry | ||
83 | r = eina_hash_del(phone_book, entry_name, NULL); | ||
84 | printf("Hash entry successfully deleted? %d\n\n", r); | ||
85 | |||
86 | // Modify the pointer data of an entry and free the old one | ||
87 | phone = eina_hash_modify(phone_book, "Richard Georg Strauss", | ||
88 | strdup("+23 45 111-11111")); | ||
89 | free(phone); | ||
90 | |||
91 | // Modify or add an entry to the hash with eina_hash_set | ||
92 | // Let's first add a new entry | ||
93 | eina_error_set(0); | ||
94 | phone = eina_hash_set(phone_book, "Raul Seixas", | ||
95 | strdup("+55 01 234-56789")); | ||
96 | if (!phone) | ||
97 | { | ||
98 | Eina_Error err = eina_error_get(); | ||
99 | if (!err) | ||
100 | { | ||
101 | printf("No previous phone found for Raul Seixas. "); | ||
102 | printf("Creating new entry.\n"); | ||
103 | } | ||
104 | else | ||
105 | printf("Error when setting phone for Raul Seixas\n"); | ||
106 | } | ||
107 | else | ||
108 | { | ||
109 | printf("Old phone for Raul Seixas was %s\n", phone); | ||
110 | free(phone); | ||
111 | } | ||
112 | |||
113 | printf("\n"); | ||
114 | |||
115 | // Now change the phone number | ||
116 | eina_error_set(0); | ||
117 | phone = eina_hash_set(phone_book, "Raul Seixas", | ||
118 | strdup("+55 02 222-22222")); | ||
119 | if (phone) | ||
120 | { | ||
121 | printf("Changing phone for Raul Seixas to +55 02 222-22222. "); | ||
122 | printf("Old phone was %s\n", phone); | ||
123 | free(phone); | ||
124 | } | ||
125 | else | ||
126 | { | ||
127 | Eina_Error err = eina_error_get(); | ||
128 | if (err) | ||
129 | printf("Error when changing phone for Raul Seixas\n"); | ||
130 | else | ||
131 | { | ||
132 | printf("No previous phone found for Raul Seixas. "); | ||
133 | printf("Creating new entry.\n"); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | // There are many ways to iterate over our Phone book. | ||
138 | // First, iterate showing the names and associated numbers. | ||
139 | printf("List of phones:\n"); | ||
140 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
141 | printf("\n"); | ||
142 | |||
143 | // Now iterate using an iterator | ||
144 | printf("List of phones:\n"); | ||
145 | it = eina_hash_iterator_tuple_new(phone_book); | ||
146 | while (eina_iterator_next(it, &data)) | ||
147 | { | ||
148 | Eina_Hash_Tuple *t = data; | ||
149 | const char *name = t->key; | ||
150 | const char *number = t->data; | ||
151 | printf("%s: %s\n", name, number); | ||
152 | } | ||
153 | eina_iterator_free(it); // Always free the iterator after its use | ||
154 | printf("\n"); | ||
155 | |||
156 | // Just iterate over the keys (names) | ||
157 | printf("List of names in the phone book:\n"); | ||
158 | it = eina_hash_iterator_key_new(phone_book); | ||
159 | while (eina_iterator_next(it, &data)) | ||
160 | { | ||
161 | const char *name = data; | ||
162 | printf("%s\n", name); | ||
163 | } | ||
164 | eina_iterator_free(it); | ||
165 | printf("\n"); | ||
166 | |||
167 | // Just iterate over the data (numbers) | ||
168 | printf("List of numbers in the phone book:\n"); | ||
169 | it = eina_hash_iterator_data_new(phone_book); | ||
170 | while (eina_iterator_next(it, &data)) | ||
171 | { | ||
172 | const char *number = data; | ||
173 | printf("%s\n", number); | ||
174 | } | ||
175 | eina_iterator_free(it); | ||
176 | printf("\n"); | ||
177 | |||
178 | // Check how many items are in the phone book | ||
179 | printf("There are %d items in the hash.\n\n", | ||
180 | eina_hash_population(phone_book)); | ||
181 | |||
182 | // Change the name (key) on an entry | ||
183 | eina_hash_move(phone_book, "Raul Seixas", "Alceu Valenca"); | ||
184 | printf("List of phones after change:\n"); | ||
185 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
186 | printf("\n"); | ||
187 | |||
188 | // Empty the phone book, but don't destroy it | ||
189 | eina_hash_free_buckets(phone_book); | ||
190 | printf("There are %d items in the hash.\n\n", | ||
191 | eina_hash_population(phone_book)); | ||
192 | |||
193 | // Phone book could still be used, but we are freeing it since we are | ||
194 | // done for now | ||
195 | eina_hash_free(phone_book); | ||
196 | |||
197 | eina_shutdown(); | ||
198 | } | ||
diff --git a/src/examples/eina/eina_hash_05.c b/src/examples/eina/eina_hash_05.c new file mode 100644 index 0000000000..6666ce2d3d --- /dev/null +++ b/src/examples/eina/eina_hash_05.c | |||
@@ -0,0 +1,201 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_hash_05.c -o eina_hash_05 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <string.h> | ||
6 | #include <Eina.h> | ||
7 | |||
8 | /* | ||
9 | * Eina Hash - phonebook | ||
10 | * | ||
11 | * This example demonstrate the use of Eina Hash by implementing a phonebook | ||
12 | * that stores its contact data into the hash. | ||
13 | * | ||
14 | * It indexes the phone numbers by Contact Full Name, so it's a hash with | ||
15 | * string keys. | ||
16 | */ | ||
17 | |||
18 | struct _Phone_Entry { | ||
19 | int32_t id; // Full name. | ||
20 | const char *number; // Phone number. | ||
21 | }; | ||
22 | |||
23 | typedef struct _Phone_Entry Phone_Entry; | ||
24 | |||
25 | static Phone_Entry _start_entries[] = { | ||
26 | { 1, "+01 23 456-78910" }, | ||
27 | { 2, "+12 34 567-89101" }, | ||
28 | { 3, "+23 45 678-91012" }, | ||
29 | { 4, "+34 56 789-10123" }, | ||
30 | { -1, NULL } | ||
31 | }; // _start_entries | ||
32 | |||
33 | static void | ||
34 | _phone_entry_free_cb(void *data) | ||
35 | { | ||
36 | free(data); | ||
37 | } | ||
38 | |||
39 | static Eina_Bool | ||
40 | _phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key, | ||
41 | void *data, void *fdata) | ||
42 | { | ||
43 | const int32_t *id = key; | ||
44 | const char *number = data; | ||
45 | printf("%d: %s\n", *id, number); | ||
46 | |||
47 | // Return EINA_FALSE to stop this callback from being called | ||
48 | return EINA_TRUE; | ||
49 | } | ||
50 | |||
51 | int | ||
52 | main(int argc, const char *argv[]) | ||
53 | { | ||
54 | Eina_Hash *phone_book = NULL; | ||
55 | int i; | ||
56 | int32_t entry_id = 4; | ||
57 | char *phone = NULL; | ||
58 | Eina_Bool r; | ||
59 | Eina_Iterator *it; | ||
60 | void *data; | ||
61 | |||
62 | eina_init(); | ||
63 | |||
64 | phone_book = eina_hash_int32_new(_phone_entry_free_cb); | ||
65 | |||
66 | // Add initial entries to our hash | ||
67 | for (i = 0; _start_entries[i].id != -1; i++) | ||
68 | { | ||
69 | eina_hash_add(phone_book, &_start_entries[i].id, | ||
70 | strdup(_start_entries[i].number)); | ||
71 | } | ||
72 | |||
73 | // Look for a specific entry and get its phone number | ||
74 | phone = eina_hash_find(phone_book, &entry_id); | ||
75 | if (phone) | ||
76 | { | ||
77 | printf("Printing entry.\n"); | ||
78 | printf("Id: %d\n", entry_id); | ||
79 | printf("Number: %s\n\n", phone); | ||
80 | } | ||
81 | |||
82 | // Delete this entry | ||
83 | r = eina_hash_del(phone_book, &entry_id, NULL); | ||
84 | printf("Hash entry successfully deleted? %d\n\n", r); | ||
85 | |||
86 | // Modify the pointer data of an entry and free the old one | ||
87 | int32_t id3 = 3; | ||
88 | phone = eina_hash_modify(phone_book, &id3, | ||
89 | strdup("+23 45 111-11111")); | ||
90 | free(phone); | ||
91 | |||
92 | // Modify or add an entry to the hash with eina_hash_set | ||
93 | // Let's first add a new entry | ||
94 | int32_t id5 = 5; | ||
95 | eina_error_set(0); | ||
96 | phone = eina_hash_set(phone_book, &id5, | ||
97 | strdup("+55 01 234-56789")); | ||
98 | if (!phone) | ||
99 | { | ||
100 | Eina_Error err = eina_error_get(); | ||
101 | if (!err) | ||
102 | { | ||
103 | printf("No previous phone found for id5. "); | ||
104 | printf("Creating new entry.\n"); | ||
105 | } | ||
106 | else | ||
107 | printf("Error when setting phone for Raul Seixas\n"); | ||
108 | } | ||
109 | else | ||
110 | { | ||
111 | printf("Old phone for id5 was %s\n", phone); | ||
112 | free(phone); | ||
113 | } | ||
114 | |||
115 | printf("\n"); | ||
116 | |||
117 | // Now change the phone number | ||
118 | eina_error_set(0); | ||
119 | phone = eina_hash_set(phone_book, &id5, | ||
120 | strdup("+55 02 222-22222")); | ||
121 | if (phone) | ||
122 | { | ||
123 | printf("Changing phone for id5 to +55 02 222-22222. "); | ||
124 | printf("Old phone was %s\n", phone); | ||
125 | free(phone); | ||
126 | } | ||
127 | else | ||
128 | { | ||
129 | Eina_Error err = eina_error_get(); | ||
130 | if (err) | ||
131 | printf("Error when changing phone for id5\n"); | ||
132 | else | ||
133 | { | ||
134 | printf("No previous phone found for id5. "); | ||
135 | printf("Creating new entry.\n"); | ||
136 | } | ||
137 | } | ||
138 | |||
139 | // There are many ways to iterate over our Phone book. | ||
140 | // First, iterate showing the names and associated numbers. | ||
141 | printf("List of phones:\n"); | ||
142 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
143 | printf("\n"); | ||
144 | |||
145 | // Now iterate using an iterator | ||
146 | printf("List of phones:\n"); | ||
147 | it = eina_hash_iterator_tuple_new(phone_book); | ||
148 | while (eina_iterator_next(it, &data)) | ||
149 | { | ||
150 | Eina_Hash_Tuple *t = data; | ||
151 | const int32_t *id = t->key; | ||
152 | const char *number = t->data; | ||
153 | printf("%d: %s\n", *id, number); | ||
154 | } | ||
155 | eina_iterator_free(it); // Always free the iterator after its use | ||
156 | printf("\n"); | ||
157 | |||
158 | // Just iterate over the keys (names) | ||
159 | printf("List of ids in the phone book:\n"); | ||
160 | it = eina_hash_iterator_key_new(phone_book); | ||
161 | while (eina_iterator_next(it, &data)) | ||
162 | { | ||
163 | const int32_t *id = data; | ||
164 | printf("%d\n", *id); | ||
165 | } | ||
166 | eina_iterator_free(it); | ||
167 | printf("\n"); | ||
168 | |||
169 | // Just iterate over the data (numbers) | ||
170 | printf("List of numbers in the phone book:\n"); | ||
171 | it = eina_hash_iterator_data_new(phone_book); | ||
172 | while (eina_iterator_next(it, &data)) | ||
173 | { | ||
174 | const char *number = data; | ||
175 | printf("%s\n", number); | ||
176 | } | ||
177 | eina_iterator_free(it); | ||
178 | printf("\n"); | ||
179 | |||
180 | // Check how many items are in the phone book | ||
181 | printf("There are %d items in the hash.\n\n", | ||
182 | eina_hash_population(phone_book)); | ||
183 | |||
184 | // Change the name (key) on an entry | ||
185 | int32_t id6 = 6; | ||
186 | eina_hash_move(phone_book, &id5, &id6); | ||
187 | printf("List of phones after change:\n"); | ||
188 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
189 | printf("\n"); | ||
190 | |||
191 | // Empty the phone book, but don't destroy it | ||
192 | eina_hash_free_buckets(phone_book); | ||
193 | printf("There are %d items in the hash.\n\n", | ||
194 | eina_hash_population(phone_book)); | ||
195 | |||
196 | // Phone book could still be used, but we are freeing it since we are | ||
197 | // done for now | ||
198 | eina_hash_free(phone_book); | ||
199 | |||
200 | eina_shutdown(); | ||
201 | } | ||
diff --git a/src/examples/eina/eina_hash_06.c b/src/examples/eina/eina_hash_06.c new file mode 100644 index 0000000000..a7a5504d2a --- /dev/null +++ b/src/examples/eina/eina_hash_06.c | |||
@@ -0,0 +1,201 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_hash_06.c -o eina_hash_06 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <string.h> | ||
6 | #include <Eina.h> | ||
7 | |||
8 | /* | ||
9 | * Eina Hash - phonebook | ||
10 | * | ||
11 | * This example demonstrate the use of Eina Hash by implementing a phonebook | ||
12 | * that stores its contact data into the hash. | ||
13 | * | ||
14 | * It indexes the phone numbers by Contact Full Name, so it's a hash with | ||
15 | * string keys. | ||
16 | */ | ||
17 | |||
18 | struct _Phone_Entry { | ||
19 | int64_t id; // Full name. | ||
20 | const char *number; // Phone number. | ||
21 | }; | ||
22 | |||
23 | typedef struct _Phone_Entry Phone_Entry; | ||
24 | |||
25 | static Phone_Entry _start_entries[] = { | ||
26 | { 1, "+01 23 456-78910" }, | ||
27 | { 2, "+12 34 567-89101" }, | ||
28 | { 3, "+23 45 678-91012" }, | ||
29 | { 4, "+34 56 789-10123" }, | ||
30 | { -1, NULL } | ||
31 | }; // _start_entries | ||
32 | |||
33 | static void | ||
34 | _phone_entry_free_cb(void *data) | ||
35 | { | ||
36 | free(data); | ||
37 | } | ||
38 | |||
39 | static Eina_Bool | ||
40 | _phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key, | ||
41 | void *data, void *fdata) | ||
42 | { | ||
43 | const int64_t *id = key; | ||
44 | const char *number = data; | ||
45 | printf("%lld: %s\n", *id, number); | ||
46 | |||
47 | // Return EINA_FALSE to stop this callback from being called | ||
48 | return EINA_TRUE; | ||
49 | } | ||
50 | |||
51 | int | ||
52 | main(int argc, const char *argv[]) | ||
53 | { | ||
54 | Eina_Hash *phone_book = NULL; | ||
55 | int i; | ||
56 | int64_t entry_id = 4; | ||
57 | char *phone = NULL; | ||
58 | Eina_Bool r; | ||
59 | Eina_Iterator *it; | ||
60 | void *data; | ||
61 | |||
62 | eina_init(); | ||
63 | |||
64 | phone_book = eina_hash_int64_new(_phone_entry_free_cb); | ||
65 | |||
66 | // Add initial entries to our hash | ||
67 | for (i = 0; _start_entries[i].id != -1; i++) | ||
68 | { | ||
69 | eina_hash_add(phone_book, &_start_entries[i].id, | ||
70 | strdup(_start_entries[i].number)); | ||
71 | } | ||
72 | |||
73 | // Look for a specific entry and get its phone number | ||
74 | phone = eina_hash_find(phone_book, &entry_id); | ||
75 | if (phone) | ||
76 | { | ||
77 | printf("Printing entry.\n"); | ||
78 | printf("Id: %lld\n", entry_id); | ||
79 | printf("Number: %s\n\n", phone); | ||
80 | } | ||
81 | |||
82 | // Delete this entry | ||
83 | r = eina_hash_del(phone_book, &entry_id, NULL); | ||
84 | printf("Hash entry successfully deleted? %d\n\n", r); | ||
85 | |||
86 | // Modify the pointer data of an entry and free the old one | ||
87 | int64_t id3 = 3; | ||
88 | phone = eina_hash_modify(phone_book, &id3, | ||
89 | strdup("+23 45 111-11111")); | ||
90 | free(phone); | ||
91 | |||
92 | // Modify or add an entry to the hash with eina_hash_set | ||
93 | // Let's first add a new entry | ||
94 | int64_t id5 = 5; | ||
95 | eina_error_set(0); | ||
96 | phone = eina_hash_set(phone_book, &id5, | ||
97 | strdup("+55 01 234-56789")); | ||
98 | if (!phone) | ||
99 | { | ||
100 | Eina_Error err = eina_error_get(); | ||
101 | if (!err) | ||
102 | { | ||
103 | printf("No previous phone found for id5. "); | ||
104 | printf("Creating new entry.\n"); | ||
105 | } | ||
106 | else | ||
107 | printf("Error when setting phone for Raul Seixas\n"); | ||
108 | } | ||
109 | else | ||
110 | { | ||
111 | printf("Old phone for id5 was %s\n", phone); | ||
112 | free(phone); | ||
113 | } | ||
114 | |||
115 | printf("\n"); | ||
116 | |||
117 | // Now change the phone number | ||
118 | eina_error_set(0); | ||
119 | phone = eina_hash_set(phone_book, &id5, | ||
120 | strdup("+55 02 222-22222")); | ||
121 | if (phone) | ||
122 | { | ||
123 | printf("Changing phone for id5 to +55 02 222-22222. "); | ||
124 | printf("Old phone was %s\n", phone); | ||
125 | free(phone); | ||
126 | } | ||
127 | else | ||
128 | { | ||
129 | Eina_Error err = eina_error_get(); | ||
130 | if (err) | ||
131 | printf("Error when changing phone for id5\n"); | ||
132 | else | ||
133 | { | ||
134 | printf("No previous phone found for id5. "); | ||
135 | printf("Creating new entry.\n"); | ||
136 | } | ||
137 | } | ||
138 | |||
139 | // There are many ways to iterate over our Phone book. | ||
140 | // First, iterate showing the names and associated numbers. | ||
141 | printf("List of phones:\n"); | ||
142 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
143 | printf("\n"); | ||
144 | |||
145 | // Now iterate using an iterator | ||
146 | printf("List of phones:\n"); | ||
147 | it = eina_hash_iterator_tuple_new(phone_book); | ||
148 | while (eina_iterator_next(it, &data)) | ||
149 | { | ||
150 | Eina_Hash_Tuple *t = data; | ||
151 | const int64_t *id = t->key; | ||
152 | const char *number = t->data; | ||
153 | printf("%lld: %s\n", *id, number); | ||
154 | } | ||
155 | eina_iterator_free(it); // Always free the iterator after its use | ||
156 | printf("\n"); | ||
157 | |||
158 | // Just iterate over the keys (names) | ||
159 | printf("List of ids in the phone book:\n"); | ||
160 | it = eina_hash_iterator_key_new(phone_book); | ||
161 | while (eina_iterator_next(it, &data)) | ||
162 | { | ||
163 | const int64_t *id = data; | ||
164 | printf("%lld\n", *id); | ||
165 | } | ||
166 | eina_iterator_free(it); | ||
167 | printf("\n"); | ||
168 | |||
169 | // Just iterate over the data (numbers) | ||
170 | printf("List of numbers in the phone book:\n"); | ||
171 | it = eina_hash_iterator_data_new(phone_book); | ||
172 | while (eina_iterator_next(it, &data)) | ||
173 | { | ||
174 | const char *number = data; | ||
175 | printf("%s\n", number); | ||
176 | } | ||
177 | eina_iterator_free(it); | ||
178 | printf("\n"); | ||
179 | |||
180 | // Check how many items are in the phone book | ||
181 | printf("There are %d items in the hash.\n\n", | ||
182 | eina_hash_population(phone_book)); | ||
183 | |||
184 | // Change the name (key) on an entry | ||
185 | int64_t id6 = 6; | ||
186 | eina_hash_move(phone_book, &id5, &id6); | ||
187 | printf("List of phones after change:\n"); | ||
188 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
189 | printf("\n"); | ||
190 | |||
191 | // Empty the phone book, but don't destroy it | ||
192 | eina_hash_free_buckets(phone_book); | ||
193 | printf("There are %d items in the hash.\n\n", | ||
194 | eina_hash_population(phone_book)); | ||
195 | |||
196 | // Phone book could still be used, but we are freeing it since we are | ||
197 | // done for now | ||
198 | eina_hash_free(phone_book); | ||
199 | |||
200 | eina_shutdown(); | ||
201 | } | ||
diff --git a/src/examples/eina/eina_hash_07.c b/src/examples/eina/eina_hash_07.c new file mode 100644 index 0000000000..1464e6b417 --- /dev/null +++ b/src/examples/eina/eina_hash_07.c | |||
@@ -0,0 +1,222 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_hash_07.c -o eina_hash_07 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <string.h> | ||
6 | #include <Eina.h> | ||
7 | |||
8 | /* | ||
9 | * Eina Hash - phonebook | ||
10 | * | ||
11 | * This example demonstrate the use of Eina Hash by implementing a phonebook | ||
12 | * that stores its contact data into the hash. | ||
13 | * | ||
14 | * It indexes the phone numbers by Contact Full Name, so it's a hash with | ||
15 | * string keys. | ||
16 | */ | ||
17 | |||
18 | struct _Phone_Entry { | ||
19 | const char *name; // Full name. | ||
20 | const char *number; // Phone number. | ||
21 | }; | ||
22 | |||
23 | typedef struct _Phone_Entry Phone_Entry; | ||
24 | |||
25 | static Phone_Entry _start_entries[] = { | ||
26 | { "Wolfgang Amadeus Mozart", "+01 23 456-78910" }, | ||
27 | { "Ludwig van Beethoven", "+12 34 567-89101" }, | ||
28 | { "Richard Georg Strauss", "+23 45 678-91012" }, | ||
29 | { "Heitor Villa-Lobos", "+34 56 789-10123" }, | ||
30 | { NULL, NULL } | ||
31 | }; // _start_entries | ||
32 | |||
33 | static const char *_nicknames[] = { | ||
34 | "mozzart", | ||
35 | "betho", | ||
36 | "george", | ||
37 | "hector", | ||
38 | NULL | ||
39 | }; | ||
40 | |||
41 | static void | ||
42 | _phone_entry_free_cb(void *data) | ||
43 | { | ||
44 | free(data); | ||
45 | } | ||
46 | |||
47 | static Eina_Bool | ||
48 | _phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key, | ||
49 | void *data, void *fdata) | ||
50 | { | ||
51 | Phone_Entry **pe = (Phone_Entry **)key; | ||
52 | const char *nick = data; | ||
53 | printf("%s: %s, nick=%s\n", (*pe)->name, (*pe)->number, nick); | ||
54 | |||
55 | // Return EINA_FALSE to stop this callback from being called | ||
56 | return EINA_TRUE; | ||
57 | } | ||
58 | |||
59 | int | ||
60 | main(int argc, const char *argv[]) | ||
61 | { | ||
62 | Eina_Hash *phone_book = NULL; | ||
63 | int i; | ||
64 | Phone_Entry *entry_vl = &_start_entries[3]; | ||
65 | Phone_Entry *p = NULL; | ||
66 | char *nick = NULL; | ||
67 | Eina_Bool r; | ||
68 | Eina_Iterator *it; | ||
69 | void *data; | ||
70 | |||
71 | eina_init(); | ||
72 | |||
73 | phone_book = eina_hash_pointer_new(_phone_entry_free_cb); | ||
74 | |||
75 | // Add initial entries to our hash | ||
76 | for (i = 0; _start_entries[i].name != NULL; i++) | ||
77 | { | ||
78 | p = &_start_entries[i]; | ||
79 | eina_hash_add(phone_book, &p, | ||
80 | strdup(_nicknames[i])); | ||
81 | } | ||
82 | printf("Phonebook:\n"); | ||
83 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
84 | printf("\n"); | ||
85 | |||
86 | // Look for a specific entry and get its nickname | ||
87 | nick = eina_hash_find(phone_book, &entry_vl); | ||
88 | if (nick) | ||
89 | { | ||
90 | printf("Printing entry.\n"); | ||
91 | printf("Name: %s\n", entry_vl->name); | ||
92 | printf("Number: %s\n", entry_vl->number); | ||
93 | printf("Nick: %s\n\n", nick); | ||
94 | } | ||
95 | |||
96 | // Delete this entry | ||
97 | r = eina_hash_del(phone_book, &entry_vl, NULL); | ||
98 | printf("Hash entry successfully deleted? %d\n\n", r); | ||
99 | |||
100 | // Modify the pointer data of an entry and free the old one | ||
101 | p = &_start_entries[2]; | ||
102 | nick = eina_hash_modify(phone_book, &p, | ||
103 | strdup("el jorge")); | ||
104 | free(nick); | ||
105 | |||
106 | // Modify or add an entry to the hash with eina_hash_set | ||
107 | // Let's first add a new entry | ||
108 | eina_error_set(0); | ||
109 | Phone_Entry *p1 = malloc(sizeof(*p1)); | ||
110 | p1->name = "Raul Seixas"; | ||
111 | p1->number = "+55 01 234-56789"; | ||
112 | nick = eina_hash_set(phone_book, &p1, | ||
113 | strdup("raulzito")); | ||
114 | if (!nick) | ||
115 | { | ||
116 | Eina_Error err = eina_error_get(); | ||
117 | if (!err) | ||
118 | { | ||
119 | printf("No previous nick found for Raul Seixas. "); | ||
120 | printf("Creating new entry.\n"); | ||
121 | } | ||
122 | else | ||
123 | printf("Error when setting nick for Raul Seixas\n"); | ||
124 | } | ||
125 | else | ||
126 | { | ||
127 | printf("Old nick for Raul Seixas was %s\n", nick); | ||
128 | free(nick); | ||
129 | } | ||
130 | |||
131 | printf("\n"); | ||
132 | |||
133 | // Now change the nick | ||
134 | eina_error_set(0); | ||
135 | nick = eina_hash_set(phone_book, &p1, | ||
136 | strdup("raulzao")); | ||
137 | if (nick) | ||
138 | { | ||
139 | printf("Changing nick for Raul Seixas to raulzao. "); | ||
140 | printf("Old nick was %s\n", nick); | ||
141 | free(nick); | ||
142 | } | ||
143 | else | ||
144 | { | ||
145 | Eina_Error err = eina_error_get(); | ||
146 | if (err) | ||
147 | printf("Error when changing nick for Raul Seixas\n"); | ||
148 | else | ||
149 | { | ||
150 | printf("No previous nick found for Raul Seixas. "); | ||
151 | printf("Creating new entry.\n"); | ||
152 | } | ||
153 | } | ||
154 | |||
155 | // There are many ways to iterate over our Phone book. | ||
156 | // First, iterate showing the names, phones and associated nicks. | ||
157 | printf("Phonebook:\n"); | ||
158 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
159 | printf("\n"); | ||
160 | |||
161 | // Now iterate using an iterator | ||
162 | printf("Phonebook:\n"); | ||
163 | it = eina_hash_iterator_tuple_new(phone_book); | ||
164 | while (eina_iterator_next(it, &data)) | ||
165 | { | ||
166 | Eina_Hash_Tuple *t = data; | ||
167 | Phone_Entry **pe = (Phone_Entry **)t->key; | ||
168 | nick = t->data; | ||
169 | printf("%s: %s, nick=%s\n", (*pe)->name, (*pe)->number, nick); | ||
170 | } | ||
171 | eina_iterator_free(it); // Always free the iterator after its use | ||
172 | printf("\n"); | ||
173 | |||
174 | // Just iterate over the keys (names) | ||
175 | printf("List of names/numbers in the phone book:\n"); | ||
176 | it = eina_hash_iterator_key_new(phone_book); | ||
177 | while (eina_iterator_next(it, &data)) | ||
178 | { | ||
179 | Phone_Entry **pe = (Phone_Entry **)data; | ||
180 | printf("%s: %s\n", (*pe)->name, (*pe)->number); | ||
181 | } | ||
182 | eina_iterator_free(it); | ||
183 | printf("\n"); | ||
184 | |||
185 | // Just iterate over the data (nicks) | ||
186 | printf("List of nicks in the phone book:\n"); | ||
187 | it = eina_hash_iterator_data_new(phone_book); | ||
188 | while (eina_iterator_next(it, &data)) | ||
189 | { | ||
190 | nick = data; | ||
191 | printf("%s\n", nick); | ||
192 | } | ||
193 | eina_iterator_free(it); | ||
194 | printf("\n"); | ||
195 | |||
196 | // Check how many items are in the phone book | ||
197 | printf("There are %d items in the hash.\n\n", | ||
198 | eina_hash_population(phone_book)); | ||
199 | |||
200 | // Change the name (key) on an entry | ||
201 | Phone_Entry *p2 = malloc(sizeof(*p2)); | ||
202 | p2->name = "Alceu Valenca"; | ||
203 | p2->number = "000000000000"; | ||
204 | eina_hash_move(phone_book, p1, p2); | ||
205 | printf("List of phones after change:\n"); | ||
206 | eina_hash_foreach(phone_book, _phone_book_foreach_cb, NULL); | ||
207 | printf("\n"); | ||
208 | |||
209 | // Empty the phone book, but don't destroy it | ||
210 | eina_hash_free_buckets(phone_book); | ||
211 | printf("There are %d items in the hash.\n\n", | ||
212 | eina_hash_population(phone_book)); | ||
213 | |||
214 | // Phone book could still be used, but we are freeing it since we are | ||
215 | // done for now | ||
216 | eina_hash_free(phone_book); | ||
217 | |||
218 | free(p1); | ||
219 | free(p2); | ||
220 | |||
221 | eina_shutdown(); | ||
222 | } | ||
diff --git a/src/examples/eina/eina_hash_08.c b/src/examples/eina/eina_hash_08.c new file mode 100644 index 0000000000..ed45f7f90f --- /dev/null +++ b/src/examples/eina/eina_hash_08.c | |||
@@ -0,0 +1,128 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_hash_08.c -o eina_hash_08 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <string.h> | ||
6 | #include <Eina.h> | ||
7 | |||
8 | /* | ||
9 | * Eina Hash - phonebook | ||
10 | * | ||
11 | * This example demonstrate the use of Eina Hash by implementing a phonebook | ||
12 | * that stores its contact data into the hash. | ||
13 | * | ||
14 | * It indexes the phone numbers by Contact Full Name, so it's a hash with | ||
15 | * string keys. | ||
16 | */ | ||
17 | |||
18 | struct _Phone_Entry { | ||
19 | const char *name; // Full name. | ||
20 | const char *number; // Phone number. | ||
21 | }; | ||
22 | |||
23 | typedef struct _Phone_Entry Phone_Entry; | ||
24 | |||
25 | static Phone_Entry _start_entries[] = { | ||
26 | { "Wolfgang Amadeus Mozart", "+01 23 456-78910" }, | ||
27 | { "Ludwig van Beethoven", "+12 34 567-89101" }, | ||
28 | { "Richard Georg Strauss", "+23 45 678-91012" }, | ||
29 | { "Heitor Villa-Lobos", "+34 56 789-10123" }, | ||
30 | { NULL, NULL } | ||
31 | }; // _start_entries | ||
32 | |||
33 | static void | ||
34 | _phone_entry_free_cb(void *data) | ||
35 | { | ||
36 | free(data); | ||
37 | } | ||
38 | |||
39 | static Eina_Bool | ||
40 | _phone_book_foreach_cb(const Eina_Hash *phone_book, const void *key, | ||
41 | void *data, void *fdata) | ||
42 | { | ||
43 | const char *name = key; | ||
44 | const char *number = data; | ||
45 | printf("%s: %s\n", name, number); | ||
46 | |||
47 | // Return EINA_FALSE to stop this callback from being called | ||
48 | return EINA_TRUE; | ||
49 | } | ||
50 | |||
51 | int | ||
52 | main(int argc, const char *argv[]) | ||
53 | { | ||
54 | Eina_Hash *phone_book = NULL; | ||
55 | int i; | ||
56 | const char *entry_name = "Heitor Villa-Lobos"; | ||
57 | int entry_size; | ||
58 | const char *saved_entry_name = "Alceu Valenca"; | ||
59 | int saved_entry_size = sizeof("Alceu Valenca"); | ||
60 | const char *phone = NULL; | ||
61 | Eina_Bool r; | ||
62 | Eina_Iterator *it; | ||
63 | void *data; | ||
64 | |||
65 | eina_init(); | ||
66 | |||
67 | phone_book = eina_hash_string_superfast_new(_phone_entry_free_cb); | ||
68 | |||
69 | // Add initial entries to our hash | ||
70 | for (i = 0; _start_entries[i].name != NULL; i++) | ||
71 | { | ||
72 | eina_hash_add(phone_book, _start_entries[i].name, | ||
73 | strdup(_start_entries[i].number)); | ||
74 | } | ||
75 | |||
76 | // Delete entries | ||
77 | r = eina_hash_del(phone_book, entry_name, NULL); | ||
78 | printf("Hash entry successfully deleted? %d\n\n", r); | ||
79 | |||
80 | int hash = eina_hash_superfast("Ludwig van Beethoven", | ||
81 | sizeof("Ludwig van Beethoven")); | ||
82 | |||
83 | r = eina_hash_del_by_key_hash(phone_book, "Ludwig van Beethoven", | ||
84 | sizeof("Ludwig van Beethoven"), hash); | ||
85 | printf("Hash entry successfully deleted? %d\n\n", r); | ||
86 | |||
87 | r = eina_hash_del_by_key(phone_book, "Richard Georg Strauss"); | ||
88 | printf("Hash entry successfully deleted? %d\n\n", r); | ||
89 | |||
90 | // add entry by hash | ||
91 | entry_name = "Raul_Seixas"; | ||
92 | entry_size = sizeof("Raul Seixas"); | ||
93 | phone = strdup("+33 33 333-33333"); | ||
94 | hash = eina_hash_superfast(entry_name, entry_size); | ||
95 | eina_hash_add_by_hash(phone_book, entry_name, entry_size, hash, phone); | ||
96 | |||
97 | // don't need to free 'phone' after the next del: | ||
98 | r = eina_hash_del_by_data(phone_book, phone); | ||
99 | printf("Hash entry successfully deleted? %d\n\n", r); | ||
100 | |||
101 | // add entry by hash directly - no copy of the key will be done | ||
102 | hash = eina_hash_superfast(saved_entry_name, saved_entry_size); | ||
103 | phone = strdup("+44 44 444-44444"); | ||
104 | eina_hash_direct_add_by_hash(phone_book, saved_entry_name, | ||
105 | saved_entry_size, hash, phone); | ||
106 | |||
107 | // find the added entry by its hash: | ||
108 | phone = eina_hash_find_by_hash(phone_book, saved_entry_name, | ||
109 | saved_entry_size, hash); | ||
110 | if (phone) | ||
111 | { | ||
112 | char *newphone = strdup("+55 55 555-55555"); | ||
113 | phone = eina_hash_modify_by_hash(phone_book, saved_entry_name, | ||
114 | saved_entry_size, hash, newphone); | ||
115 | if (phone) | ||
116 | printf("changing phone to %s, old one was %s\n", newphone, phone); | ||
117 | else | ||
118 | printf("couldn't modify entry identified by %d\n", hash); | ||
119 | } | ||
120 | else | ||
121 | { | ||
122 | printf("couldn't find entry identified by %d\n", hash); | ||
123 | } | ||
124 | |||
125 | eina_hash_free(phone_book); | ||
126 | |||
127 | eina_shutdown(); | ||
128 | } | ||
diff --git a/src/examples/eina/eina_inarray_01.c b/src/examples/eina/eina_inarray_01.c new file mode 100644 index 0000000000..2809778993 --- /dev/null +++ b/src/examples/eina/eina_inarray_01.c | |||
@@ -0,0 +1,52 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_inarray_01.c -o eina_inarray_01 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <Eina.h> | ||
5 | |||
6 | int | ||
7 | cmp(const void *a, const void *b) | ||
8 | { | ||
9 | return *(int*)a > *(int*)b; | ||
10 | } | ||
11 | |||
12 | int main(int argc, char **argv) | ||
13 | { | ||
14 | Eina_Inarray *iarr; | ||
15 | char ch, *ch2; | ||
16 | int a, *b; | ||
17 | |||
18 | eina_init(); | ||
19 | iarr = eina_inarray_new(sizeof(char), 0); | ||
20 | |||
21 | ch = 'a'; | ||
22 | eina_inarray_push(iarr, &ch); | ||
23 | ch = 'b'; | ||
24 | eina_inarray_push(iarr, &ch); | ||
25 | ch = 'c'; | ||
26 | eina_inarray_push(iarr, &ch); | ||
27 | ch = 'd'; | ||
28 | eina_inarray_push(iarr, &ch); | ||
29 | |||
30 | printf("Inline array of chars:\n"); | ||
31 | EINA_INARRAY_FOREACH(iarr, ch2) | ||
32 | printf("char: %c(pointer: %p)\n", *ch2, ch2); | ||
33 | |||
34 | eina_inarray_flush(iarr); | ||
35 | eina_inarray_step_set(iarr, sizeof(Eina_Inarray), sizeof(int), 4); | ||
36 | |||
37 | a = 97; | ||
38 | eina_inarray_push(iarr, &a); | ||
39 | a = 98; | ||
40 | eina_inarray_push(iarr, &a); | ||
41 | a = 100; | ||
42 | eina_inarray_push(iarr, &a); | ||
43 | a = 99; | ||
44 | eina_inarray_insert_sorted(iarr, &a, cmp); | ||
45 | |||
46 | printf("Inline array of integers with %d elements:\n", eina_inarray_count(iarr)); | ||
47 | EINA_INARRAY_FOREACH(iarr, b) | ||
48 | printf("int: %d(pointer: %p)\n", *b, b); | ||
49 | |||
50 | eina_inarray_free(iarr); | ||
51 | eina_shutdown(); | ||
52 | } | ||
diff --git a/src/examples/eina/eina_inarray_02.c b/src/examples/eina/eina_inarray_02.c new file mode 100644 index 0000000000..17c0667408 --- /dev/null +++ b/src/examples/eina/eina_inarray_02.c | |||
@@ -0,0 +1,33 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_inarray_02.c -o eina_inarray_02 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <Eina.h> | ||
5 | |||
6 | int | ||
7 | main(int argc, char **argv) | ||
8 | { | ||
9 | const char* strings[] = { | ||
10 | "helo", "hera", "starbuck", "kat", "boomer", | ||
11 | "hotdog", "longshot", "jammer", "crashdown", "hardball", | ||
12 | "duck", "racetrack", "apolo", "husker", "freaker", | ||
13 | "skulls", "bulldog", "flat top", "hammerhead", "gonzo" | ||
14 | }; | ||
15 | char **str, **str2; | ||
16 | Eina_Inarray *iarr; | ||
17 | int i; | ||
18 | |||
19 | eina_init(); | ||
20 | iarr = eina_inarray_new(sizeof(char *), 0); | ||
21 | |||
22 | for (i = 0; i < 20; i++){ | ||
23 | str = &strings[i]; | ||
24 | eina_inarray_push(iarr, str); | ||
25 | } | ||
26 | |||
27 | printf("Inline array of strings:\n"); | ||
28 | EINA_INARRAY_FOREACH(iarr, str2) | ||
29 | printf("string: %s(pointer: %p)\n", *str2, str2); | ||
30 | |||
31 | eina_inarray_free(iarr); | ||
32 | eina_shutdown(); | ||
33 | } | ||
diff --git a/src/examples/eina/eina_inlist_01.c b/src/examples/eina/eina_inlist_01.c new file mode 100644 index 0000000000..58a3d66854 --- /dev/null +++ b/src/examples/eina/eina_inlist_01.c | |||
@@ -0,0 +1,98 @@ | |||
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 | } | ||
diff --git a/src/examples/eina/eina_inlist_02.c b/src/examples/eina/eina_inlist_02.c new file mode 100644 index 0000000000..79dc0cdbcd --- /dev/null +++ b/src/examples/eina/eina_inlist_02.c | |||
@@ -0,0 +1,66 @@ | |||
1 | // Compile with: | ||
2 | // gcc -g eina_inlist_02.c -o eina_inlist_02 `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 | main(void) | ||
14 | { | ||
15 | struct my_struct *d, *cur; | ||
16 | int i; | ||
17 | |||
18 | Eina_Inlist *inlist = NULL; | ||
19 | Eina_List *list = NULL, *l_itr, *l_next; | ||
20 | |||
21 | eina_init(); | ||
22 | |||
23 | for (i = 0; i < 100; i++) | ||
24 | { | ||
25 | d = malloc(sizeof(*d)); | ||
26 | d->a = i; | ||
27 | d->b = i * 10; | ||
28 | inlist = eina_inlist_append(inlist, EINA_INLIST_GET(d)); | ||
29 | if ((i % 2) == 0) | ||
30 | list = eina_list_prepend(list, d); | ||
31 | } | ||
32 | |||
33 | printf("inlist=%p\n", inlist); | ||
34 | EINA_INLIST_FOREACH(inlist, cur) | ||
35 | printf("\ta=%d, b=%d\n", cur->a, cur->b); | ||
36 | |||
37 | printf("list=%p\n", list); | ||
38 | EINA_LIST_FOREACH(list, l_itr, cur) | ||
39 | printf("\ta=%d, b=%d\n", cur->a, cur->b); | ||
40 | |||
41 | printf("inlist count=%d\n", eina_inlist_count(inlist)); | ||
42 | printf("list count=%d\n\n", eina_list_count(list)); | ||
43 | |||
44 | EINA_LIST_FOREACH_SAFE(list, l_itr, l_next, cur) | ||
45 | { | ||
46 | if ((cur->a % 3) == 0) | ||
47 | list = eina_list_remove_list(list, l_itr); | ||
48 | } | ||
49 | |||
50 | printf("inlist count=%d\n", eina_inlist_count(inlist)); | ||
51 | printf("list count=%d\n\n", eina_list_count(list)); | ||
52 | |||
53 | eina_list_free(list); | ||
54 | |||
55 | while (inlist) | ||
56 | { | ||
57 | struct my_struct *aux = EINA_INLIST_CONTAINER_GET(inlist, | ||
58 | struct my_struct); | ||
59 | inlist = eina_inlist_remove(inlist, inlist); | ||
60 | free(aux); | ||
61 | } | ||
62 | |||
63 | eina_shutdown(); | ||
64 | |||
65 | return 0; | ||
66 | } | ||
diff --git a/src/examples/eina/eina_inlist_03.c b/src/examples/eina/eina_inlist_03.c new file mode 100644 index 0000000000..6a9ff64197 --- /dev/null +++ b/src/examples/eina/eina_inlist_03.c | |||
@@ -0,0 +1,75 @@ | |||
1 | // Compile with: | ||
2 | // gcc -g eina_inlist_03.c -o eina_inlist_03 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <Eina.h> | ||
5 | #include <stdio.h> | ||
6 | |||
7 | struct my_struct { | ||
8 | EINA_INLIST; | ||
9 | Eina_Inlist even; | ||
10 | int a, b; | ||
11 | }; | ||
12 | |||
13 | #define EVEN_INLIST_GET(Inlist) (& ((Inlist)->even)) | ||
14 | |||
15 | #define EVEN_INLIST_CONTAINER_GET(ptr, type) \ | ||
16 | ((type *)((char *)ptr - offsetof(type, even))) | ||
17 | |||
18 | int | ||
19 | main(void) | ||
20 | { | ||
21 | struct my_struct *d, *cur; | ||
22 | int i; | ||
23 | |||
24 | Eina_Inlist *list = NULL, *list_even = NULL, *itr; | ||
25 | |||
26 | eina_init(); | ||
27 | |||
28 | for (i = 0; i < 100; i++) | ||
29 | { | ||
30 | d = malloc(sizeof(*d)); | ||
31 | d->a = i; | ||
32 | d->b = i * 10; | ||
33 | list = eina_inlist_append(list, EINA_INLIST_GET(d)); | ||
34 | if ((i % 2) == 0) | ||
35 | list_even = eina_inlist_prepend(list_even, EVEN_INLIST_GET(d)); | ||
36 | } | ||
37 | |||
38 | printf("list=%p\n", list); | ||
39 | EINA_INLIST_FOREACH(list, cur) | ||
40 | printf("\ta=%d, b=%d\n", cur->a, cur->b); | ||
41 | |||
42 | printf("list_even=%p\n", list_even); | ||
43 | for (itr = list_even; itr != NULL; itr = itr->next) | ||
44 | { | ||
45 | cur = EVEN_INLIST_CONTAINER_GET(itr, struct my_struct); | ||
46 | printf("\ta=%d, b=%d\n", cur->a, cur->b); | ||
47 | } | ||
48 | |||
49 | printf("list count=%d\n", eina_inlist_count(list)); | ||
50 | printf("list_even count=%d\n\n", eina_inlist_count(list_even)); | ||
51 | |||
52 | itr = list_even; | ||
53 | while (itr) | ||
54 | { | ||
55 | Eina_Inlist *next = itr->next; | ||
56 | cur = EVEN_INLIST_CONTAINER_GET(itr, struct my_struct); | ||
57 | if ((cur->a % 3) == 0) | ||
58 | list_even = eina_inlist_remove(list_even, itr); | ||
59 | itr = next; | ||
60 | } | ||
61 | printf("list count=%d\n", eina_inlist_count(list)); | ||
62 | printf("list_even count=%d\n\n", eina_inlist_count(list_even)); | ||
63 | |||
64 | while (list) | ||
65 | { | ||
66 | struct my_struct *aux = EINA_INLIST_CONTAINER_GET(list, | ||
67 | struct my_struct); | ||
68 | list = eina_inlist_remove(list, list); | ||
69 | free(aux); | ||
70 | } | ||
71 | |||
72 | eina_shutdown(); | ||
73 | |||
74 | return 0; | ||
75 | } | ||
diff --git a/src/examples/eina/eina_iterator_01.c b/src/examples/eina/eina_iterator_01.c new file mode 100644 index 0000000000..88c40cc695 --- /dev/null +++ b/src/examples/eina/eina_iterator_01.c | |||
@@ -0,0 +1,66 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_iterator_01.c -o eina_iterator_01 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | |||
6 | #include <Eina.h> | ||
7 | |||
8 | static Eina_Bool | ||
9 | print_one(const void *container, void *data, void *fdata) | ||
10 | { | ||
11 | printf("%s\n", (char*)data); | ||
12 | return EINA_TRUE; | ||
13 | } | ||
14 | |||
15 | static void | ||
16 | print_eina_container(Eina_Iterator *it) | ||
17 | { | ||
18 | eina_iterator_foreach(it, print_one, NULL); | ||
19 | printf("\n"); | ||
20 | } | ||
21 | |||
22 | int | ||
23 | main(int argc, char **argv) | ||
24 | { | ||
25 | const char *strings[] = { | ||
26 | "unintersting string", "husker", "starbuck", "husker" | ||
27 | }; | ||
28 | const char *more_strings[] = { | ||
29 | "very unintersting string", | ||
30 | "what do your hear?", | ||
31 | "nothing but the rain", | ||
32 | "then grab your gun and bring the cat in" | ||
33 | }; | ||
34 | Eina_Array *array; | ||
35 | Eina_List *list = NULL; | ||
36 | Eina_Iterator *it; | ||
37 | unsigned short int i; | ||
38 | char *uninteresting; | ||
39 | |||
40 | eina_init(); | ||
41 | |||
42 | array = eina_array_new(4); | ||
43 | |||
44 | for (i = 0; i < 4; i++) | ||
45 | { | ||
46 | eina_array_push(array, strings[i]); | ||
47 | list = eina_list_append(list, more_strings[i]); | ||
48 | } | ||
49 | |||
50 | it = eina_array_iterator_new(array); | ||
51 | eina_iterator_next(it, &uninteresting); | ||
52 | print_eina_container(it); | ||
53 | eina_array_free(eina_iterator_container_get(it)); | ||
54 | eina_iterator_free(it); | ||
55 | |||
56 | it = eina_list_iterator_new(list); | ||
57 | eina_iterator_next(it, &uninteresting); | ||
58 | print_eina_container(it); | ||
59 | eina_iterator_free(it); | ||
60 | |||
61 | eina_list_free(list); | ||
62 | |||
63 | eina_shutdown(); | ||
64 | |||
65 | return 0; | ||
66 | } | ||
diff --git a/src/examples/eina/eina_list_01.c b/src/examples/eina/eina_list_01.c new file mode 100644 index 0000000000..4fcb020781 --- /dev/null +++ b/src/examples/eina/eina_list_01.c | |||
@@ -0,0 +1,44 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_list_01.c -o eina_list_01 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <Eina.h> | ||
6 | |||
7 | int | ||
8 | main(int argc, char **argv) | ||
9 | { | ||
10 | Eina_List *list = NULL; | ||
11 | Eina_List *l; | ||
12 | void *list_data; | ||
13 | |||
14 | eina_init(); | ||
15 | |||
16 | list = eina_list_append(list, "tigh"); | ||
17 | list = eina_list_append(list, "adar"); | ||
18 | list = eina_list_append(list, "baltar"); | ||
19 | list = eina_list_append(list, "roslin"); | ||
20 | |||
21 | EINA_LIST_FOREACH(list, l, list_data) | ||
22 | printf("%s\n", (char*)list_data); | ||
23 | printf("\n"); | ||
24 | |||
25 | l = eina_list_nth_list(list, 1); | ||
26 | list = eina_list_append_relative_list(list, "cain", l); | ||
27 | |||
28 | list = eina_list_append_relative(list, "zarek", "cain"); | ||
29 | |||
30 | list = eina_list_prepend(list, "adama"); | ||
31 | |||
32 | list = eina_list_prepend_relative(list, "gaeta", "cain"); | ||
33 | |||
34 | list = eina_list_prepend_relative_list(list, "lampkin", l); | ||
35 | |||
36 | EINA_LIST_FOREACH(list, l, list_data) | ||
37 | printf("%s\n", (char*)list_data); | ||
38 | |||
39 | eina_list_free(list); | ||
40 | |||
41 | eina_shutdown(); | ||
42 | |||
43 | return 0; | ||
44 | } | ||
diff --git a/src/examples/eina/eina_list_02.c b/src/examples/eina/eina_list_02.c new file mode 100644 index 0000000000..5fe31e89a8 --- /dev/null +++ b/src/examples/eina/eina_list_02.c | |||
@@ -0,0 +1,55 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_list_02.c -o eina_list_02 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <string.h> | ||
6 | #include <Eina.h> | ||
7 | |||
8 | int | ||
9 | main(int argc, char **argv) | ||
10 | { | ||
11 | Eina_List *list = NULL, *other_list = NULL; | ||
12 | Eina_List *l; | ||
13 | void *data; | ||
14 | int cmp_result; | ||
15 | Eina_Compare_Cb cmp_func = (Eina_Compare_Cb)strcmp; | ||
16 | |||
17 | eina_init(); | ||
18 | |||
19 | list = eina_list_append(list, "starbuck"); | ||
20 | list = eina_list_append(list, "appolo"); | ||
21 | list = eina_list_append(list, "boomer"); | ||
22 | |||
23 | data = eina_list_search_unsorted(list, cmp_func, "boomer"); | ||
24 | l = eina_list_search_unsorted_list(list, cmp_func, "boomer"); | ||
25 | if (l->data != data) | ||
26 | return 1; | ||
27 | |||
28 | list = eina_list_sort(list, 0, cmp_func); | ||
29 | |||
30 | data = eina_list_search_sorted(list, cmp_func, "starbuck"); | ||
31 | l = eina_list_search_sorted_list(list, cmp_func, "starbuck"); | ||
32 | if (l->data != data) | ||
33 | return 1; | ||
34 | |||
35 | list = eina_list_sorted_insert(list, cmp_func, "helo"); | ||
36 | |||
37 | l = eina_list_search_sorted_near_list(list, cmp_func, "hera", &cmp_result); | ||
38 | if (cmp_result > 0) | ||
39 | list = eina_list_prepend_relative_list(list, "hera", l); | ||
40 | else if (cmp_result < 0) | ||
41 | list = eina_list_append_relative_list(list, "hera", l); | ||
42 | |||
43 | l = eina_list_search_sorted_list(list, cmp_func, "boomer"); | ||
44 | list = eina_list_split_list(list, l, &other_list); | ||
45 | |||
46 | other_list = eina_list_sort(other_list, 0, cmp_func); | ||
47 | |||
48 | list = eina_list_sorted_merge(list, other_list, cmp_func); | ||
49 | |||
50 | eina_list_free(list); | ||
51 | |||
52 | eina_shutdown(); | ||
53 | |||
54 | return 0; | ||
55 | } | ||
diff --git a/src/examples/eina/eina_list_03.c b/src/examples/eina/eina_list_03.c new file mode 100644 index 0000000000..db531489db --- /dev/null +++ b/src/examples/eina/eina_list_03.c | |||
@@ -0,0 +1,45 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_list_03.c -o eina_list_03 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <Eina.h> | ||
6 | |||
7 | int | ||
8 | main(int argc, char **argv) | ||
9 | { | ||
10 | Eina_List *list = NULL, *r_list; | ||
11 | Eina_List *l; | ||
12 | Eina_Iterator *itr; | ||
13 | void *list_data; | ||
14 | |||
15 | eina_init(); | ||
16 | |||
17 | list = eina_list_append(list, "caprica"); | ||
18 | list = eina_list_append(list, "sagitarius"); | ||
19 | list = eina_list_append(list, "aerilon"); | ||
20 | list = eina_list_append(list, "gemenon"); | ||
21 | |||
22 | list = eina_list_promote_list(list, eina_list_nth_list(list, 2)); | ||
23 | list = eina_list_demote_list(list, eina_list_nth_list(list, 2)); | ||
24 | |||
25 | list = eina_list_remove(list, "sagitarius"); | ||
26 | |||
27 | l = eina_list_data_find_list(list, "aerilon"); | ||
28 | eina_list_data_set(l, "aquarius"); | ||
29 | |||
30 | printf("size: %d\n", eina_list_count(list)); | ||
31 | |||
32 | r_list = eina_list_reverse_clone(list); | ||
33 | |||
34 | itr = eina_list_iterator_new(r_list); | ||
35 | EINA_ITERATOR_FOREACH(itr, list_data) | ||
36 | printf("%s\n", (char*)list_data); | ||
37 | eina_iterator_free(itr); | ||
38 | |||
39 | eina_list_free(list); | ||
40 | eina_list_free(r_list); | ||
41 | |||
42 | eina_shutdown(); | ||
43 | |||
44 | return 0; | ||
45 | } | ||
diff --git a/src/examples/eina/eina_list_04.c b/src/examples/eina/eina_list_04.c new file mode 100644 index 0000000000..d92a6076f9 --- /dev/null +++ b/src/examples/eina/eina_list_04.c | |||
@@ -0,0 +1,36 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_list_04.c -o eina_list_04 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdio.h> | ||
5 | #include <Eina.h> | ||
6 | |||
7 | int | ||
8 | main(int argc, char **argv) | ||
9 | { | ||
10 | Eina_List *list = NULL; | ||
11 | Eina_List *l; | ||
12 | void *list_data; | ||
13 | |||
14 | eina_init(); | ||
15 | |||
16 | list = eina_list_append(list, eina_stringshare_add("calvin")); | ||
17 | list = eina_list_append(list, eina_stringshare_add("Leoben")); | ||
18 | list = eina_list_append(list, eina_stringshare_add("D'Anna")); | ||
19 | list = eina_list_append(list, eina_stringshare_add("Simon")); | ||
20 | list = eina_list_append(list, eina_stringshare_add("Doral")); | ||
21 | list = eina_list_append(list, eina_stringshare_add("Six")); | ||
22 | list = eina_list_append(list, eina_stringshare_add("Sharon")); | ||
23 | |||
24 | for(l = list; l; l = eina_list_next(l)) | ||
25 | printf("%s\n", (char*)l->data); | ||
26 | |||
27 | for(l = eina_list_last(list); l; l = eina_list_prev(l)) | ||
28 | printf("%s\n", (char*)eina_list_data_get(l)); | ||
29 | |||
30 | EINA_LIST_FREE(list, list_data) | ||
31 | eina_stringshare_del(list_data); | ||
32 | |||
33 | eina_shutdown(); | ||
34 | |||
35 | return 0; | ||
36 | } | ||
diff --git a/src/examples/eina/eina_log_01.c b/src/examples/eina/eina_log_01.c new file mode 100644 index 0000000000..7a20ca90ac --- /dev/null +++ b/src/examples/eina/eina_log_01.c | |||
@@ -0,0 +1,27 @@ | |||
1 | //Compile with: | ||
2 | //gcc -Wall -o eina_log_01 eina_log_01.c `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdlib.h> | ||
5 | #include <stdio.h> | ||
6 | |||
7 | #include <Eina.h> | ||
8 | |||
9 | void test_warn(void) | ||
10 | { | ||
11 | EINA_LOG_WARN("Here is a warning message"); | ||
12 | } | ||
13 | |||
14 | int main(void) | ||
15 | { | ||
16 | if (!eina_init()) | ||
17 | { | ||
18 | printf("log during the initialization of Eina_Log module\n"); | ||
19 | return EXIT_FAILURE; | ||
20 | } | ||
21 | |||
22 | test_warn(); | ||
23 | |||
24 | eina_shutdown(); | ||
25 | |||
26 | return EXIT_SUCCESS; | ||
27 | } | ||
diff --git a/src/examples/eina/eina_log_02.c b/src/examples/eina/eina_log_02.c new file mode 100644 index 0000000000..a056729b2d --- /dev/null +++ b/src/examples/eina/eina_log_02.c | |||
@@ -0,0 +1,38 @@ | |||
1 | //Compile with: | ||
2 | //gcc -Wall -o eina_log_02 eina_log_02.c `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdlib.h> | ||
5 | #include <stdio.h> | ||
6 | |||
7 | #include <Eina.h> | ||
8 | |||
9 | void test(int i) | ||
10 | { | ||
11 | EINA_LOG_DBG("Entering test"); | ||
12 | |||
13 | if (i < 0) | ||
14 | { | ||
15 | EINA_LOG_ERR("Argument is negative"); | ||
16 | return; | ||
17 | } | ||
18 | |||
19 | EINA_LOG_INFO("argument non negative"); | ||
20 | |||
21 | EINA_LOG_DBG("Exiting test"); | ||
22 | } | ||
23 | |||
24 | int main(void) | ||
25 | { | ||
26 | if (!eina_init()) | ||
27 | { | ||
28 | printf("log during the initialization of Eina_Log module\n"); | ||
29 | return EXIT_FAILURE; | ||
30 | } | ||
31 | |||
32 | test(-1); | ||
33 | test(0); | ||
34 | |||
35 | eina_shutdown(); | ||
36 | |||
37 | return EXIT_SUCCESS; | ||
38 | } | ||
diff --git a/src/examples/eina/eina_log_03.c b/src/examples/eina/eina_log_03.c new file mode 100644 index 0000000000..8dbe19e715 --- /dev/null +++ b/src/examples/eina/eina_log_03.c | |||
@@ -0,0 +1,78 @@ | |||
1 | //Compile with: | ||
2 | //gcc -Wall -o eina_log_03 eina_log_03.c `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <stdlib.h> | ||
5 | #include <stdio.h> | ||
6 | |||
7 | #include <Eina.h> | ||
8 | |||
9 | #define log(fmt, ...) \ | ||
10 | eina_log_print(EINA_LOG_LEVEL_ERR, __FILE__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__) | ||
11 | |||
12 | typedef struct _Data Data; | ||
13 | |||
14 | struct _Data | ||
15 | { | ||
16 | int to_stderr; | ||
17 | }; | ||
18 | |||
19 | void print_cb(const Eina_Log_Domain *domain, | ||
20 | Eina_Log_Level level, | ||
21 | const char *file, | ||
22 | const char *fnc, | ||
23 | int line, | ||
24 | const char *fmt, | ||
25 | void *data, | ||
26 | va_list args) | ||
27 | { | ||
28 | Data *d; | ||
29 | FILE *output; | ||
30 | char *str; | ||
31 | |||
32 | d = (Data*)data; | ||
33 | if (d->to_stderr) | ||
34 | { | ||
35 | output = stderr; | ||
36 | str = "stderr"; | ||
37 | } | ||
38 | else | ||
39 | { | ||
40 | output = stdout; | ||
41 | str = "stdout"; | ||
42 | } | ||
43 | |||
44 | fprintf(output, "%s:%s:%s (%d) %s: ", | ||
45 | domain->domain_str, file, fnc, line, str); | ||
46 | vfprintf(output, fmt, args); | ||
47 | putc('\n', output); | ||
48 | } | ||
49 | |||
50 | void test(Data *data, int i) | ||
51 | { | ||
52 | if (i < 0) | ||
53 | data->to_stderr = 0; | ||
54 | else | ||
55 | data->to_stderr = 1; | ||
56 | |||
57 | EINA_LOG_INFO("Log message..."); | ||
58 | } | ||
59 | |||
60 | int main(void) | ||
61 | { | ||
62 | Data data; | ||
63 | |||
64 | if (!eina_init()) | ||
65 | { | ||
66 | printf("log during the initialization of Eina_Log module\n"); | ||
67 | return EXIT_FAILURE; | ||
68 | } | ||
69 | |||
70 | eina_log_print_cb_set(print_cb, &data); | ||
71 | |||
72 | test(&data, -1); | ||
73 | test(&data, 0); | ||
74 | |||
75 | eina_shutdown(); | ||
76 | |||
77 | return EXIT_SUCCESS; | ||
78 | } | ||
diff --git a/src/examples/eina/eina_magic_01.c b/src/examples/eina/eina_magic_01.c new file mode 100644 index 0000000000..f42e193695 --- /dev/null +++ b/src/examples/eina/eina_magic_01.c | |||
@@ -0,0 +1,106 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_magic_01.c -o eina_magic_01 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <Eina.h> | ||
5 | |||
6 | #define BASETYPE_MAGIC 0x12345 | ||
7 | struct _person { | ||
8 | EINA_MAGIC; | ||
9 | char *name; | ||
10 | }; | ||
11 | typedef struct _person person; | ||
12 | |||
13 | #define SUBTYPE_MAGIC 0x3333 | ||
14 | struct _pilot { | ||
15 | person base; | ||
16 | EINA_MAGIC; | ||
17 | char *callsign; | ||
18 | }; | ||
19 | typedef struct _pilot pilot; | ||
20 | |||
21 | person * | ||
22 | person_new(const char *name) | ||
23 | { | ||
24 | person *ptr = malloc(sizeof(person)); | ||
25 | EINA_MAGIC_SET(ptr, BASETYPE_MAGIC); | ||
26 | ptr->name = strdup(name); | ||
27 | } | ||
28 | |||
29 | void | ||
30 | person_free(person *ptr) { | ||
31 | if (!EINA_MAGIC_CHECK(ptr, BASETYPE_MAGIC)) | ||
32 | { | ||
33 | EINA_MAGIC_FAIL(ptr, BASETYPE_MAGIC); | ||
34 | return; | ||
35 | } | ||
36 | EINA_MAGIC_SET(ptr, EINA_MAGIC_NONE); | ||
37 | free(ptr->name); | ||
38 | free(ptr); | ||
39 | } | ||
40 | |||
41 | pilot * | ||
42 | pilot_new(const char *name, const char *callsign) | ||
43 | { | ||
44 | pilot *ptr = malloc(sizeof(pilot)); | ||
45 | EINA_MAGIC_SET(ptr, SUBTYPE_MAGIC); | ||
46 | EINA_MAGIC_SET(&ptr->base, BASETYPE_MAGIC); | ||
47 | ptr->base.name = strdup(name); | ||
48 | ptr->callsign = strdup(callsign); | ||
49 | } | ||
50 | |||
51 | void | ||
52 | pilot_free(pilot *ptr) { | ||
53 | if (!EINA_MAGIC_CHECK(ptr, SUBTYPE_MAGIC)) | ||
54 | { | ||
55 | EINA_MAGIC_FAIL(ptr, SUBTYPE_MAGIC); | ||
56 | return; | ||
57 | } | ||
58 | EINA_MAGIC_SET(ptr, EINA_MAGIC_NONE); | ||
59 | EINA_MAGIC_SET(&ptr->base, EINA_MAGIC_NONE); | ||
60 | free(ptr->base.name); | ||
61 | free(ptr->callsign); | ||
62 | free(ptr); | ||
63 | } | ||
64 | |||
65 | void | ||
66 | print_person(person *ptr) | ||
67 | { | ||
68 | if (!EINA_MAGIC_CHECK(ptr, BASETYPE_MAGIC)){ | ||
69 | EINA_MAGIC_FAIL(ptr, BASETYPE_MAGIC); | ||
70 | return; | ||
71 | } | ||
72 | printf("name: %s\n", ptr->name); | ||
73 | } | ||
74 | |||
75 | void | ||
76 | print_pilot(pilot *ptr) | ||
77 | { | ||
78 | if (!EINA_MAGIC_CHECK(ptr, SUBTYPE_MAGIC)) { | ||
79 | EINA_MAGIC_FAIL(ptr, SUBTYPE_MAGIC); | ||
80 | return; | ||
81 | } | ||
82 | print_person(&ptr->base); | ||
83 | printf("callsign: %s\n", ptr->callsign); | ||
84 | } | ||
85 | |||
86 | int | ||
87 | main(int argc, char **argv) | ||
88 | { | ||
89 | person *base; | ||
90 | pilot *sub; | ||
91 | |||
92 | eina_init(); | ||
93 | eina_magic_string_set(BASETYPE_MAGIC, "person"); | ||
94 | eina_magic_string_static_set(SUBTYPE_MAGIC, "pilot"); | ||
95 | |||
96 | base = person_new("Tyrol"); | ||
97 | sub = pilot_new("thrace", "starbuck"); | ||
98 | |||
99 | print_person(base); | ||
100 | print_person((person *)sub); | ||
101 | |||
102 | print_pilot(base); //BAD | ||
103 | print_pilot(sub); | ||
104 | |||
105 | eina_shutdown(); | ||
106 | } | ||
diff --git a/src/examples/eina/eina_model_01.c b/src/examples/eina/eina_model_01.c new file mode 100644 index 0000000000..28895aa666 --- /dev/null +++ b/src/examples/eina/eina_model_01.c | |||
@@ -0,0 +1,235 @@ | |||
1 | /* | ||
2 | * Compile with: | ||
3 | * gcc -o eina_model_01 eina_model_01.c `pkg-config --cflags --libs eina` | ||
4 | */ | ||
5 | |||
6 | /* | ||
7 | * This example demonstrates the usage of Eina Model by implementing | ||
8 | * Bank Account Class, which is inherited from Base Class; | ||
9 | * and Credit Card Class, which is inherited from Bank Account Class. | ||
10 | * | ||
11 | * Base Class(Eina_Model_Type) --> Bank Account Class --> Credit Card Class | ||
12 | * | ||
13 | * Bank Account Class implements "bank_account_data_set()" and "print()" methods; | ||
14 | * Credit Card Class inherits these two and implements "credit_card_data_set()" | ||
15 | * | ||
16 | * | ||
17 | * Bank Account Class::print() calls for "_bank_account_data_print" | ||
18 | * Credit Card Class ::print() is reloaded with "_credit_card_data_print()" | ||
19 | * which calls for parent function "_bank_account_data_print" | ||
20 | * | ||
21 | */ | ||
22 | |||
23 | |||
24 | #include <Eina.h> | ||
25 | #include <eina_safety_checks.h> | ||
26 | |||
27 | /* | ||
28 | * Defining type for new model type | ||
29 | * Model will have two methods | ||
30 | */ | ||
31 | typedef struct _Bank_Account_Type | ||
32 | { | ||
33 | Eina_Model_Type parent_class; | ||
34 | void (*bank_account_data_set)(Eina_Model *, const char *name, const char *number); | ||
35 | void (*print)(Eina_Model *); | ||
36 | } Bank_Account_Type; | ||
37 | |||
38 | /* | ||
39 | * Defining type for Bank Account private data | ||
40 | */ | ||
41 | typedef struct _Bank_Account_Data | ||
42 | { | ||
43 | char name[30]; | ||
44 | char number[30]; | ||
45 | } Bank_Account_Data; | ||
46 | |||
47 | /* | ||
48 | * Defining type for Credit Card model type, which will be inherited from Bank Account model type | ||
49 | * Model will have two parent's methods and additional one | ||
50 | */ | ||
51 | typedef struct _Credit_Card_Type | ||
52 | { | ||
53 | Bank_Account_Type parent_class; | ||
54 | void (*credit_card_data_set)(Eina_Model *, const char *, const char *, int) ; | ||
55 | } Credit_Card_Type; | ||
56 | |||
57 | /* | ||
58 | * Defining type for Credit Card private data | ||
59 | */ | ||
60 | typedef struct _Credit_Card_Data | ||
61 | { | ||
62 | char number[30]; | ||
63 | char expiry_date[30]; | ||
64 | int pin; | ||
65 | } Credit_Card_Data; | ||
66 | |||
67 | static Bank_Account_Type _BANK_ACCOUNT_TYPE; | ||
68 | static Credit_Card_Type _CREDIT_CARD_TYPE; | ||
69 | static Eina_Model_Type *BANK_ACCOUNT_TYPE = (Eina_Model_Type *) &_BANK_ACCOUNT_TYPE; | ||
70 | static Eina_Model_Type *CREDIT_CARD_TYPE = (Eina_Model_Type *) &_CREDIT_CARD_TYPE; | ||
71 | |||
72 | |||
73 | /* | ||
74 | * Defining method for for Bank Account data | ||
75 | */ | ||
76 | static void | ||
77 | _bank_account_data_set(Eina_Model *mdl, const char *name, const char *number) | ||
78 | { | ||
79 | Bank_Account_Data *bdata = eina_model_type_private_data_get(mdl, BANK_ACCOUNT_TYPE); | ||
80 | |||
81 | if (!bdata) | ||
82 | printf("ERROR\n"); | ||
83 | |||
84 | if (name != NULL) | ||
85 | { | ||
86 | strncpy(bdata->name, name, sizeof(bdata->name)); | ||
87 | bdata->name[sizeof(bdata->number) - 1] = '\0'; | ||
88 | } | ||
89 | |||
90 | if (number != NULL) | ||
91 | { | ||
92 | strncpy(bdata->number, number, sizeof(bdata->number)); | ||
93 | bdata->number[sizeof(bdata->number) - 1] = '\0'; | ||
94 | } | ||
95 | |||
96 | printf("%s :: %s %p\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl); | ||
97 | } | ||
98 | |||
99 | |||
100 | static void | ||
101 | _credit_card_data_set(Eina_Model *mdl, const char *number, const char *expiry_date, int pin) | ||
102 | { | ||
103 | Credit_Card_Data *cdata = eina_model_type_private_data_get(mdl, CREDIT_CARD_TYPE); | ||
104 | |||
105 | if (!cdata) | ||
106 | printf("ERROR\n"); | ||
107 | |||
108 | if (number != NULL) | ||
109 | { | ||
110 | strncpy(cdata->number, number, sizeof(cdata->number)); | ||
111 | cdata->number[sizeof(cdata->number) - 1] = '\0'; | ||
112 | } | ||
113 | |||
114 | if (expiry_date != NULL) | ||
115 | { | ||
116 | strncpy(cdata->expiry_date, expiry_date, sizeof(cdata->expiry_date)); | ||
117 | cdata->expiry_date[sizeof(cdata->expiry_date) - 1] = '\0'; | ||
118 | } | ||
119 | |||
120 | cdata->pin = pin; | ||
121 | printf("%s :: %s %p\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl); | ||
122 | } | ||
123 | |||
124 | static void | ||
125 | _bank_account_data_print(Eina_Model *mdl) | ||
126 | { | ||
127 | const Bank_Account_Data *bdata = eina_model_type_private_data_get(mdl, BANK_ACCOUNT_TYPE); | ||
128 | |||
129 | printf("\n%s :: %s %p \n\tName: %s(%p)\n\tAccount: %s(%p)\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl | ||
130 | , bdata->name, bdata->name, bdata->number, bdata->number); | ||
131 | } | ||
132 | |||
133 | static void | ||
134 | _credit_card_data_print(Eina_Model *mdl) | ||
135 | { | ||
136 | void (*pf)(Eina_Model *); | ||
137 | const Eina_Model_Type *ptype = eina_model_type_parent_get(eina_model_type_get(mdl)); | ||
138 | //const Eina_Model_Type *ptype = eina_model_type_get(mdl); | ||
139 | |||
140 | pf = eina_model_type_method_resolve(ptype, mdl, Bank_Account_Type, print); | ||
141 | if (pf) | ||
142 | pf(mdl); | ||
143 | else | ||
144 | printf("ERROR: %d", __LINE__); | ||
145 | const Credit_Card_Data *cdata = eina_model_type_private_data_get(mdl, CREDIT_CARD_TYPE); | ||
146 | printf("%s :: %s %p \n\tNumber: %s(%p)\n\tCC Expiry Date: %s(%p)\n\tCC PIN: %d(%p)\n", eina_model_type_name_get(eina_model_type_get(mdl)) ,__func__, mdl | ||
147 | , cdata->number, cdata->number, cdata->expiry_date, cdata->expiry_date, cdata->pin, &cdata->pin); | ||
148 | } | ||
149 | |||
150 | #define BANK_ACCOUNT(x) ((Bank_Account_Type *) x) | ||
151 | #define CREDIT_CARD(x) ((Credit_Card_Type *) x) | ||
152 | |||
153 | void | ||
154 | bank_account_data_set(Eina_Model *mdl, const char *name, char *number) | ||
155 | { | ||
156 | |||
157 | EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(mdl, BANK_ACCOUNT_TYPE)); | ||
158 | |||
159 | void (*pf)(Eina_Model *, const char *, const char *); | ||
160 | pf = eina_model_method_resolve(mdl, Bank_Account_Type, bank_account_data_set); | ||
161 | if (pf) | ||
162 | pf(mdl, name, number); | ||
163 | else | ||
164 | printf("ERROR %d\n", __LINE__); | ||
165 | } | ||
166 | |||
167 | void | ||
168 | data_print(Eina_Model *mdl) | ||
169 | { | ||
170 | EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(mdl, BANK_ACCOUNT_TYPE)); | ||
171 | |||
172 | void (*pf)(Eina_Model *); | ||
173 | pf = eina_model_method_resolve(mdl, Bank_Account_Type, print); | ||
174 | if (pf) | ||
175 | pf(mdl); | ||
176 | else | ||
177 | printf("ERROR %d\n", __LINE__); | ||
178 | } | ||
179 | |||
180 | void | ||
181 | credit_card_data_set(Eina_Model *mdl, const char *number, const char *expiry_date, int pin) | ||
182 | { | ||
183 | EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(mdl, CREDIT_CARD_TYPE)); | ||
184 | |||
185 | void (*pf)(Eina_Model *, const char *, const char *, int); | ||
186 | pf = eina_model_method_resolve(mdl, Credit_Card_Type, credit_card_data_set); | ||
187 | if (pf) | ||
188 | pf(mdl, number, expiry_date, pin); | ||
189 | else | ||
190 | printf("ERROR %d\n", __LINE__); | ||
191 | } | ||
192 | |||
193 | int main(void) | ||
194 | { | ||
195 | Eina_Model *b, *cc; | ||
196 | |||
197 | eina_init(); | ||
198 | |||
199 | memset(&_BANK_ACCOUNT_TYPE, 0, sizeof(_BANK_ACCOUNT_TYPE)); | ||
200 | memset(&_CREDIT_CARD_TYPE, 0, sizeof(_CREDIT_CARD_TYPE)); | ||
201 | |||
202 | BANK_ACCOUNT_TYPE->version = EINA_MODEL_TYPE_VERSION; | ||
203 | BANK_ACCOUNT_TYPE->type_size = sizeof(Bank_Account_Type); | ||
204 | BANK_ACCOUNT_TYPE->private_size = sizeof(Bank_Account_Data); | ||
205 | BANK_ACCOUNT_TYPE->name = "Bank_Account_Model"; | ||
206 | BANK_ACCOUNT_TYPE->parent = EINA_MODEL_TYPE_GENERIC; | ||
207 | BANK_ACCOUNT(BANK_ACCOUNT_TYPE)->bank_account_data_set = _bank_account_data_set; | ||
208 | BANK_ACCOUNT(BANK_ACCOUNT_TYPE)->print = _bank_account_data_print; | ||
209 | |||
210 | CREDIT_CARD_TYPE->version = EINA_MODEL_TYPE_VERSION; | ||
211 | CREDIT_CARD_TYPE->type_size = sizeof(Credit_Card_Type); | ||
212 | CREDIT_CARD_TYPE->private_size = sizeof(Credit_Card_Data); | ||
213 | CREDIT_CARD_TYPE->name = "Credit_Card_Model"; | ||
214 | CREDIT_CARD_TYPE->parent = BANK_ACCOUNT_TYPE; | ||
215 | CREDIT_CARD(CREDIT_CARD_TYPE)->credit_card_data_set = _credit_card_data_set; | ||
216 | BANK_ACCOUNT(CREDIT_CARD_TYPE)->print = _credit_card_data_print; | ||
217 | |||
218 | b = eina_model_new(BANK_ACCOUNT_TYPE); //creating object of bank class | ||
219 | cc = eina_model_new(CREDIT_CARD_TYPE); //creating object of credit card class | ||
220 | |||
221 | bank_account_data_set(b, "Bill Clark", "8569214756"); | ||
222 | bank_account_data_set(cc, "John Smith", "3154789"); | ||
223 | |||
224 | credit_card_data_set(cc, "5803 6589 4786 3279 9173", "01/01/2015", 1234); | ||
225 | |||
226 | data_print(b); | ||
227 | data_print(cc); | ||
228 | |||
229 | eina_model_unref(b); | ||
230 | eina_model_unref(cc); | ||
231 | |||
232 | eina_shutdown(); | ||
233 | |||
234 | return 0; | ||
235 | } | ||
diff --git a/src/examples/eina/eina_model_02.c b/src/examples/eina/eina_model_02.c new file mode 100644 index 0000000000..a2cb693d67 --- /dev/null +++ b/src/examples/eina/eina_model_02.c | |||
@@ -0,0 +1,61 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_model_02.c -o eina_model_02 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <Eina.h> | ||
5 | |||
6 | static void _cb_on_deleted(void *data, Eina_Model *model, const Eina_Model_Event_Description *desc, void *event_info); | ||
7 | |||
8 | int main(void) | ||
9 | { | ||
10 | Eina_Model *m; | ||
11 | char *s; | ||
12 | int i; | ||
13 | |||
14 | eina_init(); | ||
15 | |||
16 | m = eina_model_new(EINA_MODEL_TYPE_GENERIC); | ||
17 | |||
18 | eina_model_event_callback_add(m, "deleted", _cb_on_deleted, NULL); | ||
19 | |||
20 | //Adding properties to model | ||
21 | for (i = 0; i < 5; i++) | ||
22 | { | ||
23 | Eina_Value val; | ||
24 | char name[2] = {'a'+ i, 0}; | ||
25 | eina_value_setup(&val, EINA_VALUE_TYPE_INT); | ||
26 | eina_value_set(&val, i); | ||
27 | eina_model_property_set(m, name, &val); | ||
28 | eina_value_flush(&val); | ||
29 | } | ||
30 | |||
31 | //Adding children to model | ||
32 | for (i = 0; i < 5; i++) | ||
33 | { | ||
34 | Eina_Value val; | ||
35 | Eina_Model *c = eina_model_new(EINA_MODEL_TYPE_GENERIC); | ||
36 | eina_value_setup(&val, EINA_VALUE_TYPE_INT); | ||
37 | eina_value_set(&val, i); | ||
38 | eina_model_property_set(c, "x", &val); | ||
39 | |||
40 | eina_model_event_callback_add(c, "deleted", _cb_on_deleted, NULL); | ||
41 | |||
42 | eina_model_child_append(m, c); | ||
43 | //Now that the child has been appended to a model, it's parent will manage it's lifecycle | ||
44 | eina_model_unref(c); | ||
45 | eina_value_flush(&val); | ||
46 | } | ||
47 | |||
48 | s = eina_model_to_string(m); | ||
49 | printf("model as string:\n%s\n", s); | ||
50 | |||
51 | free(s); | ||
52 | eina_model_unref(m); | ||
53 | eina_shutdown(); | ||
54 | |||
55 | return 0; | ||
56 | } | ||
57 | |||
58 | static void _cb_on_deleted(void *data, Eina_Model *model, const Eina_Model_Event_Description *desc, void *event_info) | ||
59 | { | ||
60 | printf("deleted %p\n", model); | ||
61 | } | ||
diff --git a/src/examples/eina/eina_model_03.c b/src/examples/eina/eina_model_03.c new file mode 100644 index 0000000000..a5e2562f15 --- /dev/null +++ b/src/examples/eina/eina_model_03.c | |||
@@ -0,0 +1,236 @@ | |||
1 | //Compile with: | ||
2 | //gcc -g eina_model_03.c -o eina_model_03 `pkg-config --cflags --libs eina` | ||
3 | |||
4 | #include <Eina.h> | ||
5 | #include <string.h> | ||
6 | #include <stdio.h> | ||
7 | #include <errno.h> | ||
8 | |||
9 | static Eina_Model_Type *ADDRESS_BOOK_TYPE; | ||
10 | static Eina_Model_Type *PERSON_TYPE; | ||
11 | |||
12 | static void address_book_init(void); | ||
13 | |||
14 | int main(void) | ||
15 | { | ||
16 | Eina_Model *address_book; | ||
17 | Eina_Value val; | ||
18 | int i, count; | ||
19 | char *s; | ||
20 | |||
21 | eina_init(); | ||
22 | |||
23 | address_book_init(); | ||
24 | |||
25 | address_book = eina_model_new(ADDRESS_BOOK_TYPE); | ||
26 | |||
27 | eina_value_setup(&val, EINA_VALUE_TYPE_STRING); | ||
28 | eina_value_set(&val, "addr_book.txt"); | ||
29 | eina_model_property_set(address_book, "filename", &val); | ||
30 | eina_value_flush(&val); | ||
31 | |||
32 | eina_model_load(address_book); | ||
33 | s = eina_model_to_string(address_book); | ||
34 | printf("model as string:\n%s\n\n", s); | ||
35 | free(s); | ||
36 | |||
37 | count = eina_model_child_count(address_book); | ||
38 | printf("Address Book with %d entries:\n", count); | ||
39 | for (i = 0; i < count; i++) | ||
40 | { | ||
41 | Eina_Model *person = eina_model_child_get(address_book, i); | ||
42 | Eina_Value nameval, emailval; | ||
43 | const char *name, *email; | ||
44 | |||
45 | eina_model_property_get(person, "name", &nameval); | ||
46 | eina_model_property_get(person, "email", &emailval); | ||
47 | |||
48 | eina_value_get(&nameval, &name); | ||
49 | eina_value_get(&emailval, &email); | ||
50 | |||
51 | printf("%02d \"%s\" <%s>\n", i, name, email); | ||
52 | |||
53 | // We don't need property values anymore | ||
54 | eina_value_flush(&nameval); | ||
55 | eina_value_flush(&emailval); | ||
56 | |||
57 | // We don't need our reference to person anymore | ||
58 | eina_model_unref(person); | ||
59 | } | ||
60 | |||
61 | eina_model_unref(address_book); | ||
62 | eina_shutdown(); | ||
63 | |||
64 | return 0; | ||
65 | } | ||
66 | |||
67 | // Structure Descriptions are just used internally in the type constructors: | ||
68 | static Eina_Value_Struct_Desc *ADDRESS_BOOK_DESC; | ||
69 | static Eina_Value_Struct_Desc *PERSON_DESC; | ||
70 | |||
71 | static Eina_Bool | ||
72 | _person_constructor(Eina_Model *model) | ||
73 | { | ||
74 | // call parent type constructor, like "super" in other languages: | ||
75 | if (!eina_model_type_constructor(EINA_MODEL_TYPE_STRUCT, model)) | ||
76 | return EINA_FALSE; | ||
77 | |||
78 | // Do specific setup of our internal structure, letting it know about | ||
79 | // our description | ||
80 | return eina_model_struct_set(model, PERSON_DESC, NULL); | ||
81 | } | ||
82 | |||
83 | static Eina_Bool | ||
84 | _address_book_constructor(Eina_Model *model) | ||
85 | { | ||
86 | // call parent type constructor, like "super" in other languages: | ||
87 | if (!eina_model_type_constructor(EINA_MODEL_TYPE_STRUCT, model)) | ||
88 | return EINA_FALSE; | ||
89 | |||
90 | // Do specific setup of our internal structure, letting it know about | ||
91 | // our description | ||
92 | return eina_model_struct_set(model, ADDRESS_BOOK_DESC, NULL); | ||
93 | } | ||
94 | |||
95 | static Eina_Bool | ||
96 | _address_book_load(Eina_Model *model) | ||
97 | { | ||
98 | const char *filename; | ||
99 | Eina_Value val; | ||
100 | char buf[256]; | ||
101 | FILE *f; | ||
102 | |||
103 | // We retrieve filename from property of same name: | ||
104 | eina_model_property_get(model, "filename", &val); | ||
105 | eina_value_get(&val, &filename); | ||
106 | |||
107 | EINA_SAFETY_ON_NULL_RETURN_VAL(filename, EINA_FALSE); | ||
108 | |||
109 | f = fopen(filename, "r"); | ||
110 | |||
111 | // Now that we have used filename, we must free its memory holder: | ||
112 | eina_value_flush(&val); | ||
113 | |||
114 | EINA_SAFETY_ON_NULL_RETURN_VAL(f, EINA_FALSE); | ||
115 | |||
116 | while (fgets(buf, sizeof(buf), f)) | ||
117 | { | ||
118 | Eina_Model *person; | ||
119 | char *name, *email; | ||
120 | |||
121 | if (strlen(buf) <= 1) | ||
122 | continue; | ||
123 | |||
124 | name = strtok(buf, "\t"); | ||
125 | email = strtok(NULL, "\n"); | ||
126 | |||
127 | if ((!name) || (!email)) continue; | ||
128 | |||
129 | // Create person | ||
130 | person = eina_model_new(PERSON_TYPE); | ||
131 | |||
132 | // Setup value type as string, as our properties are strings: | ||
133 | eina_value_setup(&val, EINA_VALUE_TYPE_STRING); | ||
134 | |||
135 | // Set string properties: | ||
136 | eina_value_set(&val, name); | ||
137 | eina_model_property_set(person, "name", &val); | ||
138 | |||
139 | eina_value_set(&val, email); | ||
140 | eina_model_property_set(person, "email", &val); | ||
141 | |||
142 | // Flush value, free string | ||
143 | eina_value_flush(&val); | ||
144 | |||
145 | // Add person to the end of model children | ||
146 | eina_model_child_append(model, person); | ||
147 | |||
148 | // Model already holds its reference to person, we release ours | ||
149 | eina_model_unref(person); | ||
150 | } | ||
151 | |||
152 | fclose(f); | ||
153 | return EINA_TRUE; | ||
154 | } | ||
155 | |||
156 | static void | ||
157 | address_book_init(void) | ||
158 | { | ||
159 | // Declare type for internal struct, this is just used to easily | ||
160 | // create Eina_Value_Struct_Member array for Eina_Value_Struct_Desc. | ||
161 | // | ||
162 | // We don't need this structure outside address_book_init() | ||
163 | // as it is managed automatically by Eina_Value_Struct, used by | ||
164 | // Eina_Model_Struct! Handy! :-) | ||
165 | typedef struct _Person Person; | ||
166 | struct _Person | ||
167 | { | ||
168 | const char *name; | ||
169 | const char *email; | ||
170 | }; | ||
171 | static Eina_Value_Struct_Member person_members[] = { | ||
172 | // no eina_value_type as they are not constant initializers, see below. | ||
173 | EINA_VALUE_STRUCT_MEMBER(NULL, Person, name), | ||
174 | EINA_VALUE_STRUCT_MEMBER(NULL, Person, email) | ||
175 | }; | ||
176 | // Values that cannot be set on static declarations since they are not | ||
177 | // constant initializers. It is a nitpick from C that we need to deal with | ||
178 | // here and on all our other declarations. | ||
179 | person_members[0].type = EINA_VALUE_TYPE_STRING; | ||
180 | person_members[1].type = EINA_VALUE_TYPE_STRING; | ||
181 | |||
182 | static Eina_Value_Struct_Desc person_desc = { | ||
183 | EINA_VALUE_STRUCT_DESC_VERSION, | ||
184 | NULL, // no special operations | ||
185 | person_members, | ||
186 | EINA_C_ARRAY_LENGTH(person_members), | ||
187 | sizeof(Person) | ||
188 | }; | ||
189 | static Eina_Model_Type person_type = EINA_MODEL_TYPE_INIT_NOPRIVATE | ||
190 | ("Person_Type", | ||
191 | Eina_Model_Type, | ||
192 | NULL, // no type as EINA_MODEL_TYPE_STRUCT is not constant initializer! | ||
193 | NULL, // no extra interfaces | ||
194 | NULL // no extra events); | ||
195 | ); | ||
196 | person_type.parent = EINA_MODEL_TYPE_STRUCT; | ||
197 | // Set our overloaded methods: | ||
198 | person_type.constructor = _person_constructor; | ||
199 | |||
200 | typedef struct _Address_Book Address_Book; | ||
201 | struct _Address_Book | ||
202 | { | ||
203 | const char *filename; | ||
204 | }; | ||
205 | static Eina_Value_Struct_Member address_book_members[] = { | ||
206 | // no eina_value_type as they are not constant initializers, see below. | ||
207 | EINA_VALUE_STRUCT_MEMBER(NULL, Address_Book, filename) | ||
208 | }; | ||
209 | address_book_members[0].type = EINA_VALUE_TYPE_STRING; | ||
210 | static Eina_Value_Struct_Desc address_book_desc = { | ||
211 | EINA_VALUE_STRUCT_DESC_VERSION, | ||
212 | NULL, // no special operations | ||
213 | address_book_members, | ||
214 | EINA_C_ARRAY_LENGTH(address_book_members), | ||
215 | sizeof(Address_Book) | ||
216 | }; | ||
217 | static Eina_Model_Type address_book_type = EINA_MODEL_TYPE_INIT_NOPRIVATE | ||
218 | ("Address_Book_Type", | ||
219 | Eina_Model_Type, | ||
220 | NULL, // no type as EINA_MODEL_TYPE_STRUCT is not constant initializer! | ||
221 | NULL, // no extra interfaces | ||
222 | NULL // no extra events); | ||
223 | ); | ||
224 | address_book_type.parent = EINA_MODEL_TYPE_STRUCT; | ||
225 | // Set our overloaded methods: | ||
226 | address_book_type.constructor = _address_book_constructor; | ||
227 | address_book_type.load = _address_book_load; | ||
228 | |||
229 | // Expose the configured pointers to public usage: | ||
230 | // NOTE: they are static, so they live after this function returns! | ||
231 | PERSON_TYPE = &person_type; | ||
232 | PERSON_DESC = &person_desc; | ||
233 | |||
234 | ADDRESS_BOOK_TYPE = &address_book_type; | ||
235 | ADDRESS_BOOK_DESC = &address_book_desc; | ||
236 | } | ||
diff --git a/src/examples/eina/eina_model_04_animal.c b/src/examples/eina/eina_model_04_animal.c new file mode 100644 index 0000000000..bc9f06b7a8 --- /dev/null +++ b/src/examples/eina/eina_model_04_animal.c | |||
@@ -0,0 +1,76 @@ | |||
1 | /* | ||
2 | * animal.c | ||
3 | */ | ||
4 | |||
5 | #include "eina_model_04_animal.h" | ||
6 | |||
7 | static Eina_Bool initialized = EINA_FALSE; | ||
8 | |||
9 | static void | ||
10 | _animal_eat(Eina_Model *m) | ||
11 | { | ||
12 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
13 | __func__); | ||
14 | printf("\t\t Eat Animal\n"); | ||
15 | } | ||
16 | |||
17 | static void | ||
18 | _animal_breathe(Eina_Model *m) | ||
19 | { | ||
20 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
21 | __func__); | ||
22 | printf("\t\t Breathe Animal\n"); | ||
23 | } | ||
24 | |||
25 | const char *ANIMAL_MODEL_TYPE_NAME = NULL; | ||
26 | static Animal_Type _ANIMAL_TYPE; | ||
27 | |||
28 | const Eina_Model_Type * const ANIMAL_TYPE = (Eina_Model_Type *) &_ANIMAL_TYPE; | ||
29 | |||
30 | void | ||
31 | animal_init(void) | ||
32 | { | ||
33 | Eina_Model_Type *type; | ||
34 | |||
35 | if (initialized) return; | ||
36 | initialized = EINA_TRUE; | ||
37 | |||
38 | ANIMAL_MODEL_TYPE_NAME = "Animal_Model_Type"; | ||
39 | |||
40 | type = (Eina_Model_Type *)&_ANIMAL_TYPE; | ||
41 | type->version = EINA_MODEL_TYPE_VERSION; | ||
42 | type->name = ANIMAL_MODEL_TYPE_NAME; | ||
43 | type->private_size = 0; | ||
44 | |||
45 | eina_model_type_subclass_setup(type, EINA_MODEL_TYPE_GENERIC); | ||
46 | |||
47 | /* define extra methods */ | ||
48 | |||
49 | type->type_size = sizeof(Animal_Type); | ||
50 | ANIMAL_TYPE(type)->breathe = _animal_breathe; | ||
51 | ANIMAL_TYPE(type)->eat = _animal_eat; | ||
52 | } | ||
53 | |||
54 | void | ||
55 | animal_breathe(Eina_Model *m) | ||
56 | { | ||
57 | EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, ANIMAL_TYPE)); | ||
58 | |||
59 | void (*pf)(Eina_Model *m); | ||
60 | pf = eina_model_method_resolve(m, Animal_Type, breathe); | ||
61 | EINA_SAFETY_ON_NULL_RETURN(pf); | ||
62 | printf("%s() \t", __func__); | ||
63 | pf(m); | ||
64 | } | ||
65 | |||
66 | void | ||
67 | animal_eat(Eina_Model *m) | ||
68 | { | ||
69 | EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, ANIMAL_TYPE)); | ||
70 | |||
71 | void (*pf)(Eina_Model *m); | ||
72 | pf = eina_model_method_resolve(m, Animal_Type, eat); | ||
73 | EINA_SAFETY_ON_NULL_RETURN(pf); | ||
74 | printf("%s() \t", __func__); | ||
75 | pf(m); | ||
76 | } | ||
diff --git a/src/examples/eina/eina_model_04_animal.h b/src/examples/eina/eina_model_04_animal.h new file mode 100644 index 0000000000..1af94c3831 --- /dev/null +++ b/src/examples/eina/eina_model_04_animal.h | |||
@@ -0,0 +1,26 @@ | |||
1 | /* | ||
2 | * animal.h | ||
3 | */ | ||
4 | |||
5 | #ifndef ANIMAL_H_ | ||
6 | #define ANIMAL_H_ | ||
7 | |||
8 | #include <Eina.h> | ||
9 | |||
10 | extern const char *ANIMAL_MODEL_TYPE_NAME; | ||
11 | extern const Eina_Model_Type * const ANIMAL_TYPE; | ||
12 | |||
13 | #define ANIMAL_TYPE(x) ((Animal_Type *) (eina_model_type_subclass_check((x), ANIMAL_TYPE) ? (x) : NULL)) | ||
14 | |||
15 | typedef struct _Animal_Type | ||
16 | { | ||
17 | Eina_Model_Type parent_class; | ||
18 | void (*eat)(Eina_Model *m); | ||
19 | void (*breathe)(Eina_Model *m); | ||
20 | } Animal_Type; | ||
21 | |||
22 | void animal_init(void); | ||
23 | void animal_breathe(Eina_Model *m); | ||
24 | void animal_eat(Eina_Model *m); | ||
25 | |||
26 | #endif /* ANIMAL_H_ */ | ||
diff --git a/src/examples/eina/eina_model_04_child.c b/src/examples/eina/eina_model_04_child.c new file mode 100644 index 0000000000..59b8aa5f1a --- /dev/null +++ b/src/examples/eina/eina_model_04_child.c | |||
@@ -0,0 +1,81 @@ | |||
1 | /* | ||
2 | * child.c | ||
3 | */ | ||
4 | |||
5 | #include "eina_model_04_child.h" | ||
6 | #include "eina_model_04_whistler.h" | ||
7 | |||
8 | static Eina_Bool initialized = EINA_FALSE; | ||
9 | |||
10 | static void | ||
11 | _child_cry(Eina_Model *m) | ||
12 | { | ||
13 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
14 | __func__); | ||
15 | printf("\t\t Cry Child\n"); | ||
16 | } | ||
17 | |||
18 | static void | ||
19 | _child_dive(Eina_Model *m) | ||
20 | { | ||
21 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
22 | __func__); | ||
23 | printf("\t\t Dive Child\n"); | ||
24 | } | ||
25 | |||
26 | const char *CHILD_MODEL_TYPE_NAME = NULL; | ||
27 | |||
28 | static Child_Type _CHILD_TYPE; | ||
29 | const Eina_Model_Type * const CHILD_TYPE = (Eina_Model_Type *) &_CHILD_TYPE; | ||
30 | |||
31 | static const Diver_Interface _DIVER_INTERFACE; | ||
32 | static const Eina_Model_Interface * const DIVER_INTERFACE = | ||
33 | (Eina_Model_Interface *) &_DIVER_INTERFACE; | ||
34 | |||
35 | static const Eina_Model_Interface * CLASS_INTERFACE_ARRAY[] = | ||
36 | { &_DIVER_INTERFACE.base_interface, NULL }; //this array is for model | ||
37 | |||
38 | void | ||
39 | child_init() | ||
40 | { | ||
41 | Eina_Model_Type *type; | ||
42 | |||
43 | if (initialized) return; | ||
44 | initialized = EINA_TRUE; | ||
45 | |||
46 | human_init(); | ||
47 | |||
48 | //overriding Diver Interface | ||
49 | Eina_Model_Interface * iface = (Eina_Model_Interface *) &_DIVER_INTERFACE; | ||
50 | iface->version = EINA_MODEL_INTERFACE_VERSION; | ||
51 | iface->interface_size = sizeof(Diver_Interface); | ||
52 | iface->name = DIVER_INTERFACE_NAME; | ||
53 | DIVER_INTERFACE(iface)->dive = _child_dive; | ||
54 | |||
55 | //creating instance of Child type | ||
56 | CHILD_MODEL_TYPE_NAME = "Child_Model_Type"; | ||
57 | |||
58 | type = (Eina_Model_Type *) &_CHILD_TYPE; | ||
59 | type->version = EINA_MODEL_TYPE_VERSION; | ||
60 | type->name = CHILD_MODEL_TYPE_NAME; | ||
61 | |||
62 | eina_model_type_subclass_setup(type, HUMAN_TYPE); | ||
63 | |||
64 | type->type_size = sizeof(Child_Type); | ||
65 | type->interfaces = CLASS_INTERFACE_ARRAY; | ||
66 | |||
67 | CHILD_TYPE(type)->cry = _child_cry; | ||
68 | } | ||
69 | |||
70 | //call for implemented Child Class function | ||
71 | void | ||
72 | child_cry(Eina_Model *m) | ||
73 | { | ||
74 | EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, CHILD_TYPE)); | ||
75 | |||
76 | void (*pf)(Eina_Model *m); | ||
77 | pf = eina_model_method_resolve(m, Child_Type, cry); | ||
78 | EINA_SAFETY_ON_NULL_RETURN(pf); | ||
79 | printf("%s() \t\t", __func__); | ||
80 | pf(m); | ||
81 | } | ||
diff --git a/src/examples/eina/eina_model_04_child.h b/src/examples/eina/eina_model_04_child.h new file mode 100644 index 0000000000..5424217839 --- /dev/null +++ b/src/examples/eina/eina_model_04_child.h | |||
@@ -0,0 +1,23 @@ | |||
1 | /* | ||
2 | * child.h | ||
3 | */ | ||
4 | |||
5 | #ifndef CHILD_H_ | ||
6 | #define CHILD_H_ | ||
7 | |||
8 | #include "eina_model_04_human.h" | ||
9 | |||
10 | extern const char *CHILD_MODEL_TYPE_NAME; | ||
11 | extern const Eina_Model_Type * const CHILD_TYPE; | ||
12 | #define CHILD_TYPE(x) ((Child_Type *) (eina_model_type_subclass_check((x), CHILD_TYPE) ? (x) : NULL)) | ||
13 | |||
14 | typedef struct _Child_Type | ||
15 | { | ||
16 | Human_Type parent_class; | ||
17 | void (*cry)(Eina_Model *m); | ||
18 | } Child_Type; | ||
19 | |||
20 | void child_init(); | ||
21 | void child_cry(Eina_Model *m); | ||
22 | |||
23 | #endif /* CHILD_H_ */ | ||
diff --git a/src/examples/eina/eina_model_04_human.c b/src/examples/eina/eina_model_04_human.c new file mode 100644 index 0000000000..d9a10ab2a6 --- /dev/null +++ b/src/examples/eina/eina_model_04_human.c | |||
@@ -0,0 +1,157 @@ | |||
1 | /* | ||
2 | * human.c | ||
3 | * | ||
4 | */ | ||
5 | |||
6 | #include "eina_model_04_human.h" | ||
7 | #include "eina_model_04_whistler.h" | ||
8 | |||
9 | static Eina_Bool initialized = EINA_FALSE; | ||
10 | |||
11 | static void | ||
12 | _human_eat(Eina_Model *m) | ||
13 | { | ||
14 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
15 | __func__); | ||
16 | printf("\t\t Salad\n"); | ||
17 | } | ||
18 | |||
19 | static void | ||
20 | _human_walk(Eina_Model *m) | ||
21 | { | ||
22 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
23 | __func__); | ||
24 | printf("\t\t Walk\n"); | ||
25 | } | ||
26 | |||
27 | static void | ||
28 | _human_whistle(Eina_Model *m) | ||
29 | { | ||
30 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
31 | __func__); | ||
32 | printf("\t\t Whistle Human\n"); | ||
33 | } | ||
34 | |||
35 | static void | ||
36 | _human_swim(Eina_Model *m) | ||
37 | { | ||
38 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
39 | __func__); | ||
40 | printf("\t\t Swim Human\n"); | ||
41 | } | ||
42 | |||
43 | static void | ||
44 | _human_dive(Eina_Model *m) | ||
45 | { | ||
46 | printf("%s\t%s", eina_model_type_name_get(eina_model_type_get(m)), | ||
47 | __func__); | ||
48 | printf("\t\t Dive Human\n"); | ||
49 | } | ||
50 | /* | ||
51 | * defining Human Model Instance | ||
52 | * defining Whistler Interface instance | ||
53 | * defining Swimmer Interface instance | ||
54 | * defining Diver Interface instance | ||
55 | */ | ||
56 | |||
57 | const char *HUMAN_MODEL_TYPE_NAME = NULL; | ||
58 | |||
59 | static Human_Type _HUMAN_TYPE; | ||
60 | const Eina_Model_Type * const HUMAN_TYPE = (Eina_Model_Type *) &_HUMAN_TYPE; | ||
61 | |||
62 | static const Whistler_Interface _WHISTLER_INTERFACE; | ||
63 | static const Eina_Model_Interface * const WHISTLER_INTERFACE = | ||
64 | (Eina_Model_Interface *) &_WHISTLER_INTERFACE; | ||
65 | |||
66 | static const Swimmer_Interface _SWIMMER_INTERFACE; | ||
67 | static const Eina_Model_Interface * const SWIMMER_INTERFACE = | ||
68 | (Eina_Model_Interface *) &_SWIMMER_INTERFACE; | ||
69 | |||
70 | static const Diver_Interface _DIVER_INTERFACE; | ||
71 | static const Eina_Model_Interface * const DIVER_INTERFACE = | ||
72 | (Eina_Model_Interface *) &_DIVER_INTERFACE; | ||
73 | |||
74 | /* | ||
75 | * defining parent interfaces for Diver Interface instance | ||
76 | * defining Interfaces for Human Model instance | ||
77 | */ | ||
78 | static const Eina_Model_Interface * PARENT_INTERFACES_ARRAY[] = | ||
79 | { &_SWIMMER_INTERFACE.base_interface, NULL }; //this array is for model | ||
80 | static const Eina_Model_Interface * MODEL_INTERFACES_ARRAY[] = | ||
81 | { &_WHISTLER_INTERFACE.base_interface, &_DIVER_INTERFACE.base_interface, | ||
82 | NULL }; //this array is for model | ||
83 | |||
84 | void | ||
85 | human_init() | ||
86 | { | ||
87 | Eina_Model_Type *type; | ||
88 | |||
89 | if (initialized) return; | ||
90 | initialized = EINA_TRUE; | ||
91 | |||
92 | animal_init(); | ||
93 | |||
94 | /* | ||
95 | * Initializing Whistler Interface Instance | ||
96 | */ | ||
97 | Eina_Model_Interface *iface = (Eina_Model_Interface *) &_WHISTLER_INTERFACE; | ||
98 | iface->version = EINA_MODEL_INTERFACE_VERSION; | ||
99 | iface->interface_size = sizeof(Whistler_Interface); | ||
100 | iface->name = WHISTLER_INTERFACE_NAME; | ||
101 | WHISTLER_INTERFACE(iface)->whistle = _human_whistle; | ||
102 | |||
103 | /* | ||
104 | * Initializing Swimmer Interface Instance | ||
105 | */ | ||
106 | iface = (Eina_Model_Interface *) &_SWIMMER_INTERFACE; | ||
107 | iface->version = EINA_MODEL_INTERFACE_VERSION; | ||
108 | iface->interface_size = sizeof(Swimmer_Interface); | ||
109 | iface->name = SWIMMER_INTERFACE_NAME; | ||
110 | SWIMMER_INTERFACE(iface)->swim = _human_swim; | ||
111 | |||
112 | /* | ||
113 | * Initializing Diver Interface Instance | ||
114 | * Diver_Interface is inherited from Swimmer | ||
115 | */ | ||
116 | iface = (Eina_Model_Interface *) &_DIVER_INTERFACE; | ||
117 | iface->version = EINA_MODEL_INTERFACE_VERSION; | ||
118 | iface->interface_size = sizeof(Diver_Interface); | ||
119 | iface->name = DIVER_INTERFACE_NAME; | ||
120 | iface->interfaces = PARENT_INTERFACES_ARRAY; | ||
121 | DIVER_INTERFACE(iface)->dive = _human_dive; | ||
122 | |||
123 | /* | ||
124 | * Initializing instance of Human Model | ||
125 | */ | ||
126 | |||
127 | HUMAN_MODEL_TYPE_NAME = "Human_Model_Type"; | ||
128 | |||
129 | type = (Eina_Model_Type *) &_HUMAN_TYPE; | ||
130 | type->version = EINA_MODEL_TYPE_VERSION; | ||
131 | type->name = HUMAN_MODEL_TYPE_NAME; | ||
132 | type->private_size = 0; | ||
133 | |||
134 | eina_model_type_subclass_setup(type, ANIMAL_TYPE); | ||
135 | |||
136 | type->type_size = sizeof(Human_Type); | ||
137 | type->interfaces = MODEL_INTERFACES_ARRAY; | ||
138 | |||
139 | ANIMAL_TYPE(type)->eat = _human_eat; | ||
140 | HUMAN_TYPE(type)->walk =_human_walk; | ||
141 | } | ||
142 | |||
143 | |||
144 | /* | ||
145 | * call for implemented Human Class function | ||
146 | */ | ||
147 | void | ||
148 | human_walk(Eina_Model *m) | ||
149 | { | ||
150 | EINA_SAFETY_ON_FALSE_RETURN(eina_model_instance_check(m, HUMAN_TYPE)); | ||
151 | |||
152 | void (*pf)(Eina_Model *m); | ||
153 | pf = eina_model_method_resolve(m, Human_Type, walk); | ||
154 | EINA_SAFETY_ON_NULL_RETURN(pf); | ||
155 | printf("%s() \t", __func__); | ||
156 | pf(m); | ||
157 | } | ||
diff --git a/src/examples/eina/eina_model_04_human.h b/src/examples/eina/eina_model_04_human.h new file mode 100644 index 0000000000..1ad7518010 --- /dev/null +++ b/src/examples/eina/eina_model_04_human.h | |||
@@ -0,0 +1,24 @@ | |||
1 | /* | ||
2 | * human.h | ||
3 | */ | ||
4 | |||
5 | #ifndef HUMAN_H_ | ||
6 | #define HUMAN_H_ | ||
7 | |||
8 | #include "eina_model_04_animal.h" | ||
9 | |||
10 | extern const char *HUMAN_MODEL_TYPE_NAME; | ||
11 | extern const Eina_Model_Type * const HUMAN_TYPE; | ||
12 | |||
13 | #define HUMAN_TYPE(x) ((Human_Type *) (eina_model_type_subclass_check((x), ANIMAL_TYPE) ? (x) : NULL)) | ||
14 | |||
15 | typedef struct _Human_Type | ||
16 | { | ||
17 | Animal_Type parent_class; | ||
18 | void (*walk)(Eina_Model *m); | ||
19 | } Human_Type; | ||
20 | |||
21 | void human_init(); | ||
22 | void human_walk(Eina_Model *m); | ||
23 | |||
24 | #endif /* HUMAN_H_ */ | ||
diff --git a/src/examples/eina/eina_model_04_main.c b/src/examples/eina/eina_model_04_main.c new file mode 100644 index 0000000000..0e419ec317 --- /dev/null +++ b/src/examples/eina/eina_model_04_main.c | |||
@@ -0,0 +1,110 @@ | |||
1 | /* | ||
2 | * main_animal.c | ||
3 | * compile with: gcc eina_model_04_*.c -o eina_model_04 `pkg-config --cflags --libs eina` | ||
4 | */ | ||
5 | |||
6 | /* | ||
7 | * This example demonstrates the extended usage of Eina Model. | ||
8 | * Class inheritance and interface implementation | ||
9 | * | ||
10 | * Animal Class is inherited from BaseClass and implements | ||
11 | * "_breathe_animal()" and "_eat_animal()" methods. | ||
12 | * | ||
13 | * Human Class is inherited from Animal class. | ||
14 | * Parrot Class is inherited from Animal class. | ||
15 | * | ||
16 | * Child Class is inherited from Human class. | ||
17 | * | ||
18 | * Human Class and Parrot Class implement Whistler Interface. | ||
19 | * Human Class implements Diver Interface. Diver Interface inherited from Swimmer Interface | ||
20 | * | <