2 * build: gcc -o eet_data_nested eet-data-nested.c `pkg-config --cflags --libs eet eina`
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.
19 unsigned int version; // it is recommended to use versioned configuration!
22 int not_saved_value; // example of not saved data inside!
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
36 static const char MY_CONF_FILE_ENTRY[] = "config";
38 // keep the descriptor static global, so it can be
39 // shared by different functions (load/save) of this and only this
41 static Eet_Data_Descriptor *_my_conf_descriptor;
42 static Eet_Data_Descriptor *_my_conf_sub_descriptor;
45 _my_conf_descriptor_init(void)
47 Eet_Data_Descriptor_Class eddc;
49 // The class describe the functions to use to create the type and its
50 // full allocated size.
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!
55 // The STREAM variant is better for configuration files as the values
56 // will likely change a lot.
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);
64 EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Conf_Subtype);
65 _my_conf_sub_descriptor = eet_data_descriptor_stream_new(&eddc);
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)
76 MY_CONF_SUB_ADD_BASIC(server, EET_T_STRING);
77 MY_CONF_SUB_ADD_BASIC(port, EET_T_INT);
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);
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);
88 #undef MY_CONF_ADD_BASIC
89 #undef MY_CONF_SUB_ADD_BASIC
90 } /* _my_conf_descriptor_init */
93 _my_conf_descriptor_shutdown(void)
95 eet_data_descriptor_free(_my_conf_sub_descriptor);
96 eet_data_descriptor_free(_my_conf_descriptor);
97 } /* _my_conf_descriptor_shutdown */
102 My_Conf_Type *my_conf = calloc(1, sizeof(My_Conf_Type));
103 My_Conf_Subtype *sub;
106 fprintf(stderr, "ERROR: could not calloc My_Conf_Type\n");
110 my_conf->version = 0x112233;
111 my_conf->enabled = EINA_TRUE;
113 sub = calloc(1, sizeof(My_Conf_Subtype));
116 sub->server = eina_stringshare_add("my-server.com");
118 my_conf->subs = eina_list_append(my_conf->subs, sub);
125 _my_conf_free(My_Conf_Type *my_conf)
127 My_Conf_Subtype *sub;
128 EINA_LIST_FREE(my_conf->subs, sub)
130 eina_stringshare_del(sub->server);
134 eina_stringshare_del(my_conf->name);
136 } /* _my_conf_free */
138 static My_Conf_Type *
139 _my_conf_load(const char *filename)
141 My_Conf_Type *my_conf;
142 Eet_File *ef = eet_open(filename, EET_FILE_MODE_READ);
145 fprintf(stderr, "ERROR: could not open '%s' for read\n", filename);
149 my_conf = eet_data_read(ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY);
153 if (my_conf->version < 0x112233)
156 "WARNING: version %#x was too old, upgrading it to %#x\n",
157 my_conf->version, 0x112233);
159 my_conf->version = 0x112233;
160 my_conf->enabled = EINA_TRUE;
166 } /* _my_conf_load */
169 _my_conf_save(const My_Conf_Type *my_conf,
170 const char *filename)
178 len = eina_strlcpy(tmp, filename, sizeof(tmp));
179 if (len + 12 >= (int)sizeof(tmp))
181 fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
188 snprintf(tmp + len, 12, ".%u", i);
191 while (stat(tmp, &st) == 0);
193 ef = eet_open(tmp, EET_FILE_MODE_WRITE);
196 fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
201 (ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY, my_conf, EINA_TRUE);
207 rename(tmp, filename);
211 } /* _my_conf_save */
217 My_Conf_Type *my_conf;
218 const My_Conf_Subtype *sub;
224 fprintf(stderr, "Usage:\n\t%s <input> <output>\n\n", argv[0]);
230 _my_conf_descriptor_init();
232 my_conf = _my_conf_load(argv[1]);
235 printf("creating new configuration.\n");
236 my_conf = _my_conf_new();
244 printf("My_Conf_Type:\n"
251 my_conf->name ? my_conf->name : "",
255 EINA_LIST_FOREACH(my_conf->subs, l, sub)
256 printf("\t\tserver: '%s', port: %d\n",
257 sub->server ? sub->server : "",
260 if (!_my_conf_save(my_conf, argv[2]))
263 _my_conf_free(my_conf);
266 _my_conf_descriptor_shutdown();