Added reading date from the GEDCOM file and storing it in individual.
[familia.git] / src / gedcom / familia_gedcom.c
index b547857c128312c2697a82f8867f768a36c27a07..e92de3a43ca69903b87fa585ed228fa281869cf4 100644 (file)
@@ -6,6 +6,8 @@
 #include "../storage/individual.h"
 #include "../memory_stack.h"
 
+#include "../debug.h"
+
 Gedcom_ctxt familia_gedcom_header_start (Gedcom_rec rec,
                                        int level,
                                        Gedcom_val xref,
@@ -14,11 +16,13 @@ Gedcom_ctxt familia_gedcom_header_start (Gedcom_rec rec,
                                        int parsed_tag,
                                        Gedcom_val parsed_value)
 {
+       assert(rec == REC_HEAD);
+
        fprintf(stderr, "The header starts (l: %d, tag: %s, raw:%s) \n", level, tag, raw_value);
        return (Gedcom_ctxt)1;
 }
 
-void familia_gedcom_header_end (Gedcom_rec rec, Gedcom_ctxt self)
+void familia_gedcom_header_end (Gedcom_rec rec, Gedcom_ctxt self, Gedcom_val parsed_value)
 {
        /* context will print as "1" */
        fprintf(stderr, "The header ends, context is %d\n", (int)self);
@@ -79,16 +83,19 @@ Gedcom_ctxt familia_gedcom_family_start (Gedcom_rec rec,
        struct familia_family * family = NULL;
        struct xref_value *xr = GEDCOM_XREF_PTR(xref);
 
+       assert(rec == REC_FAM);
+       assert(parsed_tag == TAG_FAM);
+
        family = familia_memory_stack_find(xr->string, FS_FAMILY);
 
        return (Gedcom_ctxt)family;
 }
-/*
-void familia_gedcom_family_end (Gedcom_rec rec, Gedcom_ctxt self)
+
+void familia_gedcom_family_end (Gedcom_rec rec, Gedcom_ctxt self, Gedcom_val parsed_value)
 {
-       familia_storage_add_family(familia_storage_get_current(), self);
+/*     familia_storage_add_family(familia_storage_get_current(), self);*/
 }
-*/
+
 Gedcom_ctxt familia_gedcom_individual_start (Gedcom_rec rec,
                                        int level,
                                        Gedcom_val xref,
@@ -100,16 +107,19 @@ Gedcom_ctxt familia_gedcom_individual_start (Gedcom_rec rec,
        struct familia_individual * individual = NULL;
        struct xref_value *xr = GEDCOM_XREF_PTR(xref);
 
+       assert(rec == REC_INDI);
+       assert(parsed_tag == TAG_INDI);
+
        individual = familia_memory_stack_find(xr->string, FS_INDIVIDUAL);
 
        return (Gedcom_ctxt)individual;
 }
-/*
-void familia_gedcom_individual_end (Gedcom_rec rec, Gedcom_ctxt self)
+
+void familia_gedcom_individual_end (Gedcom_rec rec, Gedcom_ctxt self, Gedcom_val parsed_value)
 {
-       familia_storage_add_individual(familia_storage_get_current(), self);
+/*     familia_storage_add_individual(familia_storage_get_current(), self);*/
 }
-*/
+
 Gedcom_ctxt familia_gedcom_individual_set_first_name(Gedcom_elt  elt,
                                        Gedcom_ctxt ctxt,
                                        int         level,
@@ -118,6 +128,9 @@ Gedcom_ctxt familia_gedcom_individual_set_first_name(Gedcom_elt  elt,
                                        int         parsed_tag,
                                        Gedcom_val  parsed_value)
 {
+       assert(elt == ELT_SUB_PERS_NAME);
+       assert(parsed_tag == TAG_NAME);
+
        familia_individual_set_first_name(ctxt, GEDCOM_STRING(parsed_value));
        return ctxt;
 }
@@ -137,6 +150,8 @@ Gedcom_ctxt familia_gedcom_individual_last_name_start(Gedcom_elt  elt,
                                        int         parsed_tag,
                                        Gedcom_val  parsed_value)
 {
+       assert(elt == ELT_SUB_PERS_NAME_SURN);
+
        if (GEDCOM_IS_STRING(parsed_value)) {
                familia_individual_set_last_name(ctxt, GEDCOM_STRING(parsed_value));
        }
@@ -160,7 +175,12 @@ Gedcom_ctxt familia_gedcom_individual_add_family(Gedcom_elt  elt,
                                        Gedcom_val  parsed_value)
 {
        void * family = NULL;
-       struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
+       struct xref_value *xr = NULL;
+
+       assert(elt == ELT_SUB_FAMS);
+       assert(parsed_tag == TAG_FAMS);
+
+       xr = GEDCOM_XREF_PTR(parsed_value);
 
        family = familia_memory_stack_find(xr->string, FS_FAMILY);
        familia_individual_add_family(ctxt, family);
@@ -181,7 +201,12 @@ Gedcom_ctxt familia_gedcom_individual_set_parents(Gedcom_elt  elt,
 {
        void * child = NULL;
        /* XREF_PTR(FAM) */
-       struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
+       struct xref_value *xr = NULL;
+
+       assert(elt == ELT_FAM_CHIL);
+       assert(parsed_tag == TAG_CHIL);
+
+       xr = GEDCOM_XREF_PTR(parsed_value);
 
        child = familia_memory_stack_find(xr->string, FS_INDIVIDUAL);
        familia_individual_set_parents(child, ctxt);
@@ -202,7 +227,12 @@ Gedcom_ctxt familia_gedcom_family_add_child(Gedcom_elt  elt,
 {
        void * family = NULL;
        /* XREF_PTR(FAM) */
-       struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
+       struct xref_value *xr = NULL;
+
+       assert(elt == ELT_SUB_FAMC);
+       assert(parsed_tag == TAG_FAMC);
+
+       xr = GEDCOM_XREF_PTR(parsed_value);
 
        family = familia_memory_stack_find(xr->string, FS_FAMILY);
        familia_family_add_child(family, ctxt);
@@ -224,7 +254,12 @@ Gedcom_ctxt familia_gedcom_family_set_parent1(Gedcom_elt  elt,
 {
        void * parent = NULL;
        /* XREF_PTR(INDI) */
-       struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
+       struct xref_value *xr = NULL;
+
+       assert(elt == ELT_FAM_HUSB);
+       assert(parsed_tag == TAG_HUSB);
+
+       xr = GEDCOM_XREF_PTR(parsed_value);
 
        parent = familia_memory_stack_find(xr->string, FS_FAMILY);
        /* ctxt: REC_FAM */
@@ -247,7 +282,12 @@ Gedcom_ctxt familia_gedcom_family_set_parent2(Gedcom_elt  elt,
 {
        void * parent = NULL;
        /* XREF_PTR(INDI) */
-       struct xref_value *xr = GEDCOM_XREF_PTR(parsed_value);
+       struct xref_value *xr = NULL;
+
+       assert(elt == ELT_FAM_WIFE);
+       assert(parsed_tag == TAG_WIFE);
+
+       xr = GEDCOM_XREF_PTR(parsed_value);
 
        parent = familia_memory_stack_find(xr->string, FS_FAMILY);
        /* ctxt: REC_FAM */
@@ -256,13 +296,70 @@ Gedcom_ctxt familia_gedcom_family_set_parent2(Gedcom_elt  elt,
        return ctxt;
 }
 
-/*
-void familia_gedcom_individual_family_end(Gedcom_elt  elt,
+/**
+ * Sets date for the current event. Currently supports only individual birth.
+ * TODO: Implement all events.
+ */
+Gedcom_ctxt familia_gedcom_event_set_date(Gedcom_elt  elt,
+                                       Gedcom_ctxt ctxt,
+                                       int         level,
+                                       char*       tag,
+                                       char*       raw_value,
+                                       int         parsed_tag,
+                                       Gedcom_val  parsed_value)
+{
+       struct fged_timestampable_ctxt * tctxt = ctxt;
+
+       switch(tctxt->type) {
+       case BIRTH: {
+               struct date_value dv;
+               struct familia_individual * ind = NULL;
+
+               assert(GEDCOM_IS_DATE(parsed_value));
+
+               ind = ((struct fged_ctxt *)ctxt)->object;
+               dv = GEDCOM_DATE(parsed_value);
+
+               familia_individual_set_birth_date(ind, dv);
+               break;
+       }
+       };
+
+       return ctxt;
+}
+Gedcom_ctxt familia_gedcom_timestampable_ctxt_wrap(Gedcom_elt  elt,
+                                       Gedcom_ctxt ctxt,
+                                       int         level,
+                                       char*       tag,
+                                       char*       raw_value,
+                                       int         parsed_tag,
+                                       Gedcom_val  parsed_value)
+{
+       struct fged_timestampable_ctxt * tctxt = NULL;
+       tctxt = (struct fged_timestampable_ctxt *)malloc(sizeof(struct fged_timestampable_ctxt));
+       tctxt->ctxt = ctxt;
+       tctxt->type = UNSET;
+       
+       switch(elt) {
+       case ELT_SUB_INDIV_BIRT:
+               tctxt->type = BIRTH;
+               break;
+       }
+       return tctxt;
+}
+
+void familia_gedcom_timestampable_ctxt_unwrap(Gedcom_elt  elt,
                                        Gedcom_ctxt ctxt,
                                        Gedcom_ctxt self,
                                        Gedcom_val  parsed_value)
 {
-}*/
+       struct fged_timestampable_ctxt * tctxt = NULL;
+
+       tctxt = self;
+
+       self = tctxt->ctxt;
+       free(tctxt);
+}
 
 void familia_gedcom_error (Gedcom_msg_type type, char *msg)
 {
@@ -277,9 +374,9 @@ void familia_gedcom_init()
 
        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);
+       gedcom_subscribe_to_record(REC_FAM, familia_gedcom_family_start, familia_gedcom_family_end);
        /* Create and set individual as current (set to ctxt) */
-       gedcom_subscribe_to_record(REC_INDI, familia_gedcom_individual_start, NULL);
+       gedcom_subscribe_to_record(REC_INDI, familia_gedcom_individual_start, familia_gedcom_individual_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);
        /* Set in current individual first name */
@@ -296,6 +393,9 @@ void familia_gedcom_init()
        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);
+       
+       gedcom_subscribe_to_element(ELT_SUB_EVT_DATE, familia_gedcom_event_set_date, NULL);
+       gedcom_subscribe_to_element(ELT_SUB_INDIV_BIRT, familia_gedcom_timestampable_ctxt_wrap, familia_gedcom_timestampable_ctxt_unwrap);
 
        familia_memory_stack_init();
 }