Allow elements out of context in GOM.
[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         int type = ctxt_type(ctxt);
54         switch (type) {
55           case ELT_SUB_FAM_EVT:
56           case ELT_SUB_FAM_EVT_EVEN:
57           case ELT_SUB_INDIV_ATTR:
58           case ELT_SUB_INDIV_RESI:
59           case ELT_SUB_INDIV_BIRT:
60           case ELT_SUB_INDIV_GEN:
61           case ELT_SUB_INDIV_ADOP:
62           case ELT_SUB_INDIV_EVEN:
63             ADDFUNC2_NOLIST(event,place)(ctxt, place); break;
64           default:
65             UNEXPECTED_CONTEXT(type);
66         }
67         result = MAKE_GOM_CTXT(elt, place, place);
68       }
69     }
70   }
71
72   return (Gedcom_ctxt)result;
73 }
74
75 DEFINE_SUB_MAKEFUNC(place)
76 DEFINE_SUB_SETFUNC(place)
77 DEFINE_SUB_DELETEFUNC(place)
78      
79 DEFINE_STRING_CB(place, sub_place_form_start, place_hierarchy)
80
81 DEFINE_ADDFUNC2(place, source_citation, citation)
82 DEFINE_ADDFUNC2(place, note_sub, note)
83 DEFINE_ADDFUNC2(place, user_data, extra)
84
85 void place_subscribe()
86 {
87   gedcom_subscribe_to_element(ELT_SUB_PLAC, sub_place_start, def_elt_end);
88   gedcom_subscribe_to_element(ELT_SUB_PLAC_FORM,
89                               sub_place_form_start, def_elt_end);
90 }
91
92 void UNREFALLFUNC(place)(struct place* obj)
93 {
94   if (obj) {
95     UNREFALLFUNC(source_citation)(obj->citation);
96     UNREFALLFUNC(note_sub)(obj->note);
97     UNREFALLFUNC(user_data)(obj->extra);
98   }
99 }
100
101 void CLEANFUNC(place)(struct place* place)
102 {
103   if (place) {
104     SAFE_FREE(place->value);
105     SAFE_FREE(place->place_hierarchy);
106     DESTROY_CHAIN_ELTS(source_citation, place->citation);  
107     DESTROY_CHAIN_ELTS(note_sub, place->note);
108     DESTROY_CHAIN_ELTS(user_data, place->extra);
109   }
110   SAFE_FREE(place);
111 }
112
113 int write_place(Gedcom_write_hndl hndl, int parent, struct place* place)
114 {
115   int result = 0;
116   
117   if (!place) return 1;
118   
119   result |= gedcom_write_element_str(hndl, ELT_SUB_PLAC, 0,
120                                      parent, place->value);
121   if (place->place_hierarchy)
122     result |= gedcom_write_element_str(hndl, ELT_SUB_PLAC_FORM, 0,
123                                        ELT_SUB_PLAC, place->place_hierarchy);
124   if (place->citation)
125     result |= write_citations(hndl, ELT_SUB_PLAC, place->citation);
126   if (place->note)
127     result |= write_note_subs(hndl, ELT_SUB_PLAC, place->note);
128   if (place->extra)
129     result |= write_user_data(hndl, place->extra);
130
131   return result;
132 }