Improved context handling, to allow elements out of context.
[gedcom-parse.git] / gom / association.c
1 /* Association 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 "association.h"
27 #include "individual.h"
28 #include "note_sub.h"
29 #include "source_citation.h"
30 #include "user_rec.h"
31 #include "gom.h"
32 #include "gedcom.h"
33 #include "gom_internal.h"
34
35 Gedcom_ctxt sub_assoc_start(_ELT_PARAMS_)
36 {
37   Gom_ctxt ctxt = (Gom_ctxt)parent;
38   Gom_ctxt result = NULL;
39
40   if (!ctxt)
41     NO_CONTEXT;
42   else {
43     struct association *assoc = SUB_MAKEFUNC(association)();
44     if (assoc) {
45       int type = ctxt_type(ctxt);
46       assoc->to = GEDCOM_XREF_PTR(parsed_value);
47
48       switch (type) {
49         case REC_INDI:
50           ADDFUNC2(individual,association)(ctxt, assoc);
51         default:
52           UNEXPECTED_CONTEXT(type);
53       }
54       result = MAKE_GOM_CTXT(elt, association, assoc);
55     }
56   }
57
58   return (Gedcom_ctxt)result;
59 }
60
61 DEFINE_SUB_MAKEFUNC(association)
62 DEFINE_SUB_ADDFUNC(association)
63 DEFINE_SUB_FINDFUNC(association)
64 DEFINE_SUB_REMOVEFUNC(association)
65 DEFINE_SUB_MOVEFUNC(association)
66      
67 DEFINE_STRING_CB(association, sub_assoc_rela_start, relation)
68
69 DEFINE_ADDFUNC2(association, note_sub, note)
70 DEFINE_ADDFUNC2(association, source_citation, citation)
71 DEFINE_ADDFUNC2(association, user_data, extra)
72      
73 Gedcom_ctxt sub_assoc_type_start(_ELT_PARAMS_)
74 {
75   Gom_ctxt ctxt = (Gom_ctxt)parent;
76   Gom_ctxt result = NULL;
77   
78   if (! ctxt)
79     NO_CONTEXT;
80   else {
81     struct association *obj = SAFE_CTXT_CAST(association, ctxt);
82     char *str = GEDCOM_STRING(parsed_value);
83     if (obj) {
84       obj->type = strdup(str);
85       if (! obj->type)
86         MEMORY_ERROR;
87       else {
88         set_xref_type(obj->to, str);
89         result = MAKE_GOM_CTXT(elt, association, obj);
90       }
91     }
92   }
93   return (Gedcom_ctxt)result;
94 }
95
96 void association_subscribe()
97 {
98   gedcom_subscribe_to_element(ELT_SUB_ASSO, sub_assoc_start, def_elt_end);
99   gedcom_subscribe_to_element(ELT_SUB_ASSO_TYPE, sub_assoc_type_start,
100                               def_elt_end);
101   gedcom_subscribe_to_element(ELT_SUB_ASSO_RELA, sub_assoc_rela_start,
102                               def_elt_end);
103 }
104
105 void UNREFALLFUNC(association)(struct association *obj)
106 {
107   if (obj) {
108     struct association* runner;
109     for (runner = obj; runner; runner = runner->next) {
110       unref_xref_value(runner->to);
111       UNREFALLFUNC(source_citation)(runner->citation);
112       UNREFALLFUNC(note_sub)(runner->note);
113       UNREFALLFUNC(user_data)(runner->extra);
114     }
115   }
116 }
117
118 void CLEANFUNC(association)(struct association* assoc)
119 {
120   if (assoc) {
121     SAFE_FREE(assoc->type);
122     SAFE_FREE(assoc->relation);
123     DESTROY_CHAIN_ELTS(note_sub, assoc->note);
124     DESTROY_CHAIN_ELTS(source_citation, assoc->citation);
125     DESTROY_CHAIN_ELTS(user_data, assoc->extra);
126   }
127 }
128
129 int write_associations(Gedcom_write_hndl hndl, int parent,
130                        struct association *assoc)
131 {
132   int result = 0;
133   struct association* obj;
134
135   if (!assoc) return 1;
136
137   for (obj = assoc; obj; obj = obj->next) {
138     result |= gedcom_write_element_xref(hndl, ELT_SUB_ASSO, 0, parent,
139                                         obj->to);
140     if (obj->type)
141       result |= gedcom_write_element_str(hndl, ELT_SUB_ASSO_TYPE, 0, parent,
142                                          obj->type);
143     if (obj->relation)
144       result |= gedcom_write_element_str(hndl, ELT_SUB_ASSO_RELA, 0, parent,
145                                          obj->relation);
146     if (obj->citation)
147       result |= write_citations(hndl, ELT_SUB_ASSO, obj->citation);
148     if (obj->note)
149       result |= write_note_subs(hndl, ELT_SUB_ASSO, obj->note);
150     if (obj->extra)
151       result |= write_user_data(hndl, obj->extra);
152   }
153
154   return result;
155 }