File descriptor data example
1//Compile with:
2// gcc -o eet-data-file_descriptor_01 eet-data-file_descriptor_01.c `pkg-config --cflags --libs eet eina`
3
4#include <Eina.h>
5#include <Eet.h>
6#include <stdio.h>
7#include <limits.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <unistd.h>
11
12// complex real-world structures based on elmdentica database
13typedef struct
14{
15 const char *screen_name;
16 const char *name;
17 const char *message;
18 unsigned int id;
19 unsigned int status_id;
20 unsigned int date;
21 unsigned int timeline;
22} My_Message;
23
24typedef struct
25{
26 const char *dm_to;
27 const char *message;
28} My_Post;
29
30typedef struct
31{
32 unsigned int id;
33 const char *name;
34 Eina_List *messages;
35 My_Post *posts;
36 int posts_count;
37} My_Account;
38
39typedef struct
40{
41 unsigned int version; // it is recommended to use versioned configuration!
42 Eina_Hash *accounts;
43} My_Cache;
44
45// string that represents the entry in eet file, you might like to have
46// different profiles or so in the same file, this is possible with
47// different strings
48static const char MY_CACHE_FILE_ENTRY[] = "cache";
49
50// keep the descriptor static global, so it can be
51// shared by different functions (load/save) of this and only this
52// file.
53static Eet_Data_Descriptor *_my_cache_descriptor;
54static Eet_Data_Descriptor *_my_account_descriptor;
55static Eet_Data_Descriptor *_my_message_descriptor;
56static Eet_Data_Descriptor *_my_post_descriptor;
57
58// keep file handle alive, so mmap()ed strings are all alive as well
59static Eet_File *_my_cache_file = NULL;
60static Eet_Dictionary *_my_cache_dict = NULL;
61
62static void
63_my_cache_descriptor_init(void)
64{
66
67 // The FILE variant is good for caches and things that are just
68 // appended, but needs to take care when changing strings and files must
69 // be kept open so mmap()ed strings will be kept alive.
71 _my_cache_descriptor = eet_data_descriptor_file_new(&eddc);
72
74 _my_account_descriptor = eet_data_descriptor_file_new(&eddc);
75
77 _my_message_descriptor = eet_data_descriptor_file_new(&eddc);
78
80 _my_post_descriptor = eet_data_descriptor_file_new(&eddc);
81
82 // Describe the members to be saved:
83 // Use a temporary macro so we don't type a lot, also avoid errors:
84
85#define ADD_BASIC(member, eet_type) \
86 EET_DATA_DESCRIPTOR_ADD_BASIC \
87 (_my_message_descriptor, My_Message, # member, member, eet_type)
88 ADD_BASIC(screen_name, EET_T_STRING);
89 ADD_BASIC(name, EET_T_STRING);
90 ADD_BASIC(message, EET_T_STRING);
91 ADD_BASIC(id, EET_T_UINT);
92 ADD_BASIC(status_id, EET_T_UINT);
93 ADD_BASIC(date, EET_T_UINT);
94 ADD_BASIC(timeline, EET_T_UINT);
95#undef ADD_BASIC
96
97#define ADD_BASIC(member, eet_type) \
98 EET_DATA_DESCRIPTOR_ADD_BASIC \
99 (_my_post_descriptor, My_Post, # member, member, eet_type)
100 ADD_BASIC(dm_to, EET_T_STRING);
101 ADD_BASIC(message, EET_T_STRING);
102#undef ADD_BASIC
103
104#define ADD_BASIC(member, eet_type) \
105 EET_DATA_DESCRIPTOR_ADD_BASIC \
106 (_my_account_descriptor, My_Account, # member, member, eet_type)
107 ADD_BASIC(name, EET_T_STRING);
108 ADD_BASIC(id, EET_T_UINT);
109#undef ADD_BASIC
110
112 (_my_account_descriptor, My_Account, "messages", messages,
113 _my_message_descriptor);
115 (_my_account_descriptor, My_Account, "posts", posts,
116 _my_post_descriptor);
117
118#define ADD_BASIC(member, eet_type) \
119 EET_DATA_DESCRIPTOR_ADD_BASIC \
120 (_my_cache_descriptor, My_Cache, # member, member, eet_type)
121 ADD_BASIC(version, EET_T_UINT);
122#undef ADD_BASIC
123
125 (_my_cache_descriptor, My_Cache, "accounts", accounts,
126 _my_account_descriptor);
127} /* _my_cache_descriptor_init */
128
129static void
130_my_cache_descriptor_shutdown(void)
131{
132 eet_data_descriptor_free(_my_cache_descriptor);
133 eet_data_descriptor_free(_my_account_descriptor);
134 eet_data_descriptor_free(_my_message_descriptor);
135 eet_data_descriptor_free(_my_post_descriptor);
136} /* _my_cache_descriptor_shutdown */
137
138// need to check if the pointer came from mmaped area in eet_dictionary
139// or it was allocated with eina_stringshare_add()
140static void
141_eet_string_free(const char *str)
142{
143 if (!str)
144 return;
145
146 if ((_my_cache_dict) && (eet_dictionary_string_check(_my_cache_dict, str)))
147 return;
148
150} /* _eet_string_free */
151
152static My_Message *
153_my_message_new(const char *message)
154{
155 My_Message *msg = calloc(1, sizeof(My_Message));
156 if (!msg)
157 {
158 fprintf(stderr, "ERROR: could not calloc My_Message\n");
159 return NULL;
160 }
161
162 msg->message = eina_stringshare_add(message);
163 return msg;
164} /* _my_message_new */
165
166static void
167_my_message_free(My_Message *msg)
168{
169 _eet_string_free(msg->screen_name);
170 _eet_string_free(msg->name);
171 _eet_string_free(msg->message);
172 free(msg);
173} /* _my_message_free */
174
175static Eina_Bool
176_my_post_add(My_Account *acc,
177 const char *message)
178{
179 int new_count = acc->posts_count + 1;
180 My_Post *post = realloc(acc->posts, new_count * sizeof(My_Post));
181 if (!post)
182 {
183 fprintf(stderr, "ERROR: could add My_Post\n");
184 return EINA_FALSE;
185 }
186
187 post[acc->posts_count].message = eina_stringshare_add(message);
188 post[acc->posts_count].dm_to = NULL;
189 acc->posts_count = new_count;
190 acc->posts = post;
191 return EINA_TRUE;
192} /* _my_post_new */
193
194static void
195_my_post_free(My_Post *post)
196{
197 _eet_string_free(post->dm_to);
198 _eet_string_free(post->message);
199} /* _my_post_free */
200
201static My_Account *
202_my_account_new(const char *name)
203{
204 My_Account *acc = calloc(1, sizeof(My_Account));
205 if (!acc)
206 {
207 fprintf(stderr, "ERROR: could not calloc My_Account\n");
208 return NULL;
209 }
210
211 acc->name = eina_stringshare_add(name);
212 return acc;
213} /* _my_account_new */
214
215static void
216_my_account_free(My_Account *acc)
217{
218 My_Message *m;
219 int i;
220
221 _eet_string_free(acc->name);
222
223 EINA_LIST_FREE(acc->messages, m)
224 _my_message_free(m);
225
226 for (i = 0; i < acc->posts_count; i++)
227 _my_post_free(&acc->posts[i]);
228 free(acc->posts);
229
230 free(acc);
231} /* _my_account_free */
232
233static My_Cache *
234_my_cache_new(void)
235{
236 My_Cache *my_cache = calloc(1, sizeof(My_Cache));
237 if (!my_cache)
238 {
239 fprintf(stderr, "ERROR: could not calloc My_Cache\n");
240 return NULL;
241 }
242
243 my_cache->accounts = eina_hash_string_small_new(NULL);
244
245 my_cache->version = 1;
246 return my_cache;
247} /* _my_cache_new */
248
249static Eina_Bool
250_my_cache_account_free_cb(const Eina_Hash *hash EINA_UNUSED,
251 const void *key EINA_UNUSED,
252 void *data,
253 void *fdata EINA_UNUSED)
254{
255 _my_account_free(data);
256 return EINA_TRUE;
257}
258
259static void
260_my_cache_free(My_Cache *my_cache)
261{
262 eina_hash_foreach(my_cache->accounts, _my_cache_account_free_cb, NULL);
263 eina_hash_free(my_cache->accounts);
264 free(my_cache);
265} /* _my_cache_free */
266
267static My_Account *
268_my_cache_account_find(My_Cache *my_cache,
269 const char *name)
270{
271 return eina_hash_find(my_cache->accounts, name);
272} /* _my_cache_account_find */
273
274static My_Cache *
275_my_cache_load(const char *filename)
276{
277 My_Cache *my_cache;
278 Eet_File *ef = eet_open(filename, EET_FILE_MODE_READ);
279 if (!ef)
280 {
281 fprintf(stderr, "ERROR: could not open '%s' for read\n", filename);
282 return NULL;
283 }
284
285 my_cache = eet_data_read(ef, _my_cache_descriptor, MY_CACHE_FILE_ENTRY);
286 if (!my_cache)
287 {
288 eet_close(ef);
289 return NULL;
290 }
291
292 if (my_cache->version < 1)
293 {
294 fprintf(stderr,
295 "WARNING: version %#x was too old, upgrading it to %#x\n",
296 my_cache->version, 1);
297
298 my_cache->version = 1;
299 }
300
301 if (_my_cache_file)
302 eet_close(_my_cache_file);
303
304 _my_cache_file = ef;
305 _my_cache_dict = eet_dictionary_get(ef);
306
307 return my_cache;
308} /* _my_cache_load */
309
310static Eina_Bool
311_my_cache_save(const My_Cache *my_cache,
312 const char *filename)
313{
314 char tmp[PATH_MAX];
315 Eet_File *ef;
316 Eina_Bool ret;
317 unsigned int i, len;
318 struct stat st;
319
320 len = eina_strlcpy(tmp, filename, sizeof(tmp));
321 if (len + 12 >= (int)sizeof(tmp))
322 {
323 fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
324 return EINA_FALSE;
325 }
326
327 i = 0;
328 do
329 {
330 snprintf(tmp + len, 12, ".%u", i);
331 i++;
332 }
333 while (stat(tmp, &st) == 0);
334
335 ef = eet_open(tmp, EET_FILE_MODE_WRITE);
336 if (!ef)
337 {
338 fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
339 return EINA_FALSE;
340 }
341
342 ret = eet_data_write
343 (ef, _my_cache_descriptor, MY_CACHE_FILE_ENTRY, my_cache, EINA_TRUE);
344
345 // VERY IMPORTANT NOTE:
346 // after eet_close(), all strings mmaped from file will be GONE, invalid!
347 // you'll need to free the old cache and open the new one.
348 // For cache this is okay, as you should be saving not so often or just
349 // at end.
350 //
351 // This is a trade off, you save memory by using mmap()ed strings, but
352 // you have to care about this.
353 eet_close(ef);
354
355 if (ret)
356 {
357 unlink(filename);
358 rename(tmp, filename);
359 }
360
361 return ret;
362} /* _my_cache_save */
363
364int
365main(int argc,
366 char *argv[])
367{
368 My_Cache *my_cache;
369 Eina_Iterator *it;
370 My_Account *acc;
371 int ret = 0;
372
373 if (argc < 3)
374 {
375 fprintf(stderr,
376 "Usage:\n\t%s <input> <output> [action] [action-params]\n\n"
377 "Where actions and their parameters:\n"
378 "\tacc <name>\n"
379 "\tpost <account-name> <message>\n"
380 "\tmessage <account-name> <message>\n"
381 "\n",
382 argv[0]);
383 return -1;
384 }
385
386 eina_init();
387 eet_init();
388 _my_cache_descriptor_init();
389
390 my_cache = _my_cache_load(argv[1]);
391 if (!my_cache)
392 {
393 printf("creating new cache.\n");
394 my_cache = _my_cache_new();
395 if (!my_cache)
396 {
397 ret = -2;
398 goto end;
399 }
400 }
401
402 if (argc > 3)
403 {
404 if (strcmp(argv[3], "acc") == 0)
405 {
406 if (argc == 5)
407 {
408 My_Account *acc_ = _my_cache_account_find(my_cache, argv[4]);
409 if (!acc_)
410 {
411 acc_ = _my_account_new(argv[4]);
412 eina_hash_direct_add(my_cache->accounts, acc_->name, acc_);
413 }
414 else
415 fprintf(stderr, "ERROR: account '%s' already exists.\n",
416 argv[4]);
417 }
418 else
419 fprintf(stderr,
420 "ERROR: wrong number of parameters (%d).\n",
421 argc);
422 }
423 else if (strcmp(argv[3], "post") == 0)
424 {
425 if (argc == 6)
426 {
427 My_Account *acc_ = _my_cache_account_find(my_cache, argv[4]);
428 if (acc_)
429 {
430 _my_post_add(acc_, argv[5]);
431 }
432 else
433 fprintf(stderr, "ERROR: unknown account: '%s'\n", argv[4]);
434 }
435 else
436 fprintf(stderr,
437 "ERROR: wrong number of parameters (%d).\n",
438 argc);
439 }
440 else if (strcmp(argv[3], "message") == 0)
441 {
442 if (argc == 6)
443 {
444 My_Account *acc_ = _my_cache_account_find(my_cache, argv[4]);
445 if (acc_)
446 {
447 My_Message *msg = _my_message_new(argv[5]);
448 acc_->messages = eina_list_append(acc_->messages, msg);
449 }
450 else
451 fprintf(stderr, "ERROR: unknown account: '%s'\n", argv[4]);
452 }
453 else
454 fprintf(stderr,
455 "ERROR: wrong number of parameters (%d).\n",
456 argc);
457 }
458 else
459 fprintf(stderr, "ERROR: unknown action '%s'\n", argv[2]);
460 }
461
462 printf("My_Cache:\n"
463 "\tversion.: %#x\n"
464 "\taccounts: %u\n",
465 my_cache->version,
466 eina_hash_population(my_cache->accounts));
467 it = eina_hash_iterator_data_new(my_cache->accounts);
468 EINA_ITERATOR_FOREACH(it, acc)
469 {
470 printf("\t > %-#8x '%.20s' stats: m=%u, p=%u\n",
471 acc->id, acc->name ? acc->name : "",
472 eina_list_count(acc->messages),
473 acc->posts_count);
474
475 if (eina_list_count(acc->messages))
476 {
477 const Eina_List *l;
478 const My_Message *msg;
479 printf("\t |messages:\n");
480
481 EINA_LIST_FOREACH(acc->messages, l, msg)
482 {
483 printf("\t | %-8x '%s' [%s]: '%.20s'\n",
484 msg->id,
485 msg->name ? msg->name : "",
486 msg->screen_name ? msg->screen_name : "",
487 msg->message ? msg->message : "");
488 }
489 }
490
491 if (acc->posts_count)
492 {
493 const My_Post *post;
494 int i;
495 printf("\t |posts:\n");
496
497 for (i = 0; i < acc->posts_count; i++)
498 {
499 post = &acc->posts[i];
500 if (post->dm_to)
501 printf("\t | @%s: '%.20s'\n", post->dm_to, post->message);
502 else
503 printf("\t | '%.20s'\n", post->message);
504 }
505 }
506
507 printf("\n");
508 }
510
511 if (!_my_cache_save(my_cache, argv[2]))
512 ret = -3;
513
514 _my_cache_free(my_cache);
515
516end:
517 if (_my_cache_file)
518 eet_close(_my_cache_file);
519 _my_cache_descriptor_shutdown();
520 eet_shutdown();
522
523 return ret;
524} /* main */
525
The file that provides the eet functions.
Eina Utility library.
#define EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(clas, type)
This macro is an helper that set all the parameter of an Eet_Data_Descriptor_Class correctly when you...
Definition: Eet.h:3075
#define EET_T_STRING
Data type: char *.
Definition: Eet.h:2589
EAPI void eet_data_descriptor_free(Eet_Data_Descriptor *edd)
This function frees a data descriptor when it is not needed anymore.
Definition: eet_data.c:2104
struct _Eet_Data_Descriptor Eet_Data_Descriptor
Opaque handle that have information on a type members.
Definition: Eet.h:2631
EAPI void * eet_data_read(Eet_File *ef, Eet_Data_Descriptor *edd, const char *name)
Reads a data structure from an eet file and decodes it.
Definition: eet_data.c:2379
#define EET_DATA_DESCRIPTOR_ADD_VAR_ARRAY(edd, struct_type, name, member, subtype)
Adds a variable size array type to a data descriptor.
Definition: Eet.h:3752
EAPI Eet_Data_Descriptor * eet_data_descriptor_file_new(const Eet_Data_Descriptor_Class *eddc)
This function creates a new data descriptor and returns a handle to the new data descriptor.
Definition: eet_data.c:2090
#define EET_DATA_DESCRIPTOR_ADD_LIST(edd, struct_type, name, member, subtype)
Adds a linked list type to a data descriptor.
Definition: Eet.h:3511
EAPI int eet_data_write(Eet_File *ef, Eet_Data_Descriptor *edd, const char *name, const void *data, int compress)
Writes a data structure from memory and store in an eet file.
Definition: eet_data.c:2416
#define EET_T_UINT
Data type: unsigned int.
Definition: Eet.h:2587
#define EET_DATA_DESCRIPTOR_ADD_HASH(edd, struct_type, name, member, subtype)
Adds a hash type to a data descriptor.
Definition: Eet.h:3584
EAPI Eet_Error eet_close(Eet_File *ef)
Closes an eet file handle and flush pending writes.
Definition: eet_lib.c:1899
EAPI int eet_dictionary_string_check(Eet_Dictionary *ed, const char *string)
Checks if a given string comes from a given dictionary.
Definition: eet_dictionary.c:598
EAPI Eet_File * eet_open(const char *file, Eet_File_Mode mode)
Opens an eet file on disk, and returns a handle to it.
Definition: eet_lib.c:1499
struct _Eet_File Eet_File
Opaque handle that defines an Eet file (or memory).
Definition: Eet.h:527
EAPI Eet_Dictionary * eet_dictionary_get(Eet_File *ef)
Returns a handle to the shared string dictionary of the Eet file.
Definition: eet_lib.c:2564
struct _Eet_Dictionary Eet_Dictionary
Opaque handle that defines a file-backed (mmaped) dictionary of strings.
Definition: Eet.h:533
@ EET_FILE_MODE_READ
File is read-only.
Definition: Eet.h:479
@ EET_FILE_MODE_WRITE
File is write-only.
Definition: Eet.h:480
EAPI int eet_init(void)
Initializes the EET library.
Definition: eet_lib.c:540
EAPI int eet_shutdown(void)
Shuts down the EET library.
Definition: eet_lib.c:594
EINA_API Eina_Iterator * eina_hash_iterator_data_new(const Eina_Hash *hash)
Returns a new iterator associated with a hash.
Definition: eina_hash.c:1246
EINA_API int eina_hash_population(const Eina_Hash *hash)
Returns the number of entries in the given hash table.
Definition: eina_hash.c:858
EINA_API void * eina_hash_find(const Eina_Hash *hash, const void *key)
Retrieves a specific entry in the given hash table.
Definition: eina_hash.c:1069
EINA_API void eina_hash_free(Eina_Hash *hash)
Frees the given hash table's resources.
Definition: eina_hash.c:868
EINA_API Eina_Bool eina_hash_direct_add(Eina_Hash *hash, const void *key, const void *data)
Adds an entry to the given hash table without duplicating the string.
Definition: eina_hash.c:948
EINA_API void eina_hash_foreach(const Eina_Hash *hash, Eina_Hash_Foreach func, const void *fdata)
Calls a function on every member stored in the hash table.
Definition: eina_hash.c:1223
EINA_API Eina_Hash * eina_hash_string_small_new(Eina_Free_Cb data_free_cb)
Creates a new hash table for use with strings with small bucket size.
Definition: eina_hash.c:800
struct _Eina_Hash Eina_Hash
Type for a generic hash table.
Definition: eina_hash.h:285
EINA_API void eina_iterator_free(Eina_Iterator *iterator)
Frees an iterator.
Definition: eina_iterator.c:98
#define EINA_ITERATOR_FOREACH(itr, data)
Definition for the macro to iterate over all elements easily.
Definition: eina_iterator.h:448
static unsigned int eina_list_count(const Eina_List *list)
Gets the count of the number of items in a list.
EINA_API Eina_List * eina_list_append(Eina_List *list, const void *data)
Appends the given data to the given linked list.
Definition: eina_list.c:584
#define EINA_LIST_FOREACH(list, l, _data)
Definition for the macro to iterate over a list.
Definition: eina_list.h:1415
#define EINA_LIST_FREE(list, data)
Definition for the macro to remove each list node while having access to each node's data.
Definition: eina_list.h:1629
EINA_API int eina_shutdown(void)
Shuts down the Eina library.
Definition: eina_main.c:379
EINA_API int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:291
EINA_API size_t eina_strlcpy(char *dst, const char *src, size_t siz)
Copies a c-string to another.
Definition: eina_str.c:317
EINA_API Eina_Stringshare * eina_stringshare_add(const char *str)
Retrieves an instance of a string for use in a program.
Definition: eina_stringshare.c:606
EINA_API void eina_stringshare_del(Eina_Stringshare *str)
Notes that the given string has lost an instance.
Definition: eina_stringshare.c:533
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition: eina_types.h:533
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition: eina_types.h:339
Instructs Eet about memory management for different needs under serialization and parse process.
Definition: Eet.h:2828
structure of an iterator
Definition: eina_iterator.h:159
Type for a generic double linked list.
Definition: eina_list.h:318