Better checking of library result values.
[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   struct xref_value* xr = GEDCOM_XREF_PTR(xref);
43   struct note* note = (struct note*) xr->object;
44   if (! xr->object) {
45     note = make_note_record(xr->string);
46     xr->object = (Gedcom_ctxt) note;
47   }
48   note->text = strdup(GEDCOM_STRING(parsed_value));
49   return (Gedcom_ctxt) MAKE_GOM_CTXT(rec, note, xr->object);
50 }
51
52 GET_REC_BY_XREF(note, XREF_NOTE, gom_get_note_by_xref)
53      
54 Gedcom_ctxt sub_cont_conc_start(_ELT_PARAMS_)
55 {
56   Gom_ctxt ctxt = (Gom_ctxt)parent;
57
58   if (ctxt) {
59     char *str = GEDCOM_STRING(parsed_value);
60     NL_TYPE type = (elt == ELT_SUB_CONT ? WITH_NL : WITHOUT_NL);
61     switch (ctxt->ctxt_type) {
62       case ELT_HEAD_NOTE:
63         header_add_to_note(type, ctxt, str); break;
64       case ELT_SUB_SOUR:
65         citation_add_to_desc(type, ctxt, str); break;
66       case ELT_SUB_SOUR_TEXT:
67         citation_add_to_text(type, ctxt, str); break;
68       case ELT_SUB_NOTE:
69         note_sub_add_to_note(type, ctxt, str); break;
70       case REC_NOTE:
71         note_add_to_note(type, ctxt, str); break;
72       case ELT_SOUR_AUTH:
73       case ELT_SOUR_TITL:
74       case ELT_SOUR_PUBL:
75       case ELT_SOUR_TEXT:
76         source_add_to_value(type, ctxt, str); break;
77       default:
78         UNEXPECTED_CONTEXT(ctxt->ctxt_type);
79     }
80     return (Gedcom_ctxt) make_gom_ctxt(elt, ctxt->obj_type, ctxt->ctxt_ptr);
81   }
82   else {
83     return (Gedcom_ctxt) MAKE_GOM_CTXT(elt, NULL, NULL);
84   }
85 }
86
87 void note_subscribe()
88 {
89   gedcom_subscribe_to_record(REC_NOTE, note_start, def_rec_end);
90   gedcom_subscribe_to_element(ELT_SUB_CONT, sub_cont_conc_start, def_elt_end);
91   gedcom_subscribe_to_element(ELT_SUB_CONC, sub_cont_conc_start, def_elt_end);
92 }
93
94 void note_add_to_note(NL_TYPE type, Gom_ctxt ctxt, char* str)
95 {
96   struct note *note = SAFE_CTXT_CAST(note, ctxt);
97   note->text = concat_strings (type, note->text, str);
98 }
99
100 void note_add_citation(Gom_ctxt ctxt, struct source_citation* cit)
101 {
102   struct note *note = SAFE_CTXT_CAST(note, ctxt);
103   LINK_CHAIN_ELT(source_citation, note->citation, cit)    
104 }
105
106 void note_add_user_ref(Gom_ctxt ctxt, struct user_ref_number* ref)
107 {
108   struct note *note = SAFE_CTXT_CAST(note, ctxt);
109   LINK_CHAIN_ELT(user_ref_number, note->ref, ref)
110 }
111
112 void note_set_record_id(Gom_ctxt ctxt, char *rin)
113 {
114   struct note *note = SAFE_CTXT_CAST(note, ctxt);
115   note->record_id = strdup(rin);
116 }
117
118 void note_set_change_date(Gom_ctxt ctxt, struct change_date* chan)
119 {
120   struct note *note = SAFE_CTXT_CAST(note, ctxt);
121   note->change_date = chan;
122 }
123
124 void note_add_user_data(Gom_ctxt ctxt, struct user_data* data)
125 {
126   struct note *obj = SAFE_CTXT_CAST(note, ctxt);
127   LINK_CHAIN_ELT(user_data, obj->extra, data)
128 }
129
130 void note_cleanup(struct note* note)
131 {
132   SAFE_FREE(note->xrefstr);
133   SAFE_FREE(note->text);
134   DESTROY_CHAIN_ELTS(source_citation, note->citation, citation_cleanup)
135   DESTROY_CHAIN_ELTS(user_ref_number, note->ref, user_ref_cleanup)
136   SAFE_FREE(note->record_id);
137   change_date_cleanup(note->change_date);
138   DESTROY_CHAIN_ELTS(user_data, note->extra, user_data_cleanup)
139 }
140
141 void notes_cleanup()
142 {
143   DESTROY_CHAIN_ELTS(note, gom_first_note, note_cleanup);
144 }
145
146 struct note* gom_get_first_note()
147 {
148   return gom_first_note;
149 }
150
151 struct note* make_note_record(char* xrefstr)
152 {
153   struct note* note;
154   MAKE_CHAIN_ELT(note, gom_first_note, note);
155   note->xrefstr = strdup(xrefstr);
156   return note;
157 }