Example_Eet_Data_File_Descriptor_01(3)

NAME
Example_Eet_Data_File_Descriptor_01 - 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);
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);
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);
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);
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, 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,
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, 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, 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:t%s <input> <output> [action] [action-params]n"
377                 "Where actions and their parameters:
378                 "	acc <name>
379                 "	post <account-name> <message>
380                 "	message <account-name> <message>
381                 ",
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.);
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.,
416                             argv[4]);
417                }
418              else
419                fprintf(stderr,
420                        "ERROR: wrong number of parameters (%d).,
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', argv[4]);
434                }
435              else
436                fprintf(stderr,
437                        "ERROR: wrong number of parameters (%d).,
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', argv[4]);
452                }
453              else
454                fprintf(stderr,
455                        "ERROR: wrong number of parameters (%d).,
456                        argc);
457           }
458         else
459           fprintf(stderr, "ERROR: unknown action '%s', argv[2]);
460      }
461
462    printf("My_Cache:
463           "	version.: %#x
464           "	accounts: %u,
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("	  > %-#8x '%.20s' stats: m=%u, p=%u,
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("	  |messages:);
480
481              EINA_LIST_FOREACH(acc->messages, l, msg)
482                {
483                   printf("	  |   %-8x '%s' [%s]: '%.20s',
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("	  |posts:);
496
497              for (i = 0; i < acc->posts_count; i++)
498                {
499                   post = &acc->posts[i];
500                   if (post->dm_to)
501                     printf("	  |  @%s: '%.20s', post->dm_to, post->message);
502                   else
503                     printf("	  |  '%.20s', post->message);
504                }
505           }
506
507         printf(");
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