1
2
3
6#include <stdio.h>
7#include <limits.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <unistd.h>
11
12
13typedef 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
24typedef struct
25{
26 const char *dm_to;
27 const char *message;
28} My_Post;
29
30typedef struct
31{
32 unsigned int id;
33 const char *name;
35 My_Post *posts;
36 int posts_count;
37} My_Account;
38
39typedef struct
40{
41 unsigned int version;
43} My_Cache;
44
45
46
47
48static const char MY_CACHE_FILE_ENTRY[] = "cache";
49
50
51
52
57
58
59static Eet_File *_my_cache_file = NULL;
61
62static void
63_my_cache_descriptor_init(void)
64{
66
67
68
69
72
75
78
81
82
83
84
85#define ADD_BASIC(member, eet_type) \
86 EET_DATA_DESCRIPTOR_ADD_BASIC \
87 (_my_message_descriptor, My_Message, # member, member, eet_type)
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)
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)
109#undef ADD_BASIC
110
112 (_my_account_descriptor, My_Account, "messages", messages,
113 _my_message_descriptor);
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)
122#undef ADD_BASIC
123
125 (_my_cache_descriptor, My_Cache, "accounts", accounts,
126 _my_account_descriptor);
127}
128
129static void
130_my_cache_descriptor_shutdown(void)
131{
136}
137
138
139
140static void
141_eet_string_free(const char *str)
142{
143 if (!str)
144 return;
145
147 return;
148
150}
151
152static 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
163 return msg;
164}
165
166static 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}
174
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");
185 }
186
188 post[acc->posts_count].dm_to = NULL;
189 acc->posts_count = new_count;
190 acc->posts = post;
192}
193
194static void
195_my_post_free(My_Post *post)
196{
197 _eet_string_free(post->dm_to);
198 _eet_string_free(post->message);
199}
200
201static 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
212 return acc;
213}
214
215static void
216_my_account_free(My_Account *acc)
217{
218 My_Message *m;
219 int i;
220
221 _eet_string_free(acc->name);
222
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}
232
233static 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
244
245 my_cache->version = 1;
246 return my_cache;
247}
248
252 void *data,
254{
255 _my_account_free(data);
257}
258
259static void
260_my_cache_free(My_Cache *my_cache)
261{
264 free(my_cache);
265}
266
267static My_Account *
268_my_cache_account_find(My_Cache *my_cache,
269 const char *name)
270{
272}
273
274static My_Cache *
275_my_cache_load(const char *filename)
276{
277 My_Cache *my_cache;
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 {
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)
303
304 _my_cache_file = ef;
306
307 return my_cache;
308}
309
311_my_cache_save(const My_Cache *my_cache,
312 const char *filename)
313{
314 char tmp[PATH_MAX];
317 unsigned int i, len;
318 struct stat st;
319
321 if (len + 12 >= (int)sizeof(tmp))
322 {
323 fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
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
336 if (!ef)
337 {
338 fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
340 }
341
343 (ef, _my_cache_descriptor, MY_CACHE_FILE_ENTRY, my_cache,
EINA_TRUE);
344
345
346
347
348
349
350
351
352
354
355 if (ret)
356 {
357 unlink(filename);
358 rename(tmp, filename);
359 }
360
361 return ret;
362}
363
364int
365main(int argc,
366 char *argv[])
367{
368 My_Cache *my_cache;
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
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]);
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]);
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,
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 {
478 const My_Message *msg;
479 printf("\t |messages:\n");
480
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 }
510
511 if (!_my_cache_save(my_cache, argv[2]))
512 ret = -3;
513
514 _my_cache_free(my_cache);
515
516end:
517 if (_my_cache_file)
519 _my_cache_descriptor_shutdown();
522
523 return ret;
524}
525
The file that provides the eet functions.
#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:3092
#define EET_T_STRING
Data type: char *.
Definition Eet.h:2606
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
EAPI void * eet_data_read(Eet_File *ef, Eet_Data_Descriptor *edd, const char *name)
Reads a data structure from an eet file and decodes it.
Definition eet_data.c:2379
#define EET_DATA_DESCRIPTOR_ADD_VAR_ARRAY(edd, struct_type, name, member, subtype)
Adds a variable size array type to a data descriptor.
Definition Eet.h:3769
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
#define EET_DATA_DESCRIPTOR_ADD_LIST(edd, struct_type, name, member, subtype)
Adds a linked list type to a data descriptor.
Definition Eet.h:3528
EAPI int eet_data_write(Eet_File *ef, Eet_Data_Descriptor *edd, const char *name, const void *data, int compress)
Writes a data structure from memory and store in an eet file.
Definition eet_data.c:2416
struct _Eet_Data_Descriptor Eet_Data_Descriptor
Opaque handle that have information on a type members.
Definition Eet.h:2648
#define EET_T_UINT
Data type: unsigned int.
Definition Eet.h:2604
#define EET_DATA_DESCRIPTOR_ADD_HASH(edd, struct_type, name, member, subtype)
Adds a hash type to a data descriptor.
Definition Eet.h:3601
struct _Eet_Data_Descriptor_Class Eet_Data_Descriptor_Class
Instructs Eet about memory management for different needs under serialization and parse process.
Definition Eet.h:2665
struct _Eet_Dictionary Eet_Dictionary
Opaque handle that defines a file-backed (mmaped) dictionary of strings.
Definition Eet.h:533
EAPI Eet_Error eet_close(Eet_File *ef)
Closes an eet file handle and flush pending writes.
Definition eet_lib.c:1934
EAPI int eet_dictionary_string_check(Eet_Dictionary *ed, const char *string)
Checks if a given string comes from a given dictionary.
Definition eet_dictionary.c:601
struct _Eet_File Eet_File
Opaque handle that defines an Eet file (or memory).
Definition Eet.h:527
EAPI Eet_File * eet_open(const char *file, Eet_File_Mode mode)
Opens an eet file on disk, and returns a handle to it.
Definition eet_lib.c:1525
EAPI Eet_Dictionary * eet_dictionary_get(Eet_File *ef)
Returns a handle to the shared string dictionary of the Eet file.
Definition eet_lib.c:2599
@ EET_FILE_MODE_READ
File is read-only.
Definition Eet.h:479
@ EET_FILE_MODE_WRITE
File is write-only.
Definition Eet.h:480
EAPI int eet_init(void)
Initializes the EET library.
Definition eet_lib.c:543
EAPI int eet_shutdown(void)
Shuts down the EET library.
Definition eet_lib.c:597
struct _Eina_Hash Eina_Hash
Type for a generic hash table.
Definition eina_hash.h:285
EINA_API Eina_Iterator * eina_hash_iterator_data_new(const Eina_Hash *hash)
Returns a new iterator associated with a hash.
Definition eina_hash.c:1246
EINA_API int eina_hash_population(const Eina_Hash *hash)
Returns the number of entries in the given hash table.
Definition eina_hash.c:858
EINA_API void * eina_hash_find(const Eina_Hash *hash, const void *key)
Retrieves a specific entry in the given hash table.
Definition eina_hash.c:1069
EINA_API void eina_hash_free(Eina_Hash *hash)
Frees the given hash table's resources.
Definition eina_hash.c:868
EINA_API Eina_Bool eina_hash_direct_add(Eina_Hash *hash, const void *key, const void *data)
Adds an entry to the given hash table without duplicating the string.
Definition eina_hash.c:948
EINA_API void eina_hash_foreach(const Eina_Hash *hash, Eina_Hash_Foreach func, const void *fdata)
Calls a function on every member stored in the hash table.
Definition eina_hash.c:1223
EINA_API Eina_Hash * eina_hash_string_small_new(Eina_Free_Cb data_free_cb)
Creates a new hash table for use with strings with small bucket size.
Definition eina_hash.c:800
EINA_API void eina_iterator_free(Eina_Iterator *iterator)
Frees an iterator.
Definition eina_iterator.c:98
#define EINA_ITERATOR_FOREACH(itr, data)
Definition for the macro to iterate over all elements easily.
Definition eina_iterator.h:448
struct _Eina_Iterator Eina_Iterator
Abstract type for iterators.
Definition eina_iterator.h:126
EINA_API Eina_List * eina_list_append(Eina_List *list, const void *data)
Appends the given data to the given linked list.
Definition eina_list.c:584
struct _Eina_List Eina_List
Type for a generic double linked list.
Definition eina_list.h:304
#define EINA_LIST_FOREACH(list, l, _data)
Definition for the macro to iterate over a list.
Definition eina_list.h:1415
#define EINA_LIST_FREE(list, data)
Definition for the macro to remove each list node while having access to each node's data.
Definition eina_list.h:1629
EINA_API int eina_shutdown(void)
Shuts down the Eina library.
Definition eina_main.c:379
EINA_API int eina_init(void)
Initializes the Eina library.
Definition eina_main.c:291
EINA_API size_t eina_strlcpy(char *dst, const char *src, size_t siz)
Copies a c-string to another.
Definition eina_str.c:317
EINA_API Eina_Stringshare * eina_stringshare_add(const char *str)
Retrieves an instance of a string for use in a program.
Definition eina_stringshare.c:606
EINA_API void eina_stringshare_del(Eina_Stringshare *str)
Notes that the given string has lost an instance.
Definition eina_stringshare.c:533
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition eina_types.h:539
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition eina_types.h:533
unsigned char Eina_Bool
Type to mimic a boolean.
Definition eina_types.h:527
#define EINA_UNUSED
Used to indicate that a function parameter is purposely unused.
Definition eina_types.h:339