A little simplification in the write interface.
[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
45       = (struct lds_event*)malloc(sizeof(struct lds_event));
46     if (! lds_evt)
47       MEMORY_ERROR;
48     else {
49       memset (lds_evt, 0, sizeof(struct lds_event));
50       lds_evt->event = parsed_tag;
51       lds_evt->event_name = strdup(tag);
52       if (! lds_evt->event_name) {
53         MEMORY_ERROR;
54         free(lds_evt);
55       }
56       else {
57         switch (ctxt->ctxt_type) {
58           case REC_FAM:
59             family_add_lss(ctxt, lds_evt); break;
60           case REC_INDI:
61             individual_add_lio(ctxt, lds_evt); break;
62           default:
63             UNEXPECTED_CONTEXT(ctxt->ctxt_type);
64         }
65         result = MAKE_GOM_CTXT(elt, lds_event, lds_evt);
66       }
67     }
68   }
69
70   return (Gedcom_ctxt)result;
71 }
72
73 STRING_CB(lds_event, sub_lds_event_stat_start, date_status)
74 DATE_CB(lds_event, sub_lds_event_date_start, date)
75 STRING_CB(lds_event, sub_lds_event_temp_start, temple_code)
76 STRING_CB(lds_event, sub_lds_event_plac_start, place_living_ordinance)
77 XREF_CB(lds_event, sub_lds_event_famc_start, family, make_family_record)
78      
79 void lds_event_subscribe()
80 {
81   gedcom_subscribe_to_element(ELT_SUB_LSS_SLGS,
82                               sub_lds_event_start, def_elt_end);
83   gedcom_subscribe_to_element(ELT_SUB_LIO_BAPL,
84                               sub_lds_event_start, def_elt_end);
85   gedcom_subscribe_to_element(ELT_SUB_LIO_SLGC,
86                               sub_lds_event_start, def_elt_end);
87   gedcom_subscribe_to_element(ELT_SUB_LSS_SLGS_STAT,
88                               sub_lds_event_stat_start, def_elt_end);
89   gedcom_subscribe_to_element(ELT_SUB_LIO_BAPL_STAT,
90                               sub_lds_event_stat_start, def_elt_end);
91   gedcom_subscribe_to_element(ELT_SUB_LSS_SLGS_DATE,
92                               sub_lds_event_date_start, def_elt_end);
93   gedcom_subscribe_to_element(ELT_SUB_LIO_BAPL_DATE,
94                               sub_lds_event_date_start, def_elt_end);
95   gedcom_subscribe_to_element(ELT_SUB_LSS_SLGS_TEMP,
96                               sub_lds_event_temp_start, def_elt_end);
97   gedcom_subscribe_to_element(ELT_SUB_LIO_BAPL_TEMP,
98                               sub_lds_event_temp_start, def_elt_end);
99   gedcom_subscribe_to_element(ELT_SUB_LSS_SLGS_PLAC,
100                               sub_lds_event_plac_start, def_elt_end);
101   gedcom_subscribe_to_element(ELT_SUB_LIO_BAPL_PLAC,
102                               sub_lds_event_plac_start, def_elt_end);
103   gedcom_subscribe_to_element(ELT_SUB_LIO_SLGC_FAMC,
104                               sub_lds_event_famc_start, def_elt_end);
105 }
106
107 void lds_event_add_note(Gom_ctxt ctxt, struct note_sub* note)
108 {
109   struct lds_event *lds = SAFE_CTXT_CAST(lds_event, ctxt);
110   if (lds)
111     LINK_CHAIN_ELT(note_sub, lds->note, note);    
112 }
113
114 void lds_event_add_citation(Gom_ctxt ctxt, struct source_citation* cit)
115 {
116   struct lds_event *lds = SAFE_CTXT_CAST(lds_event, ctxt);
117   if (lds)
118     LINK_CHAIN_ELT(source_citation, lds->citation, cit);    
119 }
120
121 void lds_event_add_user_data(Gom_ctxt ctxt, struct user_data* data)
122 {
123   struct lds_event *obj = SAFE_CTXT_CAST(lds_event, ctxt);
124   if (obj)
125     LINK_CHAIN_ELT(user_data, obj->extra, data);
126 }
127
128 void lds_event_cleanup(struct lds_event* lds)
129 {
130   if (lds) {
131     SAFE_FREE(lds->event_name);
132     SAFE_FREE(lds->date_status);
133     SAFE_FREE(lds->date);
134     SAFE_FREE(lds->temple_code);
135     SAFE_FREE(lds->place_living_ordinance);
136     DESTROY_CHAIN_ELTS(source_citation, lds->citation, citation_cleanup);  
137     DESTROY_CHAIN_ELTS(note_sub, lds->note, note_sub_cleanup);
138     DESTROY_CHAIN_ELTS(user_data, lds->extra, user_data_cleanup);
139   }
140 }
141
142 static int get_gedcom_elt(int parsed_tag)
143 {
144   int obj_elt = 0;
145   switch (parsed_tag) {
146     case TAG_BAPL: case TAG_CONL: case TAG_ENDL:
147       obj_elt = ELT_SUB_LIO_BAPL; break;
148     case TAG_SLGC:
149       obj_elt = ELT_SUB_LIO_SLGC; break;
150     default:
151       gedcom_warning(_("Internal error: unknown evt tag %d"), parsed_tag);
152   }
153   return obj_elt;
154 }
155   
156 int write_lds_events(Gedcom_write_hndl hndl, int parent, struct lds_event *lds)
157 {
158   int result = 0;
159   struct lds_event* obj;
160
161   if (!lds) return 1;
162
163   for (obj = lds; obj; obj = obj->next) {
164     int obj_elt = get_gedcom_elt(obj->event);
165     result |= gedcom_write_element_str(hndl, obj_elt, obj->event,
166                                        parent, NULL);
167     if (obj->date_status)
168       result |= gedcom_write_element_str(hndl, ELT_SUB_LIO_BAPL_STAT, 0,
169                                          obj_elt, obj->date_status);
170     if (obj->date)
171       result |= gedcom_write_element_date(hndl, ELT_SUB_LIO_BAPL_DATE, 0,
172                                           obj_elt, obj->date);
173     if (obj->temple_code)
174       result |= gedcom_write_element_str(hndl, ELT_SUB_LIO_BAPL_TEMP, 0,
175                                          obj_elt, obj->temple_code);
176     if (obj->place_living_ordinance)
177       result |= gedcom_write_element_str(hndl, ELT_SUB_LIO_BAPL_PLAC, 0,
178                                          obj_elt, obj->place_living_ordinance);
179     if (obj->family)
180       result |= gedcom_write_element_xref(hndl, ELT_SUB_LIO_SLGC_FAMC, 0,
181                                           obj_elt, obj->family);
182     if (obj->citation)
183       result |= write_citations(hndl, obj_elt, obj->citation);
184     if (obj->note)
185       result |= write_note_subs(hndl, obj_elt, obj->note);
186     if (obj->extra)
187       result |= write_user_data(hndl, obj->extra);
188   }
189
190   return result;
191 }