2d070c676bab02d0080ed0cff2c14a17948fed74
[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 DEFINE_MAKELINKFUNC(repository, XREF_REPO)
44      
45 DEFINE_REC_CB(repository, repo_start)
46 DEFINE_STRING_CB(repository, repo_name_start, name)
47
48 DEFINE_ADDFUNC2(repository, note_sub, note)
49 DEFINE_ADDFUNC2(repository, user_ref_number, ref)
50 DEFINE_ADDFUNC2(repository, user_data, extra)
51 DEFINE_ADDFUNC2_NOLIST(repository, address, address)
52 DEFINE_ADDFUNC2_NOLIST(repository, change_date, change_date)
53 DEFINE_ADDFUNC2_STRN(repository, phone, 3)
54 DEFINE_ADDFUNC2_STR(repository, record_id)
55
56 void repository_subscribe()
57 {
58   gedcom_subscribe_to_record(REC_REPO, repo_start, def_rec_end);
59   gedcom_subscribe_to_element(ELT_REPO_NAME, repo_name_start, def_elt_end);
60 }
61
62 void CLEANFUNC(repository)(struct repository* repo)
63 {
64   if (repo) {
65     SAFE_FREE(repo->xrefstr);
66     SAFE_FREE(repo->name);
67     CLEANFUNC(address)(repo->address);
68     SAFE_FREE(repo->phone[0]);
69     SAFE_FREE(repo->phone[1]);
70     SAFE_FREE(repo->phone[2]);
71     DESTROY_CHAIN_ELTS(note_sub, repo->note);
72     DESTROY_CHAIN_ELTS(user_ref_number, repo->ref);
73     SAFE_FREE(repo->record_id);
74     CLEANFUNC(change_date)(repo->change_date);
75     DESTROY_CHAIN_ELTS(user_data, repo->extra);
76   }
77 }
78
79 void repositories_cleanup()
80 {
81   DESTROY_CHAIN_ELTS(repository, gom_first_repository);
82 }
83
84 struct repository* gom_get_first_repository()
85 {
86   return gom_first_repository;
87 }
88
89 int write_repositories(Gedcom_write_hndl hndl)
90 {
91   int result = 0;
92   int i;
93   struct repository* obj;
94
95   for (obj = gom_first_repository; obj; obj = obj->next) {
96     result |= gedcom_write_record_str(hndl, REC_REPO, obj->xrefstr, NULL);
97     if (obj->name)
98       result |= gedcom_write_element_str(hndl, ELT_REPO_NAME, 0,
99                                          REC_REPO, obj->name);
100     if (obj->address)
101       result |= write_address(hndl, REC_REPO, obj->address);
102     for (i = 0; i < 3 && obj->phone[i]; i++)
103       result |= gedcom_write_element_str(hndl, ELT_SUB_PHON, 0, REC_REPO,
104                                          obj->phone[i]);
105     if (obj->note)
106       result |= write_note_subs(hndl, REC_REPO, obj->note);
107     if (obj->ref)
108       result |= write_user_refs(hndl, REC_REPO, obj->ref);
109     if (obj->record_id)
110       result |= gedcom_write_element_str(hndl, ELT_SUB_IDENT_RIN, 0,
111                                          REC_REPO, obj->record_id);
112     if (obj->change_date)
113       result |= write_change_date(hndl, REC_REPO, obj->change_date);
114     if (obj->extra)
115       result |= write_user_data(hndl, obj->extra);
116   }
117   
118   return result;
119 }