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