New function gom_set_xref_value.
[gedcom-parse.git] / gom / header.c
1 /* Header 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 <time.h>
25 #include "header.h"
26 #include "submission.h"
27 #include "submitter.h"
28 #include "address.h"
29 #include "user_rec.h"
30 #include "gom.h"
31 #include "gedcom.h"
32 #include "gom_internal.h"
33
34 struct header gom_header;
35
36 Gedcom_ctxt head_start(_REC_PARAMS_)
37 {
38   /* Nothing special */
39   return (Gedcom_ctxt) MAKE_GOM_CTXT(rec, header, &gom_header);
40 }
41
42 DEFINE_STRING_CB(header, head_sour_start, source.id)
43 DEFINE_STRING_CB(header, head_sour_name_start, source.name)
44 DEFINE_STRING_CB(header, head_sour_vers_start, source.version)
45 DEFINE_STRING_CB(header, head_sour_corp_start, source.corporation.name)
46 DEFINE_STRING_CB(header, head_sour_data_start, source.data.name)
47 DEFINE_DATE_CB(header, head_sour_data_date_start, source.data.date)
48 DEFINE_STRING_CB(header, head_sour_data_copr_start, source.data.copyright)
49 DEFINE_STRING_CB(header, head_dest_start, destination)
50 DEFINE_XREF_CB(header, head_subm_start, submitter, submitter)
51 DEFINE_XREF_CB(header, head_subn_start, submission, submission)
52 DEFINE_DATE_CB(header, head_date_start, date)
53 DEFINE_STRING_CB(header, head_date_time_start, time)
54 DEFINE_STRING_CB(header, head_file_start, filename)
55 DEFINE_STRING_CB(header, head_copr_start, copyright)
56 DEFINE_NULL_CB(header, head_gedc_start)
57 DEFINE_STRING_CB(header, head_gedc_vers_start, gedcom.version)
58 DEFINE_STRING_CB(header, head_gedc_form_start, gedcom.form)
59 DEFINE_STRING_CB(header, head_char_start, charset.name)
60 DEFINE_STRING_CB(header, head_char_vers_start, charset.version)
61 DEFINE_STRING_CB(header, head_lang_start, language)
62 DEFINE_NULL_CB(header, head_plac_start)
63 DEFINE_STRING_CB(header, head_plac_form_start, place_hierarchy)
64 DEFINE_NULL_CB(header, head_note_start) /* the end cb will fill the value */
65 DEFINE_STRING_END_CB(header, head_note_end, note)
66
67 DEFINE_ADDFUNC2_NOLIST(header, address, source.corporation.address)
68 DEFINE_ADDFUNC2(header, user_data, extra)
69      
70 void header_add_phone(Gom_ctxt ctxt, const char* phone)
71 {
72   struct header *head = SAFE_CTXT_CAST(header, ctxt);
73   if (head) {
74     struct header_corporation *corp = &(head->source.corporation);
75     int i = 0;
76     while (i<2 && corp->phone[i]) i++;
77     if (! corp->phone[i]) {
78       corp->phone[i] = strdup(phone);
79       if (! corp->phone[i]) MEMORY_ERROR;
80     }
81   }
82 }
83
84 void header_subscribe()
85 {
86   gedcom_subscribe_to_record(REC_HEAD, head_start, def_rec_end);
87   gedcom_subscribe_to_element(ELT_HEAD_SOUR, head_sour_start, def_elt_end);
88   gedcom_subscribe_to_element(ELT_HEAD_SOUR_NAME, head_sour_name_start,
89                               def_elt_end);
90   gedcom_subscribe_to_element(ELT_HEAD_SOUR_VERS, head_sour_vers_start,
91                               def_elt_end);
92   gedcom_subscribe_to_element(ELT_HEAD_SOUR_CORP, head_sour_corp_start,
93                               def_elt_end);
94   gedcom_subscribe_to_element(ELT_HEAD_SOUR_DATA, head_sour_data_start,
95                               def_elt_end);
96   gedcom_subscribe_to_element(ELT_HEAD_SOUR_DATA_DATE,
97                               head_sour_data_date_start, def_elt_end);
98   gedcom_subscribe_to_element(ELT_HEAD_SOUR_DATA_COPR,
99                               head_sour_data_copr_start, def_elt_end);
100   gedcom_subscribe_to_element(ELT_HEAD_DEST, head_dest_start, def_elt_end);
101   gedcom_subscribe_to_element(ELT_HEAD_DATE, head_date_start, def_elt_end);
102   gedcom_subscribe_to_element(ELT_HEAD_DATE_TIME,
103                               head_date_time_start, def_elt_end);
104   gedcom_subscribe_to_element(ELT_HEAD_SUBM, head_subm_start, def_elt_end);
105   gedcom_subscribe_to_element(ELT_HEAD_SUBN, head_subn_start, def_elt_end);
106   gedcom_subscribe_to_element(ELT_HEAD_FILE, head_file_start, def_elt_end);
107   gedcom_subscribe_to_element(ELT_HEAD_COPR, head_copr_start, def_elt_end);
108   gedcom_subscribe_to_element(ELT_HEAD_GEDC, head_gedc_start, def_elt_end);
109   gedcom_subscribe_to_element(ELT_HEAD_GEDC_VERS,
110                               head_gedc_vers_start, def_elt_end);
111   gedcom_subscribe_to_element(ELT_HEAD_GEDC_FORM,
112                               head_gedc_form_start, def_elt_end);
113   gedcom_subscribe_to_element(ELT_HEAD_CHAR, head_char_start, def_elt_end);
114   gedcom_subscribe_to_element(ELT_HEAD_CHAR_VERS,
115                               head_char_vers_start, def_elt_end);
116   gedcom_subscribe_to_element(ELT_HEAD_LANG, head_lang_start, def_elt_end);
117   gedcom_subscribe_to_element(ELT_HEAD_PLAC, head_plac_start, def_elt_end);
118   gedcom_subscribe_to_element(ELT_HEAD_PLAC_FORM,
119                               head_plac_form_start, def_elt_end);
120   gedcom_subscribe_to_element(ELT_HEAD_NOTE, head_note_start, head_note_end);
121 }
122
123 void header_cleanup()
124 {
125   SAFE_FREE(gom_header.source.id);
126   SAFE_FREE(gom_header.source.name);
127   SAFE_FREE(gom_header.source.version);
128   SAFE_FREE(gom_header.source.corporation.name);
129   CLEANFUNC(address)(gom_header.source.corporation.address);
130   SAFE_FREE(gom_header.source.corporation.phone[0]);
131   SAFE_FREE(gom_header.source.corporation.phone[1]);
132   SAFE_FREE(gom_header.source.corporation.phone[2]);
133   SAFE_FREE(gom_header.source.data.name);
134   SAFE_FREE(gom_header.source.data.date);
135   SAFE_FREE(gom_header.source.data.copyright);
136   SAFE_FREE(gom_header.destination);
137   SAFE_FREE(gom_header.date);
138   SAFE_FREE(gom_header.time);
139   SAFE_FREE(gom_header.filename);
140   SAFE_FREE(gom_header.copyright);
141   SAFE_FREE(gom_header.gedcom.version);
142   SAFE_FREE(gom_header.gedcom.form);
143   SAFE_FREE(gom_header.charset.name);
144   SAFE_FREE(gom_header.charset.version);
145   SAFE_FREE(gom_header.language);
146   SAFE_FREE(gom_header.place_hierarchy);
147   SAFE_FREE(gom_header.note);
148   DESTROY_CHAIN_ELTS(user_data, gom_header.extra);
149 }
150
151 struct header* gom_get_header()
152 {
153   return &gom_header;
154 }
155
156 int gom_header_update_timestamp(time_t t)
157 {
158   int result = 0;
159   struct tm *tm_ptr = localtime(&t);
160   result |= update_date(&gom_header.date, tm_ptr);
161   result |= update_time(&gom_header.time, tm_ptr);
162   return result;
163 }
164
165 int write_header(Gedcom_write_hndl hndl)
166 {
167   int result = 0;
168   int i;
169   
170   result |= gedcom_write_record_str(hndl, REC_HEAD, NULL, NULL);
171   if (gom_header.charset.name)
172     result |= gedcom_write_element_str(hndl, ELT_HEAD_CHAR, 0, REC_HEAD,
173                                        gom_header.charset.name);
174   if (gom_header.charset.version)
175     result |= gedcom_write_element_str(hndl, ELT_HEAD_CHAR_VERS, 0,
176                                        ELT_HEAD_CHAR,
177                                        gom_header.charset.version);
178   if (gom_header.source.id)
179     result |= gedcom_write_element_str(hndl, ELT_HEAD_SOUR, 0, REC_HEAD,
180                                        gom_header.source.id);
181   if (gom_header.source.name)
182     result |= gedcom_write_element_str(hndl, ELT_HEAD_SOUR_NAME, 0,
183                                        ELT_HEAD_SOUR,
184                                        gom_header.source.name);
185   if (gom_header.source.version)
186     result |= gedcom_write_element_str(hndl, ELT_HEAD_SOUR_VERS, 0,
187                                        ELT_HEAD_SOUR,
188                                        gom_header.source.version);
189   if (gom_header.source.corporation.name)
190     result |= gedcom_write_element_str(hndl, ELT_HEAD_SOUR_CORP, 0,
191                                        ELT_HEAD_SOUR,
192                                        gom_header.source.corporation.name);
193   if (gom_header.source.corporation.address)
194     result |= write_address(hndl, ELT_HEAD_SOUR_CORP,
195                             gom_header.source.corporation.address);
196   for (i = 0; i < 3 && gom_header.source.corporation.phone[i]; i++)
197     result |= gedcom_write_element_str(hndl, ELT_SUB_PHON, 0, ELT_HEAD_SOUR_CORP,
198                                        gom_header.source.corporation.phone[i]);
199   if (gom_header.source.data.name)
200     result |= gedcom_write_element_str(hndl, ELT_HEAD_SOUR_DATA, 0,
201                                        ELT_HEAD_SOUR,
202                                        gom_header.source.data.name);
203   if (gom_header.source.data.date)
204     result |= gedcom_write_element_date(hndl, ELT_HEAD_SOUR_DATA_DATE, 0,
205                                         ELT_HEAD_SOUR_DATA,
206                                         gom_header.source.data.date);
207   if (gom_header.source.data.copyright)
208     result |= gedcom_write_element_str(hndl, ELT_HEAD_SOUR_DATA_COPR, 0,
209                                        ELT_HEAD_SOUR_DATA,
210                                        gom_header.source.data.copyright);
211   if (gom_header.destination)
212     result |= gedcom_write_element_str(hndl, ELT_HEAD_DEST, 0, REC_HEAD,
213                                        gom_header.destination);
214   if (gom_header.date)
215     result |= gedcom_write_element_date(hndl, ELT_HEAD_DATE, 0, REC_HEAD,
216                                         gom_header.date);
217   if (gom_header.time)
218     result |= gedcom_write_element_str(hndl, ELT_HEAD_DATE_TIME, 0,
219                                        ELT_HEAD_DATE, gom_header.time);
220   if (gom_header.submitter)
221     result |= gedcom_write_element_xref(hndl, ELT_HEAD_SUBM, 0, REC_HEAD,
222                                         gom_header.submitter);
223   if (gom_header.submission)
224     result |= gedcom_write_element_xref(hndl, ELT_HEAD_SUBN, 0, REC_HEAD,
225                                         gom_header.submission);
226   if (gom_header.filename)
227     result |= gedcom_write_element_str(hndl, ELT_HEAD_FILE, 0, REC_HEAD,
228                                        gom_header.filename);
229   if (gom_header.copyright)
230     result |= gedcom_write_element_str(hndl, ELT_HEAD_COPR, 0, REC_HEAD,
231                                        gom_header.copyright);
232   result |= gedcom_write_element_str(hndl, ELT_HEAD_GEDC, 0, REC_HEAD,
233                                      NULL);
234   if (gom_header.gedcom.version)
235     result |= gedcom_write_element_str(hndl, ELT_HEAD_GEDC_VERS, 0,
236                                        ELT_HEAD_GEDC,
237                                        gom_header.gedcom.version);
238   if (gom_header.gedcom.form)
239     result |= gedcom_write_element_str(hndl, ELT_HEAD_GEDC_FORM, 0,
240                                        ELT_HEAD_GEDC,
241                                        gom_header.gedcom.form);
242   if (gom_header.language)
243     result |= gedcom_write_element_str(hndl, ELT_HEAD_LANG, 0,
244                                        REC_HEAD,
245                                        gom_header.language);
246   if (gom_header.place_hierarchy) {
247     result |= gedcom_write_element_str(hndl, ELT_HEAD_PLAC, 0, REC_HEAD,
248                                        NULL);
249     result |= gedcom_write_element_str(hndl, ELT_HEAD_PLAC_FORM, 0,
250                                        ELT_HEAD_PLAC,
251                                        gom_header.place_hierarchy);
252   }
253   if (gom_header.note)
254     result |= gedcom_write_element_str(hndl, ELT_HEAD_NOTE, 0,
255                                        REC_HEAD,
256                                        gom_header.note);
257   if (gom_header.extra)
258     result |= write_user_data(hndl, gom_header.extra);
259   return result;
260 }