5 /*#include "utf8tools.h"*/
7 #include "storage/storage.h"
8 #include "storage/individual.h"
9 #include "storage/family.h"
11 #include "memory_stack.h"
15 struct familia_memory_stack * memory_stack = NULL;
18 * Structure holding ID key - object pair.
20 struct familia_memory_stack_object {
21 /** Unique identifier parsed from the file */
24 /** Pointer to the loaded object */
29 * Structure holding relations between parser and program internals
31 struct familia_memory_stack {
32 /** Loaded objects from the parser */
33 struct familia_memory_stack_object ** objects;
35 /** Number of objects */
36 unsigned int objects_no;
39 void familia_memory_stack_init()
41 struct familia_storage * storage = familia_storage_get_current();
43 assert(memory_stack == NULL);
44 memory_stack = (struct familia_memory_stack*)malloc(sizeof(struct familia_memory_stack));
46 assert(storage != NULL);
49 void familia_memory_stack_dump()
53 assert(memory_stack != NULL);
55 fprintf(stderr, " --------------------------- ----------------- \n");
56 for (i = 0; i < memory_stack->objects_no; i++) {
57 fprintf(stderr, "| %25s | %15s |\n",
58 memory_stack->objects[i]->xref_key,
59 memory_stack->objects[i]->object ? "initialized" : "empty");
63 void familia_memory_stack_free()
67 for (i = 0; i < memory_stack->objects_no; i++) {
68 free(memory_stack->objects[i]->xref_key);
69 memory_stack->objects[i]->xref_key = NULL;
71 /* We do not remove object in each element, because it
72 * is a pointer to stored in storage memory object
75 free(memory_stack->objects[i]);
76 memory_stack->objects[i] = NULL;
78 memory_stack->objects_no = 0;
86 * Adds the given object to the memory stack
87 * @param xref_key string under which the object will be found.
88 * @param object object which will be added to the memory stack
90 void _familia_memory_stack_add(char * xref_key, void * object)
92 struct familia_memory_stack_object ** tmp = NULL;
93 struct familia_memory_stack_object * element = NULL;
94 unsigned int size = 0;
97 xref_length = strlen(xref_key);
98 size = memory_stack->objects_no + 1;
100 tmp = realloc(memory_stack->objects,
101 (size) * sizeof(struct familia_memory_stack_object *));
104 memory_stack->objects = tmp;
105 element = malloc(sizeof(struct familia_memory_stack_object));
108 element->xref_key = (char *)calloc(xref_length + 1, sizeof(char));
109 strncpy(element->xref_key, xref_key, xref_length);
110 element->object = object;
113 memory_stack->objects[memory_stack->objects_no++] = element;
116 debug("Failed adding parser stack element!");
122 * Returns object stored under xref_key of the given type in the memory stack.
123 * @param xref_key string under which the object will be found.
124 * @return pointer to the found object. NULL if it was not found
126 void * _familia_memory_stack_get(char *xref_key)
129 for (i = 0; i < memory_stack->objects_no; i++) {
130 if (strcmp(memory_stack->objects[i]->xref_key, xref_key) == 0) {
131 return memory_stack->objects[i]->object;
139 * Creates new object stored under xref_key of the given type in the memory stack.
140 * This method always returns pointer to the newly created object.
141 * NULL is returned when there was a problem with allocating memory.
142 * @param xref_key string under which the object will be found. It should be
143 * unique and its the user's order to make it unique.
144 * @param object_type type of the stored object
145 * @return pointer to the newly created object. NULL on memory allocation error.
147 void * _familia_memory_stack_create(char *xref_key, enum familia_storage_type type)
149 void * object = NULL;
150 struct familia_storage * storage = NULL;
152 storage = familia_storage_get_current();
156 object = familia_individual_new();
157 familia_storage_add_individual(storage, object);
160 object = familia_family_new();
161 familia_storage_add_family(storage, object);
165 _familia_memory_stack_add(xref_key, object);
170 void * familia_memory_stack_find(char *xref_key, enum familia_storage_type type)
172 void * object = NULL;
173 object = _familia_memory_stack_get(xref_key);
176 object = _familia_memory_stack_create(xref_key, type);