Removed dup_date and dup_age.
[gedcom-parse.git] / gom / family.c
1 /* Family object 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.h"
27 #include "individual.h"
28 #include "submitter.h"
29 #include "event.h"
30 #include "lds_event.h"
31 #include "source_citation.h"
32 #include "multimedia_link.h"
33 #include "note_sub.h"
34 #include "user_ref.h"
35 #include "change_date.h"
36 #include "user_rec.h"
37 #include "gom.h"
38 #include "gedcom.h"
39 #include "gom_internal.h"
40
41 struct family* gom_first_family = NULL;
42
43 REC_CB(family, fam_start, make_family_record)
44 GET_REC_BY_XREF(family, XREF_FAM, gom_get_family_by_xref)
45 XREF_CB(family, fam_husb_start, husband, make_individual_record)
46 XREF_CB(family, fam_wife_start, wife, make_individual_record)
47 STRING_CB(family, fam_nchi_start, nr_of_children)
48 XREF_LIST_CB(family, fam_chil_start, children, make_individual_record)
49 XREF_LIST_CB(family, fam_subm_start, submitters, make_submitter_record)
50
51 void family_subscribe()
52 {
53   gedcom_subscribe_to_record(REC_FAM, fam_start, def_rec_end);
54   gedcom_subscribe_to_element(ELT_FAM_HUSB, fam_husb_start, def_elt_end);
55   gedcom_subscribe_to_element(ELT_FAM_WIFE, fam_wife_start, def_elt_end);
56   gedcom_subscribe_to_element(ELT_FAM_CHIL, fam_chil_start, def_elt_end);
57   gedcom_subscribe_to_element(ELT_FAM_NCHI, fam_nchi_start, def_elt_end);
58   gedcom_subscribe_to_element(ELT_FAM_SUBM, fam_subm_start, def_elt_end);
59 }
60
61 void family_add_event(Gom_ctxt ctxt, struct event* evt)
62 {
63   struct family *fam = SAFE_CTXT_CAST(family, ctxt);
64   if (fam)
65     LINK_CHAIN_ELT(event, fam->event, evt);
66 }
67
68 void family_add_lss(Gom_ctxt ctxt, struct lds_event* lss)
69 {
70   struct family *fam = SAFE_CTXT_CAST(family, ctxt);
71   if (fam)
72     LINK_CHAIN_ELT(lds_event, fam->lds_spouse_sealing, lss);
73 }
74
75 void family_add_citation(Gom_ctxt ctxt, struct source_citation* cit)
76 {
77   struct family *fam = SAFE_CTXT_CAST(family, ctxt);
78   if (fam)
79     LINK_CHAIN_ELT(source_citation, fam->citation, cit);
80 }
81
82 void family_add_mm_link(Gom_ctxt ctxt, struct multimedia_link* link)
83 {
84   struct family *fam = SAFE_CTXT_CAST(family, ctxt);
85   if (fam)
86     LINK_CHAIN_ELT(multimedia_link, fam->mm_link, link);
87 }
88
89 void family_add_note(Gom_ctxt ctxt, struct note_sub* note)
90 {
91   struct family *fam = SAFE_CTXT_CAST(family, ctxt);
92   if (fam)
93     LINK_CHAIN_ELT(note_sub, fam->note, note);
94 }
95
96 void family_add_user_ref(Gom_ctxt ctxt, struct user_ref_number* ref)
97 {
98   struct family *fam = SAFE_CTXT_CAST(family, ctxt);
99   if (fam)
100     LINK_CHAIN_ELT(user_ref_number, fam->ref, ref);
101 }
102
103 void family_set_record_id(Gom_ctxt ctxt, const char *rin)
104 {
105   struct family *fam = SAFE_CTXT_CAST(family, ctxt);
106   if (fam) {
107     fam->record_id = strdup(rin);
108     if (! fam->record_id) MEMORY_ERROR;
109   }
110 }
111
112 void family_set_change_date(Gom_ctxt ctxt, struct change_date* chan)
113 {
114   struct family *fam = SAFE_CTXT_CAST(family, ctxt);
115   if (fam)
116     fam->change_date = chan;
117 }
118
119 void family_add_user_data(Gom_ctxt ctxt, struct user_data* data)
120 {
121   struct family *obj = SAFE_CTXT_CAST(family, ctxt);
122   if (obj)
123     LINK_CHAIN_ELT(user_data, obj->extra, data);
124 }
125
126 void family_cleanup(struct family* fam)
127 {
128   if (fam) {
129     SAFE_FREE(fam->xrefstr);
130     DESTROY_CHAIN_ELTS(event, fam->event, event_cleanup); 
131     DESTROY_CHAIN_ELTS(xref_list, fam->children, NULL_DESTROY);
132     SAFE_FREE(fam->nr_of_children);
133     DESTROY_CHAIN_ELTS(xref_list, fam->submitters, NULL_DESTROY);
134     DESTROY_CHAIN_ELTS(lds_event, fam->lds_spouse_sealing, lds_event_cleanup);
135     DESTROY_CHAIN_ELTS(source_citation, fam->citation, citation_cleanup);
136     DESTROY_CHAIN_ELTS(multimedia_link, fam->mm_link, multimedia_link_cleanup);
137     DESTROY_CHAIN_ELTS(note_sub, fam->note, note_sub_cleanup);
138     DESTROY_CHAIN_ELTS(user_ref_number, fam->ref, user_ref_cleanup);
139     SAFE_FREE(fam->record_id);
140     change_date_cleanup(fam->change_date);
141     DESTROY_CHAIN_ELTS(user_data, fam->extra, user_data_cleanup);
142   }
143 }
144
145 void families_cleanup()
146 {
147   DESTROY_CHAIN_ELTS(family, gom_first_family, family_cleanup);
148 }
149
150 struct family* gom_get_first_family()
151 {
152   return gom_first_family;
153 }
154
155 struct family* make_family_record(const char* xrefstr)
156 {
157   struct family* fam = NULL;
158   MAKE_CHAIN_ELT(family, gom_first_family, fam);
159   if (fam) {
160     fam->xrefstr = strdup(xrefstr);
161     if (! fam->xrefstr) MEMORY_ERROR;
162   }
163   return fam;
164 }
165
166 int write_families(Gedcom_write_hndl hndl)
167 {
168   int result = 0;
169   struct family* obj;
170
171   for (obj = gom_first_family; obj; obj = obj->next) {
172     result |= gedcom_write_record_str(hndl, REC_FAM, 0,
173                                       obj->xrefstr, NULL);
174     if (obj->event)
175       result |= write_events(hndl, REC_FAM, EVT_TYPE_FAMILY, obj->event);
176     if (obj->husband)
177       result |= gedcom_write_element_xref(hndl, ELT_FAM_HUSB, 0,
178                                           REC_FAM, obj->husband);
179     if (obj->wife)
180       result |= gedcom_write_element_xref(hndl, ELT_FAM_WIFE, 0,
181                                           REC_FAM, obj->wife);
182     result |= gom_write_xref_list(hndl, ELT_FAM_CHIL, 0,
183                                   REC_FAM, obj->children);
184     if (obj->nr_of_children)
185       result |= gedcom_write_element_str(hndl, ELT_FAM_NCHI, 0,
186                                          REC_FAM, obj->nr_of_children);
187     result |= gom_write_xref_list(hndl, ELT_FAM_SUBM, 0,
188                                   REC_FAM, obj->submitters);
189     if (obj->lds_spouse_sealing)
190       result |= write_lds_events(hndl, REC_FAM, obj->lds_spouse_sealing);
191     if (obj->citation)
192       result |= write_citations(hndl, REC_FAM, obj->citation);
193     if (obj->mm_link)
194       result |= write_multimedia_links(hndl, REC_FAM, obj->mm_link);
195     if (obj->note)
196       result |= write_note_subs(hndl, REC_FAM, obj->note);
197     if (obj->ref)
198       result |= write_user_refs(hndl, REC_FAM, obj->ref);
199     if (obj->record_id)
200       result |= gedcom_write_element_str(hndl, ELT_SUB_IDENT_RIN, 0,
201                                          REC_FAM, obj->record_id);
202     if (obj->change_date)
203       result |= write_change_date(hndl, REC_FAM, obj->change_date);
204     if (obj->extra)
205       result |= write_user_data(hndl, obj->extra);
206   }
207   
208   return result;
209 }
210