Pass complete concatenated text in end callback.
[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 STRING_CB(header, head_note_start, note)
64      
65 void header_add_address(Gom_ctxt ctxt, struct address* addr)
66 {
67   struct header *head = SAFE_CTXT_CAST(header, ctxt);
68   if (head)
69     head->source.corporation.address = addr;
70 }
71
72 void header_add_phone(Gom_ctxt ctxt, const char* phone)
73 {
74   struct header *head = SAFE_CTXT_CAST(header, ctxt);
75   if (head) {
76     struct header_corporation *corp = &(head->source.corporation);
77     int i = 0;
78     while (i<2 && corp->phone[i]) i++;
79     if (! corp->phone[i]) {
80       corp->phone[i] = strdup(phone);
81       if (! corp->phone[i]) MEMORY_ERROR;
82     }
83   }
84 }
85
86 void header_add_to_note(NL_TYPE type, Gom_ctxt ctxt, const char* str)
87 {
88   struct header *head = SAFE_CTXT_CAST(header, ctxt);
89   if (head) {
90     char *newvalue = concat_strings(type, head->note, str);
91     if (newvalue)
92       head->note = newvalue;
93     else
94       MEMORY_ERROR;
95   }
96 }
97
98 void header_add_user_data(Gom_ctxt ctxt, struct user_data* data)
99 {
100   struct header *head = SAFE_CTXT_CAST(header, ctxt);
101   if (head)
102     LINK_CHAIN_ELT(user_data, head->extra, data);
103 }
104
105 void header_subscribe()
106 {
107   gedcom_subscribe_to_record(REC_HEAD, head_start, def_rec_end);
108   gedcom_subscribe_to_element(ELT_HEAD_SOUR, head_sour_start, def_elt_end);
109   gedcom_subscribe_to_element(ELT_HEAD_SOUR_NAME, head_sour_name_start,
110                               def_elt_end);
111   gedcom_subscribe_to_element(ELT_HEAD_SOUR_VERS, head_sour_vers_start,
112                               def_elt_end);
113   gedcom_subscribe_to_element(ELT_HEAD_SOUR_CORP, head_sour_corp_start,
114                               def_elt_end);
115   gedcom_subscribe_to_element(ELT_HEAD_SOUR_DATA, head_sour_data_start,
116                               def_elt_end);
117   gedcom_subscribe_to_element(ELT_HEAD_SOUR_DATA_DATE,
118                               head_sour_data_date_start, def_elt_end);
119   gedcom_subscribe_to_element(ELT_HEAD_SOUR_DATA_COPR,
120                               head_sour_data_copr_start, def_elt_end);
121   gedcom_subscribe_to_element(ELT_HEAD_DEST, head_dest_start, def_elt_end);
122   gedcom_subscribe_to_element(ELT_HEAD_DATE, head_date_start, def_elt_end);
123   gedcom_subscribe_to_element(ELT_HEAD_DATE_TIME,
124                               head_date_time_start, def_elt_end);
125   gedcom_subscribe_to_element(ELT_HEAD_SUBM, head_subm_start, def_elt_end);
126   gedcom_subscribe_to_element(ELT_HEAD_SUBN, head_subn_start, def_elt_end);
127   gedcom_subscribe_to_element(ELT_HEAD_FILE, head_file_start, def_elt_end);
128   gedcom_subscribe_to_element(ELT_HEAD_COPR, head_copr_start, def_elt_end);
129   gedcom_subscribe_to_element(ELT_HEAD_GEDC, head_gedc_start, def_elt_end);
130   gedcom_subscribe_to_element(ELT_HEAD_GEDC_VERS,
131                               head_gedc_vers_start, def_elt_end);
132   gedcom_subscribe_to_element(ELT_HEAD_GEDC_FORM,
133                               head_gedc_form_start, def_elt_end);
134   gedcom_subscribe_to_element(ELT_HEAD_CHAR, head_char_start, def_elt_end);
135   gedcom_subscribe_to_element(ELT_HEAD_CHAR_VERS,
136                               head_char_vers_start, def_elt_end);
137   gedcom_subscribe_to_element(ELT_HEAD_LANG, head_lang_start, def_elt_end);
138   gedcom_subscribe_to_element(ELT_HEAD_PLAC, head_plac_start, def_elt_end);
139   gedcom_subscribe_to_element(ELT_HEAD_PLAC_FORM,
140                               head_plac_form_start, def_elt_end);
141   gedcom_subscribe_to_element(ELT_HEAD_NOTE, head_note_start, def_elt_end);
142 }
143
144 void header_cleanup()
145 {
146   SAFE_FREE(gom_header.source.id);
147   SAFE_FREE(gom_header.source.name);
148   SAFE_FREE(gom_header.source.version);
149   SAFE_FREE(gom_header.source.corporation.name);
150   address_cleanup(gom_header.source.corporation.address);
151   SAFE_FREE(gom_header.source.corporation.phone[0]);
152   SAFE_FREE(gom_header.source.corporation.phone[1]);
153   SAFE_FREE(gom_header.source.corporation.phone[2]);
154   SAFE_FREE(gom_header.source.data.name);
155   SAFE_FREE(gom_header.source.data.date);
156   SAFE_FREE(gom_header.source.data.copyright);
157   SAFE_FREE(gom_header.destination);
158   SAFE_FREE(gom_header.date);
159   SAFE_FREE(gom_header.time);
160   SAFE_FREE(gom_header.filename);
161   SAFE_FREE(gom_header.copyright);
162   SAFE_FREE(gom_header.gedcom.version);
163   SAFE_FREE(gom_header.gedcom.form);
164   SAFE_FREE(gom_header.charset.name);
165   SAFE_FREE(gom_header.charset.version);
166   SAFE_FREE(gom_header.language);
167   SAFE_FREE(gom_header.place_hierarchy);
168   SAFE_FREE(gom_header.note);
169   DESTROY_CHAIN_ELTS(user_data, gom_header.extra, user_data_cleanup);
170 }
171
172 struct header* gom_get_header()
173 {
174   return &gom_header;
175 }
176
177 int write_header(Gedcom_write_hndl hndl)
178 {
179   int result = 0;
180   
181   result |= gedcom_write_record_str(hndl, REC_HEAD, NULL, NULL, NULL);
182   if (gom_header.source.id)
183     result |= gedcom_write_element_str(hndl, ELT_HEAD_SOUR, NULL, REC_HEAD,
184                                        gom_header.source.id);
185   if (gom_header.source.name)
186     result |= gedcom_write_element_str(hndl, ELT_HEAD_SOUR_NAME, NULL,
187                                        ELT_HEAD_SOUR,
188                                        gom_header.source.name);
189   if (gom_header.source.version)
190     result |= gedcom_write_element_str(hndl, ELT_HEAD_SOUR_VERS, NULL,
191                                        ELT_HEAD_SOUR,
192                                        gom_header.source.version);
193   if (gom_header.source.corporation.name)
194     result |= gedcom_write_element_str(hndl, ELT_HEAD_SOUR_CORP, NULL,
195                                        ELT_HEAD_SOUR,
196                                        gom_header.source.corporation.name);
197   if (gom_header.source.data.name)
198     result |= gedcom_write_element_str(hndl, ELT_HEAD_SOUR_DATA, NULL,
199                                        ELT_HEAD_SOUR,
200                                        gom_header.source.data.name);
201   if (gom_header.source.data.copyright)
202     result |= gedcom_write_element_str(hndl, ELT_HEAD_SOUR_DATA_COPR, NULL,
203                                        ELT_HEAD_SOUR_DATA,
204                                        gom_header.source.data.copyright);
205   if (gom_header.destination)
206     result |= gedcom_write_element_str(hndl, ELT_HEAD_DEST, NULL, REC_HEAD,
207                                        gom_header.destination);
208   if (gom_header.filename)
209     result |= gedcom_write_element_str(hndl, ELT_HEAD_FILE, NULL, REC_HEAD,
210                                        gom_header.filename);
211   if (gom_header.copyright)
212     result |= gedcom_write_element_str(hndl, ELT_HEAD_COPR, NULL, REC_HEAD,
213                                        gom_header.copyright);
214   result |= gedcom_write_element_str(hndl, ELT_HEAD_GEDC, NULL, REC_HEAD,
215                                      NULL);
216   if (gom_header.gedcom.version)
217     result |= gedcom_write_element_str(hndl, ELT_HEAD_GEDC_VERS, NULL,
218                                        ELT_HEAD_GEDC,
219                                        gom_header.gedcom.version);
220   if (gom_header.gedcom.form)
221     result |= gedcom_write_element_str(hndl, ELT_HEAD_GEDC_FORM, NULL,
222                                        ELT_HEAD_GEDC,
223                                        gom_header.gedcom.form);
224   if (gom_header.charset.name)
225     result |= gedcom_write_element_str(hndl, ELT_HEAD_CHAR, NULL,
226                                        REC_HEAD,
227                                        gom_header.charset.name);
228   if (gom_header.charset.version)
229     result |= gedcom_write_element_str(hndl, ELT_HEAD_CHAR_VERS, NULL,
230                                        ELT_HEAD_CHAR,
231                                        gom_header.charset.version);
232   if (gom_header.language)
233     result |= gedcom_write_element_str(hndl, ELT_HEAD_LANG, NULL,
234                                        REC_HEAD,
235                                        gom_header.language);
236   if (gom_header.place_hierarchy) {
237     result |= gedcom_write_element_str(hndl, ELT_HEAD_PLAC, NULL, REC_HEAD,
238                                        NULL);
239     result |= gedcom_write_element_str(hndl, ELT_HEAD_PLAC_FORM, NULL,
240                                        ELT_HEAD_PLAC,
241                                        gom_header.place_hierarchy);
242   }
243   if (gom_header.note)
244     result |= gedcom_write_element_str(hndl, ELT_HEAD_NOTE, NULL,
245                                        REC_HEAD,
246                                        gom_header.note);
247   return result;
248 }