- Main Page
- Related Pages
- Modules
- Data Structures
- Files
- Examples

Nested data example
1 //Compile with:
2 // gcc -o eet-data-nested eet-data-nested.c `pkg-config --cflags --libs eet eina`
3
4 #include <Eina.h>
5 #include <Eet.h>
6 #include <stdio.h>
7 #include <limits.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11
12 // The struct that will be loaded and saved.
13 // note that only the members described in the eet_data_descriptor
14 // will be automatically handled. The other members will have their
15 // space reserved and zeroed (as it uses calloc()), but not
16 // saved or loaded from eet files.
17 typedef struct
18 {
19 unsigned int version; // it is recommended to use versioned configuration!
20 const char *name;
21 int id;
22 int not_saved_value; // example of not saved data inside!
23 Eina_Bool enabled;
24 Eina_List *subs;
25 } My_Conf_Type;
26
27 typedef struct
28 {
29 const char *server;
30 int port;
31 } My_Conf_Subtype;
32
33 // string that represents the entry in eet file, you might like to have
34 // different profiles or so in the same file, this is possible with
35 // different strings
36 static const char MY_CONF_FILE_ENTRY[] = "config";
37
38 // keep the descriptor static global, so it can be
39 // shared by different functions (load/save) of this and only this
40 // file.
41 static Eet_Data_Descriptor *_my_conf_descriptor;
42 static Eet_Data_Descriptor *_my_conf_sub_descriptor;
43
44 static void
45 _my_conf_descriptor_init(void)
46 {
47 Eet_Data_Descriptor_Class eddc;
48
49 // The class describe the functions to use to create the type and its
50 // full allocated size.
51 //
52 // Eina types are very convenient, so use them to create the descriptor,
53 // so we get eina_list, eina_hash and eina_stringshare automatically!
54 //
55 // The STREAM variant is better for configuration files as the values
56 // will likely change a lot.
57 //
58 // The other variant, FILE, is good for caches and things that are just
59 // appended, but needs to take care when changing strings and files must
60 // be kept open so mmap()ed strings will be kept alive.
61 EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Conf_Type);
62 _my_conf_descriptor = eet_data_descriptor_stream_new(&eddc);
63
64 EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Conf_Subtype);
65 _my_conf_sub_descriptor = eet_data_descriptor_stream_new(&eddc);
66
67 // Describe the members to be saved:
68 // Use a temporary macro so we don't type a lot, also avoid errors:
69 #define MY_CONF_ADD_BASIC(member, eet_type) \
70 EET_DATA_DESCRIPTOR_ADD_BASIC \
71 (_my_conf_descriptor, My_Conf_Type, # member, member, eet_type)
72 #define MY_CONF_SUB_ADD_BASIC(member, eet_type) \
73 EET_DATA_DESCRIPTOR_ADD_BASIC \
74 (_my_conf_sub_descriptor, My_Conf_Subtype, # member, member, eet_type)
75
76 MY_CONF_SUB_ADD_BASIC(server, EET_T_STRING);
77 MY_CONF_SUB_ADD_BASIC(port, EET_T_INT);
78
79 MY_CONF_ADD_BASIC(version, EET_T_UINT);
80 MY_CONF_ADD_BASIC(name, EET_T_STRING);
81 MY_CONF_ADD_BASIC(id, EET_T_INT);
82 MY_CONF_ADD_BASIC(enabled, EET_T_UCHAR);
83
84 // And add the sub descriptor as a linked list at 'subs' in the main struct
85 EET_DATA_DESCRIPTOR_ADD_LIST
86 (_my_conf_descriptor, My_Conf_Type, "subs", subs, _my_conf_sub_descriptor);
87
88 #undef MY_CONF_ADD_BASIC
89 #undef MY_CONF_SUB_ADD_BASIC
90 } /* _my_conf_descriptor_init */
91
92 static void
93 _my_conf_descriptor_shutdown(void)
94 {
95 eet_data_descriptor_free(_my_conf_sub_descriptor);
96 eet_data_descriptor_free(_my_conf_descriptor);
97 } /* _my_conf_descriptor_shutdown */
98
99 static My_Conf_Type *
100 _my_conf_new(void)
101 {
102 My_Conf_Type *my_conf = calloc(1, sizeof(My_Conf_Type));
103 My_Conf_Subtype *sub;
104 if (!my_conf)
105 {
106 fprintf(stderr, "ERROR: could not calloc My_Conf_Type\n");
107 return NULL;
108 }
109
110 my_conf->version = 0x112233;
111 my_conf->enabled = EINA_TRUE;
112
113 sub = calloc(1, sizeof(My_Conf_Subtype));
114 if (sub)
115 {
116 sub->server = eina_stringshare_add("my-server.com");
117 sub->port = 1234;
118 my_conf->subs = eina_list_append(my_conf->subs, sub);
119 }
120
121 return my_conf;
122 } /* _my_conf_new */
123
124 static void
125 _my_conf_free(My_Conf_Type *my_conf)
126 {
127 My_Conf_Subtype *sub;
128 EINA_LIST_FREE(my_conf->subs, sub)
129 {
130 eina_stringshare_del(sub->server);
131 free(sub);
132 }
133
134 eina_stringshare_del(my_conf->name);
135 free(my_conf);
136 } /* _my_conf_free */
137
138 static My_Conf_Type *
139 _my_conf_load(const char *filename)
140 {
141 My_Conf_Type *my_conf;
142 Eet_File *ef = eet_open(filename, EET_FILE_MODE_READ);
143 if (!ef)
144 {
145 fprintf(stderr, "ERROR: could not open '%s' for read\n", filename);
146 return NULL;
147 }
148
149 my_conf = eet_data_read(ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY);
150 if (!my_conf)
151 goto end;
152
153 if (my_conf->version < 0x112233)
154 {
155 fprintf(stderr,
156 "WARNING: version %#x was too old, upgrading it to %#x\n",
157 my_conf->version, 0x112233);
158
159 my_conf->version = 0x112233;
160 my_conf->enabled = EINA_TRUE;
161 }
162
163 end:
164 eet_close(ef);
165 return my_conf;
166 } /* _my_conf_load */
167
168 static Eina_Bool
169 _my_conf_save(const My_Conf_Type *my_conf,
170 const char *filename)
171 {
172 char tmp[PATH_MAX];
173 Eet_File *ef;
174 Eina_Bool ret;
175 unsigned int i, len;
176 struct stat st;
177
178 len = eina_strlcpy(tmp, filename, sizeof(tmp));
179 if (len + 12 >= (int)sizeof(tmp))
180 {
181 fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
182 return EINA_FALSE;
183 }
184
185 i = 0;
186 do
187 {
188 snprintf(tmp + len, 12, ".%u", i);
189 i++;
190 }
191 while (stat(tmp, &st) == 0);
192
193 ef = eet_open(tmp, EET_FILE_MODE_WRITE);
194 if (!ef)
195 {
196 fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
197 return EINA_FALSE;
198 }
199
200 ret = eet_data_write
201 (ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY, my_conf, EINA_TRUE);
202 eet_close(ef);
203
204 if (ret)
205 {
206 unlink(filename);
207 rename(tmp, filename);
208 }
209
210 return ret;
211 } /* _my_conf_save */
212
213 int
214 main(int argc,
215 char *argv[])
216 {
217 My_Conf_Type *my_conf;
218 const My_Conf_Subtype *sub;
219 const Eina_List *l;
220 int ret = 0;
221
222 if (argc != 3)
223 {
224 fprintf(stderr, "Usage:\n\t%s <input> <output>\n\n", argv[0]);
225 return -1;
226 }
227
228 eina_init();
229 eet_init();
230 _my_conf_descriptor_init();
231
232 my_conf = _my_conf_load(argv[1]);
233 if (!my_conf)
234 {
235 printf("creating new configuration.\n");
236 my_conf = _my_conf_new();
237 if (!my_conf)
238 {
239 ret = -2;
240 goto end;
241 }
242 }
243
244 printf("My_Conf_Type:\n"
245 "\tversion: %#x\n"
246 "\tname...: '%s'\n"
247 "\tid.....: %d\n"
248 "\tenabled: %hhu\n"
249 "\tsubs...:\n",
250 my_conf->version,
251 my_conf->name ? my_conf->name : "",
252 my_conf->id,
253 my_conf->enabled);
254
255 EINA_LIST_FOREACH(my_conf->subs, l, sub)
256 printf("\t\tserver: '%s', port: %d\n",
257 sub->server ? sub->server : "",
258 sub->port);
259
260 if (!_my_conf_save(my_conf, argv[2]))
261 ret = -3;
262
263 _my_conf_free(my_conf);
264
265 end:
266 _my_conf_descriptor_shutdown();
267 eet_shutdown();
268 eina_shutdown();
269
270 return ret;
271 } /* main */
272
eina_stringshare_del
void eina_stringshare_del(Eina_Stringshare *str)
Note that the given string has lost an instance.
Definition: eina_stringshare.c:537
eina_stringshare_add
Eina_Stringshare * eina_stringshare_add(const char *str)
Retrieve an instance of a string for use in a program.
Definition: eina_stringshare.c:610
eet_open
EAPI Eet_File * eet_open(const char *file, Eet_File_Mode mode)
Open an eet file on disk, and returns a handle to it.
Definition: eet_lib.c:1494
rename
#define rename(src, dst)
Wrapper around evil_rename().
Definition: evil_stdio.h:226
eet_data_write
EAPI int eet_data_write(Eet_File *ef, Eet_Data_Descriptor *edd, const char *name, const void *data, int compress)
Write a data structure from memory and store in an eet file.
Definition: eet_data.c:2357
EINA_FALSE
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition: eina_types.h:311
Eet_File
struct _Eet_File Eet_File
Opaque handle that defines an Eet file (or memory).
Definition: Eet.h:485
EET_FILE_MODE_READ
File is read-only.
Definition: Eet.h:466
eet_shutdown
EAPI int eet_shutdown(void)
Shut down the EET library.
Definition: eet_lib.c:614
EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET
#define EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(clas, type)
This macro is an helper that set all the parameter of an Eet_Data_Descriptor_Class correctly when you...
Definition: Eet.h:2679
eina_init
int eina_init(void)
Initialize the Eina library.
Definition: eina_main.c:247
eet_data_descriptor_free
EAPI void eet_data_descriptor_free(Eet_Data_Descriptor *edd)
This function frees a data descriptor when it is not needed anymore.
Definition: eet_data.c:2104
EET_T_STRING
#define EET_T_STRING
Data type: char *.
Definition: Eet.h:2379
EET_T_UINT
#define EET_T_UINT
Data type: unsigned int.
Definition: Eet.h:2377
EET_T_UCHAR
#define EET_T_UCHAR
Data type: unsigned char.
Definition: Eet.h:2375
EET_T_INT
#define EET_T_INT
Data type: int.
Definition: Eet.h:2371
EINA_TRUE
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:317
eina_shutdown
int eina_shutdown(void)
Shut down the Eina library.
Definition: eina_main.c:317
eina_list_append
Eina_List * eina_list_append(Eina_List *list, const void *data)
Append the given data to the given linked list.
Definition: eina_list.c:530
Eet.h
The file that provides the eet functions.
EET_FILE_MODE_WRITE
File is write-only.
Definition: Eet.h:467
eina_strlcpy
EAPI size_t eina_strlcpy(char *dst, const char *src, size_t siz) EINA_ARG_NONNULL(1
Copy a c-string to another.
eet_data_descriptor_stream_new
EAPI Eet_Data_Descriptor * eet_data_descriptor_stream_new(const Eet_Data_Descriptor_Class *eddc)
This function creates a new data descriptor and returns a handle to the new data descriptor.
Definition: eet_data.c:2084
eet_data_read
EAPI void * eet_data_read(Eet_File *ef, Eet_Data_Descriptor *edd, const char *name)
Read a data structure from an eet file and decodes it.
Definition: eet_data.c:2323
Eina.h
Eina Utility library.
_Eet_Data_Descriptor_Class
Instructs Eet about memory management for different needs under serialization and parse process...
Definition: Eet.h:2473
Eina_Bool
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:305
EET_DATA_DESCRIPTOR_ADD_LIST
#define EET_DATA_DESCRIPTOR_ADD_LIST(edd, struct_type, name, member, subtype)
Add a linked list type to a data descriptor.
Definition: Eet.h:3145
eet_close
EAPI Eet_Error eet_close(Eet_File *ef)
Close an eet file handle and flush pending writes.
Definition: eet_lib.c:1744
Eet_Data_Descriptor
struct _Eet_Data_Descriptor Eet_Data_Descriptor
Opaque handle that have information on a type members.
Definition: Eet.h:2421
EINA_LIST_FREE
#define EINA_LIST_FREE(list, data)
Macro to remove each list node while having access to each node's data.
Definition: eina_list.h:1599
_Eina_List
Type for a generic double linked list.
Definition: eina_list.h:320
EINA_LIST_FOREACH
#define EINA_LIST_FOREACH(list, l, data)
Macro to iterate over a list.
Definition: eina_list.h:1387
eet_init
EAPI int eet_init(void)
Initialize the EET library.
Definition: eet_lib.c:531