1 /* Event 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.
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.
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.
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
27 #include "individual.h"
31 #include "source_citation.h"
32 #include "multimedia_link.h"
37 #include "gom_internal.h"
39 Gedcom_ctxt sub_evt_start(_ELT_PARAMS_)
41 Gom_ctxt ctxt = (Gom_ctxt)parent;
42 Gom_ctxt result = NULL;
47 struct event *evt = SUB_MAKEFUNC(event)();
49 evt->event = parsed_tag;
50 evt->event_name = strdup(tag);
51 if (! evt->event_name) {
57 if (GEDCOM_IS_STRING(parsed_value)) {
58 evt->val = strdup(GEDCOM_STRING(parsed_value));
61 free(evt->event_name);
68 switch (ctxt->ctxt_type) {
70 ADDFUNC2(family,event)(ctxt, evt); break;
72 ADDFUNC2(individual,event)(ctxt, evt); break;
74 UNEXPECTED_CONTEXT(ctxt->ctxt_type);
76 result = MAKE_GOM_CTXT(elt, event, evt);
82 return (Gedcom_ctxt)result;
85 Gedcom_ctxt sub_attr_start(_ELT_PARAMS_)
87 Gom_ctxt ctxt = (Gom_ctxt)parent;
88 Gom_ctxt result = NULL;
93 struct event *evt = SUB_MAKEFUNC(event)();
95 evt->event = parsed_tag;
96 evt->event_name = strdup(tag);
97 if (! evt->event_name) {
103 if (GEDCOM_IS_STRING(parsed_value)) {
104 evt->val = strdup(GEDCOM_STRING(parsed_value));
107 free(evt->event_name);
114 switch (ctxt->ctxt_type) {
116 ADDFUNC2_TOVAR(individual,event,attribute)(ctxt, evt); break;
118 UNEXPECTED_CONTEXT(ctxt->ctxt_type);
120 result = MAKE_GOM_CTXT(elt, event, evt);
125 return (Gedcom_ctxt)result;
128 DEFINE_SUB_MAKEFUNC(event)
129 DEFINE_SUB_ADDFUNC(event)
130 DEFINE_SUB_FINDFUNC(event)
131 DEFINE_SUB_REMOVEFUNC(event)
132 DEFINE_SUB_MOVEFUNC(event)
134 DEFINE_STRING_CB(event, sub_evt_type_start, type)
135 DEFINE_DATE_CB(event, sub_evt_date_start, date)
136 DEFINE_AGE_CB(event, sub_evt_age_start, age)
137 DEFINE_STRING_CB(event, sub_evt_agnc_start, agency)
138 DEFINE_STRING_CB(event, sub_evt_caus_start, cause)
139 DEFINE_NULL_CB(event, sub_fam_evt_husb_wife_start)
140 DEFINE_XREF_CB(event, sub_evt_famc_start, family, family)
141 DEFINE_STRING_CB(event, sub_evt_famc_adop_start, adoption_parent)
143 DEFINE_ADDFUNC2(event, source_citation, citation)
144 DEFINE_ADDFUNC2(event, multimedia_link, mm_link)
145 DEFINE_ADDFUNC2(event, note_sub, note)
146 DEFINE_ADDFUNC2(event, user_data, extra)
147 DEFINE_ADDFUNC2_NOLIST(event, place, place)
148 DEFINE_ADDFUNC2_NOLIST(event, address, address)
149 DEFINE_ADDFUNC2_STRN(event, phone, 3)
151 Gedcom_ctxt sub_fam_evt_age_start(_ELT_PARAMS_)
153 Gom_ctxt ctxt = (Gom_ctxt)parent;
154 Gom_ctxt result = NULL;
159 struct event *evt = SAFE_CTXT_CAST(event, ctxt);
162 struct age_value age = GEDCOM_AGE(parsed_value);
163 switch (ctxt->ctxt_type) {
164 case ELT_SUB_FAM_EVT_HUSB:
165 evt->husband_age = gedcom_new_age_value(&age);
166 if (! evt->husband_age) {
171 case ELT_SUB_FAM_EVT_WIFE:
172 evt->wife_age = gedcom_new_age_value(&age);
173 if (! evt->wife_age) {
179 UNEXPECTED_CONTEXT(ctxt->ctxt_type);
182 result = MAKE_GOM_CTXT(elt, event, evt);
185 return (Gedcom_ctxt)result;
188 void event_subscribe()
190 gedcom_subscribe_to_element(ELT_SUB_FAM_EVT, sub_evt_start, def_elt_end);
191 gedcom_subscribe_to_element(ELT_SUB_FAM_EVT_EVEN,
192 sub_evt_start, def_elt_end);
193 gedcom_subscribe_to_element(ELT_SUB_INDIV_ATTR,
194 sub_attr_start, def_elt_end);
195 gedcom_subscribe_to_element(ELT_SUB_INDIV_RESI,
196 sub_attr_start, def_elt_end);
197 gedcom_subscribe_to_element(ELT_SUB_INDIV_BIRT,
198 sub_evt_start, def_elt_end);
199 gedcom_subscribe_to_element(ELT_SUB_INDIV_BIRT_FAMC,
200 sub_evt_famc_start, def_elt_end);
201 gedcom_subscribe_to_element(ELT_SUB_INDIV_GEN,
202 sub_evt_start, def_elt_end);
203 gedcom_subscribe_to_element(ELT_SUB_INDIV_ADOP,
204 sub_evt_start, def_elt_end);
205 gedcom_subscribe_to_element(ELT_SUB_INDIV_ADOP_FAMC,
206 sub_evt_famc_start, def_elt_end);
207 gedcom_subscribe_to_element(ELT_SUB_INDIV_ADOP_FAMC_ADOP,
208 sub_evt_famc_adop_start, def_elt_end);
209 gedcom_subscribe_to_element(ELT_SUB_INDIV_EVEN,
210 sub_evt_start, def_elt_end);
211 gedcom_subscribe_to_element(ELT_SUB_FAM_EVT_HUSB,
212 sub_fam_evt_husb_wife_start, def_elt_end);
213 gedcom_subscribe_to_element(ELT_SUB_FAM_EVT_WIFE,
214 sub_fam_evt_husb_wife_start, def_elt_end);
215 gedcom_subscribe_to_element(ELT_SUB_FAM_EVT_AGE,
216 sub_fam_evt_age_start, def_elt_end);
217 gedcom_subscribe_to_element(ELT_SUB_EVT_TYPE,
218 sub_evt_type_start, def_elt_end);
219 gedcom_subscribe_to_element(ELT_SUB_EVT_DATE,
220 sub_evt_date_start, def_elt_end);
221 gedcom_subscribe_to_element(ELT_SUB_EVT_AGE,
222 sub_evt_age_start, def_elt_end);
223 gedcom_subscribe_to_element(ELT_SUB_EVT_AGNC,
224 sub_evt_agnc_start, def_elt_end);
225 gedcom_subscribe_to_element(ELT_SUB_EVT_CAUS,
226 sub_evt_caus_start, def_elt_end);
229 void UNREFALLFUNC(event)(struct event* obj)
232 struct event* runner;
233 for (runner = obj; runner; runner = runner->next) {
234 UNREFALLFUNC(place)(runner->place);
235 UNREFALLFUNC(address)(runner->address);
236 UNREFALLFUNC(source_citation)(runner->citation);
237 UNREFALLFUNC(multimedia_link)(runner->mm_link);
238 UNREFALLFUNC(note_sub)(runner->note);
239 unref_xref_value(runner->family);
240 UNREFALLFUNC(user_data)(runner->extra);
245 void CLEANFUNC(event)(struct event* evt)
248 SAFE_FREE(evt->event_name);
250 SAFE_FREE(evt->type);
251 SAFE_FREE(evt->date);
252 CLEANFUNC(place)(evt->place);
253 CLEANFUNC(address)(evt->address);
254 SAFE_FREE(evt->phone[0]);
255 SAFE_FREE(evt->phone[1]);
256 SAFE_FREE(evt->phone[2]);
258 SAFE_FREE(evt->agency);
259 SAFE_FREE(evt->cause);
260 DESTROY_CHAIN_ELTS(source_citation, evt->citation);
261 DESTROY_CHAIN_ELTS(multimedia_link, evt->mm_link);
262 DESTROY_CHAIN_ELTS(note_sub, evt->note);
263 SAFE_FREE(evt->husband_age);
264 SAFE_FREE(evt->wife_age);
265 SAFE_FREE(evt->adoption_parent);
266 DESTROY_CHAIN_ELTS(user_data, evt->extra);
270 static int get_gedcom_elt(EventType evt_type, int parsed_tag)
274 case EVT_TYPE_FAMILY:
275 obj_elt = (parsed_tag == TAG_EVEN ? ELT_SUB_FAM_EVT_EVEN :
278 case EVT_TYPE_INDIV_ATTR:
279 obj_elt = (parsed_tag == TAG_RESI ? ELT_SUB_INDIV_RESI :
282 case EVT_TYPE_INDIV_EVT:
283 switch (parsed_tag) {
284 case TAG_BIRT: case TAG_CHR:
285 obj_elt = ELT_SUB_INDIV_BIRT; break;
286 case TAG_DEAT: case TAG_BURI: case TAG_CREM: case TAG_BAPM:
287 case TAG_BARM: case TAG_BASM: case TAG_BLES: case TAG_CHRA:
288 case TAG_CONF: case TAG_FCOM: case TAG_ORDN: case TAG_NATU:
289 case TAG_EMIG: case TAG_IMMI: case TAG_CENS: case TAG_PROB:
290 case TAG_WILL: case TAG_GRAD: case TAG_RETI:
291 obj_elt = ELT_SUB_INDIV_GEN; break;
293 obj_elt = ELT_SUB_INDIV_ADOP; break;
295 obj_elt = ELT_SUB_INDIV_EVEN; break;
297 gedcom_warning(_("Internal error: unknown evt tag %d"), parsed_tag);
301 gedcom_warning(_("Internal error: unknown evt type %d"), evt_type);
306 int get_gedcom_fam_elt(int elt)
310 case ELT_SUB_INDIV_BIRT:
311 fam_obj_elt = ELT_SUB_INDIV_BIRT_FAMC;
313 case ELT_SUB_INDIV_ADOP:
314 fam_obj_elt = ELT_SUB_INDIV_ADOP_FAMC;
317 gedcom_warning(_("Internal error: wrong parent for evt->family"));
322 int write_events(Gedcom_write_hndl hndl, int parent, EventType evt_type,
331 for (obj = evt; obj; obj = obj->next) {
332 int obj_elt = get_gedcom_elt(evt_type, obj->event);
333 result |= gedcom_write_element_str(hndl, obj_elt, obj->event,
336 result |= gedcom_write_element_str(hndl, ELT_SUB_EVT_TYPE, 0,
339 result |= gedcom_write_element_date(hndl, ELT_SUB_EVT_DATE, 0,
342 result |= write_place(hndl, obj_elt, obj->place);
344 result |= write_address(hndl, obj_elt, obj->address);
345 for (i = 0; i < 3 && obj->phone[i]; i++)
346 result |= gedcom_write_element_str(hndl, ELT_SUB_PHON, 0, obj_elt,
349 result |= gedcom_write_element_age(hndl, ELT_SUB_EVT_AGE, 0,
352 result |= gedcom_write_element_str(hndl, ELT_SUB_EVT_AGNC, 0,
353 obj_elt, obj->agency);
355 result |= gedcom_write_element_str(hndl, ELT_SUB_EVT_CAUS, 0,
356 obj_elt, obj->cause);
358 result |= write_citations(hndl, obj_elt, obj->citation);
360 result |= write_multimedia_links(hndl, obj_elt, obj->mm_link);
362 result |= write_note_subs(hndl, obj_elt, obj->note);
363 if (obj->husband_age) {
364 result |= gedcom_write_element_str(hndl, ELT_SUB_FAM_EVT_HUSB, 0,
366 result |= gedcom_write_element_age(hndl, ELT_SUB_FAM_EVT_AGE, 0,
367 ELT_SUB_FAM_EVT_HUSB,
371 result |= gedcom_write_element_str(hndl, ELT_SUB_FAM_EVT_WIFE, 0,
373 result |= gedcom_write_element_age(hndl, ELT_SUB_FAM_EVT_AGE, 0,
374 ELT_SUB_FAM_EVT_WIFE,
378 int fam_obj_elt = get_gedcom_fam_elt(obj_elt);
379 result |= gedcom_write_element_xref(hndl, fam_obj_elt, 0,
380 obj_elt, obj->family);
381 if (obj->adoption_parent) {
382 result |= gedcom_write_element_str(hndl, ELT_SUB_INDIV_ADOP_FAMC_ADOP,
384 obj->adoption_parent);
388 result |= write_user_data(hndl, obj->extra);