summaryrefslogtreecommitdiff
path: root/legacy/eet/src/examples
diff options
context:
space:
mode:
authorIván Briano <sachieru@gmail.com>2011-06-14 16:14:35 +0000
committerIván Briano <sachieru@gmail.com>2011-06-14 16:14:35 +0000
commitcb94f911a42e8198a7ca88cec3fca36216a676aa (patch)
treeb9e76268e8a7567359ce75704930cde075f10c77 /legacy/eet/src/examples
parent80a5464888990a67ede7633b30e8bb772c5bb3e5 (diff)
Slight improvement to Eet documentation.
It was mostly done already and examples were there, but some things were revamped, clarified and now there's a detailed explanation on more important groups. SVN revision: 60308
Diffstat (limited to 'legacy/eet/src/examples')
-rw-r--r--legacy/eet/src/examples/Makefile.am10
-rw-r--r--legacy/eet/src/examples/eet-basic.c36
-rw-r--r--legacy/eet/src/examples/eet-data-file_descriptor.c111
-rw-r--r--legacy/eet/src/examples/eet-data-nested.c3
-rw-r--r--legacy/eet/src/examples/eet-data-simple.c3
-rw-r--r--legacy/eet/src/examples/eet-file.c126
6 files changed, 240 insertions, 49 deletions
diff --git a/legacy/eet/src/examples/Makefile.am b/legacy/eet/src/examples/Makefile.am
index c760295898..46e84020f5 100644
--- a/legacy/eet/src/examples/Makefile.am
+++ b/legacy/eet/src/examples/Makefile.am
@@ -7,7 +7,13 @@ AM_CPPFLAGS = \
7-I$(top_srcdir)/src/lib \ 7-I$(top_srcdir)/src/lib \
8@EINA_CFLAGS@ 8@EINA_CFLAGS@
9 9
10pkglib_PROGRAMS = eet_data_simple eet_data_nested eet_data_file_descriptor 10pkglib_PROGRAMS = eet_basic eet_file eet_data_simple eet_data_nested eet_data_file_descriptor
11
12eet_basic_SOURCES = eet-basic.c
13eet_basic_LDADD = $(top_builddir)/src/lib/libeet.la @EINA_LIBS@
14
15eet_file_SOURCES = eet-file.c
16eet_file_LDADD = $(top_builddir)/src/lib/libeet.la @EINA_LIBS@
11 17
12eet_data_simple_SOURCES = eet-data-simple.c 18eet_data_simple_SOURCES = eet-data-simple.c
13eet_data_simple_LDADD = $(top_builddir)/src/lib/libeet.la @EINA_LIBS@ 19eet_data_simple_LDADD = $(top_builddir)/src/lib/libeet.la @EINA_LIBS@
@@ -25,6 +31,8 @@ files_DATA =
25if INSTALL_EXAMPLES 31if INSTALL_EXAMPLES
26 32
27files_DATA += \ 33files_DATA += \
34eet-basic.c \
35eet-file.c \
28eet-data-simple.c \ 36eet-data-simple.c \
29eet-data-nested.c \ 37eet-data-nested.c \
30eet-data-file_descriptor.c 38eet-data-file_descriptor.c
diff --git a/legacy/eet/src/examples/eet-basic.c b/legacy/eet/src/examples/eet-basic.c
new file mode 100644
index 0000000000..2c85a54971
--- /dev/null
+++ b/legacy/eet/src/examples/eet-basic.c
@@ -0,0 +1,36 @@
1#include <Eet.h>
2
3int
4main(void)
5{
6 Eet_File *ef;
7 char *ret;
8 int size;
9 char *entries[] =
10 {
11 "Entry 1",
12 "Big text string here compared to others",
13 "Eet is cool"
14 };
15
16 eet_init();
17
18 // blindly open an file for output and write strings with their NUL char
19 ef = eet_open("test.eet", EET_FILE_MODE_WRITE);
20 eet_write(ef, "Entry 1", entries[0], strlen(entries[0]) + 1, 0);
21 eet_write(ef, "Entry 2", entries[1], strlen(entries[1]) + 1, 1);
22 eet_write(ef, "Entry 3", entries[2], strlen(entries[2]) + 1, 0);
23 eet_close(ef);
24
25 // open the file again and blindly get the entries we wrote
26 ef = eet_open("test.eet", EET_FILE_MODE_READ);
27 ret = eet_read(ef, "Entry 1", &size);
28 printf("%s\n", ret);
29 ret = eet_read(ef, "Entry 2", &size);
30 printf("%s\n", ret);
31 ret = eet_read(ef, "Entry 3", &size);
32 printf("%s\n", ret);
33 eet_close(ef);
34
35 eet_shutdown();
36}
diff --git a/legacy/eet/src/examples/eet-data-file_descriptor.c b/legacy/eet/src/examples/eet-data-file_descriptor.c
index 70fba7b1e1..ed0f7243a7 100644
--- a/legacy/eet/src/examples/eet-data-file_descriptor.c
+++ b/legacy/eet/src/examples/eet-data-file_descriptor.c
@@ -1,3 +1,6 @@
1/*
2 * build: gcc -o eet_data_file_descriptor eet-data-file_descriptor.c `pkg-config --cflags --libs eet eina`
3 */
1#include <Eina.h> 4#include <Eina.h>
2#include <Eet.h> 5#include <Eet.h>
3#include <stdio.h> 6#include <stdio.h>
@@ -9,9 +12,9 @@
9// complex real-world structures based on elmdentica database 12// complex real-world structures based on elmdentica database
10typedef struct 13typedef struct
11{ 14{
12 const char * screen_name; 15 const char *screen_name;
13 const char * name; 16 const char *name;
14 const char * message; 17 const char *message;
15 unsigned int id; 18 unsigned int id;
16 unsigned int status_id; 19 unsigned int status_id;
17 unsigned int date; 20 unsigned int date;
@@ -20,22 +23,23 @@ typedef struct
20 23
21typedef struct 24typedef struct
22{ 25{
23 const char * dm_to; 26 const char *dm_to;
24 const char * message; 27 const char *message;
25} My_Post; 28} My_Post;
26 29
27typedef struct 30typedef struct
28{ 31{
29 unsigned int id; 32 unsigned int id;
30 const char * name; 33 const char *name;
31 Eina_List * messages; 34 Eina_List *messages;
32 Eina_List * posts; 35 My_Post *posts;
36 int posts_count;
33} My_Account; 37} My_Account;
34 38
35typedef struct 39typedef struct
36{ 40{
37 unsigned int version; // it is recommended to use versioned configuration! 41 unsigned int version; // it is recommended to use versioned configuration!
38 Eina_List * accounts; 42 Eina_Hash *accounts;
39} My_Cache; 43} My_Cache;
40 44
41// string that represents the entry in eet file, you might like to have 45// string that represents the entry in eet file, you might like to have
@@ -46,14 +50,14 @@ static const char MY_CACHE_FILE_ENTRY[] = "cache";
46// keep the descriptor static global, so it can be 50// keep the descriptor static global, so it can be
47// shared by different functions (load/save) of this and only this 51// shared by different functions (load/save) of this and only this
48// file. 52// file.
49static Eet_Data_Descriptor * _my_cache_descriptor; 53static Eet_Data_Descriptor *_my_cache_descriptor;
50static Eet_Data_Descriptor * _my_account_descriptor; 54static Eet_Data_Descriptor *_my_account_descriptor;
51static Eet_Data_Descriptor * _my_message_descriptor; 55static Eet_Data_Descriptor *_my_message_descriptor;
52static Eet_Data_Descriptor * _my_post_descriptor; 56static Eet_Data_Descriptor *_my_post_descriptor;
53 57
54// keep file handle alive, so mmap()ed strings are all alive as well 58// keep file handle alive, so mmap()ed strings are all alive as well
55static Eet_File * _my_cache_file = NULL; 59static Eet_File *_my_cache_file = NULL;
56static Eet_Dictionary * _my_cache_dict = NULL; 60static Eet_Dictionary *_my_cache_dict = NULL;
57 61
58static void 62static void
59_my_cache_descriptor_init(void) 63_my_cache_descriptor_init(void)
@@ -107,7 +111,7 @@ _my_cache_descriptor_init(void)
107 EET_DATA_DESCRIPTOR_ADD_LIST 111 EET_DATA_DESCRIPTOR_ADD_LIST
108 (_my_account_descriptor, My_Account, "messages", messages, 112 (_my_account_descriptor, My_Account, "messages", messages,
109 _my_message_descriptor); 113 _my_message_descriptor);
110 EET_DATA_DESCRIPTOR_ADD_LIST 114 EET_DATA_DESCRIPTOR_ADD_VAR_ARRAY
111 (_my_account_descriptor, My_Account, "posts", posts, 115 (_my_account_descriptor, My_Account, "posts", posts,
112 _my_post_descriptor); 116 _my_post_descriptor);
113 117
@@ -117,7 +121,7 @@ _my_cache_descriptor_init(void)
117 ADD_BASIC(version, EET_T_UINT); 121 ADD_BASIC(version, EET_T_UINT);
118#undef ADD_BASIC 122#undef ADD_BASIC
119 123
120 EET_DATA_DESCRIPTOR_ADD_LIST 124 EET_DATA_DESCRIPTOR_ADD_HASH
121 (_my_cache_descriptor, My_Cache, "accounts", accounts, 125 (_my_cache_descriptor, My_Cache, "accounts", accounts,
122 _my_account_descriptor); 126 _my_account_descriptor);
123} /* _my_cache_descriptor_init */ 127} /* _my_cache_descriptor_init */
@@ -168,18 +172,22 @@ _my_message_free(My_Message * msg)
168 free(msg); 172 free(msg);
169} /* _my_message_free */ 173} /* _my_message_free */
170 174
171static My_Post * 175static Eina_Bool
172_my_post_new(const char * message) 176_my_post_add(My_Account *acc, const char * message)
173{ 177{
174 My_Post * post = calloc(1, sizeof(My_Post)); 178 int new_count = acc->posts_count + 1;
179 My_Post *post = realloc(acc->posts, new_count * sizeof(My_Post));
175 if (!post) 180 if (!post)
176 { 181 {
177 fprintf(stderr, "ERROR: could not calloc My_Post\n"); 182 fprintf(stderr, "ERROR: could add My_Post\n");
178 return NULL; 183 return EINA_FALSE;
179 } 184 }
180 185
181 post->message = eina_stringshare_add(message); 186 post[acc->posts_count].message = eina_stringshare_add(message);
182 return post; 187 post[acc->posts_count].dm_to = NULL;
188 acc->posts_count = new_count;
189 acc->posts = post;
190 return EINA_TRUE;;
183} /* _my_post_new */ 191} /* _my_post_new */
184 192
185static void 193static void
@@ -187,7 +195,6 @@ _my_post_free(My_Post * post)
187{ 195{
188 _eet_string_free(post->dm_to); 196 _eet_string_free(post->dm_to);
189 _eet_string_free(post->message); 197 _eet_string_free(post->message);
190 free(post);
191} /* _my_post_free */ 198} /* _my_post_free */
192 199
193static My_Account * 200static My_Account *
@@ -208,15 +215,16 @@ static void
208_my_account_free(My_Account * acc) 215_my_account_free(My_Account * acc)
209{ 216{
210 My_Message * m; 217 My_Message * m;
211 My_Post * p; 218 int i;
212 219
213 _eet_string_free(acc->name); 220 _eet_string_free(acc->name);
214 221
215 EINA_LIST_FREE(acc->messages, m) 222 EINA_LIST_FREE(acc->messages, m)
216 _my_message_free(m); 223 _my_message_free(m);
217 224
218 EINA_LIST_FREE(acc->posts, p) 225 for (i = 0; i < acc->posts_count; i++)
219 _my_post_free(p); 226 _my_post_free(&acc->posts[i]);
227 free(acc->posts);
220 228
221 free(acc); 229 free(acc);
222} /* _my_account_free */ 230} /* _my_account_free */
@@ -231,29 +239,32 @@ _my_cache_new(void)
231 return NULL; 239 return NULL;
232 } 240 }
233 241
242 my_cache->accounts = eina_hash_string_small_new(NULL);
243
234 my_cache->version = 1; 244 my_cache->version = 1;
235 return my_cache; 245 return my_cache;
236} /* _my_cache_new */ 246} /* _my_cache_new */
237 247
248static Eina_Bool
249_my_cache_account_free_cb(const Eina_Hash *hash, const void *key, void *data, void *fdata)
250{
251 _my_account_free(data);
252 return EINA_TRUE;
253}
254
238static void 255static void
239_my_cache_free(My_Cache * my_cache) 256_my_cache_free(My_Cache * my_cache)
240{ 257{
241 My_Account * acc; 258 My_Account * acc;
242 EINA_LIST_FREE(my_cache->accounts, acc) 259 eina_hash_foreach(my_cache->accounts, _my_cache_account_free_cb, NULL);
243 _my_account_free(acc); 260 eina_hash_free(my_cache->accounts);
244 free(my_cache); 261 free(my_cache);
245} /* _my_cache_free */ 262} /* _my_cache_free */
246 263
247static My_Account * 264static My_Account *
248_my_cache_account_find(My_Cache * my_cache, const char * name) 265_my_cache_account_find(My_Cache * my_cache, const char * name)
249{ 266{
250 My_Account * acc; 267 return eina_hash_find(my_cache->accounts, name);
251 Eina_List * l;
252 EINA_LIST_FOREACH(my_cache->accounts, l, acc)
253 if (strcmp(acc->name, name) == 0)
254 return acc;
255
256 return NULL;
257} /* _my_cache_account_find */ 268} /* _my_cache_account_find */
258 269
259static My_Cache * 270static My_Cache *
@@ -349,6 +360,7 @@ int main(int argc, char * argv[])
349{ 360{
350 My_Cache * my_cache; 361 My_Cache * my_cache;
351 const Eina_List * l_acc; 362 const Eina_List * l_acc;
363 Eina_Iterator *it;
352 My_Account * acc; 364 My_Account * acc;
353 int ret = 0; 365 int ret = 0;
354 366
@@ -391,8 +403,7 @@ int main(int argc, char * argv[])
391 if (!acc) 403 if (!acc)
392 { 404 {
393 acc = _my_account_new(argv[4]); 405 acc = _my_account_new(argv[4]);
394 my_cache->accounts = eina_list_append 406 eina_hash_direct_add(my_cache->accounts, acc->name, acc);
395 (my_cache->accounts, acc);
396 } 407 }
397 else 408 else
398 fprintf(stderr, "ERROR: account '%s' already exists.\n", 409 fprintf(stderr, "ERROR: account '%s' already exists.\n",
@@ -410,8 +421,7 @@ int main(int argc, char * argv[])
410 My_Account * acc = _my_cache_account_find(my_cache, argv[4]); 421 My_Account * acc = _my_cache_account_find(my_cache, argv[4]);
411 if (acc) 422 if (acc)
412 { 423 {
413 My_Post * post = _my_post_new(argv[5]); 424 _my_post_add(acc, argv[5]);
414 acc->posts = eina_list_append(acc->posts, post);
415 } 425 }
416 else 426 else
417 fprintf(stderr, "ERROR: unknown account: '%s'\n", argv[4]); 427 fprintf(stderr, "ERROR: unknown account: '%s'\n", argv[4]);
@@ -447,15 +457,16 @@ int main(int argc, char * argv[])
447 "\tversion.: %#x\n" 457 "\tversion.: %#x\n"
448 "\taccounts: %u\n", 458 "\taccounts: %u\n",
449 my_cache->version, 459 my_cache->version,
450 eina_list_count(my_cache->accounts)); 460 eina_hash_population(my_cache->accounts));
451 EINA_LIST_FOREACH(my_cache->accounts, l_acc, acc) 461 it = eina_hash_iterator_data_new(my_cache->accounts);
462 EINA_ITERATOR_FOREACH(it, acc)
452 { 463 {
453 const My_Post * post; 464 const My_Post * post;
454 465
455 printf("\t > %-#8x '%.20s' stats: m=%u, p=%u\n", 466 printf("\t > %-#8x '%.20s' stats: m=%u, p=%u\n",
456 acc->id, acc->name ? acc->name : "", 467 acc->id, acc->name ? acc->name : "",
457 eina_list_count(acc->messages), 468 eina_list_count(acc->messages),
458 eina_list_count(acc->posts)); 469 acc->posts_count);
459 470
460 if (eina_list_count(acc->messages)) 471 if (eina_list_count(acc->messages))
461 { 472 {
@@ -473,14 +484,15 @@ int main(int argc, char * argv[])
473 } 484 }
474 } 485 }
475 486
476 if (eina_list_count(acc->posts)) 487 if (acc->posts_count)
477 { 488 {
478 const Eina_List * l;
479 const My_Post * post; 489 const My_Post * post;
490 int i;
480 printf("\t |posts:\n"); 491 printf("\t |posts:\n");
481 492
482 EINA_LIST_FOREACH(acc->posts, l, post) 493 for (i = 0; i < acc->posts_count; i++)
483 { 494 {
495 post = &acc->posts[i];
484 if (post->dm_to) 496 if (post->dm_to)
485 printf("\t | @%s: '%.20s'\n", post->dm_to, post->message); 497 printf("\t | @%s: '%.20s'\n", post->dm_to, post->message);
486 else 498 else
@@ -490,6 +502,7 @@ int main(int argc, char * argv[])
490 502
491 printf("\n"); 503 printf("\n");
492 } 504 }
505 eina_iterator_free(it);
493 506
494 if (!_my_cache_save(my_cache, argv[2])) 507 if (!_my_cache_save(my_cache, argv[2]))
495 ret = -3; 508 ret = -3;
@@ -497,6 +510,8 @@ int main(int argc, char * argv[])
497 _my_cache_free(my_cache); 510 _my_cache_free(my_cache);
498 511
499end: 512end:
513 if (_my_cache_file)
514 eet_close(_my_cache_file);
500 _my_cache_descriptor_shutdown(); 515 _my_cache_descriptor_shutdown();
501 eet_shutdown(); 516 eet_shutdown();
502 eina_shutdown(); 517 eina_shutdown();
diff --git a/legacy/eet/src/examples/eet-data-nested.c b/legacy/eet/src/examples/eet-data-nested.c
index b11956fbd0..882fc24264 100644
--- a/legacy/eet/src/examples/eet-data-nested.c
+++ b/legacy/eet/src/examples/eet-data-nested.c
@@ -1,3 +1,6 @@
1/*
2 * build: gcc -o eet_data_nested eet-data-nested.c `pkg-config --cflags --libs eet eina`
3 */
1#include <Eina.h> 4#include <Eina.h>
2#include <Eet.h> 5#include <Eet.h>
3#include <stdio.h> 6#include <stdio.h>
diff --git a/legacy/eet/src/examples/eet-data-simple.c b/legacy/eet/src/examples/eet-data-simple.c
index 14b0a869eb..395a8d66ab 100644
--- a/legacy/eet/src/examples/eet-data-simple.c
+++ b/legacy/eet/src/examples/eet-data-simple.c
@@ -1,3 +1,6 @@
1/*
2 * build: gcc -o eet_data_simple eet-data-simple.c `pkg-config --cflags --libs eet eina`
3 */
1#include <Eina.h> 4#include <Eina.h>
2#include <Eet.h> 5#include <Eet.h>
3#include <stdio.h> 6#include <stdio.h>
diff --git a/legacy/eet/src/examples/eet-file.c b/legacy/eet/src/examples/eet-file.c
new file mode 100644
index 0000000000..691e442e9c
--- /dev/null
+++ b/legacy/eet/src/examples/eet-file.c
@@ -0,0 +1,126 @@
1/*
2 * build: gcc -o eet_file eet-file.c `pkg-config --cflags --libs eet`
3 */
4#include <Eet.h>
5#include <stdio.h>
6#include <string.h>
7
8static int
9create_eet_file(void)
10{
11 Eet_File *ef;
12 char buf[1024], *ptr;
13 int size, len, i;
14 const char *some_strings[] = {
15 "And some more strings",
16 "spread across several",
17 "elements of an array!"
18 };
19 const char some_data[] =
20 "\x1e\xe7\x0f\x42\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x35"
21 "\x00\x00\x00\xa0\x00\x00\x00\xa0\x00\x00\x00\x24\x00\x00\x00\x11"
22 "\x00\x00\x00\x00\x2f\x6d\x69\x73\x74\x65\x72\x69\x6f\x75\x73\x2f"
23 "\x64\x61\x74\x61\x00\x41\x6e\x20\x45\x45\x54\x20\x69\x6e\x73\x69"
24 "\x64\x65\x20\x6f\x66\x20\x61\x6e\x20\x45\x45\x54\x21\x0a\x54\x68"
25 "\x69\x73\x20\x77\x61\x73\x6e\x27\x74\x20\x72\x65\x61\x6c\x6c\x79"
26 "\x20\x75\x73\x65\x66\x75\x6c\x20\x62\x75\x74\x20\x69\x74\x20\x68"
27 "\x65\x6c\x70\x65\x64\x20\x74\x6f\x20\x73\x68\x6f\x77\x20\x68\x6f"
28 "\x77\x0a\x74\x6f\x20\x75\x73\x65\x20\x65\x65\x74\x5f\x6d\x65\x6d"
29 "\x6f\x70\x65\x6e\x5f\x72\x65\x61\x64\x28\x29\x20\x74\x6f\x20\x6f"
30 "\x70\x65\x6e\x20\x61\x6e\x20\x65\x65\x74\x20\x66\x69\x6c\x65\x20"
31 "\x66\x72\x6f\x6d\x0a\x64\x61\x74\x61\x20\x61\x6c\x72\x65\x61\x64"
32 "\x79\x20\x6c\x6f\x61\x64\x65\x64\x20\x69\x6e\x20\x6d\x65\x6d\x6f"
33 "\x72\x79\x2e\x0a\x00";
34
35 ef = eet_open("/tmp/my_file.eet", EET_FILE_MODE_WRITE);
36 if (!ef) return 0;
37
38 strcpy(buf, "Here is a string of data to save!");
39 size = eet_write(ef, "/key/to_store/at", buf, sizeof(buf), 1);
40 if (!size)
41 {
42 fprintf(stderr, "Error writing data!\n");
43 eet_close(ef);
44 return 0;
45 }
46 len = strlen(buf);
47 printf("strlen() = %d, eet_write() = %d\n", len, size);
48
49 ptr = buf;
50 for (i = 0; i < 3; i++)
51 {
52 len = strlen(some_strings[i]) + 1;
53 memcpy(ptr, some_strings[i], len);
54 ptr += len;
55 }
56 eet_write(ef, "/several/strings", buf, sizeof(buf), 1);
57 eet_sync(ef);
58
59 eet_write(ef, "/some/misterious/data", some_data, sizeof(some_data) - 1, 1);
60
61 eet_delete(ef, "/several/strings");
62
63 return (eet_close(ef) == EET_ERROR_NONE);
64}
65
66int
67main(void)
68{
69 Eet_File *ef;
70 char *ret, **list;
71 int size, num, i;
72
73 eet_init();
74
75 if (!create_eet_file())
76 return -1;
77
78 ef = eet_open("/tmp/my_file.eet", EET_FILE_MODE_READ);
79 if (!ef) return -1;
80
81 list = eet_list(ef, "*", &num);
82 if (list)
83 {
84 for (i = 0; i < num; i++)
85 printf("Key stored: %s\n", list[i]);
86 free(list);
87 }
88
89 ret = eet_read(ef, "/key/to_store/at", &size);
90 if (ret)
91 {
92 printf("Data read (%i bytes):\n%s\n", size, ret);
93 free(ret);
94 }
95
96 ret = eet_read(ef, "/several/strings", &size);
97 if (ret)
98 {
99 printf("More data read (%i bytes):\n%s\n", size, ret);
100 free(ret);
101 }
102
103 ret = eet_read(ef, "/some/misterious/data", &size);
104 if (ret)
105 {
106 Eet_File *ef2;
107
108 ef2 = eet_memopen_read(ret, size);
109
110 num = eet_num_entries(ef2);
111 printf("Misterious data has %d entries\n", num);
112
113 printf("Misterious data:\n%s\n",
114 (char *)eet_read_direct(ef2, "/misterious/data", NULL));
115
116 eet_close(ef2);
117
118 free(ret);
119 }
120
121 eet_close(ef);
122
123 eet_shutdown();
124
125 return 0;
126}