Definition of MAKELINKFUNC.
[gedcom-parse.git] / gom / source.c
1 /* Source 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 "source.h"
27 #include "source_event.h"
28 #include "note_sub.h"
29 #include "source_description.h"
30 #include "repository.h"
31 #include "multimedia_link.h"
32 #include "user_ref.h"
33 #include "change_date.h"
34 #include "user_rec.h"
35 #include "gom.h"
36 #include "gedcom.h"
37 #include "gom_internal.h"
38
39 struct source* gom_first_source = NULL;
40
41 DEFINE_MAKEFUNC(source, gom_first_source)
42 DEFINE_DESTROYFUNC(source, gom_first_source)
43 DEFINE_ADDFUNC(source, XREF_SOUR)
44 DEFINE_DELETEFUNC(source)
45 DEFINE_GETXREFFUNC(source, XREF_SOUR)
46 DEFINE_MAKELINKFUNC(source, XREF_SOUR)
47      
48 DEFINE_REC_CB(source, sour_start)
49 DEFINE_NULL_CB(source, sour_data_start)
50 DEFINE_STRING_CB(source, sour_data_agnc_start, data.agency)
51 DEFINE_NULL_CB(source, sour_auth_start)  /* value set by end callback */
52 DEFINE_STRING_END_CB(source, sour_auth_end, author)
53 DEFINE_NULL_CB(source, sour_titl_start)  /* value set by end callback */
54 DEFINE_STRING_END_CB(source, sour_titl_end, title)
55 DEFINE_STRING_CB(source, sour_abbr_start, abbreviation)
56 DEFINE_NULL_CB(source, sour_publ_start)  /* value set by end callback */
57 DEFINE_STRING_END_CB(source, sour_publ_end, publication)
58 DEFINE_NULL_CB(source, sour_text_start)  /* value set by end callback */
59 DEFINE_STRING_END_CB(source, sour_text_end, text)
60 DEFINE_XREF_CB(source, sour_repo_start, repository.link, repository)
61
62 DEFINE_ADDFUNC2(source, source_event, data.event)
63 DEFINE_ADDFUNC2(source, source_description, repository.description)
64 DEFINE_ADDFUNC2(source, multimedia_link, mm_link)
65 DEFINE_ADDFUNC2(source, note_sub, note)
66 DEFINE_ADDFUNC2(source, user_ref_number, ref)
67 DEFINE_ADDFUNC2(source, user_data, extra)
68 DEFINE_ADDFUNC2_NOLIST(source, change_date, change_date)
69 DEFINE_ADDFUNC2_STR(source, record_id)
70
71 void source_subscribe()
72 {
73   gedcom_subscribe_to_record(REC_SOUR, sour_start, def_rec_end);
74   gedcom_subscribe_to_element(ELT_SOUR_DATA, sour_data_start, def_elt_end);
75   gedcom_subscribe_to_element(ELT_SOUR_DATA_AGNC, sour_data_agnc_start,
76                               def_elt_end);
77   gedcom_subscribe_to_element(ELT_SOUR_AUTH, sour_auth_start, sour_auth_end);
78   gedcom_subscribe_to_element(ELT_SOUR_TITL, sour_titl_start, sour_titl_end);
79   gedcom_subscribe_to_element(ELT_SOUR_ABBR, sour_abbr_start, def_elt_end);
80   gedcom_subscribe_to_element(ELT_SOUR_PUBL, sour_publ_start, sour_publ_end);
81   gedcom_subscribe_to_element(ELT_SOUR_TEXT, sour_text_start, sour_text_end);
82   gedcom_subscribe_to_element(ELT_SUB_REPO, sour_repo_start, def_elt_end);
83 }
84
85 void source_add_note_to_data(Gom_ctxt ctxt, struct note_sub* note)
86 {
87   struct source *sour = SAFE_CTXT_CAST(source, ctxt);
88   if (sour)
89     LINK_CHAIN_ELT(note_sub, sour->data.note, note);  
90 }
91
92 void source_add_note_to_repo(Gom_ctxt ctxt, struct note_sub* note)
93 {
94   struct source *sour = SAFE_CTXT_CAST(source, ctxt);
95   if (sour)
96     LINK_CHAIN_ELT(note_sub, sour->repository.note, note);  
97 }
98
99 void CLEANFUNC(source)(struct source* sour)
100 {
101   if (sour) {
102     SAFE_FREE(sour->xrefstr);
103     DESTROY_CHAIN_ELTS(source_event, sour->data.event);
104     SAFE_FREE(sour->data.agency)
105     DESTROY_CHAIN_ELTS(note_sub, sour->data.note);
106     SAFE_FREE(sour->author);
107     SAFE_FREE(sour->title);
108     SAFE_FREE(sour->abbreviation);
109     SAFE_FREE(sour->publication);
110     SAFE_FREE(sour->text);
111     DESTROY_CHAIN_ELTS(note_sub, sour->repository.note);
112     DESTROY_CHAIN_ELTS(source_description, sour->repository.description);
113     DESTROY_CHAIN_ELTS(multimedia_link, sour->mm_link);
114     DESTROY_CHAIN_ELTS(note_sub, sour->note);
115     DESTROY_CHAIN_ELTS(user_ref_number, sour->ref);
116     SAFE_FREE(sour->record_id);
117     CLEANFUNC(change_date)(sour->change_date);
118     DESTROY_CHAIN_ELTS(user_data, sour->extra);
119   }
120 }
121
122 void sources_cleanup()
123 {
124   DESTROY_CHAIN_ELTS(source, gom_first_source);
125 }
126
127 struct source* gom_get_first_source()
128 {
129   return gom_first_source;
130 }
131
132 int write_sources(Gedcom_write_hndl hndl)
133 {
134   int result = 0;
135   struct source* obj;
136
137   for (obj = gom_first_source; obj; obj = obj->next) {
138     result |= gedcom_write_record_str(hndl, REC_SOUR, obj->xrefstr, NULL);
139     if (obj->data.event || obj->data.agency || obj->data.note)
140       result |= gedcom_write_element_str(hndl, ELT_SOUR_DATA, 0,
141                                          REC_SOUR, NULL);
142     if (obj->data.event)
143       result |= write_source_events(hndl, ELT_SOUR_DATA, obj->data.event);
144     if (obj->data.agency)
145       result |= gedcom_write_element_str(hndl, ELT_SOUR_DATA_AGNC, 0,
146                                          ELT_SOUR_DATA, obj->data.agency);
147     if (obj->data.note)
148       result |= write_note_subs(hndl, ELT_SOUR_DATA, obj->data.note);
149     if (obj->author)
150       result |= gedcom_write_element_str(hndl, ELT_SOUR_AUTH, 0,
151                                          REC_SOUR, obj->author);
152     if (obj->title)
153       result |= gedcom_write_element_str(hndl, ELT_SOUR_TITL, 0,
154                                          REC_SOUR, obj->title);
155     if (obj->abbreviation)
156       result |= gedcom_write_element_str(hndl, ELT_SOUR_ABBR, 0,
157                                          REC_SOUR, obj->abbreviation);
158     if (obj->publication)
159       result |= gedcom_write_element_str(hndl, ELT_SOUR_PUBL, 0,
160                                          REC_SOUR, obj->publication);
161     if (obj->text)
162       result |= gedcom_write_element_str(hndl, ELT_SOUR_TEXT, 0,
163                                          REC_SOUR, obj->text);
164     if (obj->repository.link || obj->repository.note
165         || obj->repository.description) {
166       result |= gedcom_write_element_xref(hndl, ELT_SUB_REPO, 0,
167                                           REC_SOUR, obj->repository.link);
168     }
169     if (obj->repository.note)
170       result |= write_note_subs(hndl, ELT_SUB_REPO, obj->repository.note);
171     if (obj->repository.description)
172       result |= write_source_descriptions(hndl, ELT_SUB_REPO,
173                                           obj->repository.description);
174     if (obj->mm_link)
175       result |= write_multimedia_links(hndl, REC_SOUR, obj->mm_link);
176     if (obj->note)
177       result |= write_note_subs(hndl, REC_SOUR, obj->note);
178     if (obj->ref)
179       result |= write_user_refs(hndl, REC_SOUR, obj->ref);
180     if (obj->record_id)
181       result |= gedcom_write_element_str(hndl, ELT_SUB_IDENT_RIN, 0,
182                                          REC_SOUR, obj->record_id);
183     if (obj->change_date)
184       result |= write_change_date(hndl, REC_SOUR, obj->change_date);
185     if (obj->extra)
186       result |= write_user_data(hndl, obj->extra);
187   }
188   
189   return result;
190 }
191