1 /* Cross-reference manipulation routines.
2 Copyright (C) 2001,2002 The Genes Development Team
3 This file is part of the Gedcom parser library.
4 Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2001.
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.
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.
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
24 #include "gedcom_internal.h"
26 #include "gedcom.tabgen.h"
30 struct xref_value def_xref_val = { XREF_NONE, "<error>", NULL };
31 static hash_t *xrefs = NULL;
33 const char* xref_type_str[] = { N_("nothing"),
37 N_("a multimedia object"),
38 N_("a source repository"),
41 N_("a submission record"),
42 N_("an application-specific record"),
46 struct xref_value xref;
47 Xref_type defined_type;
54 hnode_t *xref_alloc(void *c UNUSED)
56 return malloc(sizeof *xref_alloc(NULL));
59 void xref_free(hnode_t *n, void *c UNUSED)
61 struct xref_node *xr = (struct xref_node *)hnode_get(n);
62 free((void*)hnode_getkey(n));
63 free(xr->xref.string);
68 void clear_xref_node(struct xref_node *xr)
70 xr->xref.type = XREF_NONE;
71 /* Make sure that the 'string' member always contains a valid string */
73 xr->xref.string = strdup("");
74 if (!xr->xref.string) MEMORY_ERROR;
75 xr->xref.object = NULL;
76 xr->defined_type = XREF_NONE;
77 xr->used_type = XREF_NONE;
78 xr->defined_line = -1;
83 struct xref_node *make_xref_node()
85 struct xref_node *xr = (struct xref_node *)malloc(sizeof(struct xref_node));
87 xr->xref.string = NULL;
95 void delete_xref_node(struct xref_node* xr)
98 free(xr->xref.string);
108 void make_xref_table()
113 /* Only register initially (if xrefs is still NULL) */
114 /* So that it is only registered once */
115 if (atexit(cleanup_xrefs) != 0) {
116 gedcom_warning(_("Could not register xref cleanup function"));
118 xrefs = hash_create(HASHCOUNT_T_MAX, NULL, NULL);
119 hash_set_allocator(xrefs, xref_alloc, xref_free, NULL);
122 int check_xref_table()
127 struct xref_node *xr;
129 /* Check for undefined and unused xrefs */
130 hash_scan_begin(&hs, xrefs);
131 while ((node = hash_scan_next(&hs))) {
132 xr = (struct xref_node *)hnode_get(node);
133 if (xr->defined_type == XREF_NONE && xr->used_type != XREF_NONE) {
134 gedcom_error(_("Cross-reference %s used on line %d is not defined"),
135 xr->xref.string, xr->used_line);
138 if (xr->used_type == XREF_NONE && xr->defined_type != XREF_NONE) {
139 gedcom_warning(_("Cross-reference %s defined on line %d is never used"),
140 xr->xref.string, xr->defined_line);
147 struct xref_node* add_xref(Xref_type xref_type, const char* xrefstr,
150 struct xref_node *xr = NULL;
151 char *key = strdup(xrefstr);
153 xr = make_xref_node();
154 xr->xref.type = xref_type;
155 xr->xref.object = object;
157 free(xr->xref.string);
158 xr->xref.string = strdup(xrefstr);
159 if (! xr->xref.string) {
162 delete_xref_node(xr);
166 hash_alloc_insert(xrefs, key, xr);
174 void remove_xref(struct xref_node* xr)
176 hnode_t *node = hash_lookup(xrefs, xr->xref.string);
177 hash_delete_free(xrefs, node);
180 int set_xref_fields(struct xref_node* xr, Xref_ctxt ctxt, Xref_type xref_type)
184 if (xr->defined_type != XREF_NONE && xr->defined_type != xref_type &&
185 xr->defined_type != XREF_ANY) {
186 if (xr->defined_line != 0)
187 gedcom_error(_("Cross-reference %s previously defined as pointer to %s, "
189 xr->xref.string, xref_type_str[xr->defined_type],
192 gedcom_error(_("Cross-reference %s previously defined as pointer to %s"),
193 xr->xref.string, xref_type_str[xr->defined_type]);
197 else if (xr->used_type != XREF_NONE && xr->used_type != xref_type) {
198 if (xr->used_line != 0)
199 gedcom_error(_("Cross-reference %s previously used as pointer to %s, "
201 xr->xref.string, xref_type_str[xr->used_type],
204 gedcom_error(_("Cross-reference %s previously used as pointer to %s"),
205 xr->xref.string, xref_type_str[xr->used_type]);
211 if (ctxt == XREF_USED)
213 if (ctxt == XREF_DEFINED && xr->defined_type == XREF_NONE) {
214 xr->defined_type = xref_type;
215 xr->defined_line = line_no;
217 else if (ctxt == XREF_USED && xr->used_type == XREF_NONE) {
218 xr->used_type = xref_type;
219 xr->used_line = line_no;
226 struct xref_value *gedcom_parse_xref(const char *raw_value,
227 Xref_ctxt ctxt, Xref_type xref_type)
229 struct xref_node *xr = NULL;
231 hnode_t *node = hash_lookup(xrefs, raw_value);
233 xr = (struct xref_node *)hnode_get(node);
236 xr = add_xref(xref_type, raw_value, NULL);
240 set_xref_fields(xr, ctxt, xref_type);
247 /* Functions for retrieving, modifying and deleting cross-references */
249 int is_valid_pointer(const char *key)
251 return (strlen(key) <= 22 &&
252 gedcom_check_token(key, STATE_NORMAL, POINTER) == 0);
255 struct xref_value* gedcom_get_by_xref(const char *key)
257 if (!is_valid_pointer(key)) {
258 gedcom_error(_("String '%s' is not a valid cross-reference key"), key);
262 hnode_t *node = hash_lookup(xrefs, key);
264 struct xref_node *xr = (struct xref_node *)hnode_get(node);
272 struct xref_value* gedcom_add_xref(Xref_type type, const char* xrefstr,
275 struct xref_node *xr = NULL;
277 if (!is_valid_pointer(xrefstr)) {
278 gedcom_error(_("String '%s' is not a valid cross-reference key"), xrefstr);
281 hnode_t *node = hash_lookup(xrefs, xrefstr);
283 gedcom_error(_("Cross-reference %s already exists"), xrefstr);
286 xr = add_xref(type, xrefstr, object);
288 set_xref_fields(xr, XREF_DEFINED, type);
297 struct xref_value* gedcom_link_xref(Xref_type type, const char* xrefstr)
299 struct xref_node *xr = NULL;
301 if (!is_valid_pointer(xrefstr)) {
302 gedcom_error(_("String '%s' is not a valid cross-reference key"), xrefstr);
305 hnode_t *node = hash_lookup(xrefs, xrefstr);
307 gedcom_error(_("Cross-reference %s not defined"), xrefstr);
310 xr = (struct xref_node *)hnode_get(node);
311 if (set_xref_fields(xr, XREF_USED, type) != 0)
322 struct xref_value* gedcom_unlink_xref(Xref_type type, const char* xrefstr)
324 struct xref_node *xr = NULL;
325 if (!is_valid_pointer(xrefstr)) {
326 gedcom_error(_("String '%s' is not a valid cross-reference key"), xrefstr);
329 hnode_t *node = hash_lookup(xrefs, xrefstr);
331 gedcom_error(_("Cross-reference %s not defined"), xrefstr);
334 xr = (struct xref_node*) hnode_get(node);
335 if (xr->defined_type != type && xr->defined_type != XREF_ANY) {
337 (_("Cross-reference %s previously defined as pointer to %s"),
338 xr->xref.string, xref_type_str[xr->defined_type]);
351 int gedcom_delete_xref(const char* xrefstr)
353 struct xref_node *xr = NULL;
356 if (!is_valid_pointer(xrefstr)) {
357 gedcom_error(_("String '%s' is not a valid cross-reference key"), xrefstr);
360 hnode_t *node = hash_lookup(xrefs, xrefstr);
362 gedcom_error(_("Cross-reference %s not defined"), xrefstr);
365 xr = (struct xref_node*) hnode_get(node);
366 if (xr->use_count != 0) {
367 gedcom_error(_("Cross-reference %s still in use"), xrefstr);