A real life example is usually the best way to see how things are used, but they also involve a lot more code than what needs to be shown, so instead of going that way, we\textquotesingle{}ll be borrowing some pieces from one in the following example.

It\textquotesingle{}s been slightly modified from the original source to show more of the varied ways in which Eet can handle our data.

\mbox{\hyperlink{eet-data-file_descriptor_01_8c-example}{This example}} shows a cache of user accounts and messages received, and it\textquotesingle{}s a bit more interactive than previous examples.

Let\textquotesingle{}s begin by looking at the structures we\textquotesingle{}ll be using. First we have one to define the messages the user receives and one for the one he posts. Straight forward and nothing new here.  
\begin{DoxyCodeInclude}
\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;

\end{DoxyCodeInclude}


One more to declare the account itself. This one will contain a list of all messages received, and the posts we make ourselves will be kept in an array. No special reason other than to show how to use arrays with Eet. 
\begin{DoxyCodeInclude}

\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;

\end{DoxyCodeInclude}


Finally, the main structure to hold our cache of accounts. We\textquotesingle{}ll be looking for these accounts by their names, so let\textquotesingle{}s keep them in a hash, using that name as the key. 
\begin{DoxyCodeInclude}

\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;

\end{DoxyCodeInclude}


As explained before, we need one descriptor for each struct we want Eet to handle, but this time we also want to keep around our Eet file and its string dictionary. You will see why in a moment. 
\begin{DoxyCodeInclude}
\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{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;

\end{DoxyCodeInclude}


The differences begin now. They aren\textquotesingle{}t much, but we\textquotesingle{}ll be creating our descriptors differently. Things can be added to our cache, but we won\textquotesingle{}t be modifying the current contents, so we can consider the data read from it to be read-\/only, and thus allow Eet to save time and memory by not duplicating thins unnecessary. 
\begin{DoxyCodeInclude}
\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);

\end{DoxyCodeInclude}


As the comment in the code explains, we are asking Eet to give us strings directly from the mapped file, which avoids having to load it in memory and data duplication. Of course, there are things to take into account when doing things this way, and they will be mentioned as we encounter those special cases.

Next comes the actual description of our data, just like we did in the previous examples. 
\begin{DoxyCodeInclude}
\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\)}
  \mbox{\hyperlink{group__Eet__Data__Group_gac38ce778d7eba37cfca8a19efb7a9b3a}{EET\_DATA\_DESCRIPTOR\_ADD\_BASIC}}     \(\backslash\)
    (\_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}

\end{DoxyCodeInclude}


And the account struct\textquotesingle{}s description doesn\textquotesingle{}t add much new, but it\textquotesingle{}s worth commenting on it. 
\begin{DoxyCodeInclude}
\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);

\end{DoxyCodeInclude}


How to add a list we\textquotesingle{}ve seen before, but now we are also adding an array. There\textquotesingle{}s nothing really special about it, but it\textquotesingle{}s important to note that the E\+E\+T\+\_\+\+D\+A\+T\+A\+\_\+\+D\+E\+S\+C\+R\+I\+P\+T\+O\+R\+\_\+\+A\+D\+D\+\_\+\+V\+A\+R\+\_\+\+A\+R\+R\+AY is used to add arrays of variable length to a descriptor. That is, arrays just like the one we defined. Since there\textquotesingle{}s no way in C to know how long they are, we need to keep track of the count ourselves and Eet needs to know how to do so as well. That\textquotesingle{}s what the {\ttfamily posts\+\_\+count} member of our struct is for. When adding our array member, this macro will look for another variable in the struct named just like the array, but with {\ttfamily \+\_\+count} attached to the end. When saving our data, Eet will know how many elements the array contains by looking into this count variable. When loading back from a file, this variable will be set to the right number of elements.

Another option for arrays is to use E\+E\+T\+\_\+\+D\+A\+T\+A\+\_\+\+D\+E\+S\+C\+R\+I\+P\+T\+O\+R\+\_\+\+A\+D\+D\+\_\+\+A\+R\+R\+AY, which takes care of fixed sized arrays. For example, let\textquotesingle{}s suppose that we want to keep track of only the last ten posts the user sent, and we declare our account struct as follows 
\begin{DoxyCode}
\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[10];
\} My\_Account;
\end{DoxyCode}
 Then we would add the array to our descriptor with 
\begin{DoxyCode}
\mbox{\hyperlink{group__Eet__Data__Group_gabc903baa4d5dc50a98f5e1a68d66fd2b}{EET\_DATA\_DESCRIPTOR\_ADD\_ARRAY}}(\_my\_account\_descriptor, My\_Account, \textcolor{stringliteral}{"posts"},
                              posts, \_my\_post\_descriptor);
\end{DoxyCode}


Notice how this time we don\textquotesingle{}t have a {\ttfamily posts\+\_\+count} variable in our struct. We could have it for the program to keep track of how many posts the array actually contains, but Eet no longer needs it. Being defined that way the array is already taking up all the memory needed for the ten elements, and it is possible in C to determine how much it is in code. When saving our data, Eet will just dump the entire memory blob into the file, regardless of how much of it is really used. So it\textquotesingle{}s important to take into consideration this kind of things when defining your data types. Each has its uses, its advantages and disadvantages and it\textquotesingle{}s up to you to decide which to use.

Now, going back to our example, we have to finish adding our data to the descriptors. We are only missing the main one for the cache, which contains our hash of accounts. Unless you are using your own hash functions when setting the descriptor class, always use hashes with string type keys. 
\begin{DoxyCodeInclude}
\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 */}

\end{DoxyCodeInclude}


If you remember, we told Eet not to duplicate memory when possible at the time of loading back our data. But this doesn\textquotesingle{}t mean everything will be loaded straight from disk and we don\textquotesingle{}t have to worry about freeing it. Data in the Eet file is compressed and encoded, so it still needs to be decoded and memory will be allocated to convert it back into something we can use. We also need to take care of anything we add in the current instance of the program. To summarize, any string we get from Eet is likely to be a pointer to the internal dictionary, and trying to free it will, in the best case, crash our application right away.

So how do we know if we have to free a string? We check if it\textquotesingle{}s part of the dictionary, and if it\textquotesingle{}s not there we can be sure it\textquotesingle{}s safe to get rid of it. 
\begin{DoxyCodeInclude}
\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 */}

\end{DoxyCodeInclude}


See how this is used when adding a new message to our cache. 
\begin{DoxyCodeInclude}
\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 */}

\end{DoxyCodeInclude}


Skipping all the utility functions used by our program (remember you can look at the full example \mbox{\hyperlink{eet-data-file_descriptor_01_8c-example}{here}}) we get to our cache loading code. Nothing out of the ordinary at first, just the same old open file, read data using our main descriptor to decode it into something we can use and check version of loaded data and if it doesn\textquotesingle{}t match, do something accordingly. 
\begin{DoxyCodeInclude}
\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}};
\}

\end{DoxyCodeInclude}


Then comes the interesting part. Remember how we kept two more global variables with our descriptors? One of them we already used to check if it was right to free a string or not, but we didn\textquotesingle{}t know where it came from. Loading our data straight from the mmapped file means that we can\textquotesingle{}t close it until we are done using it, so we need to keep its handler around until then. It also means that any changes done to the file can, and will, invalidate all our pointers to the file backed data, so if we add something and save the file, we need to reload our cache.

Thus our load function checks if we had an open file, if there is it gets closed and our variable is updated to the new handler. Then we get the string dictionary we use to check if a string is part of it or not. Updating any references to the cache data is up you as a programmer to handle properly, there\textquotesingle{}s nothing Eet can do in this situation. 
\begin{DoxyCodeInclude}

\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 */}

\end{DoxyCodeInclude}


The save function doesn\textquotesingle{}t have anything new, and all that\textquotesingle{}s left after it is the main program, which doesn\textquotesingle{}t really have anything of interest within the scope of what we are learning. 