First check-in of gedcom object model.
[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   struct association *assoc = NULL;
39
40   if (ctxt) {
41     assoc = (struct association *)malloc(sizeof(struct association));
42     memset (assoc, 0, sizeof(struct association));
43     assoc->to = GEDCOM_XREF_PTR(parsed_value);
44
45     switch (ctxt->ctxt_type) {
46       case REC_INDI:
47         individual_add_association(ctxt, assoc);
48       default:
49         UNEXPECTED_CONTEXT(ctxt->ctxt_type);
50     }
51   }
52
53   return (Gedcom_ctxt) MAKE_GOM_CTXT(elt, association, assoc);
54 }
55
56 STRING_CB(association, sub_assoc_rela_start, relation)
57      
58 Gedcom_ctxt sub_assoc_type_start(_ELT_PARAMS_)
59 {
60   char *str = GEDCOM_STRING(parsed_value);
61   struct association *obj = SAFE_CTXT_CAST(association, (Gom_ctxt)parent);
62   if (obj)
63     obj->type = strdup(str);
64   set_xref_type(obj->to, str);
65   return (Gedcom_ctxt) MAKE_GOM_CTXT(elt, association, obj);
66 }
67
68 void association_subscribe()
69 {
70   gedcom_subscribe_to_element(ELT_SUB_ASSO, sub_assoc_start, def_elt_end);
71   gedcom_subscribe_to_element(ELT_SUB_ASSO_TYPE, sub_assoc_type_start,
72                               def_elt_end);
73   gedcom_subscribe_to_element(ELT_SUB_ASSO_RELA, sub_assoc_rela_start,
74                               def_elt_end);
75 }
76
77 void association_add_note(Gom_ctxt ctxt, struct note_sub* note)
78 {
79   struct association *assoc = SAFE_CTXT_CAST(association, ctxt);
80   LINK_CHAIN_ELT(note_sub, assoc->note, note)
81 }
82
83 void association_add_citation(Gom_ctxt ctxt, struct source_citation* cit)
84 {
85   struct association *assoc = SAFE_CTXT_CAST(association, ctxt);
86   LINK_CHAIN_ELT(source_citation, assoc->citation, cit)
87 }
88
89 void association_add_user_data(Gom_ctxt ctxt, struct user_data* data)
90 {
91   struct association *obj = SAFE_CTXT_CAST(association, ctxt);
92   LINK_CHAIN_ELT(user_data, obj->extra, data)
93 }
94
95 void association_cleanup(struct association* assoc)
96 {
97   if (assoc) {
98     SAFE_FREE(assoc->type);
99     SAFE_FREE(assoc->relation);
100     DESTROY_CHAIN_ELTS(note_sub, assoc->note, note_sub_cleanup)
101     DESTROY_CHAIN_ELTS(source_citation, assoc->citation, citation_cleanup)
102     DESTROY_CHAIN_ELTS(user_data, assoc->extra, user_data_cleanup)
103   }
104 }