First check-in of gedcom object model.
[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   struct place *place = NULL;
40
41   if (ctxt) {
42     place = (struct place *)malloc(sizeof(struct place));
43     memset (place, 0, sizeof(struct place));
44     place->value = strdup(GEDCOM_STRING(parsed_value));
45
46     switch (ctxt->ctxt_type) {
47       case ELT_SUB_FAM_EVT:
48       case ELT_SUB_FAM_EVT_EVEN:
49       case ELT_SUB_INDIV_ATTR:
50       case ELT_SUB_INDIV_RESI:
51       case ELT_SUB_INDIV_BIRT:
52       case ELT_SUB_INDIV_GEN:
53       case ELT_SUB_INDIV_ADOP:
54       case ELT_SUB_INDIV_EVEN:
55         event_add_place(ctxt, place); break;
56       default:
57         UNEXPECTED_CONTEXT(ctxt->ctxt_type);
58     }
59   }
60
61   return (Gedcom_ctxt) MAKE_GOM_CTXT(elt, place, place);
62 }
63
64 STRING_CB(place, sub_place_form_start, place_hierarchy)
65
66 void place_add_citation(Gom_ctxt ctxt, struct source_citation* cit)
67 {
68   struct place *place = SAFE_CTXT_CAST(place, ctxt);
69   LINK_CHAIN_ELT(source_citation, place->citation, cit)  
70 }
71
72 void place_add_note(Gom_ctxt ctxt, struct note_sub* note)
73 {
74   struct place *place = SAFE_CTXT_CAST(place, ctxt);
75   LINK_CHAIN_ELT(note_sub, place->note, note)  
76 }
77
78 void place_add_user_data(Gom_ctxt ctxt, struct user_data* data)
79 {
80   struct place *obj = SAFE_CTXT_CAST(place, ctxt);
81   LINK_CHAIN_ELT(user_data, obj->extra, data)
82 }
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 place_cleanup(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, citation_cleanup)  
97     DESTROY_CHAIN_ELTS(note_sub, place->note, note_sub_cleanup)
98     DESTROY_CHAIN_ELTS(user_data, place->extra, user_data_cleanup)
99   }
100   SAFE_FREE(place);
101 }