Reworked the xref functions.
[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 CLEANFUNC(repository)(struct repository* repo)
62 {
63   if (repo) {
64     SAFE_FREE(repo->xrefstr);
65     SAFE_FREE(repo->name);
66     CLEANFUNC(address)(repo->address);
67     SAFE_FREE(repo->phone[0]);
68     SAFE_FREE(repo->phone[1]);
69     SAFE_FREE(repo->phone[2]);
70     DESTROY_CHAIN_ELTS(note_sub, repo->note);
71     DESTROY_CHAIN_ELTS(user_ref_number, repo->ref);
72     SAFE_FREE(repo->record_id);
73     CLEANFUNC(change_date)(repo->change_date);
74     DESTROY_CHAIN_ELTS(user_data, repo->extra);
75   }
76 }
77
78 void repositories_cleanup()
79 {
80   DESTROY_CHAIN_ELTS(repository, gom_first_repository);
81 }
82
83 struct repository* gom_get_first_repository()
84 {
85   return gom_first_repository;
86 }
87
88 int write_repositories(Gedcom_write_hndl hndl)
89 {
90   int result = 0;
91   int i;
92   struct repository* obj;
93
94   for (obj = gom_first_repository; obj; obj = obj->next) {
95     result |= gedcom_write_record_str(hndl, REC_REPO, obj->xrefstr, NULL);
96     if (obj->name)
97       result |= gedcom_write_element_str(hndl, ELT_REPO_NAME, 0,
98                                          REC_REPO, obj->name);
99     if (obj->address)
100       result |= write_address(hndl, REC_REPO, obj->address);
101     for (i = 0; i < 3 && obj->phone[i]; i++)
102       result |= gedcom_write_element_str(hndl, ELT_SUB_PHON, 0, REC_REPO,
103                                          obj->phone[i]);
104     if (obj->note)
105       result |= write_note_subs(hndl, REC_REPO, obj->note);
106     if (obj->ref)
107       result |= write_user_refs(hndl, REC_REPO, obj->ref);
108     if (obj->record_id)
109       result |= gedcom_write_element_str(hndl, ELT_SUB_IDENT_RIN, 0,
110                                          REC_REPO, obj->record_id);
111     if (obj->change_date)
112       result |= write_change_date(hndl, REC_REPO, obj->change_date);
113     if (obj->extra)
114       result |= write_user_data(hndl, obj->extra);
115   }
116   
117   return result;
118 }