Only try to delete address if present.
[gedcom-parse.git] / gom / repository.c
1 /* Repository object 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 "repository.h"
27 #include "address.h"
28 #include "note_sub.h"
29 #include "user_ref.h"
30 #include "change_date.h"
31 #include "user_rec.h"
32 #include "gom.h"
33 #include "gedcom.h"
34 #include "gom_internal.h"
35
36 struct repository* gom_first_repository = NULL;
37
38 DEFINE_MAKEFUNC(repository, gom_first_repository)
39 DEFINE_DESTROYFUNC(repository, gom_first_repository)
40 DEFINE_ADDFUNC(repository, XREF_REPO)
41 DEFINE_DELETEFUNC(repository)
42 DEFINE_GETXREFFUNC(repository, XREF_REPO)
43      
44 DEFINE_REC_CB(repository, repo_start)
45 DEFINE_STRING_CB(repository, repo_name_start, name)
46
47 DEFINE_ADDFUNC2(repository, note_sub, note)
48 DEFINE_ADDFUNC2(repository, user_ref_number, ref)
49 DEFINE_ADDFUNC2(repository, user_data, extra)
50 DEFINE_ADDFUNC2_NOLIST(repository, address, address)
51 DEFINE_ADDFUNC2_NOLIST(repository, change_date, change_date)
52 DEFINE_ADDFUNC2_STRN(repository, phone, 3)
53 DEFINE_ADDFUNC2_STR(repository, record_id)
54
55 void repository_subscribe()
56 {
57   gedcom_subscribe_to_record(REC_REPO, repo_start, def_rec_end);
58   gedcom_subscribe_to_element(ELT_REPO_NAME, repo_name_start, def_elt_end);
59 }
60
61 void UNREFALLFUNC(repository)(struct repository *obj)
62 {
63   if (obj) {
64     UNREFALLFUNC(address)(obj->address);
65     UNREFALLFUNC(note_sub)(obj->note);
66     UNREFALLFUNC(user_ref_number)(obj->ref);
67     UNREFALLFUNC(change_date)(obj->change_date);
68     UNREFALLFUNC(user_data)(obj->extra);
69   }
70 }
71
72 void CLEANFUNC(repository)(struct repository* repo)
73 {
74   if (repo) {
75     SAFE_FREE(repo->xrefstr);
76     SAFE_FREE(repo->name);
77     CLEANFUNC(address)(repo->address);
78     SAFE_FREE(repo->phone[0]);
79     SAFE_FREE(repo->phone[1]);
80     SAFE_FREE(repo->phone[2]);
81     DESTROY_CHAIN_ELTS(note_sub, repo->note);
82     DESTROY_CHAIN_ELTS(user_ref_number, repo->ref);
83     SAFE_FREE(repo->record_id);
84     CLEANFUNC(change_date)(repo->change_date);
85     DESTROY_CHAIN_ELTS(user_data, repo->extra);
86   }
87 }
88
89 void repositories_cleanup()
90 {
91   DESTROY_CHAIN_ELTS(repository, gom_first_repository);
92 }
93
94 struct repository* gom_get_first_repository()
95 {
96   return gom_first_repository;
97 }
98
99 int write_repositories(Gedcom_write_hndl hndl)
100 {
101   int result = 0;
102   int i;
103   struct repository* obj;
104
105   for (obj = gom_first_repository; obj; obj = obj->next) {
106     result |= gedcom_write_record_str(hndl, REC_REPO, obj->xrefstr, NULL);
107     if (obj->name)
108       result |= gedcom_write_element_str(hndl, ELT_REPO_NAME, 0,
109                                          REC_REPO, obj->name);
110     if (obj->address)
111       result |= write_address(hndl, REC_REPO, obj->address);
112     for (i = 0; i < 3 && obj->phone[i]; i++)
113       result |= gedcom_write_element_str(hndl, ELT_SUB_PHON, 0, REC_REPO,
114                                          obj->phone[i]);
115     if (obj->note)
116       result |= write_note_subs(hndl, REC_REPO, obj->note);
117     if (obj->ref)
118       result |= write_user_refs(hndl, REC_REPO, obj->ref);
119     if (obj->record_id)
120       result |= gedcom_write_element_str(hndl, ELT_SUB_IDENT_RIN, 0,
121                                          REC_REPO, obj->record_id);
122     if (obj->change_date)
123       result |= write_change_date(hndl, REC_REPO, obj->change_date);
124     if (obj->extra)
125       result |= write_user_data(hndl, obj->extra);
126   }
127   
128   return result;
129 }