More generic use of event.
[familia.git] / src / storage / family.c
1 #include <stdlib.h>
2
3 #include "family.h"
4 #include "individual.h"
5
6 #include "../debug.h"
7
8 struct familia_family * familia_family_new()
9 {
10         struct familia_family * family = NULL;
11         family = malloc(sizeof(struct familia_family));
12
13         if (!family) {
14                 debug("["__FILE__ "] There was a problem with allocating memory for family.\n");
15                 return NULL;
16         }
17
18         family->id = 0;
19         family->individual1 = NULL;
20         family->individual2 = NULL;
21
22         family->children = NULL;
23         family->children_no = 0;
24
25         return family;
26 }
27
28 void familia_family_free(struct familia_family * family)
29 {
30         family->individual1 = NULL;
31         family->individual2 = NULL;
32
33         if (family->children) {
34                 int i = 0;
35                 /* Remove all children links. */
36                 for (; i < family->children_no; i++) {
37                         family->children[i] = NULL;
38                 }
39                 free(family->children);
40                 family->children = NULL;
41                 family->children_no = 0;
42         }
43
44         free(family);
45 }
46
47 void familia_family_set_parent(struct familia_family * family, struct familia_individual * individual, enum FFamilyIndividualIndex index)
48 {
49         switch (index) {
50         case Individual1:
51                 family->individual1 = individual;
52                 break;
53         case Individual2:
54                 family->individual2 = individual;
55                 break;
56         }
57 }
58
59 struct familia_individual * familia_family_get_parent(struct familia_family * family, enum FFamilyIndividualIndex index)
60 {
61         struct familia_individual * individual = NULL;
62
63         switch (index) {
64         case Individual1:
65                 individual = family->individual1;
66                 break;
67         case Individual2:
68                 individual = family->individual2;
69                 break;
70         }
71
72         return individual;
73 }
74
75 struct familia_individual * familia_family_remove_parent(struct familia_family * family, enum FFamilyIndividualIndex index)
76 {
77         struct familia_individual * individual = NULL;
78
79         switch (index) {
80         case Individual1:
81                 individual = family->individual1;
82                 family->individual1 = NULL;
83         case Individual2:
84                 individual = family->individual2;
85                 family->individual2 = NULL;
86         }
87
88         return individual;
89 }
90
91 void familia_family_add_child(struct familia_family * family, struct familia_individual * individual)
92 {
93         struct familia_individual ** tmp = NULL;
94         int size = (family->children_no + 1);
95
96         tmp = realloc(family->children, size * sizeof(struct familia_individual *));
97
98         if (tmp) {
99                 /* TODO: Add children by their age */
100                 family->children = tmp;
101                 family->children[family->children_no] = individual;
102                 family->children_no++;
103 /*              familia_individual_set_parents(individual, family);*/
104         }
105         else {
106                 debug("There were problems with allocating memory for family children.\n");
107         }
108 }
109
110 struct familia_individual * familia_family_get_child_by_id(struct familia_family * family, unsigned short index)
111 {
112         struct familia_individual * child = NULL;
113
114         if (index < family->children_no) {
115                 child = family->children[index];
116         }
117
118         return child;
119 }
120
121 struct familia_individual * familia_family_remove_child_by_id(struct familia_family * family, unsigned short index)
122 {
123         struct familia_individual * child = NULL;
124
125         child = familia_family_get_child_by_id(family, index);
126
127         if (child) {
128                 int i = index;
129                 family->children[index] = NULL;
130
131                 for (; i < family->children_no - 1; i++) {
132                         family->children[i] = family->children[i + 1];
133                 }
134
135                 family->children[--(family->children_no)] = NULL;
136         }
137
138         return child;
139 }