1
2
3
6#include <stdio.h>
7#include <limits.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <unistd.h>
11
12
13
14
15
16
17typedef struct
18{
19 unsigned int version;
20 const char *name;
21 int id;
22 int not_saved_value;
25} My_Conf_Type;
26
27typedef struct
28{
29 const char *server;
30 int port;
31} My_Conf_Subtype;
32
33
34
35
36static const char MY_CONF_FILE_ENTRY[] = "config";
37
38
39
40
43
44static void
45_my_conf_descriptor_init(void)
46{
48
49
50
51
52
53
54
55
56
57
58
59
60
63
66
67
68
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
78
83
84
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}
91
92static void
93_my_conf_descriptor_shutdown(void)
94{
97}
98
99static 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;
112
113 sub = calloc(1, sizeof(My_Conf_Subtype));
114 if (sub)
115 {
117 sub->port = 1234;
119 }
120
121 return my_conf;
122}
123
124static void
125_my_conf_free(My_Conf_Type *my_conf)
126{
127 My_Conf_Subtype *sub;
129 {
131 free(sub);
132 }
133
135 free(my_conf);
136}
137
138static My_Conf_Type *
139_my_conf_load(const char *filename)
140{
141 My_Conf_Type *my_conf;
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;
161 }
162
163end:
165 return my_conf;
166}
167
169_my_conf_save(const My_Conf_Type *my_conf,
170 const char *filename)
171{
172 char tmp[PATH_MAX];
175 unsigned int i, len;
176 struct stat st;
177
179 if (len + 12 >= (int)sizeof(tmp))
180 {
181 fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
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
194 if (!ef)
195 {
196 fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
198 }
199
201 (ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY, my_conf,
EINA_TRUE);
203
204 if (ret)
205 {
206 unlink(filename);
207 rename(tmp, filename);
208 }
209
210 return ret;
211}
212
213int
214main(int argc,
215 char *argv[])
216{
217 My_Conf_Type *my_conf;
218 const My_Conf_Subtype *sub;
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
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
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
265end:
266 _my_conf_descriptor_shutdown();
269
270 return ret;
271}
272
The file that provides the eet functions.
#define EET_T_UCHAR
Data type: unsigned char.
Definition Eet.h:2602
#define EET_T_STRING
Data type: char *.
Definition Eet.h:2606
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
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:2598
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:3051
#define EET_DATA_DESCRIPTOR_ADD_LIST(edd, struct_type, name, member, subtype)
Adds a linked list type to a data descriptor.
Definition Eet.h:3528
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
struct _Eet_Data_Descriptor Eet_Data_Descriptor
Opaque handle that have information on a type members.
Definition Eet.h:2648
#define EET_T_UINT
Data type: unsigned int.
Definition Eet.h:2604
struct _Eet_Data_Descriptor_Class Eet_Data_Descriptor_Class
Instructs Eet about memory management for different needs under serialization and parse process.
Definition Eet.h:2665
EAPI Eet_Error eet_close(Eet_File *ef)
Closes an eet file handle and flush pending writes.
Definition eet_lib.c:1934
struct _Eet_File Eet_File
Opaque handle that defines an Eet file (or memory).
Definition Eet.h:527
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:1525
@ 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:543
EAPI int eet_shutdown(void)
Shuts down the EET library.
Definition eet_lib.c:597
EINA_API Eina_List * eina_list_append(Eina_List *list, const void *data)
Appends the given data to the given linked list.
Definition eina_list.c:584
struct _Eina_List Eina_List
Type for a generic double linked list.
Definition eina_list.h:304
#define EINA_LIST_FOREACH(list, l, _data)
Definition for the macro to iterate over a list.
Definition eina_list.h:1415
#define EINA_LIST_FREE(list, data)
Definition for the macro to remove each list node while having access to each node's data.
Definition eina_list.h:1629
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 Eina_Stringshare * eina_stringshare_add(const char *str)
Retrieves an instance of a string for use in a program.
Definition eina_stringshare.c:606
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