
\begin{DoxyCodeInclude}
1 \textcolor{comment}{//Compile with:}
2 \textcolor{comment}{// gcc -o eet-data-simple eet-data-simple.c `pkg-config --cflags --libs eet eina`}
3 
4 \textcolor{preprocessor}{#include <\hyperlink{Eina_8h}{Eina.h}>}
5 \textcolor{preprocessor}{#include <\hyperlink{Eet_8h}{Eet.h}>}
6 \textcolor{preprocessor}{#include <stdio.h>}
7 \textcolor{preprocessor}{#include <limits.h>}
8 \textcolor{preprocessor}{#include <sys/types.h>}
9 \textcolor{preprocessor}{#include <sys/stat.h>}
10 \textcolor{preprocessor}{#include <unistd.h>}
11 
12 \textcolor{comment}{// The struct that will be loaded and saved.}
13 \textcolor{comment}{// note that only the members described in the eet\_data\_descriptor}
14 \textcolor{comment}{// will be automatically handled. The other members will have their}
15 \textcolor{comment}{// space reserved and zeroed (as it uses calloc()), but not}
16 \textcolor{comment}{// saved or loaded from eet files.}
17 \textcolor{keyword}{typedef} \textcolor{keyword}{struct}
18 \{
19    \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} version; \textcolor{comment}{// it is recommended to use versioned configuration!}
20    \textcolor{keyword}{const} \textcolor{keywordtype}{char}  *name;
21    \textcolor{keywordtype}{int}          id;
22    \textcolor{keywordtype}{int}          not\_saved\_value; \textcolor{comment}{// example of not saved data inside!}
23    \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}    enabled;
24 \} My\_Conf\_Type;
25 
26 \textcolor{comment}{// string that represents the entry in eet file, you might like to have}
27 \textcolor{comment}{// different profiles or so in the same file, this is possible with}
28 \textcolor{comment}{// different strings}
29 \textcolor{keyword}{static} \textcolor{keyword}{const} \textcolor{keywordtype}{char} MY\_CONF\_FILE\_ENTRY[] = \textcolor{stringliteral}{"config"};
30 
31 \textcolor{comment}{// keep the descriptor static global, so it can be}
32 \textcolor{comment}{// shared by different functions (load/save) of this and only this}
33 \textcolor{comment}{// file.}
34 \textcolor{keyword}{static} \hyperlink{group__Eet__Data__Group_ga4baec0e840012480a0958c5c3bca276d}{Eet\_Data\_Descriptor} *\_my\_conf\_descriptor;
35 
36 \textcolor{keyword}{static} \textcolor{keywordtype}{void}
37 \_my\_conf\_descriptor\_init(\textcolor{keywordtype}{void})
38 \{
39    \hyperlink{struct__Eet__Data__Descriptor__Class}{Eet\_Data\_Descriptor\_Class} eddc;
40 
41    \textcolor{comment}{// The class describe the functions to use to create the type and its}
42    \textcolor{comment}{// full allocated size.}
43    \textcolor{comment}{//}
44    \textcolor{comment}{// Eina types are very convenient, so use them to create the descriptor,}
45    \textcolor{comment}{// so we get eina\_list,  eina\_hash and eina\_stringshare automatically!}
46    \textcolor{comment}{//}
47    \textcolor{comment}{// The STREAM variant is better for configuration files as the values}
48    \textcolor{comment}{// will likely change a lot.}
49    \textcolor{comment}{//}
50    \textcolor{comment}{// The other variant, FILE, is good for caches and things that are just}
51    \textcolor{comment}{// appended, but needs to take care when changing strings and files must}
52    \textcolor{comment}{// be kept open so mmap()ed strings will be kept alive.}
53    \hyperlink{group__Eet__Data__Group_ga941b1fcd6c6c72221d9390f9a0ed751c}{EET\_EINA\_STREAM\_DATA\_DESCRIPTOR\_CLASS\_SET}(&eddc, My\_Conf\_Type);
54    \_my\_conf\_descriptor = \hyperlink{group__Eet__Data__Group_gae300b4a97dbaab30a200edbb97f108c1}{eet\_data\_descriptor\_stream\_new}(&eddc);
55 
56    \textcolor{comment}{// Describe the members to be saved:}
57    \textcolor{comment}{// Use a temporary macro so we don't type a lot, also avoid errors:}
58 \textcolor{preprocessor}{#define MY\_CONF\_ADD\_BASIC(member, eet\_type) \(\backslash\)}
59 \textcolor{preprocessor}{  EET\_DATA\_DESCRIPTOR\_ADD\_BASIC             \(\backslash\)}
60 \textcolor{preprocessor}{    (\_my\_conf\_descriptor, My\_Conf\_Type, # member, member, eet\_type)}
61 
62    MY\_CONF\_ADD\_BASIC(version, \hyperlink{group__Eet__Data__Group_gacf6a3c9f06e10e20be5b02cd8c6a220b}{EET\_T\_UINT});
63    MY\_CONF\_ADD\_BASIC(name, \hyperlink{group__Eet__Data__Group_ga0d2cf409bf3f151156d74d18ac59e3d1}{EET\_T\_STRING});
64    MY\_CONF\_ADD\_BASIC(\textcolor{keywordtype}{id}, \hyperlink{group__Eet__Data__Group_ga4e196733795867f14c6e387af7287baa}{EET\_T\_INT});
65    MY\_CONF\_ADD\_BASIC(enabled, \hyperlink{group__Eet__Data__Group_ga09a1341cc158fda0ba60ad2386ecd795}{EET\_T\_UCHAR});
66 
67 \textcolor{preprocessor}{#undef MY\_CONF\_ADD\_BASIC}
68 \} \textcolor{comment}{/* \_my\_conf\_descriptor\_init */}
69 
70 \textcolor{keyword}{static} \textcolor{keywordtype}{void}
71 \_my\_conf\_descriptor\_shutdown(\textcolor{keywordtype}{void})
72 \{
73    \hyperlink{group__Eet__Data__Group_ga3e2b601720973d0e1f4683768a593c4d}{eet\_data\_descriptor\_free}(\_my\_conf\_descriptor);
74 \} \textcolor{comment}{/* \_my\_conf\_descriptor\_shutdown */}
75 
76 \textcolor{keyword}{static} My\_Conf\_Type *
77 \_my\_conf\_new(\textcolor{keywordtype}{void})
78 \{
79    My\_Conf\_Type *my\_conf = calloc(1, \textcolor{keyword}{sizeof}(My\_Conf\_Type));
80    \textcolor{keywordflow}{if} (!my\_conf)
81      \{
82         fprintf(stderr, \textcolor{stringliteral}{"ERROR: could not calloc My\_Conf\_Type\(\backslash\)n"});
83         \textcolor{keywordflow}{return} NULL;
84      \}
85 
86    my\_conf->version = 0x112233;
87    my\_conf->enabled = \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};
88    \textcolor{keywordflow}{return} my\_conf;
89 \} \textcolor{comment}{/* \_my\_conf\_new */}
90 
91 \textcolor{keyword}{static} \textcolor{keywordtype}{void}
92 \_my\_conf\_free(My\_Conf\_Type *my\_conf)
93 \{
94    \hyperlink{group__Eina__Stringshare__Group_ga4453d2aff2b325c7b45048f30df9b1f5}{eina\_stringshare\_del}(my\_conf->name);
95    free(my\_conf);
96 \} \textcolor{comment}{/* \_my\_conf\_free */}
97 
98 \textcolor{keyword}{static} My\_Conf\_Type *
99 \_my\_conf\_load(\textcolor{keyword}{const} \textcolor{keywordtype}{char} *filename)
100 \{
101    My\_Conf\_Type *my\_conf;
102    \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});
103    \textcolor{keywordflow}{if} (!ef)
104      \{
105         fprintf(stderr, \textcolor{stringliteral}{"ERROR: could not open '%s' for read\(\backslash\)n"}, filename);
106         \textcolor{keywordflow}{return} NULL;
107      \}
108 
109    my\_conf = \hyperlink{group__Eet__Data__Group_ga4d1807cbbc448304f22a8808318a3429}{eet\_data\_read}(ef, \_my\_conf\_descriptor, MY\_CONF\_FILE\_ENTRY);
110    \textcolor{keywordflow}{if} (!my\_conf)
111      \textcolor{keywordflow}{goto} end;
112 
113    \textcolor{keywordflow}{if} (my\_conf->version < 0x112233)
114      \{
115         fprintf(stderr,
116                 \textcolor{stringliteral}{"WARNING: version %#x was too old, upgrading it to %#x\(\backslash\)n"},
117                 my\_conf->version, 0x112233);
118 
119         my\_conf->version = 0x112233;
120         my\_conf->enabled = \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE};
121      \}
122 
123 end:
124    \hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}(ef);
125    \textcolor{keywordflow}{return} my\_conf;
126 \} \textcolor{comment}{/* \_my\_conf\_load */}
127 
128 \textcolor{keyword}{static} \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}
129 \_my\_conf\_save(\textcolor{keyword}{const} My\_Conf\_Type *my\_conf,
130               \textcolor{keyword}{const} \textcolor{keywordtype}{char}         *filename)
131 \{
132    \textcolor{keywordtype}{char} tmp[PATH\_MAX];
133    \hyperlink{group__Eet__File__Group_ga8d9779184a9870c1a225f1f40c76e8a7}{Eet\_File} *ef;
134    \hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool} ret;
135    \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} i, len;
136    \textcolor{keyword}{struct }stat st;
137 
138    len = \hyperlink{group__Eina__String__Group_ga6bda31d2e1b6d2737b7fe1784780cd2f}{eina\_strlcpy}(tmp, filename, \textcolor{keyword}{sizeof}(tmp));
139    \textcolor{keywordflow}{if} (len + 12 >= (\textcolor{keywordtype}{int})\textcolor{keyword}{sizeof}(tmp))
140      \{
141         fprintf(stderr, \textcolor{stringliteral}{"ERROR: file name is too big: %s\(\backslash\)n"}, filename);
142         \textcolor{keywordflow}{return} \hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE};
143      \}
144 
145    i = 0;
146    \textcolor{keywordflow}{do}
147      \{
148         snprintf(tmp + len, 12, \textcolor{stringliteral}{".%u"}, i);
149         i++;
150      \}
151    \textcolor{keywordflow}{while} (stat(tmp, &st) == 0);
152 
153    ef = \hyperlink{group__Eet__File__Group_ga5f55956b860aa2c77af9b1a880c4c256}{eet\_open}(tmp, \hyperlink{group__Eet__File__Group_ggaa8e1a7af50fefaf15eb2f69543d24998a8f395f56fba48be007c65be51d799ee9}{EET\_FILE\_MODE\_WRITE});
154    \textcolor{keywordflow}{if} (!ef)
155      \{
156         fprintf(stderr, \textcolor{stringliteral}{"ERROR: could not open '%s' for write\(\backslash\)n"}, tmp);
157         \textcolor{keywordflow}{return} \hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE};
158      \}
159 
160    ret = \hyperlink{group__Eet__Data__Group_gac66d1c49e3490bc5d803982d833ff8aa}{eet\_data\_write}
161        (ef, \_my\_conf\_descriptor, MY\_CONF\_FILE\_ENTRY, my\_conf, \hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE});
162    \hyperlink{group__Eet__File__Group_ga1675fef1f6012c1e02ae313d13dc3e03}{eet\_close}(ef);
163 
164    \textcolor{keywordflow}{if} (ret)
165      \{
166         unlink(filename);
167         rename(tmp, filename);
168      \}
169 
170    \textcolor{keywordflow}{return} ret;
171 \} \textcolor{comment}{/* \_my\_conf\_save */}
172 
173 \textcolor{keywordtype}{int}
174 main(\textcolor{keywordtype}{int}   argc,
175      \textcolor{keywordtype}{char} *argv[])
176 \{
177    My\_Conf\_Type *my\_conf;
178    \textcolor{keywordtype}{int} ret = 0;
179 
180    \textcolor{keywordflow}{if} (argc != 3)
181      \{
182         fprintf(stderr, \textcolor{stringliteral}{"Usage:\(\backslash\)n\(\backslash\)t%s <input> <output>\(\backslash\)n\(\backslash\)n"}, argv[0]);
183         \textcolor{keywordflow}{return} -1;
184      \}
185 
186    \hyperlink{group__Eina__Main__Group_gabf448ada5bc81a07342b66c6f818ee5e}{eina\_init}();
187    \hyperlink{group__Eet__Group_ga9903687bfa20fe9a9e59d4c1d213e89a}{eet\_init}();
188    \_my\_conf\_descriptor\_init();
189 
190    my\_conf = \_my\_conf\_load(argv[1]);
191    \textcolor{keywordflow}{if} (!my\_conf)
192      \{
193         printf(\textcolor{stringliteral}{"creating new configuration.\(\backslash\)n"});
194         my\_conf = \_my\_conf\_new();
195         \textcolor{keywordflow}{if} (!my\_conf)
196           \{
197              ret = -2;
198              \textcolor{keywordflow}{goto} end;
199           \}
200      \}
201 
202    printf(\textcolor{stringliteral}{"My\_Conf\_Type:\(\backslash\)n"}
203           \textcolor{stringliteral}{"\(\backslash\)tversion: %#x\(\backslash\)n"}
204           \textcolor{stringliteral}{"\(\backslash\)tname...: '%s'\(\backslash\)n"}
205           \textcolor{stringliteral}{"\(\backslash\)tid.....: %d\(\backslash\)n"}
206           \textcolor{stringliteral}{"\(\backslash\)tenabled: %hhu\(\backslash\)n"},
207           my\_conf->version,
208           my\_conf->name ? my\_conf->name : \textcolor{stringliteral}{""},
209           my\_conf->id,
210           my\_conf->enabled);
211 
212    \textcolor{keywordflow}{if} (!\_my\_conf\_save(my\_conf, argv[2]))
213      ret = -3;
214 
215    \_my\_conf\_free(my\_conf);
216 
217 end:
218    \_my\_conf\_descriptor\_shutdown();
219    \hyperlink{group__Eet__Group_gacbc7583179399d3b3e7b003e911f866f}{eet\_shutdown}();
220    \hyperlink{group__Eina__Main__Group_ga6741fcaba116e31e96ce14dcfa03f568}{eina\_shutdown}();
221 
222    \textcolor{keywordflow}{return} ret;
223 \} \textcolor{comment}{/* main */}
224 
\end{DoxyCodeInclude}
 