Completed writing of strings and xrefs.
[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 = (struct place *)malloc(sizeof(struct place));
45     if (! place)
46       MEMORY_ERROR;
47     else {
48       memset (place, 0, sizeof(struct place));
49       place->value = strdup(GEDCOM_STRING(parsed_value));
50       
51       if (!place->value) {
52         MEMORY_ERROR;
53         free(place);
54       }
55       else {
56         switch (ctxt->ctxt_type) {
57           case ELT_SUB_FAM_EVT:
58           case ELT_SUB_FAM_EVT_EVEN:
59           case ELT_SUB_INDIV_ATTR:
60           case ELT_SUB_INDIV_RESI:
61           case ELT_SUB_INDIV_BIRT:
62           case ELT_SUB_INDIV_GEN:
63           case ELT_SUB_INDIV_ADOP:
64           case ELT_SUB_INDIV_EVEN:
65             event_add_place(ctxt, place); break;
66           default:
67             UNEXPECTED_CONTEXT(ctxt->ctxt_type);
68         }
69         result = MAKE_GOM_CTXT(elt, place, place);
70       }
71     }
72   }
73
74   return (Gedcom_ctxt)result;
75 }
76
77 STRING_CB(place, sub_place_form_start, place_hierarchy)
78
79 void place_add_citation(Gom_ctxt ctxt, struct source_citation* cit)
80 {
81   struct place *place = SAFE_CTXT_CAST(place, ctxt);
82   if (place)
83     LINK_CHAIN_ELT(source_citation, place->citation, cit);  
84 }
85
86 void place_add_note(Gom_ctxt ctxt, struct note_sub* note)
87 {
88   struct place *place = SAFE_CTXT_CAST(place, ctxt);
89   if (place)
90     LINK_CHAIN_ELT(note_sub, place->note, note);  
91 }
92
93 void place_add_user_data(Gom_ctxt ctxt, struct user_data* data)
94 {
95   struct place *obj = SAFE_CTXT_CAST(place, ctxt);
96   if (obj)
97     LINK_CHAIN_ELT(user_data, obj->extra, data);
98 }
99
100 void place_subscribe()
101 {
102   gedcom_subscribe_to_element(ELT_SUB_PLAC, sub_place_start, def_elt_end);
103   gedcom_subscribe_to_element(ELT_SUB_PLAC_FORM,
104                               sub_place_form_start, def_elt_end);
105 }
106
107 void place_cleanup(struct place* place)
108 {
109   if (place) {
110     SAFE_FREE(place->value);
111     SAFE_FREE(place->place_hierarchy);
112     DESTROY_CHAIN_ELTS(source_citation, place->citation, citation_cleanup);  
113     DESTROY_CHAIN_ELTS(note_sub, place->note, note_sub_cleanup);
114     DESTROY_CHAIN_ELTS(user_data, place->extra, user_data_cleanup);
115   }
116   SAFE_FREE(place);
117 }
118
119 int write_place(Gedcom_write_hndl hndl, int parent, struct place* place)
120 {
121   int result = 0;
122   
123   if (!place) return 1;
124   
125   result |= gedcom_write_element_str(hndl, ELT_SUB_PLAC, 0,
126                                      parent, place->value);
127   if (place->place_hierarchy)
128     result |= gedcom_write_element_str(hndl, ELT_SUB_PLAC_FORM, 0,
129                                        ELT_SUB_PLAC, place->place_hierarchy);
130   if (place->citation)
131     result |= write_citations(hndl, ELT_SUB_PLAC, place->citation);
132   if (place->note)
133     result |= write_note_subs(hndl, ELT_SUB_PLAC, place->note);
134   if (place->extra)
135     result |= write_user_data(hndl, place->extra);
136
137   return result;
138 }