PageRenderTime 37ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/eet-1.4.1/src/examples/eet-data-file_descriptor.c

#
C | 506 lines | 410 code | 73 blank | 23 comment | 43 complexity | cd43135f53d3a6d056e5c98701cca436 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #include <Eina.h>
  2. #include <Eet.h>
  3. #include <stdio.h>
  4. #include <limits.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. // complex real-world structures based on elmdentica database
  9. typedef struct
  10. {
  11. const char * screen_name;
  12. const char * name;
  13. const char * message;
  14. unsigned int id;
  15. unsigned int status_id;
  16. unsigned int date;
  17. unsigned int timeline;
  18. } My_Message;
  19. typedef struct
  20. {
  21. const char * dm_to;
  22. const char * message;
  23. } My_Post;
  24. typedef struct
  25. {
  26. unsigned int id;
  27. const char * name;
  28. Eina_List * messages;
  29. Eina_List * posts;
  30. } My_Account;
  31. typedef struct
  32. {
  33. unsigned int version; // it is recommended to use versioned configuration!
  34. Eina_List * accounts;
  35. } My_Cache;
  36. // string that represents the entry in eet file, you might like to have
  37. // different profiles or so in the same file, this is possible with
  38. // different strings
  39. static const char MY_CACHE_FILE_ENTRY[] = "cache";
  40. // keep the descriptor static global, so it can be
  41. // shared by different functions (load/save) of this and only this
  42. // file.
  43. static Eet_Data_Descriptor * _my_cache_descriptor;
  44. static Eet_Data_Descriptor * _my_account_descriptor;
  45. static Eet_Data_Descriptor * _my_message_descriptor;
  46. static Eet_Data_Descriptor * _my_post_descriptor;
  47. // keep file handle alive, so mmap()ed strings are all alive as well
  48. static Eet_File * _my_cache_file = NULL;
  49. static Eet_Dictionary * _my_cache_dict = NULL;
  50. static void
  51. _my_cache_descriptor_init(void)
  52. {
  53. Eet_Data_Descriptor_Class eddc;
  54. // The FILE variant is good for caches and things that are just
  55. // appended, but needs to take care when changing strings and files must
  56. // be kept open so mmap()ed strings will be kept alive.
  57. EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Cache);
  58. _my_cache_descriptor = eet_data_descriptor_file_new(&eddc);
  59. EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Account);
  60. _my_account_descriptor = eet_data_descriptor_file_new(&eddc);
  61. EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Message);
  62. _my_message_descriptor = eet_data_descriptor_file_new(&eddc);
  63. EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Post);
  64. _my_post_descriptor = eet_data_descriptor_file_new(&eddc);
  65. // Describe the members to be saved:
  66. // Use a temporary macro so we don't type a lot, also avoid errors:
  67. #define ADD_BASIC(member, eet_type)\
  68. EET_DATA_DESCRIPTOR_ADD_BASIC\
  69. (_my_message_descriptor, My_Message, # member, member, eet_type)
  70. ADD_BASIC(screen_name, EET_T_STRING);
  71. ADD_BASIC(name, EET_T_STRING);
  72. ADD_BASIC(message, EET_T_STRING);
  73. ADD_BASIC(id, EET_T_UINT);
  74. ADD_BASIC(status_id, EET_T_UINT);
  75. ADD_BASIC(date, EET_T_UINT);
  76. ADD_BASIC(timeline, EET_T_UINT);
  77. #undef ADD_BASIC
  78. #define ADD_BASIC(member, eet_type)\
  79. EET_DATA_DESCRIPTOR_ADD_BASIC\
  80. (_my_post_descriptor, My_Post, # member, member, eet_type)
  81. ADD_BASIC(dm_to, EET_T_STRING);
  82. ADD_BASIC(message, EET_T_STRING);
  83. #undef ADD_BASIC
  84. #define ADD_BASIC(member, eet_type)\
  85. EET_DATA_DESCRIPTOR_ADD_BASIC\
  86. (_my_account_descriptor, My_Account, # member, member, eet_type)
  87. ADD_BASIC(name, EET_T_STRING);
  88. ADD_BASIC(id, EET_T_UINT);
  89. #undef ADD_BASIC
  90. EET_DATA_DESCRIPTOR_ADD_LIST
  91. (_my_account_descriptor, My_Account, "messages", messages,
  92. _my_message_descriptor);
  93. EET_DATA_DESCRIPTOR_ADD_LIST
  94. (_my_account_descriptor, My_Account, "posts", posts,
  95. _my_post_descriptor);
  96. #define ADD_BASIC(member, eet_type)\
  97. EET_DATA_DESCRIPTOR_ADD_BASIC\
  98. (_my_cache_descriptor, My_Cache, # member, member, eet_type)
  99. ADD_BASIC(version, EET_T_UINT);
  100. #undef ADD_BASIC
  101. EET_DATA_DESCRIPTOR_ADD_LIST
  102. (_my_cache_descriptor, My_Cache, "accounts", accounts,
  103. _my_account_descriptor);
  104. } /* _my_cache_descriptor_init */
  105. static void
  106. _my_cache_descriptor_shutdown(void)
  107. {
  108. eet_data_descriptor_free(_my_cache_descriptor);
  109. eet_data_descriptor_free(_my_account_descriptor);
  110. eet_data_descriptor_free(_my_message_descriptor);
  111. eet_data_descriptor_free(_my_post_descriptor);
  112. } /* _my_cache_descriptor_shutdown */
  113. // need to check if the pointer came from mmaped area in eet_dictionary
  114. // or it was allocated with eina_stringshare_add()
  115. static void
  116. _eet_string_free(const char * str)
  117. {
  118. if (!str)
  119. return;
  120. if ((_my_cache_dict) && (eet_dictionary_string_check(_my_cache_dict, str)))
  121. return;
  122. eina_stringshare_del(str);
  123. } /* _eet_string_free */
  124. static My_Message *
  125. _my_message_new(const char * message)
  126. {
  127. My_Message * msg = calloc(1, sizeof(My_Message));
  128. if (!msg)
  129. {
  130. fprintf(stderr, "ERROR: could not calloc My_Message\n");
  131. return NULL;
  132. }
  133. msg->message = eina_stringshare_add(message);
  134. return msg;
  135. } /* _my_message_new */
  136. static void
  137. _my_message_free(My_Message * msg)
  138. {
  139. _eet_string_free(msg->screen_name);
  140. _eet_string_free(msg->name);
  141. _eet_string_free(msg->message);
  142. free(msg);
  143. } /* _my_message_free */
  144. static My_Post *
  145. _my_post_new(const char * message)
  146. {
  147. My_Post * post = calloc(1, sizeof(My_Post));
  148. if (!post)
  149. {
  150. fprintf(stderr, "ERROR: could not calloc My_Post\n");
  151. return NULL;
  152. }
  153. post->message = eina_stringshare_add(message);
  154. return post;
  155. } /* _my_post_new */
  156. static void
  157. _my_post_free(My_Post * post)
  158. {
  159. _eet_string_free(post->dm_to);
  160. _eet_string_free(post->message);
  161. free(post);
  162. } /* _my_post_free */
  163. static My_Account *
  164. _my_account_new(const char * name)
  165. {
  166. My_Account * acc = calloc(1, sizeof(My_Account));
  167. if (!acc)
  168. {
  169. fprintf(stderr, "ERROR: could not calloc My_Account\n");
  170. return NULL;
  171. }
  172. acc->name = eina_stringshare_add(name);
  173. return acc;
  174. } /* _my_account_new */
  175. static void
  176. _my_account_free(My_Account * acc)
  177. {
  178. My_Message * m;
  179. My_Post * p;
  180. _eet_string_free(acc->name);
  181. EINA_LIST_FREE(acc->messages, m)
  182. _my_message_free(m);
  183. EINA_LIST_FREE(acc->posts, p)
  184. _my_post_free(p);
  185. free(acc);
  186. } /* _my_account_free */
  187. static My_Cache *
  188. _my_cache_new(void)
  189. {
  190. My_Cache * my_cache = calloc(1, sizeof(My_Cache));
  191. if (!my_cache)
  192. {
  193. fprintf(stderr, "ERROR: could not calloc My_Cache\n");
  194. return NULL;
  195. }
  196. my_cache->version = 1;
  197. return my_cache;
  198. } /* _my_cache_new */
  199. static void
  200. _my_cache_free(My_Cache * my_cache)
  201. {
  202. My_Account * acc;
  203. EINA_LIST_FREE(my_cache->accounts, acc)
  204. _my_account_free(acc);
  205. free(my_cache);
  206. } /* _my_cache_free */
  207. static My_Account *
  208. _my_cache_account_find(My_Cache * my_cache, const char * name)
  209. {
  210. My_Account * acc;
  211. Eina_List * l;
  212. EINA_LIST_FOREACH(my_cache->accounts, l, acc)
  213. if (strcmp(acc->name, name) == 0)
  214. return acc;
  215. return NULL;
  216. } /* _my_cache_account_find */
  217. static My_Cache *
  218. _my_cache_load(const char * filename)
  219. {
  220. My_Cache * my_cache;
  221. Eet_File * ef = eet_open(filename, EET_FILE_MODE_READ);
  222. if (!ef)
  223. {
  224. fprintf(stderr, "ERROR: could not open '%s' for read\n", filename);
  225. return NULL;
  226. }
  227. my_cache = eet_data_read(ef, _my_cache_descriptor, MY_CACHE_FILE_ENTRY);
  228. if (!my_cache)
  229. {
  230. eet_close(ef);
  231. return NULL;
  232. }
  233. if (my_cache->version < 1)
  234. {
  235. fprintf(stderr,
  236. "WARNING: version %#x was too old, upgrading it to %#x\n",
  237. my_cache->version, 1);
  238. my_cache->version = 1;
  239. }
  240. if (_my_cache_file)
  241. eet_close(_my_cache_file);
  242. _my_cache_file = ef;
  243. _my_cache_dict = eet_dictionary_get(ef);
  244. return my_cache;
  245. } /* _my_cache_load */
  246. static Eina_Bool
  247. _my_cache_save(const My_Cache * my_cache, const char * filename)
  248. {
  249. char tmp[PATH_MAX];
  250. Eet_File * ef;
  251. Eina_Bool ret;
  252. unsigned int i, len;
  253. struct stat st;
  254. len = eina_strlcpy(tmp, filename, sizeof(tmp));
  255. if (len + 12 >= (int)sizeof(tmp))
  256. {
  257. fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
  258. return EINA_FALSE;
  259. }
  260. i = 0;
  261. do
  262. {
  263. snprintf(tmp + len, 12, ".%u", i);
  264. i++;
  265. }
  266. while (stat(tmp, &st) == 0);
  267. ef = eet_open(tmp, EET_FILE_MODE_WRITE);
  268. if (!ef)
  269. {
  270. fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
  271. return EINA_FALSE;
  272. }
  273. ret = eet_data_write
  274. (ef, _my_cache_descriptor, MY_CACHE_FILE_ENTRY, my_cache, EINA_TRUE);
  275. // VERY IMPORTANT NOTE:
  276. // after eet_close(), all strings mmaped from file will be GONE, invalid!
  277. // you'll need to free the old cache and open the new one.
  278. // For cache this is okay, as you should be saving not so often or just
  279. // at end.
  280. //
  281. // This is a trade off, you save memory by using mmap()ed strings, but
  282. // you have to care about this.
  283. eet_close(ef);
  284. if (ret)
  285. {
  286. unlink(filename);
  287. rename(tmp, filename);
  288. }
  289. return ret;
  290. } /* _my_cache_save */
  291. int main(int argc, char * argv[])
  292. {
  293. My_Cache * my_cache;
  294. const Eina_List * l_acc;
  295. My_Account * acc;
  296. int ret = 0;
  297. if (argc < 3)
  298. {
  299. fprintf(stderr,
  300. "Usage:\n\t%s <input> <output> [action] [action-params]\n\n"
  301. "Where actions and their parameters:\n"
  302. "\tacc <name>\n"
  303. "\tpost <account-name> <message>\n"
  304. "\tmessage <account-name> <message>\n"
  305. "\n",
  306. argv[0]);
  307. return -1;
  308. }
  309. eina_init();
  310. eet_init();
  311. _my_cache_descriptor_init();
  312. my_cache = _my_cache_load(argv[1]);
  313. if (!my_cache)
  314. {
  315. printf("creating new cache.\n");
  316. my_cache = _my_cache_new();
  317. if (!my_cache)
  318. {
  319. ret = -2;
  320. goto end;
  321. }
  322. }
  323. if (argc > 3)
  324. {
  325. if (strcmp(argv[3], "acc") == 0)
  326. {
  327. if (argc == 5)
  328. {
  329. My_Account * acc = _my_cache_account_find(my_cache, argv[4]);
  330. if (!acc)
  331. {
  332. acc = _my_account_new(argv[4]);
  333. my_cache->accounts = eina_list_append
  334. (my_cache->accounts, acc);
  335. }
  336. else
  337. fprintf(stderr, "ERROR: account '%s' already exists.\n",
  338. argv[4]);
  339. }
  340. else
  341. fprintf(stderr,
  342. "ERROR: wrong number of parameters (%d).\n",
  343. argc);
  344. }
  345. else if (strcmp(argv[3], "post") == 0)
  346. {
  347. if (argc == 6)
  348. {
  349. My_Account * acc = _my_cache_account_find(my_cache, argv[4]);
  350. if (acc)
  351. {
  352. My_Post * post = _my_post_new(argv[5]);
  353. acc->posts = eina_list_append(acc->posts, post);
  354. }
  355. else
  356. fprintf(stderr, "ERROR: unknown account: '%s'\n", argv[4]);
  357. }
  358. else
  359. fprintf(stderr,
  360. "ERROR: wrong number of parameters (%d).\n",
  361. argc);
  362. }
  363. else if (strcmp(argv[3], "message") == 0)
  364. {
  365. if (argc == 6)
  366. {
  367. My_Account * acc = _my_cache_account_find(my_cache, argv[4]);
  368. if (acc)
  369. {
  370. My_Message * msg = _my_message_new(argv[5]);
  371. acc->messages = eina_list_append(acc->messages, msg);
  372. }
  373. else
  374. fprintf(stderr, "ERROR: unknown account: '%s'\n", argv[4]);
  375. }
  376. else
  377. fprintf(stderr,
  378. "ERROR: wrong number of parameters (%d).\n",
  379. argc);
  380. }
  381. else
  382. fprintf(stderr, "ERROR: unknown action '%s'\n", argv[2]);
  383. }
  384. printf("My_Cache:\n"
  385. "\tversion.: %#x\n"
  386. "\taccounts: %u\n",
  387. my_cache->version,
  388. eina_list_count(my_cache->accounts));
  389. EINA_LIST_FOREACH(my_cache->accounts, l_acc, acc)
  390. {
  391. const My_Post * post;
  392. printf("\t > %-#8x '%.20s' stats: m=%u, p=%u\n",
  393. acc->id, acc->name ? acc->name : "",
  394. eina_list_count(acc->messages),
  395. eina_list_count(acc->posts));
  396. if (eina_list_count(acc->messages))
  397. {
  398. const Eina_List * l;
  399. const My_Message * msg;
  400. printf("\t |messages:\n");
  401. EINA_LIST_FOREACH(acc->messages, l, msg)
  402. {
  403. printf("\t | %-8x '%s' [%s]: '%.20s'\n",
  404. msg->id,
  405. msg->name ? msg->name : "",
  406. msg->screen_name ? msg->screen_name : "",
  407. msg->message ? msg->message : "");
  408. }
  409. }
  410. if (eina_list_count(acc->posts))
  411. {
  412. const Eina_List * l;
  413. const My_Post * post;
  414. printf("\t |posts:\n");
  415. EINA_LIST_FOREACH(acc->posts, l, post)
  416. {
  417. if (post->dm_to)
  418. printf("\t | @%s: '%.20s'\n", post->dm_to, post->message);
  419. else
  420. printf("\t | '%.20s'\n", post->message);
  421. }
  422. }
  423. printf("\n");
  424. }
  425. if (!_my_cache_save(my_cache, argv[2]))
  426. ret = -3;
  427. _my_cache_free(my_cache);
  428. end:
  429. _my_cache_descriptor_shutdown();
  430. eet_shutdown();
  431. eina_shutdown();
  432. return ret;
  433. } /* main */