\hypertarget{eet-data-simple_8c-example}{\section{eet-\/data-\/simple.\+c}
}

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

\textcolor{preprocessor}{#include <\hyperlink{Eina_8h}{Eina.h}>}
\textcolor{preprocessor}{#include <\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}{// The struct that will be loaded and saved.}
\textcolor{comment}{// note that only the members described in the eet\_data\_descriptor}
\textcolor{comment}{// will be automatically handled. The other members will have their}
\textcolor{comment}{// space reserved and zeroed (as it uses calloc()), but not}
\textcolor{comment}{// saved or loaded from eet files.}
\textcolor{keyword}{typedef} \textcolor{keyword}{struct}
\{
   \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} version; \textcolor{comment}{// it is recommended to use versioned configuration!}
   \textcolor{keyword}{const} \textcolor{keywordtype}{char}  *name;
   \textcolor{keywordtype}{int}          id;
   \textcolor{keywordtype}{int}          not\_saved\_value; \textcolor{comment}{// example of not saved data inside!}
   \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}    enabled;
\} My\_Conf\_Type;

\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\_CONF\_FILE\_ENTRY[] = \textcolor{stringliteral}{"config"};

\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} \hyperlink{group__Eet__Data__Group_ga4baec0e840012480a0958c5c3bca276d}{Eet\_Data\_Descriptor} *\_my\_conf\_descriptor;

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

   \textcolor{comment}{// The class describe the functions to use to create the type and its}
   \textcolor{comment}{// full allocated size.}
   \textcolor{comment}{//}
   \textcolor{comment}{// Eina types are very convenient, so use them to create the descriptor,}
   \textcolor{comment}{// so we get eina\_list,  eina\_hash and eina\_stringshare automatically!}
   \textcolor{comment}{//}
   \textcolor{comment}{// The STREAM variant is better for configuration files as the values}
   \textcolor{comment}{// will likely change a lot.}
   \textcolor{comment}{//}
   \textcolor{comment}{// The other variant, FILE, 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.}
   \hyperlink{group__Eet__Data__Group_ga941b1fcd6c6c72221d9390f9a0ed751c}{EET\_EINA\_STREAM\_DATA\_DESCRIPTOR\_CLASS\_SET}(&eddc, My\_Conf\_Type);
   \_my\_conf\_descriptor = \hyperlink{group__Eet__Data__Group_gae300b4a97dbaab30a200edbb97f108c1}{eet\_data\_descriptor\_stream\_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 MY\_CONF\_ADD\_BASIC(member, eet\_type) \(\backslash\)}
\textcolor{preprocessor}{  EET\_DATA\_DESCRIPTOR\_ADD\_BASIC             \(\backslash\)}
\textcolor{preprocessor}{    (\_my\_conf\_descriptor, My\_Conf\_Type, # member, member, eet\_type)}

   MY\_CONF\_ADD\_BASIC(version, \hyperlink{group__Eet__Data__Group_gacf6a3c9f06e10e20be5b02cd8c6a220b}{EET\_T\_UINT});
   MY\_CONF\_ADD\_BASIC(name, \hyperlink{group__Eet__Data__Group_ga0d2cf409bf3f151156d74d18ac59e3d1}{EET\_T\_STRING});
   MY\_CONF\_ADD\_BASIC(\textcolor{keywordtype}{id}, \hyperlink{group__Eet__Data__Group_ga4e196733795867f14c6e387af7287baa}{EET\_T\_INT});
   MY\_CONF\_ADD\_BASIC(enabled, \hyperlink{group__Eet__Data__Group_ga09a1341cc158fda0ba60ad2386ecd795}{EET\_T\_UCHAR});

\textcolor{preprocessor}{#undef MY\_CONF\_ADD\_BASIC}
\} \textcolor{comment}{/* \_my\_conf\_descriptor\_init */}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_my\_conf\_descriptor\_shutdown(\textcolor{keywordtype}{void})
\{
   \hyperlink{group__Eet__Data__Group_ga3e2b601720973d0e1f4683768a593c4d}{eet\_data\_descriptor\_free}(\_my\_conf\_descriptor);
\} \textcolor{comment}{/* \_my\_conf\_descriptor\_shutdown */}

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

   my\_conf->version = 0x112233;
   my\_conf->enabled = \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};
   \textcolor{keywordflow}{return} my\_conf;
\} \textcolor{comment}{/* \_my\_conf\_new */}

\textcolor{keyword}{static} \textcolor{keywordtype}{void}
\_my\_conf\_free(My\_Conf\_Type *my\_conf)
\{
   \hyperlink{group__Eina__Stringshare__Group_ga4453d2aff2b325c7b45048f30df9b1f5}{eina\_stringshare\_del}(my\_conf->name);
   free(my\_conf);
\} \textcolor{comment}{/* \_my\_conf\_free */}

\textcolor{keyword}{static} My\_Conf\_Type *
\_my\_conf\_load(\textcolor{keyword}{const} \textcolor{keywordtype}{char} *filename)
\{
   My\_Conf\_Type *my\_conf;
   \hyperlink{group__Eet__File__Group_ga8d9779184a9870c1a225f1f40c76e8a7}{Eet\_File} *ef = \hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}(filename, \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\_conf = \hyperlink{group__Eet__Data__Group_ga4d1807cbbc448304f22a8808318a3429}{eet\_data\_read}(ef, \_my\_conf\_descriptor, MY\_CONF\_FILE\_ENTRY);
   \textcolor{keywordflow}{if} (!my\_conf)
     \textcolor{keywordflow}{goto} end;

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

        my\_conf->version = 0x112233;
        my\_conf->enabled = \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};
     \}

end:
   \hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}(ef);
   \textcolor{keywordflow}{return} my\_conf;
\} \textcolor{comment}{/* \_my\_conf\_load */}

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

   len = \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} \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 = \hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}(tmp, \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} \hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE};
     \}

   ret = \hyperlink{group__Eet__Data__Group_gac66d1c49e3490bc5d803982d833ff8aa}{eet\_data\_write}
       (ef, \_my\_conf\_descriptor, MY\_CONF\_FILE\_ENTRY, my\_conf, \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE});
   \hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}(ef);

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

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

\textcolor{keywordtype}{int}
main(\textcolor{keywordtype}{int}   argc,
     \textcolor{keywordtype}{char} *argv[])
\{
   My\_Conf\_Type *my\_conf;
   \textcolor{keywordtype}{int} ret = 0;

   \textcolor{keywordflow}{if} (argc != 3)
     \{
        fprintf(stderr, \textcolor{stringliteral}{"Usage:\(\backslash\)n\(\backslash\)t%s <input> <output>\(\backslash\)n\(\backslash\)n"}, argv[0]);
        \textcolor{keywordflow}{return} -1;
     \}

   \hyperlink{group__Eina__Main__Group_gabf448ada5bc81a07342b66c6f818ee5e}{eina\_init}();
   \hyperlink{group__Eet__Group_ga9903687bfa20fe9a9e59d4c1d213e89a}{eet\_init}();
   \_my\_conf\_descriptor\_init();

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

   printf(\textcolor{stringliteral}{"My\_Conf\_Type:\(\backslash\)n"}
          \textcolor{stringliteral}{"\(\backslash\)tversion: %#x\(\backslash\)n"}
          \textcolor{stringliteral}{"\(\backslash\)tname...: '%s'\(\backslash\)n"}
          \textcolor{stringliteral}{"\(\backslash\)tid.....: %d\(\backslash\)n"}
          \textcolor{stringliteral}{"\(\backslash\)tenabled: %hhu\(\backslash\)n"},
          my\_conf->version,
          my\_conf->name ? my\_conf->name : \textcolor{stringliteral}{""},
          my\_conf->id,
          my\_conf->enabled);

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

   \_my\_conf\_free(my\_conf);

end:
   \_my\_conf\_descriptor\_shutdown();
   \hyperlink{group__Eet__Group_gacbc7583179399d3b3e7b003e911f866f}{eet\_shutdown}();
   \hyperlink{group__Eina__Main__Group_ga6741fcaba116e31e96ce14dcfa03f568}{eina\_shutdown}();

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

\end{DoxyCodeInclude}
 