9 // The struct that will be loaded and saved.
10 // note that only the members described in the eet_data_descriptor
11 // will be automatically handled. The other members will have their
12 // space reserved and zeroed (as it uses calloc()), but not
13 // saved or loaded from eet files.
16 unsigned int version; // it is recommended to use versioned configuration!
19 int not_saved_value; // example of not saved data inside!
23 // string that represents the entry in eet file, you might like to have
24 // different profiles or so in the same file, this is possible with
26 static const char MY_CONF_FILE_ENTRY[] = "config";
28 // keep the descriptor static global, so it can be
29 // shared by different functions (load/save) of this and only this
31 static Eet_Data_Descriptor * _my_conf_descriptor;
34 _my_conf_descriptor_init(void)
36 Eet_Data_Descriptor_Class eddc;
38 // The class describe the functions to use to create the type and its
39 // full allocated size.
41 // Eina types are very convenient, so use them to create the descriptor,
42 // so we get eina_list, eina_hash and eina_stringshare automatically!
44 // The STREAM variant is better for configuration files as the values
45 // will likely change a lot.
47 // The other variant, FILE, is good for caches and things that are just
48 // appended, but needs to take care when changing strings and files must
49 // be kept open so mmap()ed strings will be kept alive.
50 EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Conf_Type);
51 _my_conf_descriptor = eet_data_descriptor_stream_new(&eddc);
53 // Describe the members to be saved:
54 // Use a temporary macro so we don't type a lot, also avoid errors:
55 #define MY_CONF_ADD_BASIC(member, eet_type)\
56 EET_DATA_DESCRIPTOR_ADD_BASIC\
57 (_my_conf_descriptor, My_Conf_Type, # member, member, eet_type)
59 MY_CONF_ADD_BASIC(version, EET_T_UINT);
60 MY_CONF_ADD_BASIC(name, EET_T_STRING);
61 MY_CONF_ADD_BASIC(id, EET_T_INT);
62 MY_CONF_ADD_BASIC(enabled, EET_T_UCHAR);
64 #undef MY_CONF_ADD_BASIC
65 } /* _my_conf_descriptor_init */
68 _my_conf_descriptor_shutdown(void)
70 eet_data_descriptor_free(_my_conf_descriptor);
71 } /* _my_conf_descriptor_shutdown */
76 My_Conf_Type * my_conf = calloc(1, sizeof(My_Conf_Type));
79 fprintf(stderr, "ERROR: could not calloc My_Conf_Type\n");
83 my_conf->version = 0x112233;
84 my_conf->enabled = EINA_TRUE;
89 _my_conf_free(My_Conf_Type * my_conf)
91 eina_stringshare_del(my_conf->name);
96 _my_conf_load(const char * filename)
98 My_Conf_Type * my_conf;
99 Eet_File * ef = eet_open(filename, EET_FILE_MODE_READ);
102 fprintf(stderr, "ERROR: could not open '%s' for read\n", filename);
106 my_conf = eet_data_read(ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY);
110 if (my_conf->version < 0x112233)
113 "WARNING: version %#x was too old, upgrading it to %#x\n",
114 my_conf->version, 0x112233);
116 my_conf->version = 0x112233;
117 my_conf->enabled = EINA_TRUE;
123 } /* _my_conf_load */
126 _my_conf_save(const My_Conf_Type * my_conf, const char * filename)
134 len = eina_strlcpy(tmp, filename, sizeof(tmp));
135 if (len + 12 >= (int)sizeof(tmp))
137 fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
144 snprintf(tmp + len, 12, ".%u", i);
147 while (stat(tmp, &st) == 0);
149 ef = eet_open(tmp, EET_FILE_MODE_WRITE);
152 fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
157 (ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY, my_conf, EINA_TRUE);
163 rename(tmp, filename);
167 } /* _my_conf_save */
169 int main(int argc, char * argv[])
171 My_Conf_Type * my_conf;
176 fprintf(stderr, "Usage:\n\t%s <input> <output>\n\n", argv[0]);
182 _my_conf_descriptor_init();
184 my_conf = _my_conf_load(argv[1]);
187 printf("creating new configuration.\n");
188 my_conf = _my_conf_new();
196 printf("My_Conf_Type:\n"
202 my_conf->name ? my_conf->name : "",
206 if (!_my_conf_save(my_conf, argv[2]))
209 _my_conf_free(my_conf);
212 _my_conf_descriptor_shutdown();