2 * build: gcc -o eet_data_file_descriptor eet-data-file_descriptor.c `pkg-config --cflags --libs eet eina`
12 // complex real-world structures based on elmdentica database
15 const char *screen_name;
19 unsigned int status_id;
21 unsigned int timeline;
41 unsigned int version; // it is recommended to use versioned configuration!
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
48 static const char MY_CACHE_FILE_ENTRY[] = "cache";
50 // keep the descriptor static global, so it can be
51 // shared by different functions (load/save) of this and only this
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;
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;
63 _my_cache_descriptor_init(void)
65 Eet_Data_Descriptor_Class eddc;
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);
73 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Account);
74 _my_account_descriptor = eet_data_descriptor_file_new(&eddc);
76 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Message);
77 _my_message_descriptor = eet_data_descriptor_file_new(&eddc);
79 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Post);
80 _my_post_descriptor = eet_data_descriptor_file_new(&eddc);
82 // Describe the members to be saved:
83 // Use a temporary macro so we don't type a lot, also avoid errors:
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);
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);
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);
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);
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);
124 EET_DATA_DESCRIPTOR_ADD_HASH
125 (_my_cache_descriptor, My_Cache, "accounts", accounts,
126 _my_account_descriptor);
127 } /* _my_cache_descriptor_init */
130 _my_cache_descriptor_shutdown(void)
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 */
138 // need to check if the pointer came from mmaped area in eet_dictionary
139 // or it was allocated with eina_stringshare_add()
141 _eet_string_free(const char *str)
146 if ((_my_cache_dict) && (eet_dictionary_string_check(_my_cache_dict, str)))
149 eina_stringshare_del(str);
150 } /* _eet_string_free */
153 _my_message_new(const char *message)
155 My_Message *msg = calloc(1, sizeof(My_Message));
158 fprintf(stderr, "ERROR: could not calloc My_Message\n");
162 msg->message = eina_stringshare_add(message);
164 } /* _my_message_new */
167 _my_message_free(My_Message *msg)
169 _eet_string_free(msg->screen_name);
170 _eet_string_free(msg->name);
171 _eet_string_free(msg->message);
173 } /* _my_message_free */
176 _my_post_add(My_Account *acc,
179 int new_count = acc->posts_count + 1;
180 My_Post *post = realloc(acc->posts, new_count * sizeof(My_Post));
183 fprintf(stderr, "ERROR: could add My_Post\n");
187 post[acc->posts_count].message = eina_stringshare_add(message);
188 post[acc->posts_count].dm_to = NULL;
189 acc->posts_count = new_count;
195 _my_post_free(My_Post *post)
197 _eet_string_free(post->dm_to);
198 _eet_string_free(post->message);
199 } /* _my_post_free */
202 _my_account_new(const char *name)
204 My_Account *acc = calloc(1, sizeof(My_Account));
207 fprintf(stderr, "ERROR: could not calloc My_Account\n");
211 acc->name = eina_stringshare_add(name);
213 } /* _my_account_new */
216 _my_account_free(My_Account *acc)
221 _eet_string_free(acc->name);
223 EINA_LIST_FREE(acc->messages, m)
226 for (i = 0; i < acc->posts_count; i++)
227 _my_post_free(&acc->posts[i]);
231 } /* _my_account_free */
236 My_Cache *my_cache = calloc(1, sizeof(My_Cache));
239 fprintf(stderr, "ERROR: could not calloc My_Cache\n");
243 my_cache->accounts = eina_hash_string_small_new(NULL);
245 my_cache->version = 1;
247 } /* _my_cache_new */
250 _my_cache_account_free_cb(const Eina_Hash *hash,
255 _my_account_free(data);
260 _my_cache_free(My_Cache *my_cache)
263 eina_hash_foreach(my_cache->accounts, _my_cache_account_free_cb, NULL);
264 eina_hash_free(my_cache->accounts);
266 } /* _my_cache_free */
269 _my_cache_account_find(My_Cache *my_cache,
272 return eina_hash_find(my_cache->accounts, name);
273 } /* _my_cache_account_find */
276 _my_cache_load(const char *filename)
279 Eet_File *ef = eet_open(filename, EET_FILE_MODE_READ);
282 fprintf(stderr, "ERROR: could not open '%s' for read\n", filename);
286 my_cache = eet_data_read(ef, _my_cache_descriptor, MY_CACHE_FILE_ENTRY);
293 if (my_cache->version < 1)
296 "WARNING: version %#x was too old, upgrading it to %#x\n",
297 my_cache->version, 1);
299 my_cache->version = 1;
303 eet_close(_my_cache_file);
306 _my_cache_dict = eet_dictionary_get(ef);
309 } /* _my_cache_load */
312 _my_cache_save(const My_Cache *my_cache,
313 const char *filename)
321 len = eina_strlcpy(tmp, filename, sizeof(tmp));
322 if (len + 12 >= (int)sizeof(tmp))
324 fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
331 snprintf(tmp + len, 12, ".%u", i);
334 while (stat(tmp, &st) == 0);
336 ef = eet_open(tmp, EET_FILE_MODE_WRITE);
339 fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
344 (ef, _my_cache_descriptor, MY_CACHE_FILE_ENTRY, my_cache, EINA_TRUE);
346 // VERY IMPORTANT NOTE:
347 // after eet_close(), all strings mmaped from file will be GONE, invalid!
348 // you'll need to free the old cache and open the new one.
349 // For cache this is okay, as you should be saving not so often or just
352 // This is a trade off, you save memory by using mmap()ed strings, but
353 // you have to care about this.
359 rename(tmp, filename);
363 } /* _my_cache_save */
370 const Eina_List *l_acc;
378 "Usage:\n\t%s <input> <output> [action] [action-params]\n\n"
379 "Where actions and their parameters:\n"
381 "\tpost <account-name> <message>\n"
382 "\tmessage <account-name> <message>\n"
390 _my_cache_descriptor_init();
392 my_cache = _my_cache_load(argv[1]);
395 printf("creating new cache.\n");
396 my_cache = _my_cache_new();
406 if (strcmp(argv[3], "acc") == 0)
410 My_Account *acc = _my_cache_account_find(my_cache, argv[4]);
413 acc = _my_account_new(argv[4]);
414 eina_hash_direct_add(my_cache->accounts, acc->name, acc);
417 fprintf(stderr, "ERROR: account '%s' already exists.\n",
422 "ERROR: wrong number of parameters (%d).\n",
425 else if (strcmp(argv[3], "post") == 0)
429 My_Account *acc = _my_cache_account_find(my_cache, argv[4]);
432 _my_post_add(acc, argv[5]);
435 fprintf(stderr, "ERROR: unknown account: '%s'\n", argv[4]);
439 "ERROR: wrong number of parameters (%d).\n",
442 else if (strcmp(argv[3], "message") == 0)
446 My_Account *acc = _my_cache_account_find(my_cache, argv[4]);
449 My_Message *msg = _my_message_new(argv[5]);
450 acc->messages = eina_list_append(acc->messages, msg);
453 fprintf(stderr, "ERROR: unknown account: '%s'\n", argv[4]);
457 "ERROR: wrong number of parameters (%d).\n",
461 fprintf(stderr, "ERROR: unknown action '%s'\n", argv[2]);
468 eina_hash_population(my_cache->accounts));
469 it = eina_hash_iterator_data_new(my_cache->accounts);
470 EINA_ITERATOR_FOREACH(it, acc)
474 printf("\t > %-#8x '%.20s' stats: m=%u, p=%u\n",
475 acc->id, acc->name ? acc->name : "",
476 eina_list_count(acc->messages),
479 if (eina_list_count(acc->messages))
482 const My_Message *msg;
483 printf("\t |messages:\n");
485 EINA_LIST_FOREACH(acc->messages, l, msg)
487 printf("\t | %-8x '%s' [%s]: '%.20s'\n",
489 msg->name ? msg->name : "",
490 msg->screen_name ? msg->screen_name : "",
491 msg->message ? msg->message : "");
495 if (acc->posts_count)
499 printf("\t |posts:\n");
501 for (i = 0; i < acc->posts_count; i++)
503 post = &acc->posts[i];
505 printf("\t | @%s: '%.20s'\n", post->dm_to, post->message);
507 printf("\t | '%.20s'\n", post->message);
513 eina_iterator_free(it);
515 if (!_my_cache_save(my_cache, argv[2]))
518 _my_cache_free(my_cache);
522 eet_close(_my_cache_file);
523 _my_cache_descriptor_shutdown();