#include <stdio.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
typedef struct
{
   const char  *screen_name;
   const char  *name;
   const char  *message;
   unsigned int id;
   unsigned int status_id;
   unsigned int date;
   unsigned int timeline;
} My_Message;
typedef struct
{
   const char *dm_to;
   const char *message;
} My_Post;
typedef struct
{
   unsigned int id;
   const char  *name;
   My_Post     *posts;
   int          posts_count;
} My_Account;
typedef struct
{
   unsigned int version; 
} My_Cache;
static const char MY_CACHE_FILE_ENTRY[] = "cache";
static void
_my_cache_descriptor_init(void)
{
   
   
   
   
   
#define ADD_BASIC(member, eet_type) \
  EET_DATA_DESCRIPTOR_ADD_BASIC     \
    (_my_message_descriptor, My_Message, # member, member, eet_type)
#undef ADD_BASIC
#define ADD_BASIC(member, eet_type) \
  EET_DATA_DESCRIPTOR_ADD_BASIC     \
    (_my_post_descriptor, My_Post, # member, member, eet_type)
#undef ADD_BASIC
#define ADD_BASIC(member, eet_type) \
  EET_DATA_DESCRIPTOR_ADD_BASIC     \
    (_my_account_descriptor, My_Account, # member, member, eet_type)
#undef ADD_BASIC
     (_my_account_descriptor, My_Account, "messages", messages,
     _my_message_descriptor);
     (_my_account_descriptor, My_Account, "posts", posts,
     _my_post_descriptor);
#define ADD_BASIC(member, eet_type) \
  EET_DATA_DESCRIPTOR_ADD_BASIC     \
    (_my_cache_descriptor, My_Cache, # member, member, eet_type)
#undef ADD_BASIC
     (_my_cache_descriptor, My_Cache, "accounts", accounts,
     _my_account_descriptor);
} 
static void
_my_cache_descriptor_shutdown(void)
{
} 
static void
_eet_string_free(const char *str)
{
   if (!str)
     return;
     return;
} 
static My_Message *
_my_message_new(const char *message)
{
   My_Message *msg = calloc(1, sizeof(My_Message));
   if (!msg)
     {
        fprintf(stderr, "ERROR: could not calloc My_Message\n");
        return NULL;
     }
   return msg;
} 
static void
_my_message_free(My_Message *msg)
{
   _eet_string_free(msg->screen_name);
   _eet_string_free(msg->name);
   _eet_string_free(msg->message);
   free(msg);
} 
_my_post_add(My_Account *acc,
             const char *message)
{
   int new_count = acc->posts_count + 1;
   My_Post *post = realloc(acc->posts, new_count * sizeof(My_Post));
   if (!post)
     {
        fprintf(stderr, "ERROR: could add My_Post\n");
     }
   post[acc->posts_count].dm_to = NULL;
   acc->posts_count = new_count;
   acc->posts = post;
} 
static void
_my_post_free(My_Post *post)
{
   _eet_string_free(post->dm_to);
   _eet_string_free(post->message);
} 
static My_Account *
_my_account_new(const char *name)
{
   My_Account *acc = calloc(1, sizeof(My_Account));
   if (!acc)
     {
        fprintf(stderr, "ERROR: could not calloc My_Account\n");
        return NULL;
     }
   return acc;
} 
static void
_my_account_free(My_Account *acc)
{
   My_Message *m;
   int i;
   _eet_string_free(acc->name);
     _my_message_free(m);
   for (i = 0; i < acc->posts_count; i++)
     _my_post_free(&acc->posts[i]);
   free(acc->posts);
   free(acc);
} 
static My_Cache *
_my_cache_new(void)
{
   My_Cache *my_cache = calloc(1, sizeof(My_Cache));
   if (!my_cache)
     {
        fprintf(stderr, "ERROR: could not calloc My_Cache\n");
        return NULL;
     }
   my_cache->version = 1;
   return my_cache;
} 
                          void            *data,
{
   _my_account_free(data);
}
static void
_my_cache_free(My_Cache *my_cache)
{
   free(my_cache);
} 
static My_Account *
_my_cache_account_find(My_Cache   *my_cache,
                       const char *name)
{
} 
static My_Cache *
_my_cache_load(const char *filename)
{
   My_Cache *my_cache;
   if (!ef)
     {
        fprintf(stderr, "ERROR: could not open '%s' for read\n", filename);
        return NULL;
     }
   my_cache = 
eet_data_read(ef, _my_cache_descriptor, MY_CACHE_FILE_ENTRY);
   if (!my_cache)
     {
        return NULL;
     }
   if (my_cache->version < 1)
     {
        fprintf(stderr,
                "WARNING: version %#x was too old, upgrading it to %#x\n",
                my_cache->version, 1);
        my_cache->version = 1;
     }
   if (_my_cache_file)
   _my_cache_file = ef;
   return my_cache;
} 
_my_cache_save(const My_Cache *my_cache,
               const char     *filename)
{
   char tmp[PATH_MAX];
   unsigned int i, len;
   struct stat st;
   if (len + 12 >= (int)sizeof(tmp))
     {
        fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
     }
   i = 0;
   do
     {
        snprintf(tmp + len, 12, ".%u", i);
        i++;
     }
   while (stat(tmp, &st) == 0);
   if (!ef)
     {
        fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
     }
       (ef, _my_cache_descriptor, MY_CACHE_FILE_ENTRY, my_cache, 
EINA_TRUE);
   
   
   
   
   
   
   
   
   if (ret)
     {
        unlink(filename);
        rename(tmp, filename);
     }
   return ret;
} 
int
main(int   argc,
     char *argv[])
{
   My_Cache *my_cache;
   My_Account *acc;
   int ret = 0;
   if (argc < 3)
     {
        fprintf(stderr,
                "Usage:\n\t%s <input> <output> [action] [action-params]\n\n"
                "Where actions and their parameters:\n"
                "\tacc <name>\n"
                "\tpost <account-name> <message>\n"
                "\tmessage <account-name> <message>\n"
                "\n",
                argv[0]);
        return -1;
     }
   _my_cache_descriptor_init();
   my_cache = _my_cache_load(argv[1]);
   if (!my_cache)
     {
        printf("creating new cache.\n");
        my_cache = _my_cache_new();
        if (!my_cache)
          {
             ret = -2;
             goto end;
          }
     }
   if (argc > 3)
     {
        if (strcmp(argv[3], "acc") == 0)
          {
             if (argc == 5)
               {
                  My_Account *acc_ = _my_cache_account_find(my_cache, argv[4]);
                  if (!acc_)
                    {
                       acc_ = _my_account_new(argv[4]);
                    }
                  else
                    fprintf(stderr, "ERROR: account '%s' already exists.\n",
                            argv[4]);
               }
             else
               fprintf(stderr,
                       "ERROR: wrong number of parameters (%d).\n",
                       argc);
          }
        else if (strcmp(argv[3], "post") == 0)
          {
             if (argc == 6)
               {
                  My_Account *acc_ = _my_cache_account_find(my_cache, argv[4]);
                  if (acc_)
                    {
                       _my_post_add(acc_, argv[5]);
                    }
                  else
                    fprintf(stderr, "ERROR: unknown account: '%s'\n", argv[4]);
               }
             else
               fprintf(stderr,
                       "ERROR: wrong number of parameters (%d).\n",
                       argc);
          }
        else if (strcmp(argv[3], "message") == 0)
          {
             if (argc == 6)
               {
                  My_Account *acc_ = _my_cache_account_find(my_cache, argv[4]);
                  if (acc_)
                    {
                       My_Message *msg = _my_message_new(argv[5]);
                    }
                  else
                    fprintf(stderr, "ERROR: unknown account: '%s'\n", argv[4]);
               }
             else
               fprintf(stderr,
                       "ERROR: wrong number of parameters (%d).\n",
                       argc);
          }
        else
          fprintf(stderr, "ERROR: unknown action '%s'\n", argv[2]);
     }
   printf("My_Cache:\n"
          "\tversion.: %#x\n"
          "\taccounts: %u\n",
          my_cache->version,
     {
        printf("\t  > %-#8x '%.20s' stats: m=%u, p=%u\n",
               acc->id, acc->name ? acc->name : "",
               acc->posts_count);
          {
             const My_Message *msg;
             printf("\t  |messages:\n");
               {
                  printf("\t  |   %-8x '%s' [%s]: '%.20s'\n",
                         msg->id,
                         msg->name ? msg->name : "",
                         msg->screen_name ? msg->screen_name : "",
                         msg->message ? msg->message : "");
               }
          }
        if (acc->posts_count)
          {
             const My_Post *post;
             int i;
             printf("\t  |posts:\n");
             for (i = 0; i < acc->posts_count; i++)
               {
                  post = &acc->posts[i];
                  if (post->dm_to)
                    printf("\t  |  @%s: '%.20s'\n", post->dm_to, post->message);
                  else
                    printf("\t  |  '%.20s'\n", post->message);
               }
          }
        printf("\n");
     }
   if (!_my_cache_save(my_cache, argv[2]))
     ret = -3;
   _my_cache_free(my_cache);
end:
   if (_my_cache_file)
   _my_cache_descriptor_shutdown();
   return ret;
}