6c0332a64a89727d4f1fa3ab61a2a9541c3703c3
[gedcom-parse.git] / gom / address.c
1 /* Address 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 "gom_internal.h"
27 #include "header.h"
28 #include "event.h"
29 #include "address.h"
30 #include "repository.h"
31 #include "submitter.h"
32 #include "user_rec.h"
33 #include "gom.h"
34 #include "gedcom.h"
35
36 Gedcom_ctxt sub_addr_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 address *addr = (struct address *)malloc(sizeof(struct address));
45     if (!addr)
46       MEMORY_ERROR;
47     else {
48       memset (addr, 0, sizeof(struct address));
49       switch (ctxt->ctxt_type) {
50         case ELT_HEAD_SOUR_CORP:
51           header_add_address(ctxt, addr); break;
52         case ELT_SUB_FAM_EVT:
53         case ELT_SUB_FAM_EVT_EVEN:
54         case ELT_SUB_INDIV_ATTR:
55         case ELT_SUB_INDIV_RESI:
56         case ELT_SUB_INDIV_BIRT:
57         case ELT_SUB_INDIV_GEN:
58         case ELT_SUB_INDIV_ADOP:
59         case ELT_SUB_INDIV_EVEN:
60           event_add_address(ctxt, addr); break;
61         case REC_REPO:
62           repository_add_address(ctxt, addr); break;
63         case REC_SUBM:
64           submitter_add_address(ctxt, addr); break;
65         default:
66           UNEXPECTED_CONTEXT(ctxt->ctxt_type);
67       }
68       result = MAKE_GOM_CTXT(elt, address, addr);
69     }
70   }
71   
72   return (Gedcom_ctxt)result;
73 }
74
75 Gedcom_ctxt sub_addr_cont_start(_ELT_PARAMS_)
76 {
77   Gom_ctxt ctxt = (Gom_ctxt)parent;
78   Gom_ctxt result = NULL;
79   if (! ctxt)
80     NO_CONTEXT;
81   else {
82     result = make_gom_ctxt(elt, ctxt->ctxt_type, ctxt->ctxt_ptr);
83   }
84   return (Gedcom_ctxt)result;
85 }
86
87 STRING_END_CB(address, sub_addr_end, full_label)
88 STRING_CB(address, sub_addr_adr1_start, line1)
89 STRING_CB(address, sub_addr_adr2_start, line2)
90 STRING_CB(address, sub_addr_city_start, city)
91 STRING_CB(address, sub_addr_stae_start, state)
92 STRING_CB(address, sub_addr_post_start, postal)
93 STRING_CB(address, sub_addr_ctry_start, country)
94
95 Gedcom_ctxt sub_phon_start(_ELT_PARAMS_)
96 {
97   Gom_ctxt ctxt = (Gom_ctxt)parent;
98   Gom_ctxt result = NULL;
99
100   if (! ctxt)
101     NO_CONTEXT;
102   else {
103     char *str = GEDCOM_STRING(parsed_value);
104     switch (ctxt->ctxt_type) {
105       case ELT_HEAD_SOUR_CORP:
106         header_add_phone(ctxt, str); break;
107       case ELT_SUB_FAM_EVT:
108       case ELT_SUB_INDIV_ATTR:
109       case ELT_SUB_INDIV_RESI:
110       case ELT_SUB_INDIV_BIRT:
111       case ELT_SUB_INDIV_GEN:
112       case ELT_SUB_INDIV_ADOP:
113       case ELT_SUB_INDIV_EVEN:
114         event_add_phone(ctxt, str); break;
115       case REC_REPO:
116         repository_add_phone(ctxt, str); break;
117       case REC_SUBM:
118         submitter_add_phone(ctxt, str); break;
119       default:
120         UNEXPECTED_CONTEXT(ctxt->ctxt_type);
121     }
122     result = make_gom_ctxt(elt, ctxt->obj_type, ctxt->ctxt_ptr);
123   }
124   return (Gedcom_ctxt)result;
125 }
126
127 void address_subscribe()
128 {
129   gedcom_subscribe_to_element(ELT_SUB_ADDR, sub_addr_start, sub_addr_end);
130   gedcom_subscribe_to_element(ELT_SUB_ADDR_CONT,
131                               sub_addr_cont_start, def_elt_end);
132   gedcom_subscribe_to_element(ELT_SUB_ADDR_ADR1,
133                               sub_addr_adr1_start, def_elt_end);
134   gedcom_subscribe_to_element(ELT_SUB_ADDR_ADR2,
135                               sub_addr_adr2_start, def_elt_end);
136   gedcom_subscribe_to_element(ELT_SUB_ADDR_CITY,
137                               sub_addr_city_start, def_elt_end);
138   gedcom_subscribe_to_element(ELT_SUB_ADDR_STAE,
139                               sub_addr_stae_start, def_elt_end);
140   gedcom_subscribe_to_element(ELT_SUB_ADDR_POST,
141                               sub_addr_post_start, def_elt_end);
142   gedcom_subscribe_to_element(ELT_SUB_ADDR_CTRY,
143                               sub_addr_ctry_start, def_elt_end);
144   gedcom_subscribe_to_element(ELT_SUB_PHON, sub_phon_start, def_elt_end);
145 }
146
147 void address_add_user_data(Gom_ctxt ctxt, struct user_data* data)
148 {
149   struct address *obj = SAFE_CTXT_CAST(address, ctxt);
150   if (obj)
151     LINK_CHAIN_ELT(user_data, obj->extra, data);
152 }
153
154 void address_cleanup(struct address *address)
155 {
156   if (address) {
157     SAFE_FREE(address->full_label);
158     SAFE_FREE(address->line1);
159     SAFE_FREE(address->line2);
160     SAFE_FREE(address->city);
161     SAFE_FREE(address->state);
162     SAFE_FREE(address->postal);
163     SAFE_FREE(address->country);
164     DESTROY_CHAIN_ELTS(user_data, address->extra, user_data_cleanup);
165   }
166   SAFE_FREE(address);
167 }
168
169 int write_address(Gedcom_write_hndl hndl, int parent, struct address *address)
170 {
171   int result = 0;
172
173   if (!address) return 1;
174   
175   if (address->full_label)
176     result |= gedcom_write_element_str(hndl, ELT_SUB_ADDR, 0, parent,
177                                        address->full_label);
178   if (address->line1)
179     result |= gedcom_write_element_str(hndl, ELT_SUB_ADDR_ADR1, 0,
180                                        ELT_SUB_ADDR, address->line1);
181   if (address->line2)
182     result |= gedcom_write_element_str(hndl, ELT_SUB_ADDR_ADR2, 0,
183                                        ELT_SUB_ADDR, address->line2);
184   if (address->city)
185     result |= gedcom_write_element_str(hndl, ELT_SUB_ADDR_CITY, 0,
186                                        ELT_SUB_ADDR, address->city);
187   if (address->state)
188     result |= gedcom_write_element_str(hndl, ELT_SUB_ADDR_STAE, 0,
189                                        ELT_SUB_ADDR, address->state);
190   if (address->postal)
191     result |= gedcom_write_element_str(hndl, ELT_SUB_ADDR_POST, 0,
192                                        ELT_SUB_ADDR, address->postal);
193   if (address->country)
194     result |= gedcom_write_element_str(hndl, ELT_SUB_ADDR_CTRY, 0,
195                                        ELT_SUB_ADDR, address->country);
196   if (address->extra)
197     result |= write_user_data(hndl, address->extra);
198
199   return result;
200 }