Definition of SUB_MAKEFUNC, SUB_ADDFUNC and SUB_DELETEFUNC.
[gedcom-parse.git] / gom / place.c
1 /* Place 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 "place.h"
27 #include "address.h"
28 #include "event.h"
29 #include "source_citation.h"
30 #include "note_sub.h"
31 #include "user_rec.h"
32 #include "gom.h"
33 #include "gedcom.h"
34 #include "gom_internal.h"
35
36 Gedcom_ctxt sub_place_start(_ELT_PARAMS_)
37 {
38   Gom_ctxt ctxt = (Gom_ctxt)parent;
39   Gom_ctxt result = NULL;
40
41   if (! ctxt)
42     NO_CONTEXT;
43   else {
44     struct place *place = SUB_MAKEFUNC(place)();
45     if (place) {
46       place->value = strdup(GEDCOM_STRING(parsed_value));
47       
48       if (!place->value) {
49         MEMORY_ERROR;
50         free(place);
51       }
52       else {
53         switch (ctxt->ctxt_type) {
54           case ELT_SUB_FAM_EVT:
55           case ELT_SUB_FAM_EVT_EVEN:
56           case ELT_SUB_INDIV_ATTR:
57           case ELT_SUB_INDIV_RESI:
58           case ELT_SUB_INDIV_BIRT:
59           case ELT_SUB_INDIV_GEN:
60           case ELT_SUB_INDIV_ADOP:
61           case ELT_SUB_INDIV_EVEN:
62             ADDFUNC2_NOLIST(event,place)(ctxt, place); break;
63           default:
64             UNEXPECTED_CONTEXT(ctxt->ctxt_type);
65         }
66         result = MAKE_GOM_CTXT(elt, place, place);
67       }
68     }
69   }
70
71   return (Gedcom_ctxt)result;
72 }
73
74 DEFINE_SUB_MAKEFUNC(place)
75 DEFINE_SUB_ADDFUNC(place)
76 DEFINE_SUB_DELETEFUNC(place)
77      
78 DEFINE_STRING_CB(place, sub_place_form_start, place_hierarchy)
79
80 DEFINE_ADDFUNC2(place, source_citation, citation)
81 DEFINE_ADDFUNC2(place, note_sub, note)
82 DEFINE_ADDFUNC2(place, user_data, extra)
83
84 void place_subscribe()
85 {
86   gedcom_subscribe_to_element(ELT_SUB_PLAC, sub_place_start, def_elt_end);
87   gedcom_subscribe_to_element(ELT_SUB_PLAC_FORM,
88                               sub_place_form_start, def_elt_end);
89 }
90
91 void CLEANFUNC(place)(struct place* place)
92 {
93   if (place) {
94     SAFE_FREE(place->value);
95     SAFE_FREE(place->place_hierarchy);
96     DESTROY_CHAIN_ELTS(source_citation, place->citation);  
97     DESTROY_CHAIN_ELTS(note_sub, place->note);
98     DESTROY_CHAIN_ELTS(user_data, place->extra);
99   }
100   SAFE_FREE(place);
101 }
102
103 int write_place(Gedcom_write_hndl hndl, int parent, struct place* place)
104 {
105   int result = 0;
106   
107   if (!place) return 1;
108   
109   result |= gedcom_write_element_str(hndl, ELT_SUB_PLAC, 0,
110                                      parent, place->value);
111   if (place->place_hierarchy)
112     result |= gedcom_write_element_str(hndl, ELT_SUB_PLAC_FORM, 0,
113                                        ELT_SUB_PLAC, place->place_hierarchy);
114   if (place->citation)
115     result |= write_citations(hndl, ELT_SUB_PLAC, place->citation);
116   if (place->note)
117     result |= write_note_subs(hndl, ELT_SUB_PLAC, place->note);
118   if (place->extra)
119     result |= write_user_data(hndl, place->extra);
120
121   return result;
122 }