Only try to delete address if present.
[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_SETFUNC(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 UNREFALLFUNC(place)(struct place* obj)
92 {
93   if (obj) {
94     UNREFALLFUNC(source_citation)(obj->citation);
95     UNREFALLFUNC(note_sub)(obj->note);
96     UNREFALLFUNC(user_data)(obj->extra);
97   }
98 }
99
100 void CLEANFUNC(place)(struct place* place)
101 {
102   if (place) {
103     SAFE_FREE(place->value);
104     SAFE_FREE(place->place_hierarchy);
105     DESTROY_CHAIN_ELTS(source_citation, place->citation);  
106     DESTROY_CHAIN_ELTS(note_sub, place->note);
107     DESTROY_CHAIN_ELTS(user_data, place->extra);
108   }
109   SAFE_FREE(place);
110 }
111
112 int write_place(Gedcom_write_hndl hndl, int parent, struct place* place)
113 {
114   int result = 0;
115   
116   if (!place) return 1;
117   
118   result |= gedcom_write_element_str(hndl, ELT_SUB_PLAC, 0,
119                                      parent, place->value);
120   if (place->place_hierarchy)
121     result |= gedcom_write_element_str(hndl, ELT_SUB_PLAC_FORM, 0,
122                                        ELT_SUB_PLAC, place->place_hierarchy);
123   if (place->citation)
124     result |= write_citations(hndl, ELT_SUB_PLAC, place->citation);
125   if (place->note)
126     result |= write_note_subs(hndl, ELT_SUB_PLAC, place->note);
127   if (place->extra)
128     result |= write_user_data(hndl, place->extra);
129
130   return result;
131 }