Show value that is too long.
[gedcom-parse.git] / gom / submission.c
1 /* Submission 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 "submission.h"
27 #include "submitter.h"
28 #include "user_rec.h"
29 #include "gom.h"
30 #include "gedcom.h"
31 #include "gom_internal.h"
32
33 struct submission* gom_submission = NULL;
34
35 DEFINE_MAKELINKFUNC(submission, XREF_SUBN)
36      
37 DEFINE_REC_CB(submission, subn_start)
38 DEFINE_XREF_CB(submission, subn_subm_start, submitter, submitter)
39 DEFINE_STRING_CB(submission, subn_famf_start, family_file)
40 DEFINE_STRING_CB(submission, subn_temp_start, temple_code)
41 DEFINE_STRING_CB(submission, subn_ance_start, nr_of_ancestor_gens)
42 DEFINE_STRING_CB(submission, subn_desc_start, nr_of_descendant_gens)
43 DEFINE_STRING_CB(submission, subn_ordi_start, ordinance_process_flag)
44 DEFINE_STRING_CB(submission, subn_rin_start, record_id)
45
46 DEFINE_ADDFUNC2(submission, user_data, extra)
47      
48 void submission_subscribe()
49 {
50   gedcom_subscribe_to_record(REC_SUBN, subn_start, def_rec_end);
51   gedcom_subscribe_to_element(ELT_SUBN_SUBM, subn_subm_start, def_elt_end);
52   gedcom_subscribe_to_element(ELT_SUBN_FAMF, subn_famf_start, def_elt_end);
53   gedcom_subscribe_to_element(ELT_SUBN_TEMP, subn_temp_start, def_elt_end);
54   gedcom_subscribe_to_element(ELT_SUBN_ANCE, subn_ance_start, def_elt_end);
55   gedcom_subscribe_to_element(ELT_SUBN_DESC, subn_desc_start, def_elt_end);
56   gedcom_subscribe_to_element(ELT_SUBN_ORDI, subn_ordi_start, def_elt_end);
57   gedcom_subscribe_to_element(ELT_SUBN_RIN, subn_rin_start, def_elt_end);  
58 }
59
60 void submission_cleanup()
61 {
62   if (gom_submission) {
63     SAFE_FREE(gom_submission->xrefstr);
64     SAFE_FREE(gom_submission->family_file);
65     SAFE_FREE(gom_submission->temple_code);
66     SAFE_FREE(gom_submission->nr_of_ancestor_gens);
67     SAFE_FREE(gom_submission->nr_of_descendant_gens);
68     SAFE_FREE(gom_submission->ordinance_process_flag);
69     SAFE_FREE(gom_submission->record_id);
70     DESTROY_CHAIN_ELTS(user_data, gom_submission->extra);
71     SAFE_FREE(gom_submission);
72   }
73 }
74
75 struct submission* gom_get_submission()
76 {
77   return gom_submission;
78 }
79
80 struct submission* MAKEFUNC(submission)(const char* xref)
81 {
82   if (! gom_submission) {
83     gom_submission = (struct submission*)malloc(sizeof(struct submission));
84     if (! gom_submission)
85       MEMORY_ERROR;
86     else {
87       memset(gom_submission, 0, sizeof(struct submission));
88       gom_submission->xrefstr = strdup(xref);
89       if (!gom_submission->xrefstr) MEMORY_ERROR;
90     }
91   }
92   
93   return gom_submission;
94 }
95
96 void DESTROYFUNC(submission)()
97 {
98   if (gom_submission) {
99     submission_cleanup();
100     free(gom_submission);
101     gom_submission = NULL;
102   }
103 }
104
105 struct submission* ADDFUNC(submission)(const char* xrefstr)
106 {
107   if (!gom_submission) {
108     struct xref_value* xrv = gedcom_get_by_xref(xrefstr);
109     if (xrv)
110       gom_xref_already_in_use(xrefstr);
111     else {
112       gom_submission = MAKEFUNC(submission)(xrefstr);
113       if (gom_submission) {
114         xrv = gedcom_add_xref(XREF_SUBN, xrefstr, gom_submission);
115         if (!xrv) {
116           DESTROYFUNC(submission)(gom_submission);
117           gom_submission = NULL;
118         }
119       }
120     }
121     return gom_submission;
122   }
123   else
124     return NULL;
125 }
126
127 int DELETEFUNC(submission)()
128 {
129   int result = 1;
130   if (gom_submission) {
131     result = gedcom_delete_xref(gom_submission->xrefstr);
132     if (result == 0)
133       DESTROYFUNC(submission)();
134   }
135   return result;
136 }
137
138 int write_submission(Gedcom_write_hndl hndl)
139 {
140   int result = 0;
141
142   if (gom_submission) {
143     result |= gedcom_write_record_str(hndl, REC_SUBN, 
144                                       gom_submission->xrefstr, NULL);
145     if (gom_submission->submitter)
146       result |= gedcom_write_element_xref(hndl, ELT_SUBN_SUBM, 0,
147                                           REC_SUBN, gom_submission->submitter);
148     if (gom_submission->family_file)
149       result |= gedcom_write_element_str(hndl, ELT_SUBN_FAMF, 0, REC_SUBN,
150                                          gom_submission->family_file);
151     if (gom_submission->temple_code)
152       result |= gedcom_write_element_str(hndl, ELT_SUBN_TEMP, 0, REC_SUBN,
153                                          gom_submission->temple_code);
154     if (gom_submission->nr_of_ancestor_gens)
155       result |= gedcom_write_element_str(hndl, ELT_SUBN_ANCE, 0, REC_SUBN,
156                                          gom_submission->nr_of_ancestor_gens);
157     if (gom_submission->nr_of_descendant_gens)
158       result |= gedcom_write_element_str(hndl, ELT_SUBN_DESC, 0, REC_SUBN,
159                                         gom_submission->nr_of_descendant_gens);
160     if (gom_submission->ordinance_process_flag)
161       result |= gedcom_write_element_str(hndl, ELT_SUBN_ORDI, 0, REC_SUBN,
162                                       gom_submission->ordinance_process_flag);
163     if (gom_submission->record_id)
164       result |= gedcom_write_element_str(hndl, ELT_SUBN_RIN, 0, REC_SUBN,
165                                          gom_submission->record_id);
166     if (gom_submission->extra)
167       result |= write_user_data(hndl, gom_submission->extra);
168   }
169   
170   return result;
171 }