33c4c6c512ff60c8156f476edc500b8758a275c5
[gedcom-parse.git] / gom / individual.c
1 /* Individual 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 "individual.h"
27 #include "event.h"
28 #include "personal_name.h"
29 #include "lds_event.h"
30 #include "family_link.h"
31 #include "association.h"
32 #include "submitter.h"
33 #include "source_citation.h"
34 #include "multimedia_link.h"
35 #include "note_sub.h"
36 #include "user_ref.h"
37 #include "change_date.h"
38 #include "user_rec.h"
39 #include "gom.h"
40 #include "gedcom.h"
41 #include "gom_internal.h"
42
43 struct individual* gom_first_individual = NULL;
44
45 DEFINE_MAKEFUNC(individual, gom_first_individual)
46 DEFINE_DESTROYFUNC(individual, gom_first_individual)
47 DEFINE_ADDFUNC(individual, XREF_INDI)
48 DEFINE_DELETEFUNC(individual)
49 DEFINE_GETXREFFUNC(individual, XREF_INDI)
50 DEFINE_MAKELINKFUNC(individual, XREF_INDI)
51      
52 DEFINE_REC_CB(individual, indi_start)
53 DEFINE_STRING_CB(individual, indi_resn_start, restriction_notice)
54 DEFINE_STRING_CB(individual, indi_sex_start, sex)
55 DEFINE_XREF_LIST_CB(individual, indi_subm_start, submitters, submitter)
56 DEFINE_XREF_LIST_CB(individual, indi_alia_start, alias, individual)
57 DEFINE_XREF_LIST_CB(individual, indi_anci_start, ancestor_interest, submitter)
58 DEFINE_XREF_LIST_CB(individual, indi_desi_start, descendant_interest,submitter)
59 DEFINE_STRING_CB(individual, indi_rfn_start, record_file_nr)
60 DEFINE_STRING_CB(individual, indi_afn_start, ancestral_file_nr)
61
62 DEFINE_ADDFUNC2(individual, event, event)
63 DEFINE_ADDFUNC2_TOVAR(individual, event, attribute)
64 DEFINE_ADDFUNC2(individual, personal_name, name)
65 DEFINE_ADDFUNC2(individual, lds_event, lds_individual_ordinance)
66 DEFINE_ADDFUNC2(individual, association, association)
67 DEFINE_ADDFUNC2(individual, source_citation, citation)
68 DEFINE_ADDFUNC2(individual, multimedia_link, mm_link)
69 DEFINE_ADDFUNC2(individual, note_sub, note)
70 DEFINE_ADDFUNC2(individual, user_ref_number, ref)
71 DEFINE_ADDFUNC2(individual, user_data, extra)
72 DEFINE_ADDFUNC2_NOLIST(individual, change_date, change_date)
73 DEFINE_ADDFUNC2_STR(individual, record_id)
74
75 void individual_subscribe()
76 {
77   gedcom_subscribe_to_record(REC_INDI, indi_start, def_rec_end);
78   gedcom_subscribe_to_element(ELT_INDI_RESN, indi_resn_start, def_elt_end);
79   gedcom_subscribe_to_element(ELT_INDI_SEX, indi_sex_start, def_elt_end);
80   gedcom_subscribe_to_element(ELT_INDI_SUBM, indi_subm_start, def_elt_end);
81   gedcom_subscribe_to_element(ELT_INDI_ALIA, indi_alia_start, def_elt_end);
82   gedcom_subscribe_to_element(ELT_INDI_ANCI, indi_anci_start, def_elt_end);
83   gedcom_subscribe_to_element(ELT_INDI_DESI, indi_desi_start, def_elt_end);
84   gedcom_subscribe_to_element(ELT_INDI_RFN, indi_rfn_start, def_elt_end);
85   gedcom_subscribe_to_element(ELT_INDI_AFN, indi_afn_start, def_elt_end);
86 }
87
88 void individual_add_family_link(Gom_ctxt ctxt, int ctxt_type,
89                                 struct family_link* link)
90 {
91   struct individual *indiv = SAFE_CTXT_CAST(individual, ctxt);
92   if (indiv) {
93     switch (ctxt_type) {
94       case ELT_SUB_FAMC:
95         LINK_CHAIN_ELT(family_link, indiv->child_to_family, link);
96         break;
97       case ELT_SUB_FAMS:
98         LINK_CHAIN_ELT(family_link, indiv->spouse_to_family, link);
99         break;
100       default:
101         UNEXPECTED_CONTEXT(ctxt_type);
102     }
103   }
104 }
105
106 void CLEANFUNC(individual)(struct individual* indiv)
107 {
108   if (indiv) {
109     SAFE_FREE(indiv->xrefstr);
110     SAFE_FREE(indiv->restriction_notice);
111     DESTROY_CHAIN_ELTS(personal_name, indiv->name);
112     SAFE_FREE(indiv->sex);
113     DESTROY_CHAIN_ELTS(event, indiv->event);
114     DESTROY_CHAIN_ELTS(event, indiv->attribute);
115     DESTROY_CHAIN_ELTS(lds_event, indiv->lds_individual_ordinance);
116     DESTROY_CHAIN_ELTS(family_link, indiv->child_to_family);
117     DESTROY_CHAIN_ELTS(family_link, indiv->spouse_to_family);
118     DESTROY_CHAIN_ELTS(xref_list, indiv->submitters);
119     DESTROY_CHAIN_ELTS(association, indiv->association);  
120     DESTROY_CHAIN_ELTS(xref_list, indiv->alias);
121     DESTROY_CHAIN_ELTS(xref_list, indiv->ancestor_interest);
122     DESTROY_CHAIN_ELTS(xref_list, indiv->descendant_interest);
123     DESTROY_CHAIN_ELTS(source_citation, indiv->citation);
124     DESTROY_CHAIN_ELTS(multimedia_link, indiv->mm_link);
125     DESTROY_CHAIN_ELTS(note_sub, indiv->note);
126     SAFE_FREE(indiv->record_file_nr);
127     SAFE_FREE(indiv->ancestral_file_nr);
128     DESTROY_CHAIN_ELTS(user_ref_number, indiv->ref);
129     SAFE_FREE(indiv->record_id);
130     CLEANFUNC(change_date)(indiv->change_date);
131     DESTROY_CHAIN_ELTS(user_data, indiv->extra);
132   }
133 }
134
135 void individuals_cleanup()
136 {
137   DESTROY_CHAIN_ELTS(individual, gom_first_individual);
138 }
139
140 struct individual* gom_get_first_individual()
141 {
142   return gom_first_individual;
143 }
144
145 int write_individuals(Gedcom_write_hndl hndl)
146 {
147   int result = 0;
148   struct individual* obj;
149
150   for (obj = gom_first_individual; obj; obj = obj->next) {
151     result |= gedcom_write_record_str(hndl, REC_INDI, obj->xrefstr, NULL);
152     if (obj->restriction_notice)
153       result |= gedcom_write_element_str(hndl, ELT_INDI_RESN, 0,
154                                          REC_INDI, obj->restriction_notice);
155     if (obj->name)
156       result |= write_names(hndl, REC_INDI, obj->name);
157     if (obj->sex)
158       result |= gedcom_write_element_str(hndl, ELT_INDI_SEX, 0,
159                                          REC_INDI, obj->sex);
160     if (obj->event)
161       result |= write_events(hndl, REC_INDI, EVT_TYPE_INDIV_EVT, obj->event);
162     if (obj->attribute)
163       result |= write_events(hndl, REC_INDI, EVT_TYPE_INDIV_ATTR,
164                              obj->attribute);
165     if (obj->lds_individual_ordinance)
166       result |= write_lds_events(hndl, REC_INDI,
167                                  obj->lds_individual_ordinance);
168     if (obj->child_to_family)
169       result |= write_family_links(hndl, REC_INDI, LINK_TYPE_CHILD,
170                                    obj->child_to_family);
171     if (obj->spouse_to_family)
172       result |= write_family_links(hndl, REC_INDI, LINK_TYPE_SPOUSE,
173                                    obj->spouse_to_family);
174     result |= gom_write_xref_list(hndl, ELT_INDI_SUBM, 0,
175                                   REC_INDI, obj->submitters);
176     if (obj->association)
177       result |= write_associations(hndl, REC_INDI, obj->association);
178     result |= gom_write_xref_list(hndl, ELT_INDI_ALIA, 0,
179                                   REC_INDI, obj->alias);
180     result |= gom_write_xref_list(hndl, ELT_INDI_ANCI, 0,
181                                   REC_INDI, obj->ancestor_interest);
182     result |= gom_write_xref_list(hndl, ELT_INDI_DESI, 0,
183                                   REC_INDI, obj->descendant_interest);
184     if (obj->citation)
185       result |= write_citations(hndl, REC_INDI, obj->citation);
186     if (obj->mm_link)
187       result |= write_multimedia_links(hndl, REC_INDI, obj->mm_link);
188     if (obj->note)
189       result |= write_note_subs(hndl, REC_INDI, obj->note);
190     if (obj->record_file_nr)
191       result |= gedcom_write_element_str(hndl, ELT_INDI_RFN, 0,
192                                          REC_INDI, obj->record_file_nr);
193     if (obj->ancestral_file_nr)
194       result |= gedcom_write_element_str(hndl, ELT_INDI_AFN, 0,
195                                          REC_INDI, obj->ancestral_file_nr);
196     if (obj->ref)
197       result |= write_user_refs(hndl, REC_INDI, obj->ref);
198     if (obj->record_id)
199       result |= gedcom_write_element_str(hndl, ELT_SUB_IDENT_RIN, 0,
200                                          REC_INDI, obj->record_id);
201     if (obj->change_date)
202       result |= write_change_date(hndl, REC_INDI, obj->change_date);
203     if (obj->extra)
204       result |= write_user_data(hndl, obj->extra);
205   }
206   
207   return result;
208 }
209