2 * build: gcc -o eet_data_file_descriptor_02 eet-data-file_descriptor_02.c `pkg-config --cflags --libs eet eina`
13 typedef enum _Example_Data_Type Example_Data_Type;
14 typedef struct _Example_Variant_Type Example_Variant_Type;
15 typedef struct _Example_Variant Example_Variant;
16 typedef struct _Example_Union Example_Union;
17 typedef struct _Example_Struct1 Example_Struct1;
18 typedef struct _Example_Struct2 Example_Struct2;
19 typedef struct _Example_Struct3 Example_Struct3;
20 typedef struct _Example_Lists Example_Lists;
22 enum _Example_Data_Type
35 { EET_STRUCT1, "ST1" },
36 { EET_STRUCT2, "ST2" },
37 { EET_STRUCT3, "ST3" },
41 struct _Example_Struct1
48 struct _Example_Struct2
51 unsigned long long v1;
54 struct _Example_Struct3
61 Example_Data_Type type;
70 struct _Example_Variant_Type
76 struct _Example_Variant
78 Example_Variant_Type t;
80 void *data; /* differently than the union type, we
81 * don't need to pre-allocate the memory
87 Eina_List *union_list;
88 Eina_List *variant_list;
92 _st1_set(Example_Struct1 *st1,
103 _st2_set(Example_Struct2 *st2,
105 unsigned long long v2)
112 _st3_set(Example_Struct3 *st3,
121 _union_type_get(const void *data,
124 const Example_Data_Type *u = data;
128 *unknow = EINA_FALSE;
130 for (i = 0; eet_mapping[i].name != NULL; ++i)
131 if (*u == eet_mapping[i].u)
132 return eet_mapping[i].name;
138 } /* _union_type_get */
141 _union_type_set(const char *type,
145 Example_Data_Type *u = data;
151 for (i = 0; eet_mapping[i].name != NULL; ++i)
152 if (strcmp(eet_mapping[i].name, type) == 0)
154 *u = eet_mapping[i].u;
159 } /* _union_type_set */
162 _variant_type_get(const void *data,
165 const Example_Variant_Type *type = data;
169 *unknow = type->unknow;
171 for (i = 0; eet_mapping[i].name != NULL; ++i)
172 if (strcmp(type->type, eet_mapping[i].name) == 0)
173 return eet_mapping[i].name;
176 *unknow = EINA_FALSE;
179 } /* _variant_type_get */
182 _variant_type_set(const char *type,
186 Example_Variant_Type *vt = data;
191 } /* _variant_type_set */
193 static Eet_Data_Descriptor *
196 Eet_Data_Descriptor_Class eddc;
197 Eet_Data_Descriptor *res;
199 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, Example_Struct1);
200 res = eet_data_descriptor_file_new(&eddc);
201 EET_DATA_DESCRIPTOR_ADD_BASIC(
202 res, Example_Struct1, "val1", val1, EET_T_DOUBLE);
203 EET_DATA_DESCRIPTOR_ADD_BASIC(
204 res, Example_Struct1, "stuff", stuff, EET_T_INT);
205 EET_DATA_DESCRIPTOR_ADD_BASIC(
206 res, Example_Struct1, "s1", s1, EET_T_STRING);
211 static Eet_Data_Descriptor *
214 Eet_Data_Descriptor_Class eddc;
215 Eet_Data_Descriptor *res;
217 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, Example_Struct2);
218 res = eet_data_descriptor_file_new(&eddc);
219 EET_DATA_DESCRIPTOR_ADD_BASIC(
220 res, Example_Struct2, "b1", b1, EET_T_UCHAR);
221 EET_DATA_DESCRIPTOR_ADD_BASIC(
222 res, Example_Struct2, "v1", v1, EET_T_ULONG_LONG);
227 static Eet_Data_Descriptor *
230 Eet_Data_Descriptor_Class eddc;
231 Eet_Data_Descriptor *res;
233 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, Example_Struct3);
234 res = eet_data_descriptor_file_new(&eddc);
235 EET_DATA_DESCRIPTOR_ADD_BASIC(
236 res, Example_Struct3, "body", body, EET_T_INT);
241 /* string that represents the entry in the eet file. you might like to
242 * have different profiles or so in the same file, this is possible
243 * with different strings
245 static const char CACHE_FILE_ENTRY[] = "cache";
247 /* keep the descriptor static global, so it can be shared by different
248 * functions (load/save) of this and only this file.
250 static Eet_Data_Descriptor *_lists_descriptor;
251 static Eet_Data_Descriptor *_struct_1_descriptor;
252 static Eet_Data_Descriptor *_struct_2_descriptor;
253 static Eet_Data_Descriptor *_struct_3_descriptor;
254 static Eet_Data_Descriptor *_union_descriptor;
255 static Eet_Data_Descriptor *_variant_descriptor;
256 static Eet_Data_Descriptor *_union_unified_descriptor;
257 static Eet_Data_Descriptor *_variant_unified_descriptor;
259 /* keep file handle alive, so mmap()ed strings are all alive as
261 static Eet_File *_cache_file = NULL;
262 static Eet_Dictionary *_cache_dict = NULL;
265 /* declaring types */
266 _data_descriptors_init(void)
268 Eet_Data_Descriptor_Class eddc;
270 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, Example_Lists);
271 _lists_descriptor = eet_data_descriptor_file_new(&eddc);
273 _struct_1_descriptor = _st1_dd();
274 _struct_2_descriptor = _st2_dd();
275 _struct_3_descriptor = _st3_dd();
278 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, Example_Union);
279 _union_descriptor = eet_data_descriptor_file_new(&eddc);
281 eddc.version = EET_DATA_DESCRIPTOR_CLASS_VERSION;
282 eddc.func.type_get = _union_type_get;
283 eddc.func.type_set = _union_type_set;
284 _union_unified_descriptor = eet_data_descriptor_file_new(&eddc);
286 EET_DATA_DESCRIPTOR_ADD_MAPPING(
287 _union_unified_descriptor, "ST1", _struct_1_descriptor);
288 EET_DATA_DESCRIPTOR_ADD_MAPPING(
289 _union_unified_descriptor, "ST2", _struct_2_descriptor);
290 EET_DATA_DESCRIPTOR_ADD_MAPPING(
291 _union_unified_descriptor, "ST3", _struct_3_descriptor);
293 EET_DATA_DESCRIPTOR_ADD_UNION(
294 _union_descriptor, Example_Union, "u", u, type,
295 _union_unified_descriptor);
297 EET_DATA_DESCRIPTOR_ADD_LIST(
298 _lists_descriptor, Example_Lists, "union_list", union_list,
302 EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, Example_Variant);
303 _variant_descriptor = eet_data_descriptor_file_new(&eddc);
305 eddc.version = EET_DATA_DESCRIPTOR_CLASS_VERSION;
306 eddc.func.type_get = _variant_type_get;
307 eddc.func.type_set = _variant_type_set;
308 _variant_unified_descriptor = eet_data_descriptor_stream_new(&eddc);
310 EET_DATA_DESCRIPTOR_ADD_MAPPING(
311 _variant_unified_descriptor, "ST1", _struct_1_descriptor);
312 EET_DATA_DESCRIPTOR_ADD_MAPPING(
313 _variant_unified_descriptor, "ST2", _struct_2_descriptor);
314 EET_DATA_DESCRIPTOR_ADD_MAPPING(
315 _variant_unified_descriptor, "ST3", _struct_3_descriptor);
317 EET_DATA_DESCRIPTOR_ADD_VARIANT(
318 _variant_descriptor, Example_Variant, "data", data, t,
319 _variant_unified_descriptor);
321 EET_DATA_DESCRIPTOR_ADD_LIST(
322 _lists_descriptor, Example_Lists, "variant_list", variant_list,
323 _variant_descriptor);
324 } /* _data_descriptors_init */
327 _data_descriptors_shutdown(void)
329 eet_data_descriptor_free(_lists_descriptor);
330 eet_data_descriptor_free(_struct_1_descriptor);
331 eet_data_descriptor_free(_struct_2_descriptor);
332 eet_data_descriptor_free(_struct_3_descriptor);
333 eet_data_descriptor_free(_union_descriptor);
334 eet_data_descriptor_free(_variant_descriptor);
335 eet_data_descriptor_free(_union_unified_descriptor);
336 eet_data_descriptor_free(_variant_unified_descriptor);
337 } /* _data_descriptors_shutdown */
339 /* need to check if the pointer came from mmap()ed area in
340 * eet_dictionary or it was allocated with eina_stringshare_add()
343 _string_free(const char *str)
348 if ((_cache_dict) && (eet_dictionary_string_check(_cache_dict, str)))
351 eina_stringshare_del(str);
354 static Example_Union *
355 _union_1_new(const char *v1,
359 Example_Union *un = calloc(1, sizeof(Example_Union));
363 stderr, "ERROR: could not allocate an Example_Union struct.\n");
367 un->type = EET_STRUCT1;
368 _st1_set(&(un->u.st1), atof(v1), atoi(v2), eina_stringshare_add(v3));
373 static Example_Union *
374 _union_2_new(const char *v1,
377 Example_Union *un = calloc(1, sizeof(Example_Union));
381 stderr, "ERROR: could not allocate an Example_Union struct.\n");
385 un->type = EET_STRUCT2;
386 _st2_set(&(un->u.st2), atoi(v1), atoi(v2));
391 static Example_Union *
392 _union_3_new(const char *v1)
394 Example_Union *un = calloc(1, sizeof(Example_Union));
398 stderr, "ERROR: could not allocate an Example_Union struct.\n");
402 un->type = EET_STRUCT3;
403 _st3_set(&(un->u.st3), atoi(v1));
408 static Example_Variant *
409 _variant_1_new(const char *v1,
413 Example_Struct1 *st1;
414 Example_Variant *va = calloc(1, sizeof(Example_Variant));
418 stderr, "ERROR: could not allocate an Example_Variant struct.\n");
422 va = calloc(1, sizeof (Example_Variant));
423 va->t.type = eet_mapping[0].name;
424 st1 = calloc(1, sizeof (Example_Struct1));
425 _st1_set(st1, atof(v1), atoi(v2), eina_stringshare_add(v3));
431 static Example_Variant *
432 _variant_2_new(const char *v1,
435 printf("varinant 2 new\n");
437 Example_Struct2 *st2;
438 Example_Variant *va = calloc(1, sizeof(Example_Variant));
442 stderr, "ERROR: could not allocate an Example_Variant struct.\n");
446 va = calloc(1, sizeof (Example_Variant));
448 va->t.type = eet_mapping[1].name;
450 printf("type gets %s\n", va->t.type);
452 st2 = calloc(1, sizeof (Example_Struct2));
453 _st2_set(st2, atoi(v1), atoi(v2));
459 static Example_Variant *
460 _variant_3_new(const char *v1)
462 Example_Struct3 *st3;
463 Example_Variant *va = calloc(1, sizeof(Example_Variant));
467 stderr, "ERROR: could not allocate an Example_Variant struct.\n");
471 va = calloc(1, sizeof (Example_Variant));
472 va->t.type = eet_mapping[2].name;
473 st3 = calloc(1, sizeof (Example_Struct3));
474 _st3_set(st3, atoi(v1));
480 static Example_Lists *
483 Example_Lists *example_lists = calloc(1, sizeof(Example_Lists));
486 fprintf(stderr, "ERROR: could not allocate a Example_Lists struct.\n");
490 return example_lists;
494 _union_free(Example_Union *un)
496 if (un->type == EET_STRUCT1)
498 Example_Struct1 *st1 = &(un->u.st1);
499 _string_free(st1->s1);
506 _variant_free(Example_Variant *va)
508 if (!strcmp(va->t.type, eet_mapping[0].name))
510 Example_Struct1 *st1 = va->data;
511 _string_free(st1->s1);
519 _data_free(Example_Lists *cache)
524 EINA_LIST_FREE(cache->union_list, un)
527 EINA_LIST_FREE(cache->variant_list, va)
533 static Example_Lists *
534 _data_load(const char *filename)
537 Eet_File *ef = eet_open(filename, EET_FILE_MODE_READ);
540 fprintf(stderr, "ERROR: could not open '%s' for read\n", filename);
544 data = eet_data_read(ef, _lists_descriptor, CACHE_FILE_ENTRY);
552 eet_close(_cache_file);
555 _cache_dict = eet_dictionary_get(ef);
561 _data_save(const Example_Lists *cache,
562 const char *filename)
570 len = eina_strlcpy(tmp, filename, sizeof(tmp));
571 if (len + 12 >= (int)sizeof(tmp))
573 fprintf(stderr, "ERROR: file name is too big: %s\n", filename);
580 snprintf(tmp + len, 12, ".%u", i);
583 while (stat(tmp, &st) == 0);
585 ef = eet_open(tmp, EET_FILE_MODE_WRITE);
588 fprintf(stderr, "ERROR: could not open '%s' for write\n", tmp);
593 (ef, _lists_descriptor, CACHE_FILE_ENTRY, cache, EINA_TRUE);
600 rename(tmp, filename);
607 _print_union(const Example_Union *un)
609 printf("\t | type: %s'\n", eet_mapping[un->type - 1].name);
614 printf("\t\t val1: %f\n", un->u.st1.val1);
615 printf("\t\t stuff: %d\n", un->u.st1.stuff);
616 printf("\t\t s1: %s\n", un->u.st1.s1);
620 printf("\t\t val1: %i\n", un->u.st2.b1);
621 printf("\t\t stuff: %lli\n", un->u.st2.v1);
625 printf("\t\t val1: %i\n", un->u.st3.body);
634 _print_variant(const Example_Variant *va)
636 printf("\t | type: %s'\n", va->t.type);
638 switch (va->t.type[2])
642 Example_Struct1 *st1 = va->data;
644 printf("\t\t val1: %f\n", st1->val1);
645 printf("\t\t stuff: %d\n", st1->stuff);
646 printf("\t\t s1: %s\n", st1->s1);
652 Example_Struct2 *st2 = va->data;
654 printf("\t\t val1: %i\n", st2->b1);
655 printf("\t\t stuff: %lli\n", st2->v1);
661 Example_Struct3 *st3 = va->data;
663 printf("\t\t val1: %i\n", st3->body);
676 Example_Lists *data_lists;
682 "Usage:\n\t%s <input> <output> [action action-params]\n\n"
683 "where actions and their parameters are:\n"
684 "\tunion <type> [fields]\n"
685 "\tvariant <type> [fields]\n"
693 _data_descriptors_init();
695 data_lists = _data_load(argv[1]);
698 printf("Creating new data lists.\n");
699 data_lists = _data_new();
709 if (strcmp(argv[3], "union") == 0)
713 int type = atoi(argv[4]);
716 if (type < EET_STRUCT1 || type > EET_STRUCT3)
719 "ERROR: invalid type parameter (%s).\n",
730 stderr, "ERROR: wrong number of parameters"
736 argv[5], argv[6], argv[7]);
740 stderr, "ERROR: could not create the "
741 "requested union.\n");
744 data_lists->union_list =
745 eina_list_append(data_lists->union_list, un);
752 stderr, "ERROR: wrong number of parameters"
757 un = _union_2_new(argv[5], argv[6]);
761 stderr, "ERROR: could not create the "
762 "requested union.\n");
765 data_lists->union_list =
766 eina_list_append(data_lists->union_list, un);
773 stderr, "ERROR: wrong number of parameters"
778 un = _union_3_new(argv[5]);
782 stderr, "ERROR: could not create the "
783 "requested union.\n");
786 data_lists->union_list =
787 eina_list_append(data_lists->union_list, un);
792 stderr, "ERROR: bad type of of struct passed\n");
798 "ERROR: wrong number of parameters (%d).\n",
801 else if (strcmp(argv[3], "variant") == 0)
805 int type = atoi(argv[4]);
808 if (type < EET_STRUCT1 || type > EET_STRUCT3)
811 "ERROR: invalid type parameter (%s).\n",
822 stderr, "ERROR: wrong number of parameters"
828 argv[5], argv[6], argv[7]);
832 stderr, "ERROR: could not create the "
833 "requested variant.\n");
836 data_lists->variant_list =
837 eina_list_append(data_lists->variant_list, va);
844 stderr, "ERROR: wrong number of parameters"
849 va = _variant_2_new(argv[5], argv[6]);
853 stderr, "ERROR: could not create the "
854 "requested variant.\n");
857 data_lists->variant_list =
858 eina_list_append(data_lists->variant_list, va);
865 stderr, "ERROR: wrong number of parameters"
870 va = _variant_3_new(argv[5]);
874 stderr, "ERROR: could not create the "
875 "requested variant.\n");
878 data_lists->variant_list =
879 eina_list_append(data_lists->variant_list, va);
884 stderr, "ERROR: bad type of of struct passed\n");
890 "ERROR: wrong number of parameters (%d).\n",
894 fprintf(stderr, "ERROR: unknown action '%s'\n", argv[3]);
898 printf("Cached data:\n");
900 printf("\tstats: unions=%u, variants=%u\n",
901 eina_list_count(data_lists->union_list),
902 eina_list_count(data_lists->variant_list));
904 if (eina_list_count(data_lists->union_list))
907 const Example_Union *un;
908 printf("\t * union list:\n");
910 EINA_LIST_FOREACH(data_lists->union_list, l, un)
916 if (eina_list_count(data_lists->variant_list))
919 const Example_Variant *un;
920 printf("\t * variant list:\n");
922 EINA_LIST_FOREACH(data_lists->variant_list, l, un)
930 if (!_data_save(data_lists, argv[2]))
933 _data_free(data_lists);
937 eet_close(_cache_file);
938 _data_descriptors_shutdown();