2 * build: gcc -o eet_data_simple eet-data-simple.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!
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
29 static const char MY_CONF_FILE_ENTRY[] = "config";
31 // keep the descriptor static global, so it can be
32 // shared by different functions (load/save) of this and only this
34 static Eet_Data_Descriptor *_my_conf_descriptor;
37 _my_conf_descriptor_init(void)
39 Eet_Data_Descriptor_Class eddc;
41 // The class describe the functions to use to create the type and its
42 // full allocated size.
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!
47 // The STREAM variant is better for configuration files as the values
48 // will likely change a lot.
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.
53 EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, My_Conf_Type);
54 _my_conf_descriptor = eet_data_descriptor_stream_new(&eddc);
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)
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);
67 #undef MY_CONF_ADD_BASIC
68 } /* _my_conf_descriptor_init */
71 _my_conf_descriptor_shutdown(void)
73 eet_data_descriptor_free(_my_conf_descriptor);
74 } /* _my_conf_descriptor_shutdown */
79 My_Conf_Type *my_conf = calloc(1, sizeof(My_Conf_Type));
82 fprintf(stderr, "ERROR: could not calloc My_Conf_Type\n");
86 my_conf->version = 0x112233;
87 my_conf->enabled = EINA_TRUE;
92 _my_conf_free(My_Conf_Type *my_conf)
94 eina_stringshare_del(my_conf->name);
99 _my_conf_load(const char *filename)
101 My_Conf_Type *my_conf;
102 Eet_File *ef = eet_open(filename, EET_FILE_MODE_READ);
105 fprintf(stderr, "ERROR: could not open '%s' for read\n", filename);
109 my_conf = eet_data_read(ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY);
113 if (my_conf->version < 0x112233)
116 "WARNING: version %#x was too old, upgrading it to %#x\n",
117 my_conf->version, 0x112233);
119 my_conf->version = 0x112233;
120 my_conf->enabled = EINA_TRUE;
126 } /* _my_conf_load */
129 _my_conf_save(const My_Conf_Type *my_conf,
130 const char *filename)
138 len = eina_strlcpy(tmp, filename, sizeof(tmp));
139 if (len + 12 >= (int)sizeof(tmp))
141 fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
148 snprintf(tmp + len, 12, ".%u", i);
151 while (stat(tmp, &st) == 0);
153 ef = eet_open(tmp, EET_FILE_MODE_WRITE);
156 fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
161 (ef, _my_conf_descriptor, MY_CONF_FILE_ENTRY, my_conf, EINA_TRUE);
167 rename(tmp, filename);
171 } /* _my_conf_save */
177 My_Conf_Type *my_conf;
182 fprintf(stderr, "Usage:\n\t%s <input> <output>\n\n", argv[0]);
188 _my_conf_descriptor_init();
190 my_conf = _my_conf_load(argv[1]);
193 printf("creating new configuration.\n");
194 my_conf = _my_conf_new();
202 printf("My_Conf_Type:\n"
208 my_conf->name ? my_conf->name : "",
212 if (!_my_conf_save(my_conf, argv[2]))
215 _my_conf_free(my_conf);
218 _my_conf_descriptor_shutdown();