Unlink xrefs properly when struct is deleted.
[gedcom-parse.git] / gom / association.c
1 /* Association 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 "association.h"
27 #include "individual.h"
28 #include "note_sub.h"
29 #include "source_citation.h"
30 #include "user_rec.h"
31 #include "gom.h"
32 #include "gedcom.h"
33 #include "gom_internal.h"
34
35 Gedcom_ctxt sub_assoc_start(_ELT_PARAMS_)
36 {
37   Gom_ctxt ctxt = (Gom_ctxt)parent;
38   Gom_ctxt result = NULL;
39
40   if (!ctxt)
41     NO_CONTEXT;
42   else {
43     struct association *assoc = SUB_MAKEFUNC(association)();
44     if (assoc) {
45       assoc->to = GEDCOM_XREF_PTR(parsed_value);
46       
47       switch (ctxt->ctxt_type) {
48         case REC_INDI:
49           ADDFUNC2(individual,association)(ctxt, assoc);
50         default:
51           UNEXPECTED_CONTEXT(ctxt->ctxt_type);
52       }
53       result = MAKE_GOM_CTXT(elt, association, assoc);
54     }
55   }
56
57   return (Gedcom_ctxt)result;
58 }
59
60 DEFINE_SUB_MAKEFUNC(association)
61      
62 DEFINE_STRING_CB(association, sub_assoc_rela_start, relation)
63
64 DEFINE_ADDFUNC2(association, note_sub, note)
65 DEFINE_ADDFUNC2(association, source_citation, citation)
66 DEFINE_ADDFUNC2(association, user_data, extra)
67      
68 Gedcom_ctxt sub_assoc_type_start(_ELT_PARAMS_)
69 {
70   Gom_ctxt ctxt = (Gom_ctxt)parent;
71   Gom_ctxt result = NULL;
72   
73   if (! ctxt)
74     NO_CONTEXT;
75   else {
76     struct association *obj = SAFE_CTXT_CAST(association, ctxt);
77     char *str = GEDCOM_STRING(parsed_value);
78     if (obj) {
79       obj->type = strdup(str);
80       if (! obj->type)
81         MEMORY_ERROR;
82       else {
83         set_xref_type(obj->to, str);
84         result = MAKE_GOM_CTXT(elt, association, obj);
85       }
86     }
87   }
88   return (Gedcom_ctxt)result;
89 }
90
91 void association_subscribe()
92 {
93   gedcom_subscribe_to_element(ELT_SUB_ASSO, sub_assoc_start, def_elt_end);
94   gedcom_subscribe_to_element(ELT_SUB_ASSO_TYPE, sub_assoc_type_start,
95                               def_elt_end);
96   gedcom_subscribe_to_element(ELT_SUB_ASSO_RELA, sub_assoc_rela_start,
97                               def_elt_end);
98 }
99
100 void UNREFALLFUNC(association)(struct association *obj)
101 {
102   if (obj) {
103     struct association* runner;
104     for (runner = obj; runner; runner = runner->next) {
105       unref_xref_value(runner->to);
106       UNREFALLFUNC(source_citation)(runner->citation);
107       UNREFALLFUNC(note_sub)(runner->note);
108       UNREFALLFUNC(user_data)(runner->extra);
109     }
110   }
111 }
112
113 void CLEANFUNC(association)(struct association* assoc)
114 {
115   if (assoc) {
116     SAFE_FREE(assoc->type);
117     SAFE_FREE(assoc->relation);
118     DESTROY_CHAIN_ELTS(note_sub, assoc->note);
119     DESTROY_CHAIN_ELTS(source_citation, assoc->citation);
120     DESTROY_CHAIN_ELTS(user_data, assoc->extra);
121   }
122 }
123
124 int write_associations(Gedcom_write_hndl hndl, int parent,
125                        struct association *assoc)
126 {
127   int result = 0;
128   struct association* obj;
129
130   if (!assoc) return 1;
131
132   for (obj = assoc; obj; obj = obj->next) {
133     result |= gedcom_write_element_xref(hndl, ELT_SUB_ASSO, 0, parent,
134                                         obj->to);
135     if (obj->type)
136       result |= gedcom_write_element_str(hndl, ELT_SUB_ASSO_TYPE, 0, parent,
137                                          obj->type);
138     if (obj->relation)
139       result |= gedcom_write_element_str(hndl, ELT_SUB_ASSO_RELA, 0, parent,
140                                          obj->relation);
141     if (obj->citation)
142       result |= write_citations(hndl, ELT_SUB_ASSO, obj->citation);
143     if (obj->note)
144       result |= write_note_subs(hndl, ELT_SUB_ASSO, obj->note);
145     if (obj->extra)
146       result |= write_user_data(hndl, obj->extra);
147   }
148
149   return result;
150 }