Unlink xrefs properly when struct is deleted.
[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   Gom_ctxt result = NULL;
38
39   if (! ctxt)
40     NO_CONTEXT;
41   else {
42     struct family_link *link = SUB_MAKEFUNC(family_link)();
43     if (link) {
44       link->family = GEDCOM_XREF_PTR(parsed_value);
45       
46       switch (ctxt->ctxt_type) {
47         case REC_INDI:
48           ADDFUNC2(individual,family_link)(ctxt, elt, link); break;
49         default:
50           UNEXPECTED_CONTEXT(ctxt->ctxt_type);
51       }
52       result = MAKE_GOM_CTXT(elt, family_link, link);
53     }
54   }
55
56   return (Gedcom_ctxt)result;
57 }
58
59 Gedcom_ctxt sub_fam_link_pedi_start(_ELT_PARAMS_)
60 {
61   Gom_ctxt ctxt = (Gom_ctxt)parent;
62   Gom_ctxt result = NULL;
63
64   if (! ctxt)
65     NO_CONTEXT;
66   else {
67     struct family_link *link = SAFE_CTXT_CAST(family_link, ctxt);
68     if (link) {
69       int err = 0;
70       struct pedigree *ped = NULL;
71       MAKE_CHAIN_ELT(pedigree, link->pedigree, ped);
72       if (ped) {
73         ped->pedigree = strdup(GEDCOM_STRING(parsed_value));
74         if (! ped->pedigree) {
75           MEMORY_ERROR;
76           err = 1;
77         }
78       }
79       if (! err)
80         result = MAKE_GOM_CTXT(elt, pedigree, ped);
81     }
82   }
83   return (Gedcom_ctxt)result;
84 }
85
86 DEFINE_SUB_MAKEFUNC(family_link)
87      
88 DEFINE_ADDFUNC2(family_link, note_sub, note)
89 DEFINE_ADDFUNC2(family_link, user_data, extra)
90
91 void family_link_subscribe()
92 {
93   gedcom_subscribe_to_element(ELT_SUB_FAMC, sub_fam_link_start,
94                               def_elt_end);
95   gedcom_subscribe_to_element(ELT_SUB_FAMS, sub_fam_link_start,
96                               def_elt_end);
97   gedcom_subscribe_to_element(ELT_SUB_FAMC_PEDI, sub_fam_link_pedi_start,
98                               def_elt_end);
99 }
100
101 void UNREFALLFUNC(pedigree)(struct pedigree* obj)
102 {
103   if (obj) {
104     struct pedigree* runner;
105     for (runner = obj; runner; runner = runner->next) {
106       UNREFALLFUNC(user_data)(runner->extra);
107     }
108   }
109 }
110
111 void CLEANFUNC(pedigree)(struct pedigree* ped)
112 {
113   if (ped) {
114     SAFE_FREE(ped->pedigree);
115   }
116 }
117
118 void UNREFALLFUNC(family_link)(struct family_link* obj)
119 {
120   if (obj) {
121     struct family_link* runner;
122     for (runner = obj; runner; runner = runner->next) {
123       unref_xref_value(runner->family);
124       UNREFALLFUNC(pedigree)(runner->pedigree);
125       UNREFALLFUNC(note_sub)(runner->note);
126       UNREFALLFUNC(user_data)(runner->extra);
127     }
128   }
129 }
130
131 void CLEANFUNC(family_link)(struct family_link *link)
132 {
133   if (link) {
134     DESTROY_CHAIN_ELTS(pedigree, link->pedigree);
135     DESTROY_CHAIN_ELTS(note_sub, link->note);
136     DESTROY_CHAIN_ELTS(user_data, link->extra);
137   }
138 }
139
140 int write_pedigrees(Gedcom_write_hndl hndl, int parent, struct pedigree* ped)
141 {
142   int result = 0;
143   struct pedigree* obj;
144
145   if (!ped) return 1;
146
147   for (obj = ped; obj; obj = obj->next) {
148     result |= gedcom_write_element_str(hndl, ELT_SUB_FAMC_PEDI, 0, parent,
149                                        obj->pedigree);
150     if (obj->extra)
151       result |= write_user_data(hndl, obj->extra);  
152   }
153   
154   return result;
155 }
156
157 int write_family_links(Gedcom_write_hndl hndl, int parent, LinkType type,
158                        struct family_link *link)
159 {
160   int result = 0;
161   struct family_link* obj;
162   int elt = (type == LINK_TYPE_CHILD ? ELT_SUB_FAMC : ELT_SUB_FAMS);
163
164   if (!link) return 1;
165
166   for (obj = link; obj; obj = obj->next) {
167     result |= gedcom_write_element_xref(hndl, elt, 0, parent,
168                                         obj->family);
169     if (obj->pedigree)
170       result |= write_pedigrees(hndl, elt, obj->pedigree);
171     if (obj->note)
172       result |= write_note_subs(hndl, elt, obj->note);
173     if (obj->extra)
174       result |= write_user_data(hndl, obj->extra);      
175   }
176
177   return result;
178 }