Allow elements out of context in GOM.
[gedcom-parse.git] / gom / submitter.c
1 /* Submitter 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 <stdlib.h>
25 #include <string.h>
26 #include "submitter.h"
27 #include "address.h"
28 #include "multimedia_link.h"
29 #include "change_date.h"
30 #include "user_rec.h"
31 #include "gom.h"
32 #include "gedcom.h"
33 #include "gom_internal.h"
34
35 struct submitter* gom_first_submitter = NULL;
36
37 DEFINE_MAKEFUNC(submitter, gom_first_submitter)
38 DEFINE_DESTROYFUNC(submitter, gom_first_submitter)
39 DEFINE_ADDFUNC(submitter, XREF_SUBM)
40 DEFINE_DELETEFUNC(submitter)
41 DEFINE_GETXREFFUNC(submitter, XREF_SUBM)
42      
43 DEFINE_REC_CB(submitter, subm_start)
44 DEFINE_STRING_CB(submitter, subm_name_start, name)
45 DEFINE_STRING_CB(submitter, subm_rfn_start, record_file_nr)
46 DEFINE_STRING_CB(submitter, subm_rin_start, record_id)
47
48 DEFINE_ADDFUNC2(submitter, multimedia_link, mm_link)
49 DEFINE_ADDFUNC2(submitter, user_data, extra)
50 DEFINE_ADDFUNC2_NOLIST(submitter, address, address)
51 DEFINE_ADDFUNC2_NOLIST(submitter, change_date, change_date)
52 DEFINE_ADDFUNC2_STRN(submitter, phone, 3)
53
54 Gedcom_ctxt subm_lang_start(_ELT_PARAMS_)
55 {
56   Gom_ctxt ctxt = (Gom_ctxt)parent;
57   Gom_ctxt result = NULL;
58
59   if (! ctxt)
60     NO_CONTEXT;
61   else {
62     struct submitter *subm = SAFE_CTXT_CAST(submitter, ctxt);
63
64     if (subm) {
65       int err = 0;
66       char *str = GEDCOM_STRING(parsed_value);
67       int i = 0;
68
69       while (i<2 && subm->language[i]) i++;
70       if (! subm->language[i]) {
71         subm->language[i] = strdup(str);
72         if (! subm->language[i]) {
73           MEMORY_ERROR;
74           err = 1;
75         }
76       }
77       if (! err)
78         result = MAKE_GOM_CTXT(elt, submitter, subm);
79     }
80   }
81   
82   return (Gedcom_ctxt)result;
83 }
84
85 void submitter_subscribe()
86 {
87   gedcom_subscribe_to_record(REC_SUBM, subm_start, def_rec_end);
88   gedcom_subscribe_to_element(ELT_SUBM_NAME, subm_name_start, def_elt_end);
89   gedcom_subscribe_to_element(ELT_SUBM_LANG, subm_lang_start, def_elt_end);
90   gedcom_subscribe_to_element(ELT_SUBM_RFN, subm_rfn_start, def_elt_end);
91   gedcom_subscribe_to_element(ELT_SUBM_RIN, subm_rin_start, def_elt_end);
92 }
93
94 void UNREFALLFUNC(submitter)(struct submitter *obj)
95 {
96   if (obj) {
97     UNREFALLFUNC(address)(obj->address);
98     UNREFALLFUNC(multimedia_link)(obj->mm_link);
99     UNREFALLFUNC(change_date)(obj->change_date);
100     UNREFALLFUNC(user_data)(obj->extra);
101   }
102 }
103
104 void CLEANFUNC(submitter)(struct submitter* rec)
105 {
106   if (rec) {
107     SAFE_FREE(rec->xrefstr);
108     SAFE_FREE(rec->name);
109     CLEANFUNC(address)(rec->address);
110     SAFE_FREE(rec->phone[0]);
111     SAFE_FREE(rec->phone[1]);
112     SAFE_FREE(rec->phone[2]);
113     DESTROY_CHAIN_ELTS(multimedia_link, rec->mm_link);
114     SAFE_FREE(rec->language[0]);
115     SAFE_FREE(rec->language[1]);
116     SAFE_FREE(rec->language[2]);
117     SAFE_FREE(rec->record_file_nr);
118     SAFE_FREE(rec->record_id);
119     CLEANFUNC(change_date)(rec->change_date);
120     DESTROY_CHAIN_ELTS(user_data, rec->extra);
121   }
122 }
123
124 void submitters_cleanup()
125 {
126   DESTROY_CHAIN_ELTS(submitter, gom_first_submitter);
127 }
128
129 struct submitter* gom_get_first_submitter()
130 {
131   return gom_first_submitter;
132 }
133
134 int write_submitters(Gedcom_write_hndl hndl)
135 {
136   int result = 0;
137   int i;
138   struct submitter* obj;
139
140   for (obj = gom_first_submitter; obj; obj = obj->next) {
141     result |= gedcom_write_record_str(hndl, REC_SUBM, obj->xrefstr, NULL);
142     if (obj->name)
143       result |= gedcom_write_element_str(hndl, ELT_SUBM_NAME, 0, REC_SUBM,
144                                          obj->name);
145     if (obj->address)
146       result |= write_address(hndl, REC_SUBM, obj->address);
147     for (i = 0; i < 3 && obj->phone[i]; i++)
148       result |= gedcom_write_element_str(hndl, ELT_SUB_PHON, 0, REC_SUBM,
149                                          obj->phone[i]);
150     if (obj->mm_link)
151       result |= write_multimedia_links(hndl, REC_SUBM, obj->mm_link);
152     for (i = 0; i < 3 && obj->language[i]; i++)
153       result |= gedcom_write_element_str(hndl, ELT_SUBM_LANG, 0, REC_SUBM,
154                                          obj->language[i]);
155     if (obj->record_file_nr)
156       result |= gedcom_write_element_str(hndl, ELT_SUBM_RFN, 0, REC_SUBM,
157                                          obj->record_file_nr);
158     if (obj->record_id)
159       result |= gedcom_write_element_str(hndl, ELT_SUBM_RIN, 0, REC_SUBM,
160                                          obj->record_id);
161     if (obj->change_date)
162       result |= write_change_date(hndl, REC_SUBM, obj->change_date);
163     if (obj->extra)
164       result |= write_user_data(hndl, obj->extra);
165   }
166   
167   return result;
168 }
169