Removed dup_date and dup_age.
[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 = make_note_record(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 STRING_END_REC_CB(note, note_end, text)
55 GET_REC_BY_XREF(note, XREF_NOTE, gom_get_note_by_xref)
56      
57 Gedcom_ctxt sub_cont_conc_start(_ELT_PARAMS_)
58 {
59   Gom_ctxt ctxt = (Gom_ctxt)parent;
60   Gom_ctxt result = NULL;
61
62   if (! ctxt)
63     NO_CONTEXT;
64   else
65     result = make_gom_ctxt(elt, ctxt->obj_type, ctxt->ctxt_ptr);
66   
67   return (Gedcom_ctxt)result;
68 }
69
70 void note_subscribe()
71 {
72   gedcom_subscribe_to_record(REC_NOTE, note_start, note_end);
73   gedcom_subscribe_to_element(ELT_SUB_CONT, sub_cont_conc_start, def_elt_end);
74   gedcom_subscribe_to_element(ELT_SUB_CONC, sub_cont_conc_start, def_elt_end);
75 }
76
77 void note_add_citation(Gom_ctxt ctxt, struct source_citation* cit)
78 {
79   struct note *note = SAFE_CTXT_CAST(note, ctxt);
80   if (note)
81     LINK_CHAIN_ELT(source_citation, note->citation, cit);    
82 }
83
84 void note_add_user_ref(Gom_ctxt ctxt, struct user_ref_number* ref)
85 {
86   struct note *note = SAFE_CTXT_CAST(note, ctxt);
87   if (note)
88     LINK_CHAIN_ELT(user_ref_number, note->ref, ref);
89 }
90
91 void note_set_record_id(Gom_ctxt ctxt, const char *rin)
92 {
93   struct note *note = SAFE_CTXT_CAST(note, ctxt);
94   if (note) {
95     note->record_id = strdup(rin);
96     if (! note->record_id) MEMORY_ERROR;
97   }
98 }
99
100 void note_set_change_date(Gom_ctxt ctxt, struct change_date* chan)
101 {
102   struct note *note = SAFE_CTXT_CAST(note, ctxt);
103   if (note)
104     note->change_date = chan;
105 }
106
107 void note_add_user_data(Gom_ctxt ctxt, struct user_data* data)
108 {
109   struct note *obj = SAFE_CTXT_CAST(note, ctxt);
110   if (obj)
111     LINK_CHAIN_ELT(user_data, obj->extra, data);
112 }
113
114 void note_cleanup(struct note* note)
115 {
116   if (note) {
117     SAFE_FREE(note->xrefstr);
118     SAFE_FREE(note->text);
119     DESTROY_CHAIN_ELTS(source_citation, note->citation, citation_cleanup);
120     DESTROY_CHAIN_ELTS(user_ref_number, note->ref, user_ref_cleanup);
121     SAFE_FREE(note->record_id);
122     change_date_cleanup(note->change_date);
123     DESTROY_CHAIN_ELTS(user_data, note->extra, user_data_cleanup);
124   }
125 }
126
127 void notes_cleanup()
128 {
129   DESTROY_CHAIN_ELTS(note, gom_first_note, note_cleanup);
130 }
131
132 struct note* gom_get_first_note()
133 {
134   return gom_first_note;
135 }
136
137 struct note* make_note_record(const char* xrefstr)
138 {
139   struct note* note = NULL;
140   MAKE_CHAIN_ELT(note, gom_first_note, note);
141   if (note) {
142     note->xrefstr = strdup(xrefstr);
143     if (! note->xrefstr) MEMORY_ERROR;
144   }
145   return note;
146 }
147
148 int write_notes(Gedcom_write_hndl hndl)
149 {
150   int result = 0;
151   struct note* obj;
152
153   for (obj = gom_first_note; obj; obj = obj->next) {
154     result |= gedcom_write_record_str(hndl, REC_NOTE, 0,
155                                       obj->xrefstr, obj->text);
156     if (obj->citation)
157       result |= write_citations(hndl, REC_NOTE, obj->citation);
158     if (obj->ref)
159       result |= write_user_refs(hndl, REC_NOTE, obj->ref);
160     if (obj->record_id)
161       result |= gedcom_write_element_str(hndl, ELT_SUB_IDENT_RIN, 0,
162                                          REC_NOTE, obj->record_id);
163     if (obj->change_date)
164       result |= write_change_date(hndl, REC_NOTE, obj->change_date);
165     if (obj->extra)
166       result |= write_user_data(hndl, obj->extra);
167   }
168   
169   return result;
170 }
171