- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files
- Examples

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
13 typedef 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
24 typedef struct
25 {
26 const char *dm_to;
27 const char *message;
28 } My_Post;
29
30 typedef 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
39 typedef 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
48 static 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.
53 static Eet_Data_Descriptor *_my_cache_descriptor;
54 static Eet_Data_Descriptor *_my_account_descriptor;
55 static Eet_Data_Descriptor *_my_message_descriptor;
56 static Eet_Data_Descriptor *_my_post_descriptor;
57
58 // keep file handle alive, so mmap()ed strings are all alive as well
59 static Eet_File *_my_cache_file = NULL;
60 static Eet_Dictionary *_my_cache_dict = NULL;
61
62 static void
63 _my_cache_descriptor_init(void)
64 {
65 Eet_Data_Descriptor_Class eddc;
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.
70 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Cache);
71 _my_cache_descriptor = eet_data_descriptor_file_new(&eddc);
72
73 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Account);
74 _my_account_descriptor = eet_data_descriptor_file_new(&eddc);
75
76 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Message);
77 _my_message_descriptor = eet_data_descriptor_file_new(&eddc);
78
79 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Post);
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
111 EET_DATA_DESCRIPTOR_ADD_LIST
112 (_my_account_descriptor, My_Account, "messages", messages,
113 _my_message_descriptor);
114 EET_DATA_DESCRIPTOR_ADD_VAR_ARRAY
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
124 EET_DATA_DESCRIPTOR_ADD_HASH
125 (_my_cache_descriptor, My_Cache, "accounts", accounts,
126 _my_account_descriptor);
127 } /* _my_cache_descriptor_init */
128
129 static 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()
140 static 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
149 eina_stringshare_del(str);
150 } /* _eet_string_free */
151
152 static 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
166 static 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
175 static 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
194 static 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
201 static 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
215 static 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
233 static 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
249 static 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
259 static 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
267 static 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
274 static 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
310 static 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
364 int
365 main(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 }
509 eina_iterator_free(it);
510
511 if (!_my_cache_save(my_cache, argv[2]))
512 ret = -3;
513
514 _my_cache_free(my_cache);
515
516 end:
517 if (_my_cache_file)
518 eet_close(_my_cache_file);
519 _my_cache_descriptor_shutdown();
520 eet_shutdown();
521 eina_shutdown();
522
523 return ret;
524 } /* main */
525
eina_stringshare_del
EAPI void eina_stringshare_del(Eina_Stringshare *str)
Note that the given string has lost an instance.
Definition: eina_stringshare.c:537
eina_stringshare_add
EAPI Eina_Stringshare * eina_stringshare_add(const char *str)
Retrieve an instance of a string for use in a program.
Definition: eina_stringshare.c:610
EET_DATA_DESCRIPTOR_ADD_VAR_ARRAY
#define EET_DATA_DESCRIPTOR_ADD_VAR_ARRAY(edd, struct_type, name, member, subtype)
Add a variable size array type to a data descriptor.
Definition: Eet.h:3671
eet_open
EAPI Eet_File * eet_open(const char *file, Eet_File_Mode mode)
Open an eet file on disk, and returns a handle to it.
Definition: eet_lib.c:1500
eet_data_write
EAPI int eet_data_write(Eet_File *ef, Eet_Data_Descriptor *edd, const char *name, const void *data, int compress)
Write a data structure from memory and store in an eet file.
Definition: eet_data.c:2395
EINA_FALSE
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition: eina_types.h:316
Eet_File
struct _Eet_File Eet_File
Opaque handle that defines an Eet file (or memory).
Definition: Eet.h:518
eina_hash_direct_add
Eina_Bool eina_hash_direct_add(Eina_Hash *hash, const void *key, const void *data)
Add an entry to the given hash table without duplicating the string key.
Definition: eina_hash.c:921
eina_hash_string_small_new
Eina_Hash * eina_hash_string_small_new(Eina_Free_Cb data_free_cb)
Create a new hash table for use with strings with small bucket size.
Definition: eina_hash.c:772
eina_iterator_free
void eina_iterator_free(Eina_Iterator *iterator)
Free an iterator.
Definition: eina_iterator.c:96
EET_FILE_MODE_READ
File is read-only.
Definition: Eet.h:474
eet_shutdown
EAPI int eet_shutdown(void)
Shut down the EET library.
Definition: eet_lib.c:619
eina_init
EAPI int eina_init(void)
Initialize the Eina library.
Definition: eina_main.c:249
eina_list_count
static unsigned int eina_list_count(const Eina_List *list)
Get the count of the number of items in a list.
EET_DATA_DESCRIPTOR_ADD_HASH
#define EET_DATA_DESCRIPTOR_ADD_HASH(edd, struct_type, name, member, subtype)
Add a hash type to a data descriptor.
Definition: Eet.h:3526
eina_hash_foreach
void eina_hash_foreach(const Eina_Hash *hash, Eina_Hash_Foreach func, const void *fdata)
Call a function on every member stored in the hash table.
Definition: eina_hash.c:1197
eet_data_descriptor_free
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
EET_T_STRING
#define EET_T_STRING
Data type: char *.
Definition: Eet.h:2557
EINA_ITERATOR_FOREACH
#define EINA_ITERATOR_FOREACH(itr,data)
Macro to iterate over all elements easily.
Definition: eina_iterator.h:332
EET_T_UINT
#define EET_T_UINT
Data type: unsigned int.
Definition: Eet.h:2555
eina_hash_free
void eina_hash_free(Eina_Hash *hash)
Free the given hash table resources.
Definition: eina_hash.c:840
Eet_Dictionary
struct _Eet_Dictionary Eet_Dictionary
Opaque handle that defines a file-backed (mmaped) dictionary of strings.
Definition: Eet.h:524
eet_dictionary_string_check
EAPI int eet_dictionary_string_check(Eet_Dictionary *ed, const char *string)
Check if a given string comes from a given dictionary.
Definition: eet_dictionary.c:489
EINA_TRUE
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:322
eina_shutdown
EAPI int eina_shutdown(void)
Shut down the Eina library.
Definition: eina_main.c:319
eina_list_append
Eina_List * eina_list_append(Eina_List *list, const void *data)
Append the given data to the given linked list.
Definition: eina_list.c:547
Eet.h
The file that provides the eet functions.
Eina_Hash
struct _Eina_Hash Eina_Hash
Type for a generic hash table.
Definition: eina_hash.h:288
EET_FILE_MODE_WRITE
File is write-only.
Definition: Eet.h:475
eina_strlcpy
EAPI size_t eina_strlcpy(char *dst, const char *src, size_t siz) EINA_ARG_NONNULL(1
Copy a c-string to another.
eet_data_read
EAPI void * eet_data_read(Eet_File *ef, Eet_Data_Descriptor *edd, const char *name)
Read a data structure from an eet file and decodes it.
Definition: eet_data.c:2361
Eina.h
Eina Utility library.
_Eet_Data_Descriptor_Class
Instructs Eet about memory management for different needs under serialization and parse process...
Definition: Eet.h:2795
Eina_Bool
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:310
EET_DATA_DESCRIPTOR_ADD_LIST
#define EET_DATA_DESCRIPTOR_ADD_LIST(edd, struct_type, name, member, subtype)
Add a linked list type to a data descriptor.
Definition: Eet.h:3476
eet_close
EAPI Eet_Error eet_close(Eet_File *ef)
Close an eet file handle and flush pending writes.
Definition: eet_lib.c:1750
Eet_Data_Descriptor
struct _Eet_Data_Descriptor Eet_Data_Descriptor
Opaque handle that have information on a type members.
Definition: Eet.h:2599
EINA_LIST_FREE
#define EINA_LIST_FREE(list, data)
Macro to remove each list node while having access to each node's data.
Definition: eina_list.h:1609
eina_hash_find
void * eina_hash_find(const Eina_Hash *hash, const void *key)
Retrieve a specific entry in the given hash table.
Definition: eina_hash.c:1043
eina_hash_iterator_data_new
Eina_Iterator * eina_hash_iterator_data_new(const Eina_Hash *hash)
Returned a new iterator associated to hash data.
Definition: eina_hash.c:1220
eet_data_descriptor_file_new
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
_Eina_Iterator
structure of an iterator
Definition: eina_iterator.h:158
eina_hash_population
int eina_hash_population(const Eina_Hash *hash)
Returns the number of entries in the given hash table.
Definition: eina_hash.c:830
_Eina_List
Type for a generic double linked list.
Definition: eina_list.h:320
rename
#define rename(src, dst)
Wrapper around evil_rename().
Definition: evil_stdio.h:52
EINA_LIST_FOREACH
#define EINA_LIST_FOREACH(list, l, data)
Macro to iterate over a list.
Definition: eina_list.h:1397
EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET
#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:3042
eet_dictionary_get
EAPI Eet_Dictionary * eet_dictionary_get(Eet_File *ef)
Return a handle to the shared string dictionary of the Eet file.
Definition: eet_lib.c:2647
eet_init
EAPI int eet_init(void)
Initialize the EET library.
Definition: eet_lib.c:536