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