9 // complex real-world structures based on elmdentica database
12 const char * screen_name;
16 unsigned int status_id;
18 unsigned int timeline;
37 unsigned int version; // it is recommended to use versioned configuration!
41 // string that represents the entry in eet file, you might like to have
42 // different profiles or so in the same file, this is possible with
44 static const char MY_CACHE_FILE_ENTRY[] = "cache";
46 // keep the descriptor static global, so it can be
47 // shared by different functions (load/save) of this and only this
49 static Eet_Data_Descriptor * _my_cache_descriptor;
50 static Eet_Data_Descriptor * _my_account_descriptor;
51 static Eet_Data_Descriptor * _my_message_descriptor;
52 static Eet_Data_Descriptor * _my_post_descriptor;
54 // keep file handle alive, so mmap()ed strings are all alive as well
55 static Eet_File * _my_cache_file = NULL;
56 static Eet_Dictionary * _my_cache_dict = NULL;
59 _my_cache_descriptor_init(void)
61 Eet_Data_Descriptor_Class eddc;
63 // The FILE variant is good for caches and things that are just
64 // appended, but needs to take care when changing strings and files must
65 // be kept open so mmap()ed strings will be kept alive.
66 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Cache);
67 _my_cache_descriptor = eet_data_descriptor_file_new(&eddc);
69 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Account);
70 _my_account_descriptor = eet_data_descriptor_file_new(&eddc);
72 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Message);
73 _my_message_descriptor = eet_data_descriptor_file_new(&eddc);
75 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Post);
76 _my_post_descriptor = eet_data_descriptor_file_new(&eddc);
78 // Describe the members to be saved:
79 // Use a temporary macro so we don't type a lot, also avoid errors:
81 #define ADD_BASIC(member, eet_type)\
82 EET_DATA_DESCRIPTOR_ADD_BASIC\
83 (_my_message_descriptor, My_Message, # member, member, eet_type)
84 ADD_BASIC(screen_name, EET_T_STRING);
85 ADD_BASIC(name, EET_T_STRING);
86 ADD_BASIC(message, EET_T_STRING);
87 ADD_BASIC(id, EET_T_UINT);
88 ADD_BASIC(status_id, EET_T_UINT);
89 ADD_BASIC(date, EET_T_UINT);
90 ADD_BASIC(timeline, EET_T_UINT);
93 #define ADD_BASIC(member, eet_type)\
94 EET_DATA_DESCRIPTOR_ADD_BASIC\
95 (_my_post_descriptor, My_Post, # member, member, eet_type)
96 ADD_BASIC(dm_to, EET_T_STRING);
97 ADD_BASIC(message, EET_T_STRING);
100 #define ADD_BASIC(member, eet_type)\
101 EET_DATA_DESCRIPTOR_ADD_BASIC\
102 (_my_account_descriptor, My_Account, # member, member, eet_type)
103 ADD_BASIC(name, EET_T_STRING);
104 ADD_BASIC(id, EET_T_UINT);
107 EET_DATA_DESCRIPTOR_ADD_LIST
108 (_my_account_descriptor, My_Account, "messages", messages,
109 _my_message_descriptor);
110 EET_DATA_DESCRIPTOR_ADD_LIST
111 (_my_account_descriptor, My_Account, "posts", posts,
112 _my_post_descriptor);
114 #define ADD_BASIC(member, eet_type)\
115 EET_DATA_DESCRIPTOR_ADD_BASIC\
116 (_my_cache_descriptor, My_Cache, # member, member, eet_type)
117 ADD_BASIC(version, EET_T_UINT);
120 EET_DATA_DESCRIPTOR_ADD_LIST
121 (_my_cache_descriptor, My_Cache, "accounts", accounts,
122 _my_account_descriptor);
123 } /* _my_cache_descriptor_init */
126 _my_cache_descriptor_shutdown(void)
128 eet_data_descriptor_free(_my_cache_descriptor);
129 eet_data_descriptor_free(_my_account_descriptor);
130 eet_data_descriptor_free(_my_message_descriptor);
131 eet_data_descriptor_free(_my_post_descriptor);
132 } /* _my_cache_descriptor_shutdown */
134 // need to check if the pointer came from mmaped area in eet_dictionary
135 // or it was allocated with eina_stringshare_add()
137 _eet_string_free(const char * str)
142 if ((_my_cache_dict) && (eet_dictionary_string_check(_my_cache_dict, str)))
145 eina_stringshare_del(str);
146 } /* _eet_string_free */
149 _my_message_new(const char * message)
151 My_Message * msg = calloc(1, sizeof(My_Message));
154 fprintf(stderr, "ERROR: could not calloc My_Message\n");
158 msg->message = eina_stringshare_add(message);
160 } /* _my_message_new */
163 _my_message_free(My_Message * msg)
165 _eet_string_free(msg->screen_name);
166 _eet_string_free(msg->name);
167 _eet_string_free(msg->message);
169 } /* _my_message_free */
172 _my_post_new(const char * message)
174 My_Post * post = calloc(1, sizeof(My_Post));
177 fprintf(stderr, "ERROR: could not calloc My_Post\n");
181 post->message = eina_stringshare_add(message);
186 _my_post_free(My_Post * post)
188 _eet_string_free(post->dm_to);
189 _eet_string_free(post->message);
191 } /* _my_post_free */
194 _my_account_new(const char * name)
196 My_Account * acc = calloc(1, sizeof(My_Account));
199 fprintf(stderr, "ERROR: could not calloc My_Account\n");
203 acc->name = eina_stringshare_add(name);
205 } /* _my_account_new */
208 _my_account_free(My_Account * acc)
213 _eet_string_free(acc->name);
215 EINA_LIST_FREE(acc->messages, m)
218 EINA_LIST_FREE(acc->posts, p)
222 } /* _my_account_free */
227 My_Cache * my_cache = calloc(1, sizeof(My_Cache));
230 fprintf(stderr, "ERROR: could not calloc My_Cache\n");
234 my_cache->version = 1;
236 } /* _my_cache_new */
239 _my_cache_free(My_Cache * my_cache)
242 EINA_LIST_FREE(my_cache->accounts, acc)
243 _my_account_free(acc);
245 } /* _my_cache_free */
248 _my_cache_account_find(My_Cache * my_cache, const char * name)
252 EINA_LIST_FOREACH(my_cache->accounts, l, acc)
253 if (strcmp(acc->name, name) == 0)
257 } /* _my_cache_account_find */
260 _my_cache_load(const char * filename)
263 Eet_File * ef = eet_open(filename, EET_FILE_MODE_READ);
266 fprintf(stderr, "ERROR: could not open '%s' for read\n", filename);
270 my_cache = eet_data_read(ef, _my_cache_descriptor, MY_CACHE_FILE_ENTRY);
277 if (my_cache->version < 1)
280 "WARNING: version %#x was too old, upgrading it to %#x\n",
281 my_cache->version, 1);
283 my_cache->version = 1;
287 eet_close(_my_cache_file);
290 _my_cache_dict = eet_dictionary_get(ef);
293 } /* _my_cache_load */
296 _my_cache_save(const My_Cache * my_cache, const char * filename)
304 len = eina_strlcpy(tmp, filename, sizeof(tmp));
305 if (len + 12 >= (int)sizeof(tmp))
307 fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
314 snprintf(tmp + len, 12, ".%u", i);
317 while (stat(tmp, &st) == 0);
319 ef = eet_open(tmp, EET_FILE_MODE_WRITE);
322 fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
327 (ef, _my_cache_descriptor, MY_CACHE_FILE_ENTRY, my_cache, EINA_TRUE);
329 // VERY IMPORTANT NOTE:
330 // after eet_close(), all strings mmaped from file will be GONE, invalid!
331 // you'll need to free the old cache and open the new one.
332 // For cache this is okay, as you should be saving not so often or just
335 // This is a trade off, you save memory by using mmap()ed strings, but
336 // you have to care about this.
342 rename(tmp, filename);
346 } /* _my_cache_save */
348 int main(int argc, char * argv[])
351 const Eina_List * l_acc;
358 "Usage:\n\t%s <input> <output> [action] [action-params]\n\n"
359 "Where actions and their parameters:\n"
361 "\tpost <account-name> <message>\n"
362 "\tmessage <account-name> <message>\n"
370 _my_cache_descriptor_init();
372 my_cache = _my_cache_load(argv[1]);
375 printf("creating new cache.\n");
376 my_cache = _my_cache_new();
386 if (strcmp(argv[3], "acc") == 0)
390 My_Account * acc = _my_cache_account_find(my_cache, argv[4]);
393 acc = _my_account_new(argv[4]);
394 my_cache->accounts = eina_list_append
395 (my_cache->accounts, acc);
398 fprintf(stderr, "ERROR: account '%s' already exists.\n",
403 "ERROR: wrong number of parameters (%d).\n",
406 else if (strcmp(argv[3], "post") == 0)
410 My_Account * acc = _my_cache_account_find(my_cache, argv[4]);
413 My_Post * post = _my_post_new(argv[5]);
414 acc->posts = eina_list_append(acc->posts, post);
417 fprintf(stderr, "ERROR: unknown account: '%s'\n", argv[4]);
421 "ERROR: wrong number of parameters (%d).\n",
424 else if (strcmp(argv[3], "message") == 0)
428 My_Account * acc = _my_cache_account_find(my_cache, argv[4]);
431 My_Message * msg = _my_message_new(argv[5]);
432 acc->messages = eina_list_append(acc->messages, msg);
435 fprintf(stderr, "ERROR: unknown account: '%s'\n", argv[4]);
439 "ERROR: wrong number of parameters (%d).\n",
443 fprintf(stderr, "ERROR: unknown action '%s'\n", argv[2]);
450 eina_list_count(my_cache->accounts));
451 EINA_LIST_FOREACH(my_cache->accounts, l_acc, acc)
453 const My_Post * post;
455 printf("\t > %-#8x '%.20s' stats: m=%u, p=%u\n",
456 acc->id, acc->name ? acc->name : "",
457 eina_list_count(acc->messages),
458 eina_list_count(acc->posts));
460 if (eina_list_count(acc->messages))
463 const My_Message * msg;
464 printf("\t |messages:\n");
466 EINA_LIST_FOREACH(acc->messages, l, msg)
468 printf("\t | %-8x '%s' [%s]: '%.20s'\n",
470 msg->name ? msg->name : "",
471 msg->screen_name ? msg->screen_name : "",
472 msg->message ? msg->message : "");
476 if (eina_list_count(acc->posts))
479 const My_Post * post;
480 printf("\t |posts:\n");
482 EINA_LIST_FOREACH(acc->posts, l, post)
485 printf("\t | @%s: '%.20s'\n", post->dm_to, post->message);
487 printf("\t | '%.20s'\n", post->message);
494 if (!_my_cache_save(my_cache, argv[2]))
497 _my_cache_free(my_cache);
500 _my_cache_descriptor_shutdown();