First check-in of gedcom object model.
[gedcom-parse.git] / gom / family_link.c
1 /* Family link sub-structure in the gedcom object model.
2    Copyright (C) 2002 The Genes Development Team
3    This file is part of the Gedcom parser library.
4    Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2002.
5
6    The Gedcom parser library is free software; you can redistribute it
7    and/or modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The Gedcom parser library is distributed in the hope that it will be
12    useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the Gedcom parser library; if not, write to the
18    Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 /* $Id$ */
22 /* $Name$ */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include "family_link.h"
27 #include "individual.h"
28 #include "note_sub.h"
29 #include "user_rec.h"
30 #include "gom.h"
31 #include "gedcom.h"
32 #include "gom_internal.h"
33
34 Gedcom_ctxt sub_fam_link_start(_ELT_PARAMS_)
35 {
36   Gom_ctxt ctxt = (Gom_ctxt)parent;
37   struct family_link *link = NULL;
38
39   if (ctxt) {
40     link = (struct family_link *)malloc(sizeof(struct family_link));
41     memset (link, 0, sizeof(struct family_link));
42     link->family = GEDCOM_XREF_PTR(parsed_value);
43
44     switch (ctxt->ctxt_type) {
45       case REC_INDI:
46         individual_add_family_link(ctxt, elt, link); break;
47       default:
48         UNEXPECTED_CONTEXT(ctxt->ctxt_type);
49     }
50   }
51
52   return (Gedcom_ctxt) MAKE_GOM_CTXT(elt, family_link, link);
53 }
54
55 Gedcom_ctxt sub_fam_link_pedi_start(_ELT_PARAMS_)
56 {
57   Gom_ctxt ctxt = (Gom_ctxt)parent;
58   struct family_link *link = SAFE_CTXT_CAST(family_link, ctxt);
59   struct pedigree *ped;
60   MAKE_CHAIN_ELT(pedigree, link->pedigree, ped);
61   ped->pedigree = strdup(GEDCOM_STRING(parsed_value));
62   return (Gedcom_ctxt) MAKE_GOM_CTXT(elt, pedigree, ped);
63 }
64
65 void family_link_subscribe()
66 {
67   gedcom_subscribe_to_element(ELT_SUB_FAMC, sub_fam_link_start,
68                               def_elt_end);
69   gedcom_subscribe_to_element(ELT_SUB_FAMS, sub_fam_link_start,
70                               def_elt_end);
71   gedcom_subscribe_to_element(ELT_SUB_FAMC_PEDI, sub_fam_link_pedi_start,
72                               def_elt_end);
73 }
74
75 void family_link_add_note(Gom_ctxt ctxt, struct note_sub* note)
76 {
77   struct family_link *link = SAFE_CTXT_CAST(family_link, ctxt);
78   LINK_CHAIN_ELT(note_sub, link->note, note)
79 }
80
81 void family_link_add_user_data(Gom_ctxt ctxt, struct user_data* data)
82 {
83   struct family_link *obj = SAFE_CTXT_CAST(family_link, ctxt);
84   LINK_CHAIN_ELT(user_data, obj->extra, data)
85 }
86
87 void pedigree_cleanup(struct pedigree* ped)
88 {
89   if (ped) {
90     SAFE_FREE(ped->pedigree);
91   }
92 }
93
94 void family_link_cleanup(struct family_link *link)
95 {
96   if (link) {
97     DESTROY_CHAIN_ELTS(pedigree, link->pedigree, pedigree_cleanup)
98     DESTROY_CHAIN_ELTS(note_sub, link->note, note_sub_cleanup)
99     DESTROY_CHAIN_ELTS(user_data, link->extra, user_data_cleanup)
100   }
101 }