\hypertarget{eet-data-file_descriptor_01_8c-example}{}\section{eet-\/data-\/file\+\_\+descriptor\+\_\+01.\+c}

\begin{DoxyCodeInclude}
\textcolor{comment}{//Compile with:}
\textcolor{comment}{// gcc -o eet-data-file\_descriptor\_01 eet-data-file\_descriptor\_01.c `pkg-config --cflags --libs eet eina`}

\textcolor{preprocessor}{#include <\mbox{\hyperlink{Eina_8h}{Eina.h}}>}
\textcolor{preprocessor}{#include <\mbox{\hyperlink{Eet_8h}{Eet.h}}>}
\textcolor{preprocessor}{#include <stdio.h>}
\textcolor{preprocessor}{#include <limits.h>}
\textcolor{preprocessor}{#include <sys/types.h>}
\textcolor{preprocessor}{#include <sys/stat.h>}
\textcolor{preprocessor}{#include <unistd.h>}

\textcolor{comment}{// complex real-world structures based on elmdentica database}
\textcolor{keyword}{typedef} \textcolor{keyword}{struct}
\{
   \textcolor{keyword}{const} \textcolor{keywordtype}{char}  *screen\_name;
   \textcolor{keyword}{const} \textcolor{keywordtype}{char}  *name;
   \textcolor{keyword}{const} \textcolor{keywordtype}{char}  *message;
   \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} id;
   \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} status\_id;
   \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} date;
   \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} timeline;
\} My\_Message;

\textcolor{keyword}{typedef} \textcolor{keyword}{struct}
\{
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *dm\_to;
   \textcolor{keyword}{const} \textcolor{keywordtype}{char} *message;
\} My\_Post;

\textcolor{keyword}{typedef} \textcolor{keyword}{struct}
\{
   \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} id;
   \textcolor{keyword}{const} \textcolor{keywordtype}{char}  *name;
   \mbox{\hyperlink{struct__Eina__List}{Eina\_List}}   *messages;
   My\_Post     *posts;
   \textcolor{keywordtype}{int}          posts\_count;
\} My\_Account;

\textcolor{keyword}{typedef} \textcolor{keyword}{struct}
\{
   \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} version; \textcolor{comment}{// it is recommended to use versioned configuration!}
   \mbox{\hyperlink{group__Eina__Hash__Group_ga9a89d7d3ee260d3a60572ebf651b07e0}{Eina\_Hash}}   *accounts;
\} My\_Cache;

\textcolor{comment}{// string that represents the entry in eet file, you might like to have}
\textcolor{comment}{// different profiles or so in the same file, this is possible with}
\textcolor{comment}{// different strings}
\textcolor{keyword}{static} \textcolor{keyword}{const} \textcolor{keywordtype}{char} MY\_CACHE\_FILE\_ENTRY[] = \textcolor{stringliteral}{"cache"};

\textcolor{comment}{// keep the descriptor static global, so it can be}
\textcolor{comment}{// shared by different functions (load/save) of this and only this}
\textcolor{comment}{// file.}
\textcolor{keyword}{static} \mbox{\hyperlink{group__Eet__Data__Group_ga4baec0e840012480a0958c5c3bca276d}{Eet\_Data\_Descriptor}} *\_my\_cache\_descriptor;
\textcolor{keyword}{static} \mbox{\hyperlink{group__Eet__Data__Group_ga4baec0e840012480a0958c5c3bca276d}{Eet\_Data\_Descriptor}} *\_my\_account\_descriptor;
\textcolor{keyword}{static} \mbox{\hyperlink{group__Eet__Data__Group_ga4baec0e840012480a0958c5c3bca276d}{Eet\_Data\_Descriptor}} *\_my\_message\_descriptor;
\textcolor{keyword}{static} \mbox{\hyperlink{group__Eet__Data__Group_ga4baec0e840012480a0958c5c3bca276d}{Eet\_Data\_Descriptor}} *\_my\_post\_descriptor;

\textcolor{comment}{// keep file handle alive, so mmap()ed strings are all alive as well}
\textcolor{keyword}{static} \mbox{\hyperlink{group__Eet__File__Group_ga8d9779184a9870c1a225f1f40c76e8a7}{Eet\_File}} *\_my\_cache\_file = NULL;
\textcolor{keyword}{static} \mbox{\hyperlink{group__Eet__File__Group_gaf0984c8280699ca5a3c4966f5580324c}{Eet\_Dictionary}} *\_my\_cache\_dict = NULL;

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_my\_cache\_descriptor\_init(\textcolor{keywordtype}{void})
\{
   \mbox{\hyperlink{struct__Eet__Data__Descriptor__Class}{Eet\_Data\_Descriptor\_Class}} eddc;

   \textcolor{comment}{// The FILE variant is good for caches and things that are just}
   \textcolor{comment}{// appended, but needs to take care when changing strings and files must}
   \textcolor{comment}{// be kept open so mmap()ed strings will be kept alive.}
   \mbox{\hyperlink{group__Eet__Data__Group_ga0bf94c07d8e69cfaac8dd2d615d7542c}{EET\_EINA\_FILE\_DATA\_DESCRIPTOR\_CLASS\_SET}}(&eddc, My\_Cache);
   \_my\_cache\_descriptor = \mbox{\hyperlink{group__Eet__Data__Group_ga2a7251edf1f60ced86c4860207331bb6}{eet\_data\_descriptor\_file\_new}}(&eddc);

   \mbox{\hyperlink{group__Eet__Data__Group_ga0bf94c07d8e69cfaac8dd2d615d7542c}{EET\_EINA\_FILE\_DATA\_DESCRIPTOR\_CLASS\_SET}}(&eddc, My\_Account);
   \_my\_account\_descriptor = \mbox{\hyperlink{group__Eet__Data__Group_ga2a7251edf1f60ced86c4860207331bb6}{eet\_data\_descriptor\_file\_new}}(&eddc);

   \mbox{\hyperlink{group__Eet__Data__Group_ga0bf94c07d8e69cfaac8dd2d615d7542c}{EET\_EINA\_FILE\_DATA\_DESCRIPTOR\_CLASS\_SET}}(&eddc, My\_Message);
   \_my\_message\_descriptor = \mbox{\hyperlink{group__Eet__Data__Group_ga2a7251edf1f60ced86c4860207331bb6}{eet\_data\_descriptor\_file\_new}}(&eddc);

   \mbox{\hyperlink{group__Eet__Data__Group_ga0bf94c07d8e69cfaac8dd2d615d7542c}{EET\_EINA\_FILE\_DATA\_DESCRIPTOR\_CLASS\_SET}}(&eddc, My\_Post);
   \_my\_post\_descriptor = \mbox{\hyperlink{group__Eet__Data__Group_ga2a7251edf1f60ced86c4860207331bb6}{eet\_data\_descriptor\_file\_new}}(&eddc);

   \textcolor{comment}{// Describe the members to be saved:}
   \textcolor{comment}{// Use a temporary macro so we don't type a lot, also avoid errors:}

\textcolor{preprocessor}{#define ADD\_BASIC(member, eet\_type) \(\backslash\)}
\textcolor{preprocessor}{  EET\_DATA\_DESCRIPTOR\_ADD\_BASIC     \(\backslash\)}
\textcolor{preprocessor}{    (\_my\_message\_descriptor, My\_Message, # member, member, eet\_type)}
   ADD\_BASIC(screen\_name, \mbox{\hyperlink{group__Eet__Data__Group_ga0d2cf409bf3f151156d74d18ac59e3d1}{EET\_T\_STRING}});
   ADD\_BASIC(name, \mbox{\hyperlink{group__Eet__Data__Group_ga0d2cf409bf3f151156d74d18ac59e3d1}{EET\_T\_STRING}});
   ADD\_BASIC(message, \mbox{\hyperlink{group__Eet__Data__Group_ga0d2cf409bf3f151156d74d18ac59e3d1}{EET\_T\_STRING}});
   ADD\_BASIC(\textcolor{keywordtype}{id}, \mbox{\hyperlink{group__Eet__Data__Group_gacf6a3c9f06e10e20be5b02cd8c6a220b}{EET\_T\_UINT}});
   ADD\_BASIC(status\_id, \mbox{\hyperlink{group__Eet__Data__Group_gacf6a3c9f06e10e20be5b02cd8c6a220b}{EET\_T\_UINT}});
   ADD\_BASIC(date, \mbox{\hyperlink{group__Eet__Data__Group_gacf6a3c9f06e10e20be5b02cd8c6a220b}{EET\_T\_UINT}});
   ADD\_BASIC(timeline, \mbox{\hyperlink{group__Eet__Data__Group_gacf6a3c9f06e10e20be5b02cd8c6a220b}{EET\_T\_UINT}});
\textcolor{preprocessor}{#undef ADD\_BASIC}

\textcolor{preprocessor}{#define ADD\_BASIC(member, eet\_type) \(\backslash\)}
\textcolor{preprocessor}{  EET\_DATA\_DESCRIPTOR\_ADD\_BASIC     \(\backslash\)}
\textcolor{preprocessor}{    (\_my\_post\_descriptor, My\_Post, # member, member, eet\_type)}
   ADD\_BASIC(dm\_to, \mbox{\hyperlink{group__Eet__Data__Group_ga0d2cf409bf3f151156d74d18ac59e3d1}{EET\_T\_STRING}});
   ADD\_BASIC(message, \mbox{\hyperlink{group__Eet__Data__Group_ga0d2cf409bf3f151156d74d18ac59e3d1}{EET\_T\_STRING}});
\textcolor{preprocessor}{#undef ADD\_BASIC}

\textcolor{preprocessor}{#define ADD\_BASIC(member, eet\_type) \(\backslash\)}
\textcolor{preprocessor}{  EET\_DATA\_DESCRIPTOR\_ADD\_BASIC     \(\backslash\)}
\textcolor{preprocessor}{    (\_my\_account\_descriptor, My\_Account, # member, member, eet\_type)}
   ADD\_BASIC(name, \mbox{\hyperlink{group__Eet__Data__Group_ga0d2cf409bf3f151156d74d18ac59e3d1}{EET\_T\_STRING}});
   ADD\_BASIC(\textcolor{keywordtype}{id}, \mbox{\hyperlink{group__Eet__Data__Group_gacf6a3c9f06e10e20be5b02cd8c6a220b}{EET\_T\_UINT}});
\textcolor{preprocessor}{#undef ADD\_BASIC}

   \mbox{\hyperlink{group__Eet__Data__Group_gabb4c7833ba053f2322e6b667a9ec7f04}{EET\_DATA\_DESCRIPTOR\_ADD\_LIST}}
     (\_my\_account\_descriptor, My\_Account, \textcolor{stringliteral}{"messages"}, messages,
     \_my\_message\_descriptor);
   \mbox{\hyperlink{group__Eet__Data__Group_ga7adad32d2eba76566533353632f00ae4}{EET\_DATA\_DESCRIPTOR\_ADD\_VAR\_ARRAY}}
     (\_my\_account\_descriptor, My\_Account, \textcolor{stringliteral}{"posts"}, posts,
     \_my\_post\_descriptor);

\textcolor{preprocessor}{#define ADD\_BASIC(member, eet\_type) \(\backslash\)}
\textcolor{preprocessor}{  EET\_DATA\_DESCRIPTOR\_ADD\_BASIC     \(\backslash\)}
\textcolor{preprocessor}{    (\_my\_cache\_descriptor, My\_Cache, # member, member, eet\_type)}
   ADD\_BASIC(version, \mbox{\hyperlink{group__Eet__Data__Group_gacf6a3c9f06e10e20be5b02cd8c6a220b}{EET\_T\_UINT}});
\textcolor{preprocessor}{#undef ADD\_BASIC}

   \mbox{\hyperlink{group__Eet__Data__Group_gad5e348ead4ba0b441c1e020f9bf23365}{EET\_DATA\_DESCRIPTOR\_ADD\_HASH}}
     (\_my\_cache\_descriptor, My\_Cache, \textcolor{stringliteral}{"accounts"}, accounts,
     \_my\_account\_descriptor);
\} \textcolor{comment}{/* \_my\_cache\_descriptor\_init */}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_my\_cache\_descriptor\_shutdown(\textcolor{keywordtype}{void})
\{
   \mbox{\hyperlink{group__Eet__Data__Group_ga3e2b601720973d0e1f4683768a593c4d}{eet\_data\_descriptor\_free}}(\_my\_cache\_descriptor);
   \mbox{\hyperlink{group__Eet__Data__Group_ga3e2b601720973d0e1f4683768a593c4d}{eet\_data\_descriptor\_free}}(\_my\_account\_descriptor);
   \mbox{\hyperlink{group__Eet__Data__Group_ga3e2b601720973d0e1f4683768a593c4d}{eet\_data\_descriptor\_free}}(\_my\_message\_descriptor);
   \mbox{\hyperlink{group__Eet__Data__Group_ga3e2b601720973d0e1f4683768a593c4d}{eet\_data\_descriptor\_free}}(\_my\_post\_descriptor);
\} \textcolor{comment}{/* \_my\_cache\_descriptor\_shutdown */}

\textcolor{comment}{// need to check if the pointer came from mmaped area in eet\_dictionary}
\textcolor{comment}{// or it was allocated with eina\_stringshare\_add()}
\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_eet\_string\_free(\textcolor{keyword}{const} \textcolor{keywordtype}{char} *str)
\{
   \textcolor{keywordflow}{if} (!str)
     \textcolor{keywordflow}{return};

   \textcolor{keywordflow}{if} ((\_my\_cache\_dict) && (\mbox{\hyperlink{group__Eet__File__Group_ga53290da52edb929b85e51f25e79d27c8}{eet\_dictionary\_string\_check}}(\_my\_cache\_dict, str)))
     \textcolor{keywordflow}{return};

   \mbox{\hyperlink{group__Eina__Stringshare__Group_ga495411b0bc85b9c0d59ec7255a025260}{eina\_stringshare\_del}}(str);
\} \textcolor{comment}{/* \_eet\_string\_free */}

\textcolor{keyword}{static} My\_Message *
\_my\_message\_new(\textcolor{keyword}{const} \textcolor{keywordtype}{char} *message)
\{
   My\_Message *msg = calloc(1, \textcolor{keyword}{sizeof}(My\_Message));
   \textcolor{keywordflow}{if} (!msg)
     \{
        fprintf(stderr, \textcolor{stringliteral}{"ERROR: could not calloc My\_Message\(\backslash\)n"});
        \textcolor{keywordflow}{return} NULL;
     \}

   msg->message = \mbox{\hyperlink{group__Eina__Stringshare__Group_ga5293082680141132ffcacc28866ef0ef}{eina\_stringshare\_add}}(message);
   \textcolor{keywordflow}{return} msg;
\} \textcolor{comment}{/* \_my\_message\_new */}

\textcolor{keyword}{static} \textcolor{keywordtype}{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);
\} \textcolor{comment}{/* \_my\_message\_free */}

\textcolor{keyword}{static} \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}
\_my\_post\_add(My\_Account *acc,
             \textcolor{keyword}{const} \textcolor{keywordtype}{char} *message)
\{
   \textcolor{keywordtype}{int} new\_count = acc->posts\_count + 1;
   My\_Post *post = realloc(acc->posts, new\_count * \textcolor{keyword}{sizeof}(My\_Post));
   \textcolor{keywordflow}{if} (!post)
     \{
        fprintf(stderr, \textcolor{stringliteral}{"ERROR: could add My\_Post\(\backslash\)n"});
        \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}};
     \}

   post[acc->posts\_count].message = \mbox{\hyperlink{group__Eina__Stringshare__Group_ga5293082680141132ffcacc28866ef0ef}{eina\_stringshare\_add}}(message);
   post[acc->posts\_count].dm\_to = NULL;
   acc->posts\_count = new\_count;
   acc->posts = post;
   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};
\} \textcolor{comment}{/* \_my\_post\_new */}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_my\_post\_free(My\_Post *post)
\{
   \_eet\_string\_free(post->dm\_to);
   \_eet\_string\_free(post->message);
\} \textcolor{comment}{/* \_my\_post\_free */}

\textcolor{keyword}{static} My\_Account *
\_my\_account\_new(\textcolor{keyword}{const} \textcolor{keywordtype}{char} *name)
\{
   My\_Account *acc = calloc(1, \textcolor{keyword}{sizeof}(My\_Account));
   \textcolor{keywordflow}{if} (!acc)
     \{
        fprintf(stderr, \textcolor{stringliteral}{"ERROR: could not calloc My\_Account\(\backslash\)n"});
        \textcolor{keywordflow}{return} NULL;
     \}

   acc->name = \mbox{\hyperlink{group__Eina__Stringshare__Group_ga5293082680141132ffcacc28866ef0ef}{eina\_stringshare\_add}}(name);
   \textcolor{keywordflow}{return} acc;
\} \textcolor{comment}{/* \_my\_account\_new */}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_my\_account\_free(My\_Account *acc)
\{
   My\_Message *m;
   \textcolor{keywordtype}{int} i;

   \_eet\_string\_free(acc->name);

   \mbox{\hyperlink{group__Eina__List__Group_gae27a2c686a0a11693f9b06957c9bba83}{EINA\_LIST\_FREE}}(acc->messages, m)
     \_my\_message\_free(m);

   \textcolor{keywordflow}{for} (i = 0; i < acc->posts\_count; i++)
     \_my\_post\_free(&acc->posts[i]);
   free(acc->posts);

   free(acc);
\} \textcolor{comment}{/* \_my\_account\_free */}

\textcolor{keyword}{static} My\_Cache *
\_my\_cache\_new(\textcolor{keywordtype}{void})
\{
   My\_Cache *my\_cache = calloc(1, \textcolor{keyword}{sizeof}(My\_Cache));
   \textcolor{keywordflow}{if} (!my\_cache)
     \{
        fprintf(stderr, \textcolor{stringliteral}{"ERROR: could not calloc My\_Cache\(\backslash\)n"});
        \textcolor{keywordflow}{return} NULL;
     \}

   my\_cache->accounts = \mbox{\hyperlink{group__Eina__Hash__Group_gaad2e43204f79ee5a8f19470ff5ba4ac8}{eina\_hash\_string\_small\_new}}(NULL);

   my\_cache->version = 1;
   \textcolor{keywordflow}{return} my\_cache;
\} \textcolor{comment}{/* \_my\_cache\_new */}

\textcolor{keyword}{static} \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}
\_my\_cache\_account\_free\_cb(\textcolor{keyword}{const} \mbox{\hyperlink{group__Eina__Hash__Group_ga9a89d7d3ee260d3a60572ebf651b07e0}{Eina\_Hash}} *hash \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},
                          \textcolor{keyword}{const} \textcolor{keywordtype}{void}      *key \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},
                          \textcolor{keywordtype}{void}            *data,
                          \textcolor{keywordtype}{void}            *fdata \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}})
\{
   \_my\_account\_free(data);
   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_my\_cache\_free(My\_Cache *my\_cache)
\{
   \mbox{\hyperlink{group__Eina__Hash__Group_ga2c23f8425d3afcc61827ab67b46d7190}{eina\_hash\_foreach}}(my\_cache->accounts, \_my\_cache\_account\_free\_cb, NULL);
   \mbox{\hyperlink{group__Eina__Hash__Group_gadb6acb720ce267cc1cd4f51893539f59}{eina\_hash\_free}}(my\_cache->accounts);
   free(my\_cache);
\} \textcolor{comment}{/* \_my\_cache\_free */}

\textcolor{keyword}{static} My\_Account *
\_my\_cache\_account\_find(My\_Cache   *my\_cache,
                       \textcolor{keyword}{const} \textcolor{keywordtype}{char} *name)
\{
   \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Eina__Hash__Group_ga45703d00fbabffc86e6836ffe98a4c92}{eina\_hash\_find}}(my\_cache->accounts, name);
\} \textcolor{comment}{/* \_my\_cache\_account\_find */}

\textcolor{keyword}{static} My\_Cache *
\_my\_cache\_load(\textcolor{keyword}{const} \textcolor{keywordtype}{char} *filename)
\{
   My\_Cache *my\_cache;
   \mbox{\hyperlink{group__Eet__File__Group_ga8d9779184a9870c1a225f1f40c76e8a7}{Eet\_File}} *ef = \mbox{\hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}}(filename, \mbox{\hyperlink{group__Eet__File__Group_ggaa8e1a7af50fefaf15eb2f69543d24998a416f7e9a76b8131a44bbeedb13060c81}{EET\_FILE\_MODE\_READ}});
   \textcolor{keywordflow}{if} (!ef)
     \{
        fprintf(stderr, \textcolor{stringliteral}{"ERROR: could not open '%s' for read\(\backslash\)n"}, filename);
        \textcolor{keywordflow}{return} NULL;
     \}

   my\_cache = \mbox{\hyperlink{group__Eet__Data__Group_ga4d1807cbbc448304f22a8808318a3429}{eet\_data\_read}}(ef, \_my\_cache\_descriptor, MY\_CACHE\_FILE\_ENTRY);
   \textcolor{keywordflow}{if} (!my\_cache)
     \{
        \mbox{\hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}}(ef);
        \textcolor{keywordflow}{return} NULL;
     \}

   \textcolor{keywordflow}{if} (my\_cache->version < 1)
     \{
        fprintf(stderr,
                \textcolor{stringliteral}{"WARNING: version %#x was too old, upgrading it to %#x\(\backslash\)n"},
                my\_cache->version, 1);

        my\_cache->version = 1;
     \}

   \textcolor{keywordflow}{if} (\_my\_cache\_file)
     \mbox{\hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}}(\_my\_cache\_file);

   \_my\_cache\_file = ef;
   \_my\_cache\_dict = \mbox{\hyperlink{group__Eet__File__Group_gab45ad39596d2fb54a88fda93e7fa0b0f}{eet\_dictionary\_get}}(ef);

   \textcolor{keywordflow}{return} my\_cache;
\} \textcolor{comment}{/* \_my\_cache\_load */}

\textcolor{keyword}{static} \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}
\_my\_cache\_save(\textcolor{keyword}{const} My\_Cache *my\_cache,
               \textcolor{keyword}{const} \textcolor{keywordtype}{char}     *filename)
\{
   \textcolor{keywordtype}{char} tmp[PATH\_MAX];
   \mbox{\hyperlink{group__Eet__File__Group_ga8d9779184a9870c1a225f1f40c76e8a7}{Eet\_File}} *ef;
   \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}} ret;
   \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} i, len;
   \textcolor{keyword}{struct }stat st;

   len = \mbox{\hyperlink{group__Eina__String__Group_ga6bda31d2e1b6d2737b7fe1784780cd2f}{eina\_strlcpy}}(tmp, filename, \textcolor{keyword}{sizeof}(tmp));
   \textcolor{keywordflow}{if} (len + 12 >= (\textcolor{keywordtype}{int})\textcolor{keyword}{sizeof}(tmp))
     \{
        fprintf(stderr, \textcolor{stringliteral}{"ERROR: file name is too big: %s\(\backslash\)n"}, filename);
        \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}};
     \}

   i = 0;
   \textcolor{keywordflow}{do}
     \{
        snprintf(tmp + len, 12, \textcolor{stringliteral}{".%u"}, i);
        i++;
     \}
   \textcolor{keywordflow}{while} (stat(tmp, &st) == 0);

   ef = \mbox{\hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}}(tmp, \mbox{\hyperlink{group__Eet__File__Group_ggaa8e1a7af50fefaf15eb2f69543d24998a8f395f56fba48be007c65be51d799ee9}{EET\_FILE\_MODE\_WRITE}});
   \textcolor{keywordflow}{if} (!ef)
     \{
        fprintf(stderr, \textcolor{stringliteral}{"ERROR: could not open '%s' for write\(\backslash\)n"}, tmp);
        \textcolor{keywordflow}{return} \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}};
     \}

   ret = \mbox{\hyperlink{group__Eet__Data__Group_gac66d1c49e3490bc5d803982d833ff8aa}{eet\_data\_write}}
       (ef, \_my\_cache\_descriptor, MY\_CACHE\_FILE\_ENTRY, my\_cache, \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}});

   \textcolor{comment}{// VERY IMPORTANT NOTE:}
   \textcolor{comment}{// after eet\_close(), all strings mmaped from file will be GONE, invalid!}
   \textcolor{comment}{// you'll need to free the old cache and open the new one.}
   \textcolor{comment}{// For cache this is okay, as you should be saving not so often or just}
   \textcolor{comment}{// at end.}
   \textcolor{comment}{//}
   \textcolor{comment}{// This is a trade off, you save memory by using mmap()ed strings, but}
   \textcolor{comment}{// you have to care about this.}
   \mbox{\hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}}(ef);

   \textcolor{keywordflow}{if} (ret)
     \{
        unlink(filename);
        rename(tmp, filename);
     \}

   \textcolor{keywordflow}{return} ret;
\} \textcolor{comment}{/* \_my\_cache\_save */}

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{int}   argc,
     \textcolor{keywordtype}{char} *argv[])
\{
   My\_Cache *my\_cache;
   \mbox{\hyperlink{struct__Eina__Iterator}{Eina\_Iterator}} *it;
   My\_Account *acc;
   \textcolor{keywordtype}{int} ret = 0;

   \textcolor{keywordflow}{if} (argc < 3)
     \{
        fprintf(stderr,
                \textcolor{stringliteral}{"Usage:\(\backslash\)n\(\backslash\)t%s <input> <output> [action] [action-params]\(\backslash\)n\(\backslash\)n"}
                \textcolor{stringliteral}{"Where actions and their parameters:\(\backslash\)n"}
                \textcolor{stringliteral}{"\(\backslash\)tacc <name>\(\backslash\)n"}
                \textcolor{stringliteral}{"\(\backslash\)tpost <account-name> <message>\(\backslash\)n"}
                \textcolor{stringliteral}{"\(\backslash\)tmessage <account-name> <message>\(\backslash\)n"}
                \textcolor{stringliteral}{"\(\backslash\)n"},
                argv[0]);
        \textcolor{keywordflow}{return} -1;
     \}

   \mbox{\hyperlink{group__Eina__Main__Group_gafee4bfc0e509b6ffed6598edf6f4b3de}{eina\_init}}();
   \mbox{\hyperlink{group__Eet__Group_ga9903687bfa20fe9a9e59d4c1d213e89a}{eet\_init}}();
   \_my\_cache\_descriptor\_init();

   my\_cache = \_my\_cache\_load(argv[1]);
   \textcolor{keywordflow}{if} (!my\_cache)
     \{
        printf(\textcolor{stringliteral}{"creating new cache.\(\backslash\)n"});
        my\_cache = \_my\_cache\_new();
        \textcolor{keywordflow}{if} (!my\_cache)
          \{
             ret = -2;
             \textcolor{keywordflow}{goto} end;
          \}
     \}

   \textcolor{keywordflow}{if} (argc > 3)
     \{
        \textcolor{keywordflow}{if} (strcmp(argv[3], \textcolor{stringliteral}{"acc"}) == 0)
          \{
             \textcolor{keywordflow}{if} (argc == 5)
               \{
                  My\_Account *acc\_ = \_my\_cache\_account\_find(my\_cache, argv[4]);
                  \textcolor{keywordflow}{if} (!acc\_)
                    \{
                       acc\_ = \_my\_account\_new(argv[4]);
                       \mbox{\hyperlink{group__Eina__Hash__Group_ga50aa7d4303e5efbe8dc7f593b59b9f63}{eina\_hash\_direct\_add}}(my\_cache->accounts, acc\_->name, acc\_);
                    \}
                  \textcolor{keywordflow}{else}
                    fprintf(stderr, \textcolor{stringliteral}{"ERROR: account '%s' already exists.\(\backslash\)n"},
                            argv[4]);
               \}
             \textcolor{keywordflow}{else}
               fprintf(stderr,
                       \textcolor{stringliteral}{"ERROR: wrong number of parameters (%d).\(\backslash\)n"},
                       argc);
          \}
        \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (strcmp(argv[3], \textcolor{stringliteral}{"post"}) == 0)
          \{
             \textcolor{keywordflow}{if} (argc == 6)
               \{
                  My\_Account *acc\_ = \_my\_cache\_account\_find(my\_cache, argv[4]);
                  \textcolor{keywordflow}{if} (acc\_)
                    \{
                       \_my\_post\_add(acc\_, argv[5]);
                    \}
                  \textcolor{keywordflow}{else}
                    fprintf(stderr, \textcolor{stringliteral}{"ERROR: unknown account: '%s'\(\backslash\)n"}, argv[4]);
               \}
             \textcolor{keywordflow}{else}
               fprintf(stderr,
                       \textcolor{stringliteral}{"ERROR: wrong number of parameters (%d).\(\backslash\)n"},
                       argc);
          \}
        \textcolor{keywordflow}{else} \textcolor{keywordflow}{if} (strcmp(argv[3], \textcolor{stringliteral}{"message"}) == 0)
          \{
             \textcolor{keywordflow}{if} (argc == 6)
               \{
                  My\_Account *acc\_ = \_my\_cache\_account\_find(my\_cache, argv[4]);
                  \textcolor{keywordflow}{if} (acc\_)
                    \{
                       My\_Message *msg = \_my\_message\_new(argv[5]);
                       acc\_->messages = \mbox{\hyperlink{group__Eina__List__Group_ga92ec14d1a1040a361c4d8cd0ab7b0ecc}{eina\_list\_append}}(acc\_->messages, msg);
                    \}
                  \textcolor{keywordflow}{else}
                    fprintf(stderr, \textcolor{stringliteral}{"ERROR: unknown account: '%s'\(\backslash\)n"}, argv[4]);
               \}
             \textcolor{keywordflow}{else}
               fprintf(stderr,
                       \textcolor{stringliteral}{"ERROR: wrong number of parameters (%d).\(\backslash\)n"},
                       argc);
          \}
        \textcolor{keywordflow}{else}
          fprintf(stderr, \textcolor{stringliteral}{"ERROR: unknown action '%s'\(\backslash\)n"}, argv[2]);
     \}

   printf(\textcolor{stringliteral}{"My\_Cache:\(\backslash\)n"}
          \textcolor{stringliteral}{"\(\backslash\)tversion.: %#x\(\backslash\)n"}
          \textcolor{stringliteral}{"\(\backslash\)taccounts: %u\(\backslash\)n"},
          my\_cache->version,
          \mbox{\hyperlink{group__Eina__Hash__Group_ga9289542f6724083eab306e5bb3ab10b0}{eina\_hash\_population}}(my\_cache->accounts));
   it = \mbox{\hyperlink{group__Eina__Hash__Group_ga5191350d6044d93b6518a957f12f5ac3}{eina\_hash\_iterator\_data\_new}}(my\_cache->accounts);
   \mbox{\hyperlink{group__Eina__Iterator__Group_gac7314580e18868ef229f5d2814562794}{EINA\_ITERATOR\_FOREACH}}(it, acc)
     \{
        printf(\textcolor{stringliteral}{"\(\backslash\)t  > %-#8x '%.20s' stats: m=%u, p=%u\(\backslash\)n"},
               acc->id, acc->name ? acc->name : \textcolor{stringliteral}{""},
               \mbox{\hyperlink{group__Eina__List__Group_ga301729054a498a2df09a5f1dd760697e}{eina\_list\_count}}(acc->messages),
               acc->posts\_count);

        \textcolor{keywordflow}{if} (\mbox{\hyperlink{group__Eina__List__Group_ga301729054a498a2df09a5f1dd760697e}{eina\_list\_count}}(acc->messages))
          \{
             \textcolor{keyword}{const} \mbox{\hyperlink{struct__Eina__List}{Eina\_List}} *l;
             \textcolor{keyword}{const} My\_Message *msg;
             printf(\textcolor{stringliteral}{"\(\backslash\)t  |messages:\(\backslash\)n"});

             \mbox{\hyperlink{group__Eina__List__Group_gac6efafc19f526d916cdb9757240a8773}{EINA\_LIST\_FOREACH}}(acc->messages, l, msg)
               \{
                  printf(\textcolor{stringliteral}{"\(\backslash\)t  |   %-8x '%s' [%s]: '%.20s'\(\backslash\)n"},
                         msg->id,
                         msg->name ? msg->name : \textcolor{stringliteral}{""},
                         msg->screen\_name ? msg->screen\_name : \textcolor{stringliteral}{""},
                         msg->message ? msg->message : \textcolor{stringliteral}{""});
               \}
          \}

        \textcolor{keywordflow}{if} (acc->posts\_count)
          \{
             \textcolor{keyword}{const} My\_Post *post;
             \textcolor{keywordtype}{int} i;
             printf(\textcolor{stringliteral}{"\(\backslash\)t  |posts:\(\backslash\)n"});

             \textcolor{keywordflow}{for} (i = 0; i < acc->posts\_count; i++)
               \{
                  post = &acc->posts[i];
                  \textcolor{keywordflow}{if} (post->dm\_to)
                    printf(\textcolor{stringliteral}{"\(\backslash\)t  |  @%s: '%.20s'\(\backslash\)n"}, post->dm\_to, post->message);
                  \textcolor{keywordflow}{else}
                    printf(\textcolor{stringliteral}{"\(\backslash\)t  |  '%.20s'\(\backslash\)n"}, post->message);
               \}
          \}

        printf(\textcolor{stringliteral}{"\(\backslash\)n"});
     \}
   \mbox{\hyperlink{group__Eina__Iterator__Group_gaf9261991a4b05dc63e3401278a3281f8}{eina\_iterator\_free}}(it);

   \textcolor{keywordflow}{if} (!\_my\_cache\_save(my\_cache, argv[2]))
     ret = -3;

   \_my\_cache\_free(my\_cache);

end:
   \textcolor{keywordflow}{if} (\_my\_cache\_file)
     \mbox{\hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}}(\_my\_cache\_file);
   \_my\_cache\_descriptor\_shutdown();
   \mbox{\hyperlink{group__Eet__Group_gacbc7583179399d3b3e7b003e911f866f}{eet\_shutdown}}();
   \mbox{\hyperlink{group__Eina__Main__Group_ga2c8b4020427108a30c44707af22c99d3}{eina\_shutdown}}();

   \textcolor{keywordflow}{return} ret;
\} \textcolor{comment}{/* main */}

\end{DoxyCodeInclude}
 