From: Rafał Długołęcki Date: Sun, 12 Jan 2014 00:32:35 +0000 (+0100) Subject: Added parents to the individual structure and children to family structure. X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=338b1e4b2fd96379fa007962949fa99a7e070cd6;p=familia.git Added parents to the individual structure and children to family structure. --- diff --git a/INSTALL b/INSTALL index 007e939..2099840 100644 --- a/INSTALL +++ b/INSTALL @@ -12,8 +12,8 @@ without warranty of any kind. Basic Installation ================== - Briefly, the shell commands `./configure; make; make install' should -configure, build, and install this package. The following + Briefly, the shell command `./configure && make && make install' +should configure, build, and install this package. The following more-detailed instructions are generic; see the `README' file for instructions specific to this package. Some packages provide this `INSTALL' file but do not implement all of the features documented diff --git a/doc/model.dia b/doc/model.dia index 2e8dd4b..5490dcd 100644 Binary files a/doc/model.dia and b/doc/model.dia differ diff --git a/src/storage/family.c b/src/storage/family.c index e08223c..f380609 100644 --- a/src/storage/family.c +++ b/src/storage/family.c @@ -18,6 +18,9 @@ struct familia_family * familia_family_new() family->individual1 = NULL; family->individual2 = NULL; + family->children = NULL; + family->children_no = 0; + return family; } @@ -26,6 +29,17 @@ void familia_family_free(struct familia_family * family) family->individual1 = NULL; family->individual2 = NULL; + if (family->children) { + int i = 0; + /* Remove all children links. */ + for (; i < family->children_no; i++) { + family->children[i] = NULL; + } + free(family->children); + family->children = NULL; + family->children_no = 0; + } + free(family); } @@ -43,21 +57,78 @@ void familia_family_set_individual(struct familia_family * family, struct famili struct familia_individual * familia_family_get_individual(struct familia_family * family, enum FFamilyIndividualIndex index) { + struct familia_individual * individual = NULL; + switch (index) { case Individual1: - return family->individual1; + individual = family->individual1; case Individual2: - return family->individual2; + individual = family->individual2; } - return NULL; + + return individual; } struct familia_individual * familia_family_remove_individual(struct familia_family * family, enum FFamilyIndividualIndex index) { + struct familia_individual * individual = NULL; + switch (index) { case Individual1: + individual = family->individual1; family->individual1 = NULL; case Individual2: + individual = family->individual2; family->individual2 = NULL; } + + return individual; +} + +void familia_family_add_child(struct familia_family * family, struct familia_individual * individual) +{ + struct familia_individual ** tmp = NULL; + int size = (family->children_no + 1); + + tmp = realloc(family->children, size * sizeof(struct familia_individual *)); + + if (tmp) { + family->children = tmp; + family->children[family->children_no] = individual; + family->children_no++; + } + else { + debug("There were problems with allocating memory for family children.\n"); + } +} + +struct familia_individual * familia_family_get_child_by_id(struct familia_family * family, unsigned short index) +{ + struct familia_individual * child = NULL; + + if (index < family->children_no) { + child = family->children[index]; + } + + return child; +} + +struct familia_individual * familia_family_remove_child_by_id(struct familia_family * family, unsigned short index) +{ + struct familia_individual * child = NULL; + + child = familia_family_get_child_by_id(family, index); + + if (child) { + int i = index; + family->children[index] = NULL; + + for (; i < family->children_no - 1; i++) { + family->children[i] = family->children[i + 1]; + } + + family->children[--(family->children_no)] = NULL; + } + + return child; } diff --git a/src/storage/family.h b/src/storage/family.h index d369167..a8dc2fd 100644 --- a/src/storage/family.h +++ b/src/storage/family.h @@ -17,8 +17,8 @@ * * ****************************************************************************/ -#ifndef _FAMILIA_MARRIAGE_H -#define _FAMILIA_MARRIAGE_H +#ifndef _FAMILIA_FAMILY_H +#define _FAMILIA_FAMILY_H #include "individual.h" @@ -39,6 +39,12 @@ struct familia_family { /** Second individual of family */ struct familia_individual * individual2; + + /** Children of this family */ + struct familia_individual ** children; + + /** Number of children of this family */ + unsigned short children_no; }; /** @@ -50,7 +56,7 @@ struct familia_family * familia_family_new(); /** * Frees allocated memory of the given family - * DISCLAIMER! This function does not remove linked individuals. You have to + * DISCLAIMER! This function does not free memory of linked individuals. You have to * remove them manually from storage. * @parameter individual free */ @@ -72,11 +78,40 @@ struct familia_individual * familia_family_get_individual(struct familia_family /** * Removes individual from the given family - * DISCLAIMER! This function does not remove linked individuals. You have to + * DISCLAIMER! This function does not free memory of linked individuals. You have to * remove them manually from storage. * @parameter family to remove the individual * @parameter individual index of which individual to remove + * @return returns removed individual, NULL if such does not exist. */ struct familia_individual * familia_family_remove_individual(struct familia_family * family, enum FFamilyIndividualIndex index); -#endif /*_FAMILIA_MARRIAGE_H */ +/** + * Adds new child in the given family + * @parameter family to set the individual + * @parameter individual which will be added + */ +void familia_family_add_child(struct familia_family * family, struct familia_individual * individual); + +/** + * Gets the nth child from the given family + * Child with index = 0 is the oldest. Bigger the index, the child is younger + * @parameter family to get the child + * @parameter individual index of which individual get + * @return selected child. If index is bigger than child_no of the family + * returns NULL. + */ +struct familia_individual * familia_family_get_child_by_id(struct familia_family * family, unsigned short index); + +/** + * Removes child from the given family + * DISCLAIMER! This function does not free memory of linked children. You have to + * remove them manually from storage. + * @parameter family to remove the child + * @parameter index of which child should be removed + * @return removed child. If such does not exist or index is bigger than child_no + * of the family, returns NULL. + */ +struct familia_individual * familia_family_remove_child_by_id(struct familia_family * family, unsigned short index); + +#endif /*_FAMILIA_FAMILY_H */ diff --git a/src/storage/individual.c b/src/storage/individual.c index 6293ce8..5e55bb6 100644 --- a/src/storage/individual.c +++ b/src/storage/individual.c @@ -170,3 +170,18 @@ void familia_individual_remove_family_by_id(struct familia_individual * individu individual->families_no--; } } + +void familia_individual_set_parents(struct familia_individual * individual, struct familia_family * family) +{ + individual->parents = family; +} + +struct familia_family * familia_individual_get_parents(struct familia_individual * individual) +{ + return individual->parents; +} + +void familia_individual_remove_parents(struct familia_individual * individual) +{ + individual->parents = NULL; +} diff --git a/src/storage/individual.h b/src/storage/individual.h index cd4d448..87045c1 100644 --- a/src/storage/individual.h +++ b/src/storage/individual.h @@ -40,6 +40,9 @@ struct familia_individual { /** Number of families */ unsigned int families_no; + + /** Parent family. Family this individual comes from. */ + struct familia_family * parents; }; /** @@ -51,7 +54,7 @@ struct familia_individual * familia_individual_new(); /** * Frees allocated memory of the given individual - * DISCLAIMER! This function does not remove linked families. You have to + * DISCLAIMER! This function does not free memory of linked families. You have to * remove them manually from storage. * @parameter individual to free */ @@ -96,17 +99,41 @@ void familia_individual_add_family(struct familia_individual * individual, struc * Gets family with the given id of the given individual * @parameter individual from whom get the family * @parameter family id of the individual - * @return individuals last name + * @return selected family or NULL if such does not exists or index is out of + * array bounds */ struct familia_family * familia_individual_get_family_by_id(struct familia_individual * individual, unsigned int id); /** * Removes family with the given id from the given individual - * DISCLAIMER! This function does not remove linked families. You have to + * DISCLAIMER! This function does not free memory of linked families. You have to * remove them manually from storage. * @parameter individual from whom family will be removed * @parameter family id of the individual */ void familia_individual_remove_family_by_id(struct familia_individual * individual, unsigned int id); +/** + * Sets parents family of the given individual + * @parameter individual to set parents + * @parameter parents to set + */ +void familia_individual_set_parents(struct familia_individual * individual, struct familia_family * family); + +/** + * Gets family with the given id of the given individual + * @parameter individual from whom get the family + * @parameter family id of the individual + * @return individuals last name + */ +struct familia_family * familia_individual_get_parents(struct familia_individual * individual); + +/** + * Removes parents from the given individual + * DISCLAIMER! This function does not free memory of linked family. You have to + * remove them manually from storage. + * @parameter individual from whom parents will be removed + */ +void familia_individual_remove_parents(struct familia_individual * individual); + #endif /*_FAMILIA_INDIVIDUAL_H */