More checking on validity of strings.
[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 REC_CB(repository, repo_start, make_repository_record)
39 GET_REC_BY_XREF(repository, XREF_REPO, gom_get_repository_by_xref)
40 STRING_CB(repository, repo_name_start, name)
41
42 void repository_subscribe()
43 {
44   gedcom_subscribe_to_record(REC_REPO, repo_start, def_rec_end);
45   gedcom_subscribe_to_element(ELT_REPO_NAME, repo_name_start, def_elt_end);
46 }
47
48 void repository_add_address(Gom_ctxt ctxt, struct address* address)
49 {
50   struct repository *repo = SAFE_CTXT_CAST(repository, ctxt);
51   if (repo)
52     repo->address = address;
53 }
54
55 void repository_add_phone(Gom_ctxt ctxt, const char *phone)
56 {
57   struct repository *repo = SAFE_CTXT_CAST(repository, ctxt);
58   if (repo) {
59     int i = 0;
60     while (i<2 && repo->phone[i]) i++;
61     if (! repo->phone[i]) {
62       repo->phone[i] = strdup(phone);
63       if (! repo->phone[i]) MEMORY_ERROR;
64     }
65   }
66 }
67
68 void repository_add_note(Gom_ctxt ctxt, struct note_sub* note)
69 {
70   struct repository *repo = SAFE_CTXT_CAST(repository, ctxt);
71   if (repo)
72     LINK_CHAIN_ELT(note_sub, repo->note, note);
73 }
74
75 void repository_add_user_ref(Gom_ctxt ctxt, struct user_ref_number* ref)
76 {
77   struct repository *repo = SAFE_CTXT_CAST(repository, ctxt);
78   if (repo)
79     LINK_CHAIN_ELT(user_ref_number, repo->ref, ref);
80 }
81
82 void repository_set_record_id(Gom_ctxt ctxt, const char *rin)
83 {
84   struct repository *repo = SAFE_CTXT_CAST(repository, ctxt);
85   if (repo) {
86     repo->record_id = strdup(rin);
87     if (! repo->record_id) MEMORY_ERROR;
88   }
89 }
90
91 void repository_set_change_date(Gom_ctxt ctxt, struct change_date* chan)
92 {
93   struct repository *repo = SAFE_CTXT_CAST(repository, ctxt);
94   if (repo)
95     repo->change_date = chan;
96 }
97
98 void repository_add_user_data(Gom_ctxt ctxt, struct user_data* data)
99 {
100   struct repository *obj = SAFE_CTXT_CAST(repository, ctxt);
101   if (obj)
102     LINK_CHAIN_ELT(user_data, obj->extra, data);
103 }
104
105 void repository_cleanup(struct repository* repo)
106 {
107   if (repo) {
108     SAFE_FREE(repo->xrefstr);
109     SAFE_FREE(repo->name);
110     address_cleanup(repo->address);
111     SAFE_FREE(repo->phone[0]);
112     SAFE_FREE(repo->phone[1]);
113     SAFE_FREE(repo->phone[2]);
114     DESTROY_CHAIN_ELTS(note_sub, repo->note, note_sub_cleanup);
115     DESTROY_CHAIN_ELTS(user_ref_number, repo->ref, user_ref_cleanup);
116     SAFE_FREE(repo->record_id);
117     change_date_cleanup(repo->change_date);
118     DESTROY_CHAIN_ELTS(user_data, repo->extra, user_data_cleanup);
119   }
120 }
121
122 void repositories_cleanup()
123 {
124   DESTROY_CHAIN_ELTS(repository, gom_first_repository, repository_cleanup);
125 }
126
127 struct repository* gom_get_first_repository()
128 {
129   return gom_first_repository;
130 }
131
132 struct repository* make_repository_record(const char* xrefstr)
133 {
134   struct repository* repo = NULL;
135   MAKE_CHAIN_ELT(repository, gom_first_repository, repo);
136   if (repo) {
137     repo->xrefstr = strdup(xrefstr);
138     if (! repo->xrefstr) MEMORY_ERROR;
139   }
140   return repo;
141 }