Correct sub_note_end callback: parsed value can be NULL.
[gedcom-parse.git] / gom / note_sub.c
1 /* Note 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 "note.h"
27 #include "note_sub.h"
28 #include "place.h"
29 #include "event.h"
30 #include "source_citation.h"
31 #include "multimedia_link.h"
32 #include "lds_event.h"
33 #include "family.h"
34 #include "change_date.h"
35 #include "personal_name.h"
36 #include "family_link.h"
37 #include "association.h"
38 #include "individual.h"
39 #include "multimedia.h"
40 #include "repository.h"
41 #include "source.h"
42 #include "user_rec.h"
43 #include "gom.h"
44 #include "gedcom.h"
45 #include "gom_internal.h"
46
47 Gedcom_ctxt sub_note_start(_ELT_PARAMS_)
48 {
49   Gom_ctxt ctxt = (Gom_ctxt)parent;
50   Gom_ctxt result = NULL;
51
52   if (! ctxt)
53     NO_CONTEXT;
54   else {
55     struct note_sub *note = SUB_MAKEFUNC(note_sub)();
56     if (note) {
57       if (GEDCOM_IS_XREF_PTR(parsed_value))
58         note->reference = GEDCOM_XREF_PTR(parsed_value);
59
60       switch (ctxt->ctxt_type) {
61         case ELT_SUB_PLAC:
62           ADDFUNC2(place,note_sub)(ctxt, note); break;
63         case ELT_SUB_FAM_EVT:
64         case ELT_SUB_FAM_EVT_EVEN:
65         case ELT_SUB_INDIV_ATTR:
66         case ELT_SUB_INDIV_RESI:
67         case ELT_SUB_INDIV_BIRT:
68         case ELT_SUB_INDIV_GEN:
69         case ELT_SUB_INDIV_ADOP:
70         case ELT_SUB_INDIV_EVEN:
71           ADDFUNC2(event,note_sub)(ctxt, note); break;
72         case ELT_SUB_SOUR:
73           ADDFUNC2(source_citation,note_sub)(ctxt, note); break;
74         case ELT_SUB_MULTIM_OBJE:
75           ADDFUNC2(multimedia_link,note_sub)(ctxt, note); break;
76         case ELT_SUB_LSS_SLGS:
77         case ELT_SUB_LIO_BAPL:
78         case ELT_SUB_LIO_SLGC:
79           ADDFUNC2(lds_event,note_sub)(ctxt, note); break;
80         case REC_FAM:
81           ADDFUNC2(family,note_sub)(ctxt, note); break;
82         case ELT_SUB_CHAN:
83           ADDFUNC2(change_date,note_sub)(ctxt, note); break;
84         case ELT_SUB_PERS_NAME:
85           ADDFUNC2(personal_name,note_sub)(ctxt, note); break;
86         case ELT_SUB_FAMC: 
87         case ELT_SUB_FAMS:
88           ADDFUNC2(family_link,note_sub)(ctxt, note); break;
89         case ELT_SUB_ASSO:
90           ADDFUNC2(association,note_sub)(ctxt, note); break;
91         case REC_INDI:
92           ADDFUNC2(individual,note_sub)(ctxt, note); break;
93         case REC_OBJE:
94           ADDFUNC2(multimedia,note_sub)(ctxt, note); break;
95         case REC_REPO:
96           ADDFUNC2(repository,note_sub)(ctxt, note); break;
97         case ELT_SOUR_DATA:
98           source_add_note_to_data(ctxt, note); break;
99         case ELT_SUB_REPO:
100           source_add_note_to_repo(ctxt, note); break;
101         case REC_SOUR:
102           ADDFUNC2(source,note_sub)(ctxt, note); break;
103         default:
104           UNEXPECTED_CONTEXT(ctxt->ctxt_type);
105       }
106       result = MAKE_GOM_CTXT(elt, note_sub, note);
107     }
108   }
109
110   return (Gedcom_ctxt)result;
111 }
112
113 void sub_note_end(_ELT_END_PARAMS_)
114 {
115   if (GEDCOM_IS_STRING(parsed_value)) {
116     Gom_ctxt ctxt = (Gom_ctxt)self;
117     if (! ctxt)
118       NO_CONTEXT;
119     else {
120       struct note_sub *obj = SAFE_CTXT_CAST(note_sub, ctxt);
121       if (obj) {
122         char *str = GEDCOM_STRING(parsed_value);
123         char *newvalue = strdup(str);
124         if (! newvalue)
125           MEMORY_ERROR;
126         else
127           obj->text = newvalue;
128       }
129       destroy_gom_ctxt(ctxt);
130     }
131   }
132 }
133
134 DEFINE_SUB_MAKEFUNC(note_sub)
135      
136 DEFINE_ADDFUNC2(note_sub, source_citation, citation)
137 DEFINE_ADDFUNC2(note_sub, user_data, extra)
138      
139 void note_sub_subscribe()
140 {
141   gedcom_subscribe_to_element(ELT_SUB_NOTE, sub_note_start, sub_note_end);
142 }
143
144 void UNREFALLFUNC(note_sub)(struct note_sub* obj)
145 {
146   if (obj) {
147     struct note_sub* runner;
148     for (runner = obj; runner; runner = runner->next) {
149       unref_xref_value(runner->reference);
150       UNREFALLFUNC(source_citation)(runner->citation);
151       UNREFALLFUNC(user_data)(runner->extra);
152     }
153   }
154 }
155
156 void CLEANFUNC(note_sub)(struct note_sub* note)
157 {
158   if (note) {
159     SAFE_FREE(note->text);
160     DESTROY_CHAIN_ELTS(source_citation, note->citation);
161     DESTROY_CHAIN_ELTS(user_data, note->extra);
162   }
163 }
164
165 int write_note_subs(Gedcom_write_hndl hndl, int parent, struct note_sub* note)
166 {
167   int result = 0;
168   struct note_sub* obj;
169
170   if (!note) return 1;
171
172   for (obj = note; obj; obj = obj->next) {
173     if (obj->reference) {
174       result |= gedcom_write_element_xref(hndl, ELT_SUB_NOTE, 0,
175                                           parent, obj->reference);
176     }
177     else {
178       result |= gedcom_write_element_str(hndl, ELT_SUB_NOTE, 0,
179                                          parent, obj->text);
180     }
181     if (obj->citation)
182       result |= write_citations(hndl, ELT_SUB_NOTE, obj->citation);
183     if (obj->extra)
184       result |= write_user_data(hndl, obj->extra);
185   }
186
187   return result;
188 }