8587b11acbea0a5218b3e6d6276f57db3a3aa38c
[gedcom-parse.git] / gom / lds_event.c
1 /* LDS spouse sealing 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 "lds_event.h"
27 #include "family.h"
28 #include "individual.h"
29 #include "note_sub.h"
30 #include "source_citation.h"
31 #include "user_rec.h"
32 #include "gom.h"
33 #include "gedcom.h"
34 #include "gom_internal.h"
35
36 Gedcom_ctxt sub_lds_event_start(_ELT_PARAMS_)
37 {
38   Gom_ctxt ctxt = (Gom_ctxt)parent;
39   Gom_ctxt result = NULL;
40
41   if (! ctxt)
42     NO_CONTEXT;
43   else {
44     struct lds_event *lds_evt = SUB_MAKEFUNC(lds_event)();
45     if (lds_evt) {
46       lds_evt->event = parsed_tag;
47       lds_evt->event_name = strdup(tag);
48       if (! lds_evt->event_name) {
49         MEMORY_ERROR;
50         free(lds_evt);
51       }
52       else {
53         switch (ctxt->ctxt_type) {
54           case REC_FAM:
55             ADDFUNC2(family,lds_event)(ctxt, lds_evt); break;
56           case REC_INDI:
57             ADDFUNC2(individual,lds_event)(ctxt, lds_evt); break;
58           default:
59             UNEXPECTED_CONTEXT(ctxt->ctxt_type);
60         }
61         result = MAKE_GOM_CTXT(elt, lds_event, lds_evt);
62       }
63     }
64   }
65
66   return (Gedcom_ctxt)result;
67 }
68
69 DEFINE_SUB_MAKEFUNC(lds_event)
70      
71 DEFINE_STRING_CB(lds_event, sub_lds_event_stat_start, date_status)
72 DEFINE_DATE_CB(lds_event, sub_lds_event_date_start, date)
73 DEFINE_STRING_CB(lds_event, sub_lds_event_temp_start, temple_code)
74 DEFINE_STRING_CB(lds_event, sub_lds_event_plac_start, place_living_ordinance)
75 DEFINE_XREF_CB(lds_event, sub_lds_event_famc_start, family, family)
76
77 DEFINE_ADDFUNC2(lds_event, note_sub, note)
78 DEFINE_ADDFUNC2(lds_event, source_citation, citation)
79 DEFINE_ADDFUNC2(lds_event, user_data, extra)
80      
81 void lds_event_subscribe()
82 {
83   gedcom_subscribe_to_element(ELT_SUB_LSS_SLGS,
84                               sub_lds_event_start, def_elt_end);
85   gedcom_subscribe_to_element(ELT_SUB_LIO_BAPL,
86                               sub_lds_event_start, def_elt_end);
87   gedcom_subscribe_to_element(ELT_SUB_LIO_SLGC,
88                               sub_lds_event_start, def_elt_end);
89   gedcom_subscribe_to_element(ELT_SUB_LSS_SLGS_STAT,
90                               sub_lds_event_stat_start, def_elt_end);
91   gedcom_subscribe_to_element(ELT_SUB_LIO_BAPL_STAT,
92                               sub_lds_event_stat_start, def_elt_end);
93   gedcom_subscribe_to_element(ELT_SUB_LSS_SLGS_DATE,
94                               sub_lds_event_date_start, def_elt_end);
95   gedcom_subscribe_to_element(ELT_SUB_LIO_BAPL_DATE,
96                               sub_lds_event_date_start, def_elt_end);
97   gedcom_subscribe_to_element(ELT_SUB_LSS_SLGS_TEMP,
98                               sub_lds_event_temp_start, def_elt_end);
99   gedcom_subscribe_to_element(ELT_SUB_LIO_BAPL_TEMP,
100                               sub_lds_event_temp_start, def_elt_end);
101   gedcom_subscribe_to_element(ELT_SUB_LSS_SLGS_PLAC,
102                               sub_lds_event_plac_start, def_elt_end);
103   gedcom_subscribe_to_element(ELT_SUB_LIO_BAPL_PLAC,
104                               sub_lds_event_plac_start, def_elt_end);
105   gedcom_subscribe_to_element(ELT_SUB_LIO_SLGC_FAMC,
106                               sub_lds_event_famc_start, def_elt_end);
107 }
108
109 void CLEANFUNC(lds_event)(struct lds_event* lds)
110 {
111   if (lds) {
112     SAFE_FREE(lds->event_name);
113     SAFE_FREE(lds->date_status);
114     SAFE_FREE(lds->date);
115     SAFE_FREE(lds->temple_code);
116     SAFE_FREE(lds->place_living_ordinance);
117     DESTROY_CHAIN_ELTS(source_citation, lds->citation);  
118     DESTROY_CHAIN_ELTS(note_sub, lds->note);
119     DESTROY_CHAIN_ELTS(user_data, lds->extra);
120   }
121 }
122
123 static int get_gedcom_elt(int parsed_tag)
124 {
125   int obj_elt = 0;
126   switch (parsed_tag) {
127     case TAG_BAPL: case TAG_CONL: case TAG_ENDL:
128       obj_elt = ELT_SUB_LIO_BAPL; break;
129     case TAG_SLGC:
130       obj_elt = ELT_SUB_LIO_SLGC; break;
131     default:
132       gedcom_warning(_("Internal error: unknown evt tag %d"), parsed_tag);
133   }
134   return obj_elt;
135 }
136   
137 int write_lds_events(Gedcom_write_hndl hndl, int parent, struct lds_event *lds)
138 {
139   int result = 0;
140   struct lds_event* obj;
141
142   if (!lds) return 1;
143
144   for (obj = lds; obj; obj = obj->next) {
145     int obj_elt = get_gedcom_elt(obj->event);
146     result |= gedcom_write_element_str(hndl, obj_elt, obj->event,
147                                        parent, NULL);
148     if (obj->date_status)
149       result |= gedcom_write_element_str(hndl, ELT_SUB_LIO_BAPL_STAT, 0,
150                                          obj_elt, obj->date_status);
151     if (obj->date)
152       result |= gedcom_write_element_date(hndl, ELT_SUB_LIO_BAPL_DATE, 0,
153                                           obj_elt, obj->date);
154     if (obj->temple_code)
155       result |= gedcom_write_element_str(hndl, ELT_SUB_LIO_BAPL_TEMP, 0,
156                                          obj_elt, obj->temple_code);
157     if (obj->place_living_ordinance)
158       result |= gedcom_write_element_str(hndl, ELT_SUB_LIO_BAPL_PLAC, 0,
159                                          obj_elt, obj->place_living_ordinance);
160     if (obj->family)
161       result |= gedcom_write_element_xref(hndl, ELT_SUB_LIO_SLGC_FAMC, 0,
162                                           obj_elt, obj->family);
163     if (obj->citation)
164       result |= write_citations(hndl, obj_elt, obj->citation);
165     if (obj->note)
166       result |= write_note_subs(hndl, obj_elt, obj->note);
167     if (obj->extra)
168       result |= write_user_data(hndl, obj->extra);
169   }
170
171   return result;
172 }