Definition of SUB_MAKEFUNC, SUB_ADDFUNC and SUB_DELETEFUNC.
[gedcom-parse.git] / gom / family_link.c
1 /* Family link 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 "family_link.h"
27 #include "individual.h"
28 #include "note_sub.h"
29 #include "user_rec.h"
30 #include "gom.h"
31 #include "gedcom.h"
32 #include "gom_internal.h"
33
34 Gedcom_ctxt sub_fam_link_start(_ELT_PARAMS_)
35 {
36   Gom_ctxt ctxt = (Gom_ctxt)parent;
37   Gom_ctxt result = NULL;
38
39   if (! ctxt)
40     NO_CONTEXT;
41   else {
42     struct family_link *link
43       = (struct family_link *)malloc(sizeof(struct family_link));
44     if (! link)
45       MEMORY_ERROR;
46     else {
47       memset (link, 0, sizeof(struct family_link));
48       link->family = GEDCOM_XREF_PTR(parsed_value);
49       
50       switch (ctxt->ctxt_type) {
51         case REC_INDI:
52           ADDFUNC2(individual,family_link)(ctxt, elt, link); break;
53         default:
54           UNEXPECTED_CONTEXT(ctxt->ctxt_type);
55       }
56       result = MAKE_GOM_CTXT(elt, family_link, link);
57     }
58   }
59
60   return (Gedcom_ctxt)result;
61 }
62
63 Gedcom_ctxt sub_fam_link_pedi_start(_ELT_PARAMS_)
64 {
65   Gom_ctxt ctxt = (Gom_ctxt)parent;
66   Gom_ctxt result = NULL;
67
68   if (! ctxt)
69     NO_CONTEXT;
70   else {
71     struct family_link *link = SAFE_CTXT_CAST(family_link, ctxt);
72     if (link) {
73       int err = 0;
74       struct pedigree *ped = NULL;
75       MAKE_CHAIN_ELT(pedigree, link->pedigree, ped);
76       if (ped) {
77         ped->pedigree = strdup(GEDCOM_STRING(parsed_value));
78         if (! ped->pedigree) {
79           MEMORY_ERROR;
80           err = 1;
81         }
82       }
83       if (! err)
84         result = MAKE_GOM_CTXT(elt, pedigree, ped);
85     }
86   }
87   return (Gedcom_ctxt)result;
88 }
89
90 DEFINE_ADDFUNC2(family_link, note_sub, note)
91 DEFINE_ADDFUNC2(family_link, user_data, extra)
92
93 void family_link_subscribe()
94 {
95   gedcom_subscribe_to_element(ELT_SUB_FAMC, sub_fam_link_start,
96                               def_elt_end);
97   gedcom_subscribe_to_element(ELT_SUB_FAMS, sub_fam_link_start,
98                               def_elt_end);
99   gedcom_subscribe_to_element(ELT_SUB_FAMC_PEDI, sub_fam_link_pedi_start,
100                               def_elt_end);
101 }
102
103 void CLEANFUNC(pedigree)(struct pedigree* ped)
104 {
105   if (ped) {
106     SAFE_FREE(ped->pedigree);
107   }
108 }
109
110 void CLEANFUNC(family_link)(struct family_link *link)
111 {
112   if (link) {
113     DESTROY_CHAIN_ELTS(pedigree, link->pedigree);
114     DESTROY_CHAIN_ELTS(note_sub, link->note);
115     DESTROY_CHAIN_ELTS(user_data, link->extra);
116   }
117 }
118
119 int write_pedigrees(Gedcom_write_hndl hndl, int parent, struct pedigree* ped)
120 {
121   int result = 0;
122   struct pedigree* obj;
123
124   if (!ped) return 1;
125
126   for (obj = ped; obj; obj = obj->next) {
127     result |= gedcom_write_element_str(hndl, ELT_SUB_FAMC_PEDI, 0, parent,
128                                        obj->pedigree);
129     if (obj->extra)
130       result |= write_user_data(hndl, obj->extra);  
131   }
132   
133   return result;
134 }
135
136 int write_family_links(Gedcom_write_hndl hndl, int parent, LinkType type,
137                        struct family_link *link)
138 {
139   int result = 0;
140   struct family_link* obj;
141   int elt = (type == LINK_TYPE_CHILD ? ELT_SUB_FAMC : ELT_SUB_FAMS);
142
143   if (!link) return 1;
144
145   for (obj = link; obj; obj = obj->next) {
146     result |= gedcom_write_element_xref(hndl, elt, 0, parent,
147                                         obj->family);
148     if (obj->pedigree)
149       result |= write_pedigrees(hndl, elt, obj->pedigree);
150     if (obj->note)
151       result |= write_note_subs(hndl, elt, obj->note);
152     if (obj->extra)
153       result |= write_user_data(hndl, obj->extra);      
154   }
155
156   return result;
157 }