Simple data example
1//Compile with:
2// gcc -o eet-data-simple eet-data-simple.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.
17typedef 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} My_Conf_Type;
25
26// string that represents the entry in eet file, you might like to have
27// different profiles or so in the same file, this is possible with
28// different strings
29static const char MY_CONF_FILE_ENTRY[] = "config";
30
31// keep the descriptor static global, so it can be
32// shared by different functions (load/save) of this and only this
33// file.
34static Eet_Data_Descriptor *_my_conf_descriptor;
35
36static void
37_my_conf_descriptor_init(void)
38{
40
41 // The class describe the functions to use to create the type and its
42 // full allocated size.
43 //
44 // Eina types are very convenient, so use them to create the descriptor,
45 // so we get eina_list, eina_hash and eina_stringshare automatically!
46 //
47 // The STREAM variant is better for configuration files as the values
48 // will likely change a lot.
49 //
50 // The other variant, FILE, is good for caches and things that are just
51 // appended, but needs to take care when changing strings and files must
52 // be kept open so mmap()ed strings will be kept alive.
54 _my_conf_descriptor = eet_data_descriptor_stream_new(&eddc);
55
56 // Describe the members to be saved:
57 // Use a temporary macro so we don't type a lot, also avoid errors:
58#define MY_CONF_ADD_BASIC(member, eet_type) \
59 EET_DATA_DESCRIPTOR_ADD_BASIC \
60 (_my_conf_descriptor, My_Conf_Type, # member, member, eet_type)
61
62 MY_CONF_ADD_BASIC(version, EET_T_UINT);
63 MY_CONF_ADD_BASIC(name, EET_T_STRING);
64 MY_CONF_ADD_BASIC(id, EET_T_INT);
65 MY_CONF_ADD_BASIC(enabled, EET_T_UCHAR);
66
67#undef MY_CONF_ADD_BASIC
68} /* _my_conf_descriptor_init */
69
70static void
71_my_conf_descriptor_shutdown(void)
72{
73 eet_data_descriptor_free(_my_conf_descriptor);
74} /* _my_conf_descriptor_shutdown */
75
76static My_Conf_Type *
77_my_conf_new(void)
78{
79 My_Conf_Type *my_conf = calloc(1, sizeof(My_Conf_Type));
80 if (!my_conf)
81 {
82 fprintf(stderr, "ERROR: could not calloc My_Conf_Type\n");
83 return NULL;
84 }
85
86 my_conf->version = 0x112233;
87 my_conf->enabled = EINA_TRUE;
88 return my_conf;
89} /* _my_conf_new */
90
91static void
92_my_conf_free(My_Conf_Type *my_conf)
93{
94 eina_stringshare_del(my_conf->name);
95 free(my_conf);
96} /* _my_conf_free */
97
98static My_Conf_Type *
99_my_conf_load(const char *filename)
100{
101 My_Conf_Type *my_conf;
102 Eet_File *ef = eet_open(filename, EET_FILE_MODE_READ);
103 if (!ef)
104 {
105 fprintf(stderr, "ERROR: could not open '%s' for read\n", filename);
106 return NULL;
107 }
108
109 my_conf = eet_data_read(ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY);
110 if (!my_conf)
111 goto end;
112
113 if (my_conf->version < 0x112233)
114 {
115 fprintf(stderr,
116 "WARNING: version %#x was too old, upgrading it to %#x\n",
117 my_conf->version, 0x112233);
118
119 my_conf->version = 0x112233;
120 my_conf->enabled = EINA_TRUE;
121 }
122
123end:
124 eet_close(ef);
125 return my_conf;
126} /* _my_conf_load */
127
128static Eina_Bool
129_my_conf_save(const My_Conf_Type *my_conf,
130 const char *filename)
131{
132 char tmp[PATH_MAX];
133 Eet_File *ef;
134 Eina_Bool ret;
135 unsigned int i, len;
136 struct stat st;
137
138 len = eina_strlcpy(tmp, filename, sizeof(tmp));
139 if (len + 12 >= (int)sizeof(tmp))
140 {
141 fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
142 return EINA_FALSE;
143 }
144
145 i = 0;
146 do
147 {
148 snprintf(tmp + len, 12, ".%u", i);
149 i++;
150 }
151 while (stat(tmp, &st) == 0);
152
153 ef = eet_open(tmp, EET_FILE_MODE_WRITE);
154 if (!ef)
155 {
156 fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
157 return EINA_FALSE;
158 }
159
160 ret = eet_data_write
161 (ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY, my_conf, EINA_TRUE);
162 eet_close(ef);
163
164 if (ret)
165 {
166 unlink(filename);
167 rename(tmp, filename);
168 }
169
170 return ret;
171} /* _my_conf_save */
172
173int
174main(int argc,
175 char *argv[])
176{
177 My_Conf_Type *my_conf;
178 int ret = 0;
179
180 if (argc != 3)
181 {
182 fprintf(stderr, "Usage:\n\t%s <input> <output>\n\n", argv[0]);
183 return -1;
184 }
185
186 eina_init();
187 eet_init();
188 _my_conf_descriptor_init();
189
190 my_conf = _my_conf_load(argv[1]);
191 if (!my_conf)
192 {
193 printf("creating new configuration.\n");
194 my_conf = _my_conf_new();
195 if (!my_conf)
196 {
197 ret = -2;
198 goto end;
199 }
200 }
201
202 printf("My_Conf_Type:\n"
203 "\tversion: %#x\n"
204 "\tname...: '%s'\n"
205 "\tid.....: %d\n"
206 "\tenabled: %hhu\n",
207 my_conf->version,
208 my_conf->name ? my_conf->name : "",
209 my_conf->id,
210 my_conf->enabled);
211
212 if (!_my_conf_save(my_conf, argv[2]))
213 ret = -3;
214
215 _my_conf_free(my_conf);
216
217end:
218 _my_conf_descriptor_shutdown();
219 eet_shutdown();
221
222 return ret;
223} /* main */
224
The file that provides the eet functions.
Eina Utility library.
#define EET_T_UCHAR
Data type: unsigned char.
Definition: Eet.h:2585
#define EET_T_STRING
Data type: char *.
Definition: Eet.h:2589
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
struct _Eet_Data_Descriptor Eet_Data_Descriptor
Opaque handle that have information on a type members.
Definition: Eet.h:2631
EAPI void * eet_data_read(Eet_File *ef, Eet_Data_Descriptor *edd, const char *name)
Reads a data structure from an eet file and decodes it.
Definition: eet_data.c:2379
#define EET_T_INT
Data type: int.
Definition: Eet.h:2581
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
#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:3034
EAPI int eet_data_write(Eet_File *ef, Eet_Data_Descriptor *edd, const char *name, const void *data, int compress)
Writes a data structure from memory and store in an eet file.
Definition: eet_data.c:2416
#define EET_T_UINT
Data type: unsigned int.
Definition: Eet.h:2587
EAPI Eet_Error eet_close(Eet_File *ef)
Closes an eet file handle and flush pending writes.
Definition: eet_lib.c:1899
EAPI Eet_File * eet_open(const char *file, Eet_File_Mode mode)
Opens an eet file on disk, and returns a handle to it.
Definition: eet_lib.c:1499
struct _Eet_File Eet_File
Opaque handle that defines an Eet file (or memory).
Definition: Eet.h:527
@ EET_FILE_MODE_READ
File is read-only.
Definition: Eet.h:479
@ EET_FILE_MODE_WRITE
File is write-only.
Definition: Eet.h:480
EAPI int eet_init(void)
Initializes the EET library.
Definition: eet_lib.c:540
EAPI int eet_shutdown(void)
Shuts down the EET library.
Definition: eet_lib.c:594
EINA_API int eina_shutdown(void)
Shuts down the Eina library.
Definition: eina_main.c:379
EINA_API int eina_init(void)
Initializes the Eina library.
Definition: eina_main.c:291
EINA_API size_t eina_strlcpy(char *dst, const char *src, size_t siz)
Copies a c-string to another.
Definition: eina_str.c:317
EINA_API void eina_stringshare_del(Eina_Stringshare *str)
Notes that the given string has lost an instance.
Definition: eina_stringshare.c:533
#define EINA_TRUE
boolean value TRUE (numerical value 1)
Definition: eina_types.h:539
#define EINA_FALSE
boolean value FALSE (numerical value 0)
Definition: eina_types.h:533
unsigned char Eina_Bool
Type to mimic a boolean.
Definition: eina_types.h:527
Instructs Eet about memory management for different needs under serialization and parse process.
Definition: Eet.h:2828