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