Use of libutf8
[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 = (struct note_sub *)malloc(sizeof(struct note_sub));
56     if (! note)
57       MEMORY_ERROR;
58     else {
59       int err = 0;
60       memset (note, 0, sizeof(struct note_sub));
61       if (GEDCOM_IS_STRING(parsed_value)) {
62         note->text = strdup(GEDCOM_STRING(parsed_value));
63         if (! note->text) {
64           MEMORY_ERROR;
65           free(note);
66           err = 1;
67         }
68       }
69       else if (GEDCOM_IS_XREF_PTR(parsed_value))
70         note->reference = GEDCOM_XREF_PTR(parsed_value);
71
72       if (! err) {
73         switch (ctxt->ctxt_type) {
74           case ELT_SUB_PLAC:
75             place_add_note(ctxt, note); break;
76           case ELT_SUB_FAM_EVT:
77           case ELT_SUB_FAM_EVT_EVEN:
78           case ELT_SUB_INDIV_ATTR:
79           case ELT_SUB_INDIV_RESI:
80           case ELT_SUB_INDIV_BIRT:
81           case ELT_SUB_INDIV_GEN:
82           case ELT_SUB_INDIV_ADOP:
83           case ELT_SUB_INDIV_EVEN:
84             event_add_note(ctxt, note); break;
85           case ELT_SUB_SOUR:
86             citation_add_note(ctxt, note); break;
87           case ELT_SUB_MULTIM_OBJE:
88             multimedia_link_add_note(ctxt, note); break;
89           case ELT_SUB_LSS_SLGS:
90           case ELT_SUB_LIO_BAPL:
91           case ELT_SUB_LIO_SLGC:
92             lds_event_add_note(ctxt, note); break;
93           case REC_FAM:
94             family_add_note(ctxt, note); break;
95           case ELT_SUB_CHAN:
96             change_date_add_note(ctxt, note); break;
97           case ELT_SUB_PERS_NAME:
98             name_add_note(ctxt, note); break;
99           case ELT_SUB_FAMC: 
100           case ELT_SUB_FAMS:
101             family_link_add_note(ctxt, note); break;
102           case ELT_SUB_ASSO:
103             association_add_note(ctxt, note); break;
104           case REC_INDI:
105             individual_add_note(ctxt, note); break;
106           case REC_OBJE:
107             multimedia_add_note(ctxt, note); break;
108           case REC_REPO:
109             repository_add_note(ctxt, note); break;
110           case ELT_SOUR_DATA:
111             source_add_note_to_data(ctxt, note); break;
112           case ELT_SUB_REPO:
113             source_add_note_to_repo(ctxt, note); break;
114           case REC_SOUR:
115             source_add_note(ctxt, note); break;
116           default:
117             UNEXPECTED_CONTEXT(ctxt->ctxt_type);
118         }
119         result = MAKE_GOM_CTXT(elt, note_sub, note);
120       }
121     }
122   }
123
124   return (Gedcom_ctxt)result;
125 }
126
127 void note_sub_subscribe()
128 {
129   gedcom_subscribe_to_element(ELT_SUB_NOTE, sub_note_start, def_elt_end);
130 }
131
132 void note_sub_add_citation(Gom_ctxt ctxt, struct source_citation* cit)
133 {
134   struct note_sub *note = SAFE_CTXT_CAST(note_sub, ctxt);
135   if (note)
136     LINK_CHAIN_ELT(source_citation, note->citation, cit);    
137 }
138
139 void note_sub_add_to_note(NL_TYPE type, Gom_ctxt ctxt, const char* str)
140 {
141   struct note_sub *note = SAFE_CTXT_CAST(note_sub, ctxt);
142   if (note) {
143     char *newvalue = concat_strings (type, note->text, str);
144     if (newvalue)
145       note->text = newvalue;
146     else
147       MEMORY_ERROR;
148   }
149 }
150
151 void note_sub_add_user_data(Gom_ctxt ctxt, struct user_data* data)
152 {
153   struct note_sub *obj = SAFE_CTXT_CAST(note_sub, ctxt);
154   if (obj)
155     LINK_CHAIN_ELT(user_data, obj->extra, data);;
156 }
157
158 void note_sub_cleanup(struct note_sub* note)
159 {
160   if (note) {
161     SAFE_FREE(note->text);
162     DESTROY_CHAIN_ELTS(source_citation, note->citation, citation_cleanup);
163     DESTROY_CHAIN_ELTS(user_data, note->extra, user_data_cleanup);
164   }
165 }