Only try to delete address if present.
[gedcom-parse.git] / gom / note.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 "header.h"
27 #include "source_citation.h"
28 #include "note.h"
29 #include "note_sub.h"
30 #include "user_ref.h"
31 #include "change_date.h"
32 #include "source.h"
33 #include "user_rec.h"
34 #include "gom.h"
35 #include "gedcom.h"
36 #include "gom_internal.h"
37
38 struct note* gom_first_note = NULL;
39
40 Gedcom_ctxt note_start(_REC_PARAMS_)
41 {
42   Gom_ctxt result = NULL;
43   struct xref_value* xr = GEDCOM_XREF_PTR(xref);
44   struct note* note = (struct note*) xr->object;
45   if (! note) {
46     note = MAKEFUNC(note)(xr->string);
47     xr->object = (Gedcom_ctxt) note;
48   }
49   if (note)
50     result = MAKE_GOM_CTXT(rec, note, xr->object);
51   return (Gedcom_ctxt)result;
52 }
53
54 DEFINE_MAKEFUNC(note, gom_first_note)
55 DEFINE_DESTROYFUNC(note, gom_first_note)
56 DEFINE_ADDFUNC(note, XREF_NOTE)
57 DEFINE_DELETEFUNC(note)
58 DEFINE_GETXREFFUNC(note, XREF_NOTE)
59      
60 DEFINE_STRING_END_REC_CB(note, note_end, text)
61
62 DEFINE_ADDFUNC2(note, source_citation, citation)
63 DEFINE_ADDFUNC2(note, user_ref_number, ref)
64 DEFINE_ADDFUNC2(note, user_data, extra)
65 DEFINE_ADDFUNC2_NOLIST(note, change_date, change_date)
66 DEFINE_ADDFUNC2_STR(note, record_id)
67      
68 Gedcom_ctxt sub_cont_conc_start(_ELT_PARAMS_)
69 {
70   Gom_ctxt ctxt = (Gom_ctxt)parent;
71   Gom_ctxt result = NULL;
72
73   if (! ctxt)
74     NO_CONTEXT;
75   else
76     result = make_gom_ctxt(elt, ctxt->obj_type, ctxt->ctxt_ptr);
77   
78   return (Gedcom_ctxt)result;
79 }
80
81 void note_subscribe()
82 {
83   gedcom_subscribe_to_record(REC_NOTE, note_start, note_end);
84   gedcom_subscribe_to_element(ELT_SUB_CONT, sub_cont_conc_start, def_elt_end);
85   gedcom_subscribe_to_element(ELT_SUB_CONC, sub_cont_conc_start, def_elt_end);
86 }
87
88 void UNREFALLFUNC(note)(struct note *obj)
89 {
90   if (obj) {
91     UNREFALLFUNC(source_citation)(obj->citation);
92     UNREFALLFUNC(user_ref_number)(obj->ref);
93     UNREFALLFUNC(change_date)(obj->change_date);
94     UNREFALLFUNC(user_data)(obj->extra);
95   }
96 }
97
98 void CLEANFUNC(note)(struct note* note)
99 {
100   if (note) {
101     SAFE_FREE(note->xrefstr);
102     SAFE_FREE(note->text);
103     DESTROY_CHAIN_ELTS(source_citation, note->citation);
104     DESTROY_CHAIN_ELTS(user_ref_number, note->ref);
105     SAFE_FREE(note->record_id);
106     CLEANFUNC(change_date)(note->change_date);
107     DESTROY_CHAIN_ELTS(user_data, note->extra);
108   }
109 }
110
111 void notes_cleanup()
112 {
113   DESTROY_CHAIN_ELTS(note, gom_first_note);
114 }
115
116 struct note* gom_get_first_note()
117 {
118   return gom_first_note;
119 }
120
121 int write_notes(Gedcom_write_hndl hndl)
122 {
123   int result = 0;
124   struct note* obj;
125
126   for (obj = gom_first_note; obj; obj = obj->next) {
127     result |= gedcom_write_record_str(hndl, REC_NOTE, obj->xrefstr, obj->text);
128     if (obj->citation)
129       result |= write_citations(hndl, REC_NOTE, obj->citation);
130     if (obj->ref)
131       result |= write_user_refs(hndl, REC_NOTE, obj->ref);
132     if (obj->record_id)
133       result |= gedcom_write_element_str(hndl, ELT_SUB_IDENT_RIN, 0,
134                                          REC_NOTE, obj->record_id);
135     if (obj->change_date)
136       result |= write_change_date(hndl, REC_NOTE, obj->change_date);
137     if (obj->extra)
138       result |= write_user_data(hndl, obj->extra);
139   }
140   
141   return result;
142 }
143