Fixed storage dump display. Changed some variables and function names to more obvious...
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Mon, 20 Jan 2014 00:38:35 +0000 (01:38 +0100)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Mon, 20 Jan 2014 00:38:35 +0000 (01:38 +0100)
src/gedcom/familia_gedcom.c
src/memory_stack.c
src/storage/family.c
src/storage/family.h
src/storage/individual.c
src/storage/storage.c

index dcb80328a5cfc883de9192ce3818e4da4720f234..b547857c128312c2697a82f8867f768a36c27a07 100644 (file)
@@ -25,7 +25,7 @@ void familia_gedcom_header_end (Gedcom_rec rec, Gedcom_ctxt self)
 }
 
 Gedcom_ctxt familia_gedcom_header_source_start(Gedcom_elt  elt,
-                                       Gedcom_ctxt parent,
+                                       Gedcom_ctxt ctxt,
                                        int         level,
                                        char*       tag,
                                        char*       raw_value,
@@ -34,12 +34,12 @@ Gedcom_ctxt familia_gedcom_header_source_start(Gedcom_elt  elt,
 {
        char *source = GEDCOM_STRING(parsed_value);
        printf("This file was created by %s\n", source);
-       return parent;
+       return ctxt;
 }
 
 /*
 void familia_gedcom_header_source_end(Gedcom_elt  elt,
-                                       Gedcom_ctxt parent,
+                                       Gedcom_ctxt ctxt,
                                        Gedcom_ctxt self,
                                        Gedcom_val  parsed_value)
 {
@@ -48,7 +48,7 @@ void familia_gedcom_header_source_end(Gedcom_elt  elt,
 */
 
 Gedcom_ctxt familia_gedcom_header_version_start(Gedcom_elt  elt,
-                                       Gedcom_ctxt parent,
+                                       Gedcom_ctxt ctxt,
                                        int         level,
                                        char*       tag,
                                        char*       raw_value,
@@ -57,11 +57,11 @@ Gedcom_ctxt familia_gedcom_header_version_start(Gedcom_elt  elt,
 {
        char *source = GEDCOM_STRING(parsed_value);
        printf("Program version: %s (l: %d, tag: %s, raw:%s) \n", source, level, tag, raw_value);
-       return parent;
+       return ctxt;
 }
 /*
 void familia_gedcom_header_version_end(Gedcom_elt  elt,
-                                       Gedcom_ctxt parent,
+                                       Gedcom_ctxt ctxt,
                                        Gedcom_ctxt self,
                                        Gedcom_val  parsed_value)
 {
@@ -111,26 +111,26 @@ void familia_gedcom_individual_end (Gedcom_rec rec, Gedcom_ctxt self)
 }
 */
 Gedcom_ctxt familia_gedcom_individual_set_first_name(Gedcom_elt  elt,
-                                       Gedcom_ctxt parent,
+                                       Gedcom_ctxt ctxt,
                                        int         level,
                                        char*       tag,
                                        char*       raw_value,
                                        int         parsed_tag,
                                        Gedcom_val  parsed_value)
 {
-       familia_individual_set_first_name(parent, GEDCOM_STRING(parsed_value));
-       return parent;
+       familia_individual_set_first_name(ctxt, GEDCOM_STRING(parsed_value));
+       return ctxt;
 }
 /*
 void familia_gedcom_individual_first_name_end(Gedcom_elt  elt,
-                                       Gedcom_ctxt parent,
+                                       Gedcom_ctxt ctxt,
                                        Gedcom_ctxt self,
                                        Gedcom_val  parsed_value)
 {
 }
 */
 Gedcom_ctxt familia_gedcom_individual_last_name_start(Gedcom_elt  elt,
-                                       Gedcom_ctxt parent,
+                                       Gedcom_ctxt ctxt,
                                        int         level,
                                        char*       tag,
                                        char*       raw_value,
@@ -138,13 +138,13 @@ Gedcom_ctxt familia_gedcom_individual_last_name_start(Gedcom_elt  elt,
                                        Gedcom_val  parsed_value)
 {
        if (GEDCOM_IS_STRING(parsed_value)) {
-               familia_individual_set_last_name(parent, GEDCOM_STRING(parsed_value));
+               familia_individual_set_last_name(ctxt, GEDCOM_STRING(parsed_value));
        }
-       return parent;
+       return ctxt;
 }
 /*
 void familia_gedcom_individual_last_name_end(Gedcom_elt  elt,
-                                       Gedcom_ctxt parent,
+                                       Gedcom_ctxt ctxt,
                                        Gedcom_ctxt self,
                                        Gedcom_val  parsed_value)
 {
@@ -152,47 +152,113 @@ void familia_gedcom_individual_last_name_end(Gedcom_elt  elt,
 */
 
 Gedcom_ctxt familia_gedcom_individual_add_family(Gedcom_elt  elt,
-                                       Gedcom_ctxt parent,
+                                       Gedcom_ctxt ctxt,
                                        int         level,
                                        char*       tag,
                                        char*       raw_value,
                                        int         parsed_tag,
                                        Gedcom_val  parsed_value)
 {
-       void * object = NULL;
+       void * family = NULL;
        struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
 
-       object = familia_memory_stack_find(xr->string, FS_FAMILY);
-       familia_individual_add_family(parent, object);
+       family = familia_memory_stack_find(xr->string, FS_FAMILY);
+       familia_individual_add_family(ctxt, family);
 
-       return parent;
+       return ctxt;
+}
+
+/**
+ * Set parents for child
+ */
+Gedcom_ctxt familia_gedcom_individual_set_parents(Gedcom_elt  elt,
+                                       Gedcom_ctxt ctxt,
+                                       int         level,
+                                       char*       tag,
+                                       char*       raw_value,
+                                       int         parsed_tag,
+                                       Gedcom_val  parsed_value)
+{
+       void * child = NULL;
+       /* XREF_PTR(FAM) */
+       struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
+
+       child = familia_memory_stack_find(xr->string, FS_INDIVIDUAL);
+       familia_individual_set_parents(child, ctxt);
+
+       return ctxt;
 }
 
 /**
  * Adds child to the family
  */
 Gedcom_ctxt familia_gedcom_family_add_child(Gedcom_elt  elt,
-                                       Gedcom_ctxt parent,
+                                       Gedcom_ctxt ctxt,
                                        int         level,
                                        char*       tag,
                                        char*       raw_value,
                                        int         parsed_tag,
                                        Gedcom_val  parsed_value)
 {
-       void * object = NULL;
+       void * family = NULL;
        /* XREF_PTR(FAM) */
        struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
 
-       object = familia_memory_stack_find(xr->string, FS_FAMILY);
-       /* parent: REC_INDI */
-       familia_family_add_child(object, parent);
+       family = familia_memory_stack_find(xr->string, FS_FAMILY);
+       familia_family_add_child(family, ctxt);
+
+       return ctxt;
+}
+
+/**
+ * Adds first parent to the family
+ * Parent is the first individual in family. It can be e.g. Husband
+ */
+Gedcom_ctxt familia_gedcom_family_set_parent1(Gedcom_elt  elt,
+                                       Gedcom_ctxt ctxt,
+                                       int         level,
+                                       char*       tag,
+                                       char*       raw_value,
+                                       int         parsed_tag,
+                                       Gedcom_val  parsed_value)
+{
+       void * parent = NULL;
+       /* XREF_PTR(INDI) */
+       struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
+
+       parent = familia_memory_stack_find(xr->string, FS_FAMILY);
+       /* ctxt: REC_FAM */
+       familia_family_set_parent(ctxt, parent, Individual1);
+
+       return ctxt;
+}
+
+/**
+ * Adds second parent to the family
+ * Parent is the first individual in family. It can be e.g. Wife
+ */
+Gedcom_ctxt familia_gedcom_family_set_parent2(Gedcom_elt  elt,
+                                       Gedcom_ctxt ctxt,
+                                       int         level,
+                                       char*       tag,
+                                       char*       raw_value,
+                                       int         parsed_tag,
+                                       Gedcom_val  parsed_value)
+{
+       void * parent = NULL;
+       /* XREF_PTR(INDI) */
+       struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
+
+       parent = familia_memory_stack_find(xr->string, FS_FAMILY);
+       /* ctxt: REC_FAM */
+       familia_family_set_parent(ctxt, parent, Individual2);
 
-       return parent;
+       return ctxt;
 }
 
 /*
 void familia_gedcom_individual_family_end(Gedcom_elt  elt,
-                                       Gedcom_ctxt parent,
+                                       Gedcom_ctxt ctxt,
                                        Gedcom_ctxt self,
                                        Gedcom_val  parsed_value)
 {
@@ -209,33 +275,27 @@ void familia_gedcom_init()
 {
        gedcom_set_message_handler(familia_gedcom_error);
 
-       gedcom_subscribe_to_record(REC_HEAD,
-                               familia_gedcom_header_start,
-                               familia_gedcom_header_end);
-       gedcom_subscribe_to_element(ELT_HEAD_SOUR,
-                               familia_gedcom_header_source_start,
-                               NULL);
-       gedcom_subscribe_to_element(ELT_HEAD_SOUR_VERS,
-                               familia_gedcom_header_version_start,
-                               NULL);
-       gedcom_subscribe_to_record(REC_FAM,
-                               familia_gedcom_family_start,
-                               NULL);
-       gedcom_subscribe_to_record(REC_INDI,
-                               familia_gedcom_individual_start,
-                               NULL);
-       gedcom_subscribe_to_element(ELT_SUB_PERS_NAME,
-                               familia_gedcom_individual_set_first_name,
-                               NULL);
-       gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_SURN,
-                               familia_gedcom_individual_last_name_start,
-                               NULL);
-       gedcom_subscribe_to_element(ELT_SUB_FAMS,
-                               familia_gedcom_family_add_child,
-                               NULL);
-       gedcom_subscribe_to_element(ELT_SUB_FAMC,
-                               familia_gedcom_family_add_child,
-                               NULL);
+       gedcom_subscribe_to_record(REC_HEAD, familia_gedcom_header_start, familia_gedcom_header_end);
+       /* Create and set family as current (set to ctxt) */
+       gedcom_subscribe_to_record(REC_FAM, familia_gedcom_family_start, NULL);
+       /* Create and set individual as current (set to ctxt) */
+       gedcom_subscribe_to_record(REC_INDI, familia_gedcom_individual_start, NULL);
+       gedcom_subscribe_to_element(ELT_HEAD_SOUR, familia_gedcom_header_source_start, NULL);
+       gedcom_subscribe_to_element(ELT_HEAD_SOUR_VERS, familia_gedcom_header_version_start, NULL);
+       /* Set in current individual first name */
+       gedcom_subscribe_to_element(ELT_SUB_PERS_NAME, familia_gedcom_individual_set_first_name, NULL);
+       /* Set in current individual last name */
+       gedcom_subscribe_to_element(ELT_SUB_PERS_NAME_SURN, familia_gedcom_individual_last_name_start, NULL);
+       /* Add to current individual new family */
+       gedcom_subscribe_to_element(ELT_SUB_FAMS, familia_gedcom_individual_add_family, NULL);
+       /* Set current family as parents to child */
+       gedcom_subscribe_to_element(ELT_FAM_CHIL, familia_gedcom_individual_set_parents, NULL);
+       /* Set current individual new parents */
+       gedcom_subscribe_to_element(ELT_SUB_FAMC, familia_gedcom_family_add_child, NULL);
+       /* Set in current family first parent (e.g. husband) */
+       gedcom_subscribe_to_element(ELT_FAM_HUSB, familia_gedcom_family_set_parent1, NULL);
+       /* Set in current family second parent (e.g. wife) */
+       gedcom_subscribe_to_element(ELT_FAM_WIFE, familia_gedcom_family_set_parent2, NULL);
 
        familia_memory_stack_init();
 }
index 9e32862c85c9799d36db0fc249f95770fc4937b7..61493150e1df4a39f0e205c94e5a489d74bfc9dd 100644 (file)
@@ -42,6 +42,8 @@ void familia_memory_stack_init()
 
        assert(memory_stack == NULL);
        memory_stack = (struct familia_memory_stack*)malloc(sizeof(struct familia_memory_stack));
+       memory_stack->objects = NULL;
+       memory_stack->objects_no = 0;
 
        assert(storage != NULL);
 }
index 1508cd929446ebc4f0fb9ec87114c6945586d82d..6e53a6a34ccef3e294bebe8aa4bd2d8b90b709ef 100644 (file)
@@ -44,7 +44,7 @@ void familia_family_free(struct familia_family * family)
        free(family);
 }
 
-void familia_family_set_individual(struct familia_family * family, struct familia_individual * individual, enum FFamilyIndividualIndex index)
+void familia_family_set_parent(struct familia_family * family, struct familia_individual * individual, enum FFamilyIndividualIndex index)
 {
        switch (index) {
        case Individual1:
@@ -56,21 +56,23 @@ 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 * familia_family_get_parent(struct familia_family * family, enum FFamilyIndividualIndex index)
 {
        struct familia_individual * individual = NULL;
 
        switch (index) {
        case Individual1:
                individual = family->individual1;
+               break;
        case Individual2:
                individual = family->individual2;
+               break;
        }
 
        return individual;
 }
 
-struct familia_individual * familia_family_remove_individual(struct familia_family * family, enum FFamilyIndividualIndex index)
+struct familia_individual * familia_family_remove_parent(struct familia_family * family, enum FFamilyIndividualIndex index)
 {
        struct familia_individual * individual = NULL;
 
@@ -98,7 +100,7 @@ void familia_family_add_child(struct familia_family * family, struct familia_ind
                family->children = tmp;
                family->children[family->children_no] = individual;
                family->children_no++;
-               familia_individual_set_parents(individual, family);
+/*             familia_individual_set_parents(individual, family);*/
        }
        else {
                debug("There were problems with allocating memory for family children.\n");
index 50fb19cfb8757e5debda2424b568b5664a315eb3..adf2dac5502daca279cf5375c2d34b7ce6d82abc 100644 (file)
@@ -67,14 +67,14 @@ void familia_family_free(struct familia_family * family);
  * @parameter family to set the individual
  * @parameter individual which will be set
  */
-void familia_family_set_individual(struct familia_family * family, struct familia_individual * individual, enum FFamilyIndividualIndex index);
+void familia_family_set_parent(struct familia_family * family, struct familia_individual * individual, enum FFamilyIndividualIndex index);
 
 /**
  * Gets individual from the given family
  * @parameter family to get the individual
  * @parameter individual index of which individual get
  */
-struct familia_individual * familia_family_get_individual(struct familia_family * family, enum FFamilyIndividualIndex index);
+struct familia_individual * familia_family_get_parent(struct familia_family * family, enum FFamilyIndividualIndex index);
 
 /**
  * Removes individual from the given family
@@ -84,7 +84,7 @@ struct familia_individual * familia_family_get_individual(struct familia_family
  * @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);
+struct familia_individual * familia_family_remove_parent(struct familia_family * family, enum FFamilyIndividualIndex index);
 
 /**
  * Adds new child in the given family
index 856796d1f9d7767914e7dbd86e44dd04d2d9de78..33a0a08c9f5e809b583e28092db013ed349ef0b5 100644 (file)
@@ -20,6 +20,7 @@ struct familia_individual * familia_individual_new()
        individual->last_name = NULL;
        individual->families = NULL;
        individual->families_no = 0;
+       individual->parents = NULL;
 
        return individual;
 }
@@ -48,6 +49,10 @@ void familia_individual_free(struct familia_individual * individual)
                individual->families_no = 0;
        }
 
+       if (individual->parents) {
+               individual->parents = NULL;
+       }
+
        free(individual);
 }
 
@@ -74,10 +79,6 @@ void familia_individual_set_first_name(struct familia_individual * individual, c
 
 char * familia_individual_get_first_name(struct familia_individual * individual)
 {
-       if (!individual) {
-               return NULL;
-       }
-
        return individual->first_name;
 }
 
index 8b00a716308ffc9eceae0c1d8f1b88b785b904de..c54e6a0026a8e0c2feee5d41208e93ca74de1085 100644 (file)
@@ -35,7 +35,7 @@ struct familia_storage * familia_storage_new()
        storage->families = NULL;
        storage->families_no = 0;
 
-       _familia_storage_positions_init();
+/*     _familia_storage_positions_init();*/
 
        return storage;
 }
@@ -64,7 +64,7 @@ void familia_storage_free(struct familia_storage * storage)
                storage->families_no = 0;
        }
 
-       _familia_storage_positions_free();
+/*     _familia_storage_positions_free();*/
 }
 
 void familia_storage_add_individual(struct familia_storage * storage, struct familia_individual * individual)
@@ -76,6 +76,7 @@ void familia_storage_add_individual(struct familia_storage * storage, struct fam
                tmp = realloc(storage->individuals, size * sizeof(struct familia_individual *));
                
                if (tmp) {
+                       individual->id = storage->individuals_no;
                        storage->individuals = tmp;
                        storage->individuals[storage->individuals_no] = individual;
                        storage->individuals_no++;
@@ -142,6 +143,7 @@ void familia_storage_add_family(struct familia_storage * storage, struct familia
                tmp = realloc(storage->families, size * sizeof(struct familia_family *));
                
                if (tmp) {
+                       family->id = storage->families_no;
                        storage->families = tmp;
                        storage->families[storage->families_no] = family;
                        storage->families_no++;
@@ -201,60 +203,74 @@ void familia_storage_remove_family_by_id(struct familia_storage * storage, unsig
 
 void familia_storage_dump_all()
 {
-       int i;
+       unsigned int i;
        struct familia_storage * storage = NULL;
        struct familia_family * family = NULL;
        struct familia_individual * individual = NULL;
+       struct familia_individual * i1 = NULL;
+       struct familia_individual * i2 = NULL;
        
        storage = familia_storage_get_current();
        
        for (i = 0; i < storage->individuals_no; i++) {
+               struct familia_family * f = NULL;
+
                individual = storage->individuals[i];
+
                printf("Individual (%d)\n", individual->id);
                printf("\t First name: %s\n", individual->first_name);
                printf("\t Last name: %s\n", individual->last_name);
+
                if (individual->families_no > 0) {
-                       int j;
-                       struct familia_family * f = NULL;
-                       struct familia_individual * i1 = NULL;
-                       struct familia_individual * i2 = NULL;
+                       unsigned int j;
 
                        printf("\t Families:\n");
                        for (j = 0; j < individual->families_no; j++) {
                                f = individual->families[j];
-                               i1 = familia_family_get_individual(f, Individual1);
-                               i2 = familia_family_get_individual(f, Individual2);
+                               i1 = familia_family_get_parent(f, Individual1);
+                               i2 = familia_family_get_parent(f, Individual2);
                                
-                               printf("\t > %s&%s\n", familia_individual_get_first_name(i1), familia_individual_get_first_name(i2));
+                               printf("\t > %s&%s\n",
+                                       i1 ? familia_individual_get_first_name(i1) : "NULL",
+                                       i2 ? familia_individual_get_first_name(i2) : "NULL");
                        }
-                       printf("\t Parents:\n");
-                       f = familia_individual_get_parents(individual);
-                       i1 = familia_family_get_individual(f, Individual1);
-                       i2 = familia_family_get_individual(f, Individual2);
-                       
-                       printf("\t > %s&%s\n", familia_individual_get_first_name(i1), familia_individual_get_first_name(i2));
+               }
+
+               printf("\t Parents:\n");
+               f = NULL;
+               f = familia_individual_get_parents(individual);
+
+               if (f) {
+                       i1 = familia_family_get_parent(f, Individual1);
+                       i2 = familia_family_get_parent(f, Individual2);
+               
+                       printf("\t > %s&%s\n",
+                               i1 ? familia_individual_get_first_name(i1) : "NULL",
+                               i2 ? familia_individual_get_first_name(i2) : "NULL");
+               }
+               else {
+                       printf("\t (none)\n");
                }
        }
 
 
        for (i = 0; i < storage->families_no; i++) {
-               int j;
-               struct familia_individual * i1 = NULL;
-               struct familia_individual * i2 = NULL;
+               unsigned int j;
 
                family = storage->families[i];
 
                printf("Family (%d)\n", family->id);
-               i1 = familia_family_get_individual(family, Individual1);
-               i2 = familia_family_get_individual(family, Individual2);
-               printf("\t First parent: %s\n", familia_individual_get_first_name(i1));
-               printf("\t Second parent: %s\n", familia_individual_get_first_name(i2));
+               i1 = familia_family_get_parent(family, Individual1);
+               printf("\t First parent: %s\n", i1 ? familia_individual_get_first_name(i1) : "NULL");
+
+               i2 = familia_family_get_parent(family, Individual2);
+               printf("\t Second parent: %s\n", i2 ? familia_individual_get_first_name(i2) : "NULL");
 
                printf("\t Children:\n");
                if (family->children_no > 0) {
                        for (j = 0; j < family->children_no; j++) {
                                i1 = family->children[j];
-                               printf("\t > %s\n", familia_individual_get_first_name(i1));
+                               printf("\t > %s\n", i1 ? familia_individual_get_first_name(i1) : "NULL");
                        }
                }
        }