1 /****************************************************************************
2 * Familia Lignum - Genealogical program *
3 * Copyright (C) 2011-2012 Rafał Długołęcki <rafal@dlugolecki.net.pl> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; version 2 of the License. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License along *
15 * with this program; if not, write to the Free Software Foundation, Inc., *
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
18 ****************************************************************************/
26 #include "positions.h"
29 * Structure holding individual and position pair
31 struct familia_storage_individual_position {
32 struct familia_individual *ind;
37 * Structure holding family and position pair
39 struct familia_storage_family_position {
40 struct familia_family *fam;
45 * Structure holding individual and family positions
47 struct familia_storage_positions {
48 struct familia_storage_individual_position **individuals;
50 unsigned int individuals_no;
52 struct familia_storage_family_position **families;
54 unsigned int families_no;
58 * Global variable for storing positions of graphic objects.
60 * Example graphic object is an individual or family...
62 struct familia_storage_positions * storage_positions;
65 * Initializes storage for individual and family positions.
67 void _familia_storage_positions_init()
69 if (!storage_positions) {
70 storage_positions = malloc(sizeof(struct familia_storage_positions));
72 if (!storage_positions) {
73 debug("["__FILE__ "] There was a problem with allocating memory for storage of positions.\n");
76 storage_positions->individuals_no = 0;
77 storage_positions->families_no = 0;
83 * Frees storage for the positions
85 void _familia_storage_positions_free()
87 if (storage_positions) {
88 if (storage_positions->individuals) {
89 free(storage_positions->individuals);
92 if (storage_positions->families) {
93 free(storage_positions->families);
96 free(storage_positions);
97 storage_positions = NULL;
102 * Adds new position for the given individual.
104 * This function should be invoked only internaly and thus is not declared in
105 * header. Usage without caution can create duplicates in global variable
106 * storage_positions. Variable storage_positions is not prepared for such duplicates
107 * and this can provide to errors.
109 void _familia_positions_add_individual_position(struct familia_individual *individual, struct position *pos)
111 struct familia_storage_individual_position ** tmp = NULL;
112 unsigned int alloc_size = sizeof(struct familia_storage_individual_position *);
114 int size = (storage_positions->individuals_no + 1);
116 tmp = realloc(storage_positions->individuals, size * alloc_size);
119 storage_positions->individuals = tmp;
120 storage_positions->individuals[storage_positions->individuals_no]->ind = individual;
121 storage_positions->individuals[storage_positions->individuals_no]->pos = pos;
122 storage_positions->individuals_no++;
125 debug("There were problems with allocating memory for storage of individual positions->\n");
130 * Adds new position for the given family.
132 * This function should be invoked only internaly and thus is not declared in
133 * header. Usage without caution can create duplicates in global variable
134 * storage_positions. Variable storage_positions is not prepared for such duplicates
135 * and this can provide to errors.
137 void _familia_positions_add_family_position(struct familia_family *family, struct position *pos)
139 struct familia_storage_family_position ** tmp = NULL;
140 unsigned int alloc_size = sizeof(struct familia_storage_family_position *);
142 int size = (storage_positions->families_no + 1);
144 tmp = realloc(storage_positions->families, size * alloc_size);
147 storage_positions->families = tmp;
148 storage_positions->families[storage_positions->families_no]->fam = family;
149 storage_positions->families[storage_positions->families_no]->pos = pos;
150 storage_positions->families_no++;
153 debug("There were problems with allocating memory for storage of family positions.\n");
157 void familia_positions_set_individual_position(struct familia_individual *individual, struct position *pos)
159 struct position * p = NULL;
161 p = familia_position_get_individual_position(individual);
163 /* If not found create a new one */
165 _familia_positions_add_individual_position(individual, pos);
169 void familia_positions_set_family_position(struct familia_family *family, struct position *pos)
171 struct position * p = NULL;
173 p = familia_position_get_family_position(family);
175 /* If not found create a new one */
177 _familia_positions_add_family_position(family, pos);
181 struct position * familia_position_get_individual_position(struct familia_individual *individual)
187 * Uninitialized global. Return error.
189 if (!storage_positions || !storage_positions->individuals) {
190 debug("Trying to request individual position, but positions storage is not initialized.\n");
196 for (i = 0; i < storage_positions->individuals_no; i++) {
197 if (storage_positions->individuals[i]->ind->id == id) {
198 return storage_positions->individuals[i]->pos;
203 * Entry not found for a given individual. Return NULL.
208 struct position * familia_position_get_family_position(struct familia_family *family)
214 * Uninitialized global. Return error.
216 if (!storage_positions || !storage_positions->families) {
217 debug("Trying to request family position, but positions storage is not initialized.\n");
223 for (i = 0; i < storage_positions->families_no; i++) {
224 if (storage_positions->families[i]->fam->id == id) {
225 return storage_positions->families[i]->pos;
230 * Entry not found for a given individual. Return error.