470812925bf041d59aba517b1cad8243ab77076f
[gedcom-parse.git] / gom / source_citation.c
1 /* Source citation sub-structure 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 "source_citation.h"
27 #include "place.h"
28 #include "event.h"
29 #include "note_sub.h"
30 #include "multimedia_link.h"
31 #include "lds_event.h"
32 #include "family.h"
33 #include "personal_name.h"
34 #include "individual.h"
35 #include "association.h"
36 #include "note.h"
37 #include "user_rec.h"
38 #include "gom.h"
39 #include "gedcom.h"
40 #include "gom_internal.h"
41
42 Gedcom_ctxt sub_citation_start(_ELT_PARAMS_)
43 {
44   Gom_ctxt ctxt = (Gom_ctxt)parent;
45   struct source_citation *cit = NULL;
46
47   if (ctxt) {
48     cit = (struct source_citation *)malloc(sizeof(struct source_citation));
49     memset (cit, 0, sizeof(struct source_citation));
50     if (GEDCOM_IS_STRING(parsed_value))
51       cit->description = strdup(GEDCOM_STRING(parsed_value));
52     else if (GEDCOM_IS_XREF_PTR(parsed_value))
53       cit->reference = GEDCOM_XREF_PTR(parsed_value);
54
55     switch (ctxt->ctxt_type) {
56       case ELT_SUB_PLAC:
57         place_add_citation(ctxt, cit); break;
58       case ELT_SUB_FAM_EVT:
59       case ELT_SUB_FAM_EVT_EVEN:
60       case ELT_SUB_INDIV_ATTR:
61       case ELT_SUB_INDIV_RESI:
62       case ELT_SUB_INDIV_BIRT:
63       case ELT_SUB_INDIV_GEN:
64       case ELT_SUB_INDIV_ADOP:
65       case ELT_SUB_INDIV_EVEN:
66         event_add_citation(ctxt, cit); break;
67       case ELT_SUB_NOTE:
68         note_sub_add_citation(ctxt, cit); break;
69       case ELT_SUB_LSS_SLGS:
70       case ELT_SUB_LIO_BAPL:
71       case ELT_SUB_LIO_SLGC:
72         lds_event_add_citation(ctxt, cit); break;
73       case REC_FAM:
74         family_add_citation(ctxt, cit); break;
75       case ELT_SUB_PERS_NAME:
76         name_add_citation(ctxt, cit); break;
77       case REC_INDI:
78         individual_add_citation(ctxt, cit); break;
79       case ELT_SUB_ASSO:
80         association_add_citation(ctxt, cit); break;
81       case REC_NOTE:
82         note_add_citation(ctxt, cit); break;
83       default:
84         UNEXPECTED_CONTEXT(ctxt->ctxt_type);
85     }
86   }
87
88   return (Gedcom_ctxt) MAKE_GOM_CTXT(elt, source_citation, cit);
89 }
90
91 Gedcom_ctxt sub_cit_text_start(_ELT_PARAMS_)
92 {
93   Gom_ctxt ctxt = (Gom_ctxt)parent;
94   struct source_citation *cit = SAFE_CTXT_CAST(source_citation, ctxt);
95   struct text *t;
96   MAKE_CHAIN_ELT(text, cit->text, t);
97   t->text = strdup(GEDCOM_STRING(parsed_value));
98   return (Gedcom_ctxt) MAKE_GOM_CTXT(elt, text, t);
99 }
100
101 STRING_CB(source_citation, sub_cit_page_start, page)
102 STRING_CB(source_citation, sub_cit_even_start, event)
103 STRING_CB(source_citation, sub_cit_even_role_start, role)
104 NULL_CB(source_citation, sub_cit_data_start)
105 DATE_CB(source_citation, sub_cit_data_date_start, date)
106 STRING_CB(source_citation, sub_cit_quay_start, quality)
107      
108 void citation_subscribe()
109 {
110   gedcom_subscribe_to_element(ELT_SUB_SOUR, sub_citation_start, def_elt_end);
111   gedcom_subscribe_to_element(ELT_SUB_SOUR_PAGE, sub_cit_page_start,
112                               def_elt_end);
113   gedcom_subscribe_to_element(ELT_SUB_SOUR_EVEN, sub_cit_even_start,
114                               def_elt_end);
115   gedcom_subscribe_to_element(ELT_SUB_SOUR_EVEN_ROLE, sub_cit_even_role_start,
116                               def_elt_end);
117   gedcom_subscribe_to_element(ELT_SUB_SOUR_DATA, sub_cit_data_start,
118                               def_elt_end);
119   gedcom_subscribe_to_element(ELT_SUB_SOUR_DATA_DATE, sub_cit_data_date_start,
120                               def_elt_end);
121   gedcom_subscribe_to_element(ELT_SUB_SOUR_TEXT, sub_cit_text_start,
122                               def_elt_end);
123   gedcom_subscribe_to_element(ELT_SUB_SOUR_QUAY, sub_cit_quay_start,
124                               def_elt_end);
125 }
126
127 void citation_add_note(Gom_ctxt ctxt, struct note_sub* note)
128 {
129   struct source_citation *cit = SAFE_CTXT_CAST(source_citation, ctxt);
130   LINK_CHAIN_ELT(note_sub, cit->note, note)    
131 }
132
133 void citation_add_mm_link(Gom_ctxt ctxt, struct multimedia_link* mm)
134 {
135   struct source_citation *cit = SAFE_CTXT_CAST(source_citation, ctxt);
136   LINK_CHAIN_ELT(multimedia_link, cit->mm_link, mm)    
137 }
138
139 void citation_add_to_desc(NL_TYPE type, Gom_ctxt ctxt, char* str)
140 {
141   struct source_citation *cit = SAFE_CTXT_CAST(source_citation, ctxt);
142   cit->description = concat_strings (type, cit->description, str);
143 }
144
145 void citation_add_to_text(NL_TYPE type, Gom_ctxt ctxt, char* str)
146 {
147   struct text *t = SAFE_CTXT_CAST(text, ctxt);
148   t->text = concat_strings (type, t->text, str);
149 }
150
151 void citation_add_user_data(Gom_ctxt ctxt, struct user_data* data)
152 {
153   struct source_citation *obj = SAFE_CTXT_CAST(source_citation, ctxt);
154   LINK_CHAIN_ELT(user_data, obj->extra, data)
155 }
156
157 void text_cleanup(struct text* t)
158 {
159   if (t) {
160     SAFE_FREE(t->text);
161   }
162 }
163
164 void citation_cleanup(struct source_citation* cit)
165 {
166   if (cit) {
167     SAFE_FREE(cit->description);
168     SAFE_FREE(cit->page);
169     SAFE_FREE(cit->event);
170     SAFE_FREE(cit->role);
171     SAFE_FREE(cit->date);
172     DESTROY_CHAIN_ELTS(text, cit->text, text_cleanup)
173     SAFE_FREE(cit->quality);
174     DESTROY_CHAIN_ELTS(multimedia_link, cit->mm_link, multimedia_link_cleanup)
175     DESTROY_CHAIN_ELTS(note_sub, cit->note, note_sub_cleanup)
176     DESTROY_CHAIN_ELTS(user_data, cit->extra, user_data_cleanup)
177   }
178 }